<?xml version="1.0" encoding="utf-8"?>
<iso:schema 
  xmlns="http://purl.oclc.org/dsdl/schematron"
  xmlns:iso="http://purl.oclc.org/dsdl/schematron" 
  xmlns:dp="http://www.pnml.org/version-2009/grammar/pnml"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  queryBinding='xslt2'
  schemaVersion='ISO19757-3'
  see='http://standards.iso.org/ittf/PubliclyAvailableStandards/index.html'
  defaultPhase='#ALL'>
  <iso:title>A Schematron mini-schema for PNML PT nets constraints specification. Schema-aware version.</iso:title>
  <iso:ns prefix='dp' uri='http://www.pnml.org/version-2009/grammar/pnml'/>
  <p> 
    This schema provides Schematron rules you may use to further check 
    the validity of a PT Net Document, in addition to the original PNML Schema in RELAX NG.
    This schema reuses the rules designed to check PNML Core Model Documents,
    since PT Nets are built upon the Core Model. This is the schema-aware (SA) version
    of constraints on PT Nets, since we provide rules about arc inscriptions and place markings
    to be positive integers and non negative integers respectively. These rules enforce
    the data type of inscriptions and markings.
    
    File name                      : PTNetconstraints-SA.sch
    Requires                       : CMConstraints.sch
    Version                        : 2010
    Copyright notice               : Copyright 2010 Lom Hillah (AFNOR)
    License                        : GNU GPL v3 (See http://www.gnu.org/licenses/)
    Revisions  					           -
    (Number/Date/Author/[Comment]) : 1/August 13, 2009/L.H./Inception
                                   : 2/March 3, 2010/L.H/Reuse CoreModel constraints with extends
                                   : 3/April 26, 2010/L.H/Added requirement info
                                   : 4/May 2, 2010/L.H/Schema-aware version
                                   : 5/May 18, 2010/L.H/Rule for token graphics
  </p>
  
  <iso:phase 
    id="PTNet"
    see="http://www.pnml.org/version-2009/grammar/ptnet.pntd">
    <p>
      In this schema we provide a phase for PT nets.
      Since the Core Model schema is included, its phase 
      will be activated  as well during validation.
      
      If you extends this schema with another phase, you may set a default phase
      for the validation in the @defaultPhase attribute of the iso:schema element.
    </p>
 
    <iso:active pattern="CheckArcSourceTargetDifferent"/>
    <iso:active pattern="CheckTokenGraphics"/>
    <iso:active pattern="CheckPlaceMarking"/>
    <iso:active pattern="CheckArcInscription"/>
  </iso:phase>
  
  <!-- Pattern for PT nets-->
  <iso:pattern id="CheckArcSourceTargetDifferent">
    <iso:title>Checks that no arc connects two nodes of the same kind</iso:title>
    <iso:rule context="dp:arc[@source and @target]">
      <let name="placeSource" value="key('nodeId', current()/@source)/(self::dp:place|self::dp:referencePlace)[@id = current()/@source]"/>
      <let name="transitionTarget" value="key('nodeId', current()/@target)/(self::dp:transition|self::dp:referenceTransition)[@id = current()/@target]"/>
      <let name="transitionSource" value="key('nodeId', current()/@source)/(self::dp:transition|self::dp:referenceTransition)[@id = current()/@source]"/>
      <let name="placeTarget" value="key('nodeId', current()/@target)/(self::dp:place|self::dp:referencePlace)[@id = current()/@target]"/>
      <iso:assert test="(count($placeSource) = 1 and count($transitionTarget) = 1) or 
        (count($transitionSource) = 1 and count($placeTarget) = 1)" diagnostics="differentNodeKinds">
        The arc '<value-of select="@id"/>' must connect two different kinds of nodes.
      </iso:assert>
    </iso:rule>
  </iso:pattern>
  
  <iso:pattern id="CheckTokenGraphics">
    <iso:title>Checks in initial markings' tool specific that the number of token graphics
      is the same as the initial marking. First, checks that in the context of token graphics,
      the toolspecific parent has a tool attribute whose value is org.pnml.tool.
      See clause 5.3.1 of ISO/IEC 15909-2:2009
    </iso:title>
    <iso:rule context="dp:initialMarking/dp:toolspecific/dp:tokengraphics">
      <iso:assert test="parent::dp:toolspecific[@tool='org.pnml.tool']">
        The tool specific parent of token graphics of the initial marking of place '<value-of select="ancestor::dp:place/@id"/>'
        must have an attribute tool whose value is 'org.pnml.tool'.
      </iso:assert>
      <iso:assert test="count(dp:tokenposition) = ancestor::dp:initialMarking/dp:text">
        The number of token graphics of the initial marking of place '<value-of select="ancestor::dp:place/@id"/>'
        must be the same as the initial marking of that place.
      </iso:assert>
    </iso:rule>
  </iso:pattern>
  
  <!-- Schema-aware rules -->
  <iso:pattern id="CheckPlaceMarking">
    <iso:title>Checks every place marking is a non negative integer. This is a SA rule.</iso:title>
    <iso:rule context="dp:place/dp:initialMarking/dp:text">
      <iso:assert test="string(.) castable as xs:nonNegativeInteger">
        The inscription of place '<value-of select="ancestor::dp:place/@id"/>' should be a positive integer.
      </iso:assert>
    </iso:rule>
  </iso:pattern>
  
  <iso:pattern id="CheckArcInscription">
    <iso:title>Checks every arc inscription is a positive integer. This is a SA rule.</iso:title>
    <iso:rule context="dp:arc/dp:inscription/dp:text">
      <iso:assert test="string(.) castable as xs:positiveInteger">
        The inscription of arc '<value-of select="ancestor::dp:arc/@id"/>' should be a positive integer.
      </iso:assert>
    </iso:rule>
  </iso:pattern>
  
  <!--
    Include all the children of the referenced schematron mini-schema for Core Model.
    See http://www.eccnet.com/pipermail/schematron/2009-April/000178.html
  -->
  <iso:extends href="./CMconstraints.sch"/>
  
  <iso:diagnostics> 
    <iso:diagnostic id="differentNodeKinds"> 
      In a Place/Transition net, an arc may connect a place to a transition or vice versa.
      It may also connect a place to a referenceTransition (or vice versa) or a transition to 
      a referencePlace (or vice versa).
      It may also connect a referencePlace to a referenceTransition (or vice versa).
    </iso:diagnostic> 
  </iso:diagnostics> 
  
</iso:schema>

