protegeproject / swrlapi

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

creating SWRL rule using imported ontologies #25

Closed zzw95 closed 7 years ago

zzw95 commented 7 years ago

Hi, There is a class People in family.owl and a class PeopleA in familyA.owl, and map.owl imports family.owl and familyA.owl .And I want to map the content in between (family -> familyA) .So I create some swrl rules in map.owl. But there is an exception like "Exception in thread "main" org.swrlapi.parser.SWRLParseException: Invalid SWRL atom predicate 'A'". Cheers.

Code:

        OWLOntologyManager m=OWLManager.createOWLOntologyManager();
        File owlFile = new File("C:\\Users\\zzw\\Desktop\\Ontology\\map.owl");
        File file1 = new File("C:\\Users\\zzw\\Desktop\\Ontology\\family.owl");
        File file2 = new File("C:\\Users\\zzw\\Desktop\\Ontology\\familyA.owl");

        IRI iri1= IRI.create("http://semanticweb.org/zzw/ontologies/2016/10/family");
        IRI iri2= IRI.create("http://semanticweb.org/zzw/ontologies/2016/11/familyA");
        m.getIRIMappers().add(new SimpleIRIMapper(iri1,                 IRI.create("C:\\Users\\zzw\\Desktop\\Ontology\\family.owl")));
        m.getIRIMappers().add(new SimpleIRIMapper(iri2, IRI.create("C:\\Users\\zzw\\Desktop\\Ontology\\familyA.owl")));
        OWLOntology onto1=m.loadOntologyFromOntologyDocument(file1);
        OWLOntology onto2=m.loadOntologyFromOntologyDocument(file2);
        OWLOntology onto=m.loadOntologyFromOntologyDocument(owlFile);
        IRI iri=onto.getOntologyID().getOntologyIRI().get();

        OWLDataFactory factory=m.getOWLDataFactory();
        OWLImportsDeclaration importDeclaration1 = factory.getOWLImportsDeclaration(iri1);
        OWLImportsDeclaration importDeclaration2 = factory.getOWLImportsDeclaration(iri2);
        AddImport addImport1=new AddImport(onto, importDeclaration1);
        AddImport addImport2=new AddImport(onto, importDeclaration2);
        m.saveOntology(onto);

       SWRLRuleEngine swrlRuleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);
       swrlRuleEngine.createSWRLRule("rule1", "People(?p)->PeopleA(?p)");
       SQWRLQueryEngine queryEngine = SWRLAPIFactory.createSQWRLQueryEngine(ontology);
       String query="People(?p)->sqwrl:select(?p)";
       SQWRLResult result= queryEngine.runSQWRLQuery("output",query);
       System.out.println(result.toString());;
cedaradmin commented 7 years ago

The error does not correspond to the query. The parser is complaining about a reference to an OWL entity A that is not present in the associated ontology.

csnyulas commented 7 years ago

I would just add to @cedaradmin 's comment that you should tell us in which line is the exception thrown, because it is not obvious.

Also it would be very helpful if we could see the ontologies, because the problem may lie there.

And finally, if the map.owl ontology imports the other two, than you will need to assign some prefixes to the namespaces of entities in family.owl and familyA.owl ontologies (if you haven't already), and use the prefixed name of the entities in your SWRL rule. For example, if you define the prefix fam for http://semanticweb.org/zzw/ontologies/2016/10/family#, and fama for http://semanticweb.org/zzw/ontologies/2016/11/familyA#, your SWRL rule should like like:

fam:People(?p)->fama:PeopleA(?p)
zzw95 commented 7 years ago

Thanks! I make it if I assign the prefixes of impogies

csnyulas commented 7 years ago

@zzw95 some parts of your message got lost. Should we understand that it works if you set prefixes for your imported ontologies, and use those prefixes in your rule? If so, can you please close this issue? (Or tell us and we will close it)

Ebiyejames commented 7 years ago

Hello,

I am a research student and new to ontology, protege, SWRL and SQWRL.

I saw this code in github and test it to see if I could use the ideas from this code to solve my problem but I am having the following runtime errors: I will be grateful for any help to solve this problem.

Kind regards Ebiye James

Runtime error Exception in thread "main" org.swrlapi.sqwrl.exceptions.SQWRLInvalidColumnNameException: invalid column name name at org.swrlapi.factory.DefaultSQWRLResultManager.checkColumnName(DefaultSQWRLResultManager.java:949) at org.swrlapi.factory.DefaultSQWRLResultManager.getColumnIndex(DefaultSQWRLResultManager.java:674) at org.swrlapi.factory.DefaultSQWRLResultManager.getValue(DefaultSQWRLResultManager.java:370) at org.swrlapi.factory.DefaultSQWRLResultManager.hasNamedIndividualValue(DefaultSQWRLResultManager.java:520) at org.swrlapi.factory.DefaultSQWRLResultManager.getNamedIndividual(DefaultSQWRLResultManager.java:404) at SQWRLQuery.main(SQWRLQuery.java:29)

See code public class SQWRLQuery { public static void main(String[] args) throws OWLOntologyCreationException, SQWRLException, SWRLParseException{ OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(new File("MyPerson.owl"));

    SQWRLQueryEngine queryEngine = SWRLAPIFactory.createSQWRLQueryEngine(ontology);

    queryEngine.createSQWRLQuery("Q1", "Person(?p) -> sqwrl:select(?p)");

    SQWRLResult result = queryEngine.runSQWRLQuery("Q1");

    while (result.next()) {
        //SQWRLNamedIndividualResultValue nameValue = result.getNamedIndividual("name");
        //SQWRLNamedIndividualResultValue salaryValue = result.getNamedIndividual("salary");
          System.out.println("Name: " + ((Object) result.getNamedIndividual("name")).toString());
          System.out.println("Name: " + "has committed");
          System.out.println("Name: " + ((Object) result.getNamedIndividual("name")).toString());
         }

}

}

martinjoconnor commented 7 years ago

The example on the wiki for result processing was a little misleading. I have updated it here:

https://github.com/protegeproject/swrlapi/wiki/SQWRLQueryAPI#processing-sqwrl-query-results

You need to match the names of the columns to the variables used in the select statement.