Storing patterns as SPARQL / SPIN constructs

Post Reply
Message
Author
vvagr
Posts: 282
Joined: Mon Feb 27, 2012 11:01 pm
Location: Moscow, Russia
Contact:

Storing patterns as SPARQL / SPIN constructs

#1 Post by vvagr »

This is continuation of a discussion on RDF pattern representation started in http://15926.org/viewtopic.php?f=3&t=222 . The same HydrostaticTestPressure pattern is used as an example (FOL representation of this pattern was published in http://15926.org/viewtopic.php?f=3&t=224).

Use of SPARQL query to describe patterns is a natural choice - SPARQL is after all specifically a pattern-matching language. It is easy to construct generic SPARQL query describing HydrostaticTestPressure RDF sub-graph in two variants of the pattern - an option for a class and an option for an individual.

Code: Select all

@prefix rdl: <http://posccaesar.org/rdl/> .
@prefix tpl: <http://data.posccaesar.org/tpl/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix dm: <http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. 

SELECT ?hasPosessor ?hasPressure ?hasScale
WHERE { {?x1 a tpl:ClassOfParticipationDefinition ;
          tpl:hasActivityType ?activity ;
          tpl:hasParticipantType ?hasPossessor ;
          tpl:hasParticipantRole rdl:RDS4398439 ;           # TESTED
          tpl:hasCardinalityOfActivity rdl:RDS4367382 ;     # ZERO-TO-INF (faked ID)
          tpl:hasCardinalityOfParticipant rdl:RDS2387482 .  # EXACTLY ONE (faked ID)

        ?x2 a tpl:ClassOfParticipationDefinition ;
          tpl:hasActivityType ?activity ;
          tpl:hasParticipantType ?fluid ;
          tpl:hasParticipantRole rdl:RDS13108820 ;            # TEST FLUID
          tpl:hasCardinalityOfActivity rdl:RDS4367382 ;       # ZERO-TO-INF (faked ID)
          tpl:hasCardinalityOfParticipant rdl:RDS2387482 .    # EXACTLY ONE (faked ID)

        ?x3 a tpl:ClassOfIndividualHasIndirectPropertyWithValue ;
          tpl:hasPossessorType ?fluid ;
          tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
          tpl:valPropertyValue ?hasPressure ;
          tpl:hasScale ?hasScale .            

        ?hasPossessor rdfs:subClassOf rdl:RDS430694 .       # PRESSURE-RATED ARTEFACT

        ?hasScale a dm:Scale .

        ?activity rdfs:subClassOf rdl:RDS9706787 .          # HYDROSTATIC TESTING

        ?fluid rdfs:subClassOf rdl:RDS5605044 .             # UNION OF ALLOWABLE TEST FLUIDS (faked ID)

         FILTER ( datatype(?hasPressure) = xsd:float ) . }

        UNION

        {?x1 a tpl:ParticipationOfIndividualInActivity ;
          tpl:hasActivity ?activity ;
          tpl:hasParticipant ?hasPossessor ;
          tpl:hasParticipationType rdl:RDS43923435345 .       # PARTICIPATION IN HYDROSTATIC TESTING AS TESTED (faked ID)

        ?x2 a tpl:ParticipationOfIndividualInActivity ;
          tpl:hasActivity ?activity ;
          tpl:hasParticipant ?fluid ;
          tpl:hasParticipationType rdl:RDS142388242 .         # PARTICIPATION IN HYDROSTATIC TESTING AS TEST FLUID (faked ID)
          
        ?x3 a tpl:IndividualHasIndirectPropertyWithValue ;
          tpl:hasPossessor ?fluid ;
          tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
          tpl:valPropertyValue ?hasPressure ;
          tpl:hasScale ?hasScale .

        ?hasPossessor a rdl:RDS430694 .             # PRESSURE-RATED ARTEFACT

        ?hasScale a dm:Scale .

        ?activity a rdl:RDS9706787 .                # HYDROSTATIC TESTING

        ?fluid a rdl:RDS5605044 .                   # UNION OF ALLOWABLE TEST FLUIDS (faked ID)

        FILTER ( datatype(?hasPressure) = xsd:float ) . } }
Unfortunately native SPARQL query can not be stored as triples and retrieved from RDL via standard protocol (SPARQL). To resolve this problem we were directed to the SPIN standard http://spinrdf.org/ .

