Search code examples
javaeclipsejenaontologyjena-rules

Jena API Rules Test : how many and what are the methods to write rules in Jena?


I succeeded in creating, modifying, merging ontologies with Jena API and Eclipse. I succeeded also in starting the OWL reasoner and Pellet reasoner in Eclipse.

Now I would like to understand how can I write a simple rule in Eclipse using Jena API and if there is one or more methods for doing this.

For example, in Protégé I used to write a rule in SWRL like this, to calculate the mean of a triangular distribution:

TriangularDistribution(?t), hasLowerLimit(?t,?a), hasUpperLimit(?t,?c), hasMode(?t,?b), add(?ab, ?a, ?b), add(?abc, ?ab, ?c), divide(?m, ?abc, 3) -> hasMean(?t, ?m)

How can I translate it using Jena?

======== UPGRADE =========

In order to better understand how can I create rules in Jena, I followed the example in https://jena.apache.org/documentation/inference/#RULEexamples

I created three files: The first is "ReasoningJena.java"

    package test01;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
import com.hp.hpl.jena.util.*;
import java.io.*;

public class ReasoningJena {
    private static Model reason(Model input) {

        // Register a namespace to be used in the rules
        String flUri = "http://www.snee.com/ns/demo#";
        PrintUtil.registerPrefix("fl", flUri);

        // Create an (RDF) specification of a hybrid reasoner which loads its rules from an external file.
        Model m = ModelFactory.createDefaultModel();
        Resource configuration =  m.createResource();
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
        configuration.addProperty(ReasonerVocabulary.PROPruleSet,  "C:/Users/pecore/Desktop/file.rules");

        // Create an instance of such a reasoner
        Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);

        // Infere new knowledge on the input model, generating a new one
        InfModel infmodel = ModelFactory.createInfModel(reasoner, input);

        return infmodel;
        }


        private static Model readModelFromFile(String filePath) throws Exception {

        // Create an empty model
        Model model = ModelFactory.createDefaultModel();

        // Use the FileManager to find the input file
        InputStream in = new FileInputStream(new File(filePath));

        // Read the RDF/XML file
        model.read(in, "");

        return model;
        }

        public static void main(String[] args){
            try {



                Model model = readModelFromFile("C:/Users/pecore/Desktop/demoData.rdf");
                System.out.println("The model has " + model.size() + " statements");
                // Do the reasoning
                model = reason(model);
                System.out.println("After inferencing the model has " + model.size() + " statements");


            }
            catch(Exception e) {
                e.printStackTrace();
            }  
        }
     }

The rdf file is : "demoData.rdf"

  <?xml version="1.0"?>

<!DOCTYPE rdf:RDF [
    <!ENTITY rdf  'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
    <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>
    <!ENTITY demo 'http://jena.hpl.hp.com/demo#'>
]>

<rdf:RDF xmlns:rdf="&rdf;" 
         xmlns:rdfs="&rdfs;" 
         xmlns:demo="&demo;"
         xmlns="&demo;"
         >

    <demo:TransProp rdf:about="&demo;p" />

    <rdf:Description rdf:about="&demo;a">
        <p rdf:resource="&demo;b" />
    </rdf:Description>

    <rdf:Description rdf:about="&demo;c">
        <p rdf:resource="&demo;a" />
    </rdf:Description>

    <rdf:Description rdf:about="&demo;b">
        <p rdf:resource="&demo;d" />
    </rdf:Description>

</rdf:RDF>

And the rules file is : "file.rules"

@prefix = "http://www.snee.com/ns/demo#";
[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A > demo:p ?C) ];

I run all in Eclipse SW Version: Luna Service Release 2 (4.4.2) with JDK 1.7 And I obtained these errors:

 log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The model has 4 statements
com.hp.hpl.jena.shared.PrefixMapping$IllegalPrefixException: =

    at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.checkLegal(PrefixMappingImpl.java:157)
    at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.setNsPrefix(PrefixMappingImpl.java:67)
    at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.setNsPrefixes(PrefixMappingImpl.java:147)
    at com.hp.hpl.jena.reasoner.rulesys.Rule$Parser.registerPrefixMap(Rule.java:716)
    at com.hp.hpl.jena.reasoner.rulesys.Rule.rulesParserFromReader(Rule.java:567)
    at com.hp.hpl.jena.reasoner.rulesys.Util.loadRuleParserFromResourceFile(Util.java:264)
    at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.loadRules(FBRuleReasoner.java:270)
    at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner.doSetParameter(GenericRuleReasoner.java:301)
    at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.doSetRDFNodeParameter(FBRuleReasoner.java:370)
    at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.loadConfiguration(FBRuleReasoner.java:101)
    at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner.<init>(GenericRuleReasoner.java:96)
    at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasonerFactory.create(GenericRuleReasonerFactory.java:56)
    at test01.ReasoningJena.reason(ReasoningJena.java:24)
    at test01.ReasoningJena.main(ReasoningJena.java:55)

Why?

=========== UPDATE 2 ============ I made some changes on "file.rules" internal code correcting it as:

@prefix demo: <http://domain/demo#>.
[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A > demo:p ?C) ]

and I obtained this error code:

 log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    org.apache.jena.riot.RiotException: [line: 1, col: 8 ] The processing instruction target matching "[xX][mM][lL]" is not allowed.
        at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
        at org.apache.jena.riot.lang.LangRDFXML$ErrorHandlerBridge.fatalError(LangRDFXML.java:253)
        at com.hp.hpl.jena.rdfxml.xmlinput.impl.ARPSaxErrorHandler.fatalError(ARPSaxErrorHandler.java:48)
        at com.hp.hpl.jena.rdfxml.xmlinput.impl.XMLHandler.warning(XMLHandler.java:200)
        at com.hp.hpl.jena.rdfxml.xmlinput.impl.XMLHandler.fatalError(XMLHandler.java:230)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.scanPIData(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanPIData(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.scanPI(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at com.hp.hpl.jena.rdfxml.xmlinput.impl.RDFXMLParser.parse(RDFXMLParser.java:151)
        at com.hp.hpl.jena.rdfxml.xmlinput.ARP.load(ARP.java:119)
        at org.apache.jena.riot.lang.LangRDFXML.parse(LangRDFXML.java:143)
        at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTLang.read(RDFParserRegistry.java:185)
        at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:906)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:257)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:243)
        at org.apache.jena.riot.adapters.RDFReaderRIOT_Web.read(RDFReaderRIOT_Web.java:62)
        at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:247)
        at zero.readModelFromFile(zero.java:40)
        at zero.main(zero.java:50)

Solution

  • There are a few different questions here. There are lots of examples on Stack Overflow and elsewhere of how to write Jena rules, so I think the key question here is how to rewrite this specific SWRL rule. Without your showing any attempts, we don't know what problems you were running into, but I think that a SWRL rule like:

    TriangularDistribution(?t), hasLowerLimit(?t,?a), hasUpperLimit(?t,?c), hasMode(?t,?b), add(?ab, ?a, ?b), add(?abc, ?ab, ?c), divide(?m, ?abc, 3) → hasMean(?t, ?m)

    would simply become something like this:

    @prefix : <...>
    
    [(?x rdf:type TriangularDistribution)
     (?t :hasLowerLimit ?a)
     (?t :hasUpperLimit ?c)
     (?t :hasMode ?b)
     sum(?a, ?b, ?ab)
     add(?ab, ?c, ?abc)
     quotient(?abc, 3, ?m)
     ->
     (?t :hasMean ?m)]
    

    I didn't remember the math builtins in Jena, so I looked at the list of builtin primitives.