Search code examples
javardfjenajena-rules

Writing a Jena rule to add properties to a resource with some specific property values?


I'm trying to write a Jena rule whose antecedent matches a location where the network's bandwith is 450^^xsd:float. I've tried the following two rules. The first gives me no results. The second matches for the networks labeled Gate 23 and Gate 15, but only Gate 15 should match.

[AdaptedModel: 
 (?d2 rdf:type perSys:NetworkCharacteristics),
 (?d2 perSys:Bandwidth '450'^^xsd:float)
 (?d2 perSys:SpecificLocation ?a)
->
 (?d2 perSys:AdaptedSpecificLocation ?a)]
[AdaptedModel: 
 (?d2 rdf:type perSys:NetworkCharacteristics),
 (?d2 perSys:Bandwidth '450'^^xsd:float)
 (?d3 perSys:SpecificLocation ?a)
->
 (?d3 perSys:AdaptedSpecificLocation ?a)]

This is my RDF data:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:perSys="http://localhost:8080/NetworkContextWS/onto/NetworkContextDescription#">
  <perSys:PervasiveContext>
    <perSys:HasNetworkContext>
      <perSys:NetworkContext rdf:about="file:///home/taylorj/n1111">
        <perSys:Rules>
          <perSys:NetworkSecurity>
            <perSys:NetworkSecurityState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Open Network</perSys:NetworkSecurityState>
            <perSys:NetworkKey rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >No Key</perSys:NetworkKey>
          </perSys:NetworkSecurity>
        </perSys:Rules>
        <perSys:Network>
          <perSys:NetworkCharacteristics>
            <perSys:SubNetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >HSDPA</perSys:SubNetworkType>
            <perSys:NetworkState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >CONNECTED</perSys:NetworkState>
            <perSys:Bandwidth rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >450</perSys:Bandwidth>
            <perSys:LinkSpeed rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >65</perSys:LinkSpeed>
            <perSys:NetworkAvailability rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Available</perSys:NetworkAvailability>
            <perSys:NetworkName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >AF23_WI-FI_9F1B</perSys:NetworkName>
          </perSys:NetworkCharacteristics>
        </perSys:Network>
        <perSys:Network>
          <perSys:NetworkTraficStats>
            <perSys:NumberBytesReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >15599522</perSys:NumberBytesReceived>
            <perSys:NumberPacketsReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >24922</perSys:NumberPacketsReceived>
            <perSys:NumberBytesTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >4111415</perSys:NumberBytesTransmited>
            <perSys:NumberPacketsTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >26455</perSys:NumberPacketsTransmited>
            <perSys:NetworkUpload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >0</perSys:NetworkUpload>
            <perSys:NetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Connected</perSys:NetworkType>
            <perSys:NetworkDownload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >1</perSys:NetworkDownload>
            <perSys:TotalData rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >1</perSys:TotalData>
          </perSys:NetworkTraficStats>
        </perSys:Network>
        <perSys:Time>
          <perSys:TimeCharacteristics>
            <perSys:ConnexionTime rdf:datatype="http://www.w3.org/2001/XMLSchema#time"
            >15:00:00</perSys:ConnexionTime>
            <perSys:ConnexionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
            >2015-02-06</perSys:ConnexionDate>
          </perSys:TimeCharacteristics>
        </perSys:Time>
        <perSys:Preferences>
          <perSys:PreferencesCharacteristics>
            <perSys:PreferencesName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >preferencesName</perSys:PreferencesName>
            <perSys:PreferencesType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >preferencesValue</perSys:PreferencesType>
          </perSys:PreferencesCharacteristics>
        </perSys:Preferences>
        <perSys:Device>
          <perSys:DeviceCharacteristics>
            <perSys:DeviceName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Bekri-Laptop</perSys:DeviceName>
            <perSys:DeviceType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Laptop</perSys:DeviceType>
          </perSys:DeviceCharacteristics>
        </perSys:Device>
        <perSys:Location>
          <perSys:LocationCharacteristics>
            <perSys:SpecificLocation rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Gate 23</perSys:SpecificLocation>
            <perSys:FeatureName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Pittsburgh International Airport</perSys:FeatureName>
          </perSys:LocationCharacteristics>
        </perSys:Location>
      </perSys:NetworkContext>
    </perSys:HasNetworkContext>
    <perSys:HasNetworkContext>
      <perSys:NetworkContext rdf:about="file:///home/taylorj/lm333">
        <perSys:Rules>
          <perSys:NetworkSecurity>
            <perSys:NetworkSecurityState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Open Network</perSys:NetworkSecurityState>
            <perSys:NetworkKey rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >No Key</perSys:NetworkKey>
          </perSys:NetworkSecurity>
        </perSys:Rules>
        <perSys:Network>
          <perSys:NetworkCharacteristics>
            <perSys:SubNetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >HSDPA</perSys:SubNetworkType>
            <perSys:NetworkState rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >CONNECTED</perSys:NetworkState>
            <perSys:Bandwidth rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >256</perSys:Bandwidth>
            <perSys:LinkSpeed rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >65</perSys:LinkSpeed>
            <perSys:NetworkAvailability rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Available</perSys:NetworkAvailability>
            <perSys:NetworkName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >AF23_WI-FI_9F1B</perSys:NetworkName>
          </perSys:NetworkCharacteristics>
        </perSys:Network>
        <perSys:Network>
          <perSys:NetworkTraficStats>
            <perSys:NumberBytesReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >15599522</perSys:NumberBytesReceived>
            <perSys:NumberPacketsReceived rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >24922</perSys:NumberPacketsReceived>
            <perSys:NumberBytesTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >4111415</perSys:NumberBytesTransmited>
            <perSys:NumberPacketsTransmited rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
            >26455</perSys:NumberPacketsTransmited>
            <perSys:NetworkUpload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >0</perSys:NetworkUpload>
            <perSys:NetworkType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Connected</perSys:NetworkType>
            <perSys:NetworkDownload rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >1</perSys:NetworkDownload>
            <perSys:TotalData rdf:datatype="http://www.w3.org/2001/XMLSchema#float"
            >1</perSys:TotalData>
          </perSys:NetworkTraficStats>
        </perSys:Network>
        <perSys:Time>
          <perSys:TimeCharacteristics>
            <perSys:ConnexionTime rdf:datatype="http://www.w3.org/2001/XMLSchema#time"
            >15:00:00</perSys:ConnexionTime>
            <perSys:ConnexionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
            >2015-02-06</perSys:ConnexionDate>
          </perSys:TimeCharacteristics>
        </perSys:Time>
        <perSys:Preferences>
          <perSys:PreferencesCharacteristics>
            <perSys:PreferencesName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >preferencesName</perSys:PreferencesName>
            <perSys:PreferencesType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >preferencesValue</perSys:PreferencesType>
          </perSys:PreferencesCharacteristics>
        </perSys:Preferences>
        <perSys:Device>
          <perSys:DeviceCharacteristics>
            <perSys:DeviceName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Bekri-Laptop</perSys:DeviceName>
            <perSys:DeviceType rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Laptop</perSys:DeviceType>
          </perSys:DeviceCharacteristics>
        </perSys:Device>
        <perSys:Location>
          <perSys:LocationCharacteristics>
            <perSys:SpecificLocation rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Gate 15</perSys:SpecificLocation>
            <perSys:FeatureName rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >Pittsburgh International Airport</perSys:FeatureName>
          </perSys:LocationCharacteristics>
        </perSys:Location>
      </perSys:NetworkContext>
    </perSys:HasNetworkContext>
  </perSys:PervasiveContext>
</rdf:RDF>

Solution

  • The second rule is wrong

    Your second rule is broken because there's no connection between the value of the d2 and d3 variables:

    [AdaptedModel: 
    (?d2 rdf:type perSys:NetworkCharacteristics),
    (?d2 perSys:Bandwidth '450'^^xsd:float)
    (?d3 perSys:SpecificLocation ?a)
    ->
    (?d3 perSys:AdaptedSpecificLocation ?a)]
    

    This roughly says:

    If some X has type NetworkCharacterstics and has bandwidth 450, and some Y has a specific location Z, then Y has an adapted specific location Z.

    There's no connection between X (which has the constraint about 450), so everything with a specific location gets connected to an adapted specific location.

    Why the first rule doesn't work, and how to fix it

    Let's look at the part of your data where 450 appears, and where perSys:SpecificLocations appear. It's easier to read in the N3 serialization:

    <.../n1111>
            a                   perSys:NetworkContext ;
            perSys:Location     [ a                        perSys:LocationCharacteristics ;
                                  perSys:FeatureName       "Pittsburgh International Airport"^^<http://www.w3.org/2001/XMLSchema#string> ;
                                  perSys:SpecificLocation  "Gate 23"^^<http://www.w3.org/2001/XMLSchema#string>
                                ] ;
            perSys:Network      [ a                           perSys:NetworkCharacteristics ;
                                  perSys:Bandwidth            "450"^^<http://www.w3.org/2001/XMLSchema#float> ;
                                  perSys:LinkSpeed            "65"^^<http://www.w3.org/2001/XMLSchema#float> ;
                                  perSys:NetworkAvailability  "Available"^^<http://www.w3.org/2001/XMLSchema#string> ;
                                  perSys:NetworkName          "AF23_WI-FI_9F1B"^^<http://www.w3.org/2001/XMLSchema#string> ;
                                  perSys:NetworkState         "CONNECTED"^^<http://www.w3.org/2001/XMLSchema#string> ;
                                  perSys:SubNetworkType       "HSDPA"^^<http://www.w3.org/2001/XMLSchema#string>
                                ] ;
    

    The thing that has bandwidth 450 is a blank node and has type perSys:NetworkCharacteristics, so it can match the first two parts of the rule. It doesn't have a perSys:SpecificLocation, though, so you can't match the third part. The thing that has a perSys:SpecificLocation is another blank node.

    I don't know where you actually want to put the perSys:AdaptedSpecificLocation triple (i.e., I don't know what the subject is supposed to be), but you'll need to match the shape of your data with something like this:

    [(?context perSys:Location ?location)
     (?location perSys:SpecificLocation ?specificLocation)
     (?context perSys:Network ?network)
     (?network perSys:Bandwidth '450'^^xsd:float)
    ->
     (?location perSys:AdaptedSpecificLocation ?specificLocation)]
    

    If you were to draw the graph structure, it would look like the following. The solid lines are the relationships that are in your data, and the dashed lines are the ones that will be inferred by the rule.

    depiction of rule

    See that you have to match the whole structure of the data? There's a context, and it has a location and a network. The location has a specific location, and the network has a bandwidth. When all of those match, you can say that the location has an adapted specific location. This is how you have to "connect" the variables; they need to be related to each other somehow.