It was not easy for me to select appropriate SPIN container to encapsulate SPARQL pattern description. Looks like SPIN requires strict determination - whether you will use SPIN rule to search for data, to verify data or to modify it. As we understand now, ISO 15926 patterns will be used for all these purposes simultaneously. Patterns can be used to construct RDF data from data in the native engineering applications, patterns can be used to search in RDF data to verify it or to write data to native engineering applications, and patterns can be used to search RDF data and replace it with other RDF data (for template lowering-lifting, for example). Such universal usage is broader then any specific containers available in SPIN.

Therefore I've tried to describe our intended SPARQL queries as SPIN Templates (http://spinrdf.org/spl.html#templates). This container allows description of pattern parameters, allows classification and labelling of patterns.

Obviously specialized software will be able to extract SPARQL pattern description from the container and use it for any of the purposes mentioned above. Whether a generic SPIN engine will make some sense out of this form - is an open question for me.

Hope SPIN experts will look at this, correct mistakes or show better solutions if available.

Code: Select all

@prefix rdl: <http://posccaesar.org/rdl/> .
@prefix tpl: <http://data.posccaesar.org/tpl/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix dm: <http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. 
@prefix spin: <http://spinrdf.org/spin#>.
@prefix sp: <http://spinrdf.org/sp#>.
@prefix spl: <http://spinrdf.org/spl#>.
@prefix ptrn: <http://data.posccaesar.org/ptrn/>.

ptrn:HydroTestPressureForClass a spin:SelectTemplate ;
      rdfs:subClassOf   ptrn:HydroTestPressure ;
      rdfs:comment "For ?hasPossessor class members hydrostatic test pressure property equals ?hasPressure on ?hasScale scale" ;
      rdfs:label "Hydrostatic test pressure (class)"^^xsd:string ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Tested entity (class)" ;
                spl:predicate ptrn:hasPossessor ;
              ] ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Hydrostatic test pressure value" ;
                spl:predicate ptrn:hasPressure ;
                spl:valueType xsd:float
              ] ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Hydrostatic test pressure UOM" ;
                spl:predicate ptrn:hasScale ;
                spl:valueType dm:Scale
              ] ;
      spin:labelTemplate "Hydrostatic test pressure for {?hasPossessor} is {?hasPressure} {?hasScale}"^^xsd:string ;
      spin:body
              [ a       sp:Select ;
                sp:text """
                    SELECT ?hasPosessor ?hasPressure ?hasScale
                    WHERE { ?x1 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?hasPossessor ;
                              tpl:hasParticipantRole rdl:RDS4398439 ;           # TESTED
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;     # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .  # EXACTLY ONE (faked ID)

                            ?x2 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?fluid ;
                              tpl:hasParticipantRole rdl:RDS13108820 ;            # TEST FLUID
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;       # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .    # EXACTLY ONE (faked ID)

                            ?x3 a tpl:ClassOfIndividualHasIndirectPropertyWithValue ;
                              tpl:hasPossessorType ?fluid ;
                              tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                              tpl:valPropertyValue ?hasPressure ;
                              tpl:hasScale ?hasScale .    

                            ?hasPossessor rdfs:subClassOf rdl:RDS430694 .       # PRESSURE-RATED ARTEFACT       

                            ?activity rdfs:subClassOf rdl:RDS9706787 .          # HYDROSTATIC TESTING

                            ?fluid rdfs:subClassOf rdl:RDS5605044 .             # UNION OF ALLOWABLE TEST FLUIDS (faked ID) }
                    """
              ] .

ptrn:HydroTestPressureForIndividual a spin:SelectTemplate ;
      rdfs:subClassOf   ptrn:HydroTestPressure ;
      rdfs:comment "For ?hasPossessor object hydrostatic test pressure property equals ?hasPressure on ?hasScale scale" ;
      rdfs:label "Hydrostatic test pressure (individual)"^^xsd:string ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Tested entity (individual)" ;
                spl:predicate ptrn:hasPossessor ;
                spl:valueType rdl:RDS430694             # PRESSURE-RATED ARTEFACT
              ] ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Hydrostatic test pressure value" ;
                spl:predicate ptrn:hasPressure ;
                spl:valueType xsd:float
              ] ;
      spin:constraint
              [ a       spl:Argument ;
                rdfs:comment "Hydrostatic test pressure UOM" ;
                spl:predicate ptrn:hasScale ;
                spl:valueType dm:Scale
              ] ;
      spin:labelTemplate "Hydrostatic test pressure for {?hasPossessor} is {?hasPressure} {?hasScale}"^^xsd:string ;
      spin:body
              [ a       sp:Select ;
                sp:text """
                    SELECT ?hasPosessor ?hasPressure ?hasScale
                    WHERE { ?x1 a tpl:ParticipationOfIndividualInActivity ;
                              tpl:hasActivity ?activity ;
                              tpl:hasParticipant ?hasPossessor ;
                              tpl:hasParticipationType rdl:RDS43923435345 .       # PARTICIPATION IN HYDROSTATIC TESTING AS TESTED (faked ID)

                            ?x2 a tpl:ParticipationOfIndividualInActivity ;
                              tpl:hasActivity ?activity ;
                              tpl:hasParticipant ?fluid ;
                              tpl:hasParticipationType rdl:RDS142388242 .         # PARTICIPATION IN HYDROSTATIC TESTING AS TEST FLUID (faked ID)
                              
                            ?x3 a tpl:IndividualHasIndirectPropertyWithValue ;
                              tpl:hasPossessor ?fluid ;
                              tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                              tpl:valPropertyValue ?hasPressure ;
                              tpl:hasScale ?hasScale .

                            ?activity a rdl:RDS9706787 .                # HYDROSTATIC TESTING

                            ?fluid a rdl:RDS5605044 .                   # UNION OF ALLOWABLE TEST FLUIDS (faked ID)
                    """
              ] .

vvagr
Posts: 282
Joined: Mon Feb 27, 2012 11:01 pm
Location: Moscow, Russia
Contact:

Re: Storing patterns as SPARQL / SPIN constructs

#2 Post by vvagr »

It is possible to use another SPIN construct - Magic Property http://spinrdf.org/spin.html#spin-magic

Two Magic Properties can be defined based on the initial query:

Code: Select all

prtn:hasPressureInHydrostaticPressureTest
  a       spin:MagicProperty ;
  rdfs:label "Pressure in Hydrostatic Pressure test"^^xsd:string ;
  rdfs:subClassOf spin:MagicProperties ;
  spin:constraint
          [ a       spl:Argument ;
            rdfs:comment "Hydrostatic test pressure value"^^xsd:string ;
            spl:predicate sp:arg1 ;
            spl:valueType xsd:float
          ] ;
  spin:body
          [ a       sp:Select ;
            sp:text """
                    SELECT ?hasPressureInHydrostaticPressureTest
                    WHERE {{ ?x1 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?arg1 ;
                              tpl:hasParticipantRole rdl:RDS4398439 ;           # TESTED
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;     # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .  # EXACTLY ONE (faked ID)

                            ?x2 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?fluid ;
                              tpl:hasParticipantRole rdl:RDS13108820 ;            # TEST FLUID
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;       # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .    # EXACTLY ONE (faked ID)

                            ?x3 a tpl:ClassOfIndividualHasIndirectPropertyWithValue ;
                              tpl:hasPossessorType ?fluid ;
                              tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                              tpl:valPropertyValue ?hasPressureInHydrostaticPressureTest ;
                              tpl:hasScale ?hasScaleInHydrostaticPressureTest .   

                            ?arg1 rdfs:subClassOf rdl:RDS430694 .       # PRESSURE-RATED ARTEFACT  

                            ?hasScaleInHydrostaticPressureTest a dm:Scale .     

                            ?activity rdfs:subClassOf rdl:RDS9706787 .          # HYDROSTATIC TESTING

                            ?fluid rdfs:subClassOf rdl:RDS5605044 .             # UNION OF ALLOWABLE TEST FLUIDS (faked ID) 
                          }

                          UNION            

                          {?x1 a tpl:ParticipationOfIndividualInActivity ;
                            tpl:hasActivity ?activity ;
                            tpl:hasParticipant ?arg1 ;
                            tpl:hasParticipationType rdl:RDS43923435345 .       # PARTICIPATION IN HYDROSTATIC TESTING AS TESTED (faked ID)

                          ?x2 a tpl:ParticipationOfIndividualInActivity ;
                            tpl:hasActivity ?activity ;
                            tpl:hasParticipant ?fluid ;
                            tpl:hasParticipationType rdl:RDS142388242 .         # PARTICIPATION IN HYDROSTATIC TESTING AS TEST FLUID (faked ID)
                           
                          ?x3 a tpl:IndividualHasIndirectPropertyWithValue ;
                            tpl:hasPossessor ?fluid ;
                            tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                            tpl:valPropertyValue ?hasPressureInHydrostaticPressureTest ;
                            tpl:hasScale ?hasScaleInHydrostaticPressureTest .

                          ?arg1 a rdl:RDS430694 .             # PRESSURE-RATED ARTEFACT

                          ?hasScaleInHydrostaticPressureTest a dm:Scale .

                          ?activity a rdl:RDS9706787 .                # HYDROSTATIC TESTING

                          ?fluid a rdl:RDS5605044 .                   # UNION OF ALLOWABLE TEST FLUIDS (faked ID) }}

                """
          ] .

Code: Select all

prtn:hasScaleInHydrostaticPressureTest
  a       spin:MagicProperty ;
  rdfs:label "Pressure UOM in Hydrostatic Pressure test"^^xsd:string ;
  rdfs:subClassOf spin:MagicProperties ;
  spin:constraint
          [ a       spl:Argument ;
            rdfs:comment "Hydrostatic test pressure UOM"^^xsd:string ;
            spl:predicate sp:arg1 ;
            spl:valueType dm:Scale
          ] ;
  spin:body
          [ a       sp:Select ;
            sp:text """
                    SELECT ?hasScaleInHydrostaticPressureTest
                    WHERE {{ ?x1 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?arg1 ;
                              tpl:hasParticipantRole rdl:RDS4398439 ;           # TESTED
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;     # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .  # EXACTLY ONE (faked ID)

                            ?x2 a tpl:ClassOfParticipationDefinition ;
                              tpl:hasActivityType ?activity ;
                              tpl:hasParticipantType ?fluid ;
                              tpl:hasParticipantRole rdl:RDS13108820 ;            # TEST FLUID
                              tpl:hasCardinalityOfActivity rdl:RDS4367382 ;       # ZERO-TO-INF (faked ID)
                              tpl:hasCardinalityOfParticipant rdl:RDS2387482 .    # EXACTLY ONE (faked ID)

                            ?x3 a tpl:ClassOfIndividualHasIndirectPropertyWithValue ;
                              tpl:hasPossessorType ?fluid ;
                              tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                              tpl:valPropertyValue ?hasPressureInHydrostaticPressureTest ;
                              tpl:hasScale ?hasScaleInHydrostaticPressureTest .   

                            ?arg1 rdfs:subClassOf rdl:RDS430694 .       # PRESSURE-RATED ARTEFACT  

                            ?hasScaleInHydrostaticPressureTest a dm:Scale .     

                            ?activity rdfs:subClassOf rdl:RDS9706787 .          # HYDROSTATIC TESTING

                            ?fluid rdfs:subClassOf rdl:RDS5605044 .             # UNION OF ALLOWABLE TEST FLUIDS (faked ID) 
                          }

                          UNION            

                          {?x1 a tpl:ParticipationOfIndividualInActivity ;
                            tpl:hasActivity ?activity ;
                            tpl:hasParticipant ?arg1 ;
                            tpl:hasParticipationType rdl:RDS43923435345 .       # PARTICIPATION IN HYDROSTATIC TESTING AS TESTED (faked ID)

                          ?x2 a tpl:ParticipationOfIndividualInActivity ;
                            tpl:hasActivity ?activity ;
                            tpl:hasParticipant ?fluid ;
                            tpl:hasParticipationType rdl:RDS142388242 .         # PARTICIPATION IN HYDROSTATIC TESTING AS TEST FLUID (faked ID)
                           
                          ?x3 a tpl:IndividualHasIndirectPropertyWithValue ;
                            tpl:hasPossessor ?fluid ;
                            tpl:hasIndirectPropertyType rdl:RDS361349 ;         # HYDROSTATIC TEST PRESSURE
                            tpl:valPropertyValue ?hasPressureInHydrostaticPressureTest ;
                            tpl:hasScale ?hasScaleInHydrostaticPressureTest .

                          ?arg1 a rdl:RDS430694 .             # PRESSURE-RATED ARTEFACT

                          ?hasScaleInHydrostaticPressureTest a dm:Scale .

                          ?activity a rdl:RDS9706787 .                # HYDROSTATIC TESTING

                          ?fluid a rdl:RDS5605044 .                   # UNION OF ALLOWABLE TEST FLUIDS (faked ID) }}

                """
          ] .

With magic properties the search for the pattern becomes very simple:

Code: Select all

SELECT ?hasPosessor ?hasPressure ?hasScale
WHERE { ?hasPossessor  ptrn:hasPressureInHydrostaticPressureTest ?hasPressure .
        ?hasPossessor  ptrn:hasScaleInHydrostaticPressureTest ?hasScale .}
These definitions become very similar to ISO 15926 templates.

The naming of the pattern itself is absent from magic property definition, this requires further clarifications.

Post Reply