protegeproject / swrlapi

Java API for working with the SWRL rule and SQWRL query languages
Other
99 stars 40 forks source link

SWRL can infer in protege, but can't infer in swrlruleengine #72

Closed liuchang-999 closed 3 years ago

liuchang-999 commented 3 years ago

I wrote a SWRL rule. In protege, I can infer mayhaspreservice. However, the property mayhaspreservice can not be found after using the infer() method in swrlruleengine.

OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(new File(path));    
SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);

SWRLAPIRule rule = ruleEngine.createSWRLRule("r1","ServiceChain(?sc) ^ Service(?s) ^ hasInput(?sc, ?in1) ^ label(?s, "RadiometricCalibration") ^radiometricResolution(?in1, ?rr1) ^ hasInput(?sc, ?in2) ^ radiometricResolution(?in2, ?rr2) ^ swrlb:equal(?rr1, ?rr2) -> mayhasPreService(?sc,?s)");
ruleEngine.infer();

SQWRLQueryEngine queryEngine = SWRLAPIFactory.createSQWRLQueryEngine(ontology); 
SQWRLResult result = queryEngine.runSQWRLQuery("q","ServiceChain(?sc) ^ mayhasPreService(?sc,?s) -> sqwrl:select(?sc,?s)");

I cannot view the query result mayhasPreService.

I tried "r2" again, and this rule yielded results. It seems to be a problem with my swrl syntax。

SWRLAPIRule rule2 =ruleEngine.createSWRLRule("r2", "ServiceChain(?sc) ^ Service(?s) ^ label(?s, "RadiometricCalibration") -> mayhasPreService(?sc, ?s)");

How should I change the syntax of the “r1”?thank you!

martinjoconnor commented 3 years ago

Have you tried running the query interactively in Protege?

Try that and if it still does not work, post the ontology here.

liuchang-999 commented 3 years ago
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#"
     xml:base="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
     xmlns:swrl="http://www.w3.org/2003/11/swrl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:untitled-ontology-37="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#"
     xmlns:swrla="http://swrl.stanford.edu/ontologies/3.3/swrla.owl#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37"/>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Annotation properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled -->

    <owl:AnnotationProperty rdf:about="http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled"/>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#hasInput -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#hasInput"/>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#hasOutput -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#hasOutput"/>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#mayhasPreService -->

    <owl:ObjectProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#mayhasPreService"/>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#label -->

    <owl:DatatypeProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#label">
        <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    </owl:DatatypeProperty>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#radiometricResolution -->

    <owl:DatatypeProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#radiometricResolution">
        <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    </owl:DatatypeProperty>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#time -->

    <owl:DatatypeProperty rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#time">
        <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
    </owl:DatatypeProperty>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Data -->

    <owl:Class rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Data"/>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#PreService -->

    <owl:Class rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#PreService"/>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Service -->

    <owl:Class rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Service"/>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#ServiceChain -->

    <owl:Class rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#ServiceChain"/>

    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#BHJC -->

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#BHJC">
        <rdf:type rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#ServiceChain"/>
        <hasInput rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image1"/>
        <hasInput rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image2"/>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image1 -->

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image1">
        <rdf:type rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Data"/>
        <radiometricResolution>like</radiometricResolution>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image2 -->

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#image2">
        <rdf:type rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Data"/>
        <radiometricResolution>like</radiometricResolution>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#rrService1 -->

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#rrService1">
        <rdf:type rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Service"/>
         <label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">RadiometricCalibration</label>
        <time rdf:datatype="http://www.w3.org/2001/XMLSchema#int">10</time>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#rrService2 -->

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#rrService2">
        <rdf:type rdf:resource="http://www.semanticweb.org/lc/ontologies/2021/5/untitled-ontology-37#Service"/>
         <label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">RadiometricCalibration</label>
        <time rdf:datatype="http://www.w3.org/2001/XMLSchema#int">5</time>
    </owl:NamedIndividual>
</rdf:RDF>

<!-- Generated by the OWL API (version 4.2.8.20170104-2310) https://github.com/owlcs/owlapi -->
martinjoconnor commented 3 years ago

This OWL file does not contain any SWRL rules or SQWRL queries? (Also, I would add as an attachment the next time. Change the .owl extension to .txt because GitHub does not support .owl file extensions on upload.)

When I manually added a rule I got the following exception:

[Line: 1, Column: 1]: built-in exception thrown by built-in swrlb:equal in rule S1: exception thrown by built-in swrlb:equal in rule S1: unsupported argument type: expecting boolean, numeric, string, xsd:date, xsd:dateTime, xsd:time, or xsd:duration argument for comparison, got "like"^^rdf:PlainLiteral for (0-offset) argument #0: exception thrown by built-in swrlb:equal in rule S1: unsupported argument type: expecting boolean, numeric, string, xsd:date, xsd:dateTime, xsd:time, or xsd:duration argument for comparison, got "like"^^rdf:PlainLiteral for (0-offset) argument #0: unsupported argument type: expecting boolean, numeric, string, xsd:date, xsd:dateTime, xsd:time, or xsd:duration argument for comparison, got "like"^^rdf:PlainLiteral for (0-offset) argument #0

I would suggest experimenting with rule creation and debugging in Protege first.

martinjoconnor commented 3 years ago

You also appear to be using a very old version of Protege. I would upgrade to the latest version.

liuchang-999 commented 3 years ago

Thank you very much for your advice,I solved it. I use protege 5.2.I found that the attribute "label" of my individual "rrservice1" was not added xsd:string .