jenajena-rules

Create a new object using the inference rules


I have a semantic network. Is it possible to use a jena framework to create a new object in the semantic web based on some rule. For example there is an object has a certain property, then you need to create a new object and make a connection between them. Is it possible?


Solution

  • If you needs are reasonably simple you can use SPARQL CONSTRUCT queries, i.e.

    CONSTRUCT { ?p :hasGrandfather ?g . }
    
    WHERE {
       ?p      :hasParent ?parent .
       ?parent :hasParent ?g .
       ?g      :gender    :male .
    }
    

    will cause triples to be generated for stating grandfather relations.

    If your needs are more sophisticated, you can achieve this with SHACL for which an implementation on top of Jena exists. I will give a brief example. Assume you have the following RDF data:

    @prefix ex: <http://example.com/ns#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    
    ex:InvalidRectangle
        a ex:Rectangle .
    
    ex:NonSquareRectangle
        a ex:Rectangle ;
        ex:height 2 ;
        ex:width 3 .
    
    ex:SquareRectangle
        a ex:Rectangle ;
        ex:height 4 ;
        ex:width 4 . 
    

    for which you define the following shape file:

    @prefix ex: <http://example.com/ns#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix dash: <http://datashapes.org/dash#> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    
    ex:Rectangle
        a rdfs:Class, sh:NodeShape ;
        rdfs:label "Rectangle" ;
        sh:property [
            sh:path ex:height ;
            sh:datatype xsd:integer ;
            sh:maxCount 1 ;
            sh:minCount 1 ;
            sh:name "height" ;
        ] ;
        sh:property [
            sh:path ex:width ;
            sh:datatype xsd:integer ;
            sh:maxCount 1 ;
            sh:minCount 1 ;
            sh:name "width" ;
        ] ;
        sh:rule [
            a sh:TripleRule ;
            sh:subject sh:this ;
            sh:predicate rdf:type ;
            sh:object ex:Square ;
            sh:condition ex:Rectangle ;
            sh:condition [
                sh:property [
                    sh:path ex:width ;
                    sh:equals ex:height ;
                ] ;
            ] ;
        ] .
    

    It will generate the following RDF data:

    @prefix owl:   <http://www.w3.org/2002/07/owl#> .
    @prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
    @prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
    
    <http://example.com/ns#SquareRectangle>
            a       <http://example.com/ns#Square> .
    

    which you can add to your RDF store.

    This example with code can be found here, as well as a more advanced example.