Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------Code---------------------------------------
- public class Main {
- public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
- // Create OWLOntology instance using the OWLAPI
- OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
- // Load the ontology
- OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(
- new File("/home/satya/owlapi/owlapi_swrl/src/main/resources/test.rdf"));
- // Prefix manager
- PrefixManager prefixManager = ontology.getOWLOntologyManager().getOntologyFormat(ontology).asPrefixOWLDocumentFormat();
- // Define prefixes
- prefixManager.setPrefix("dexpi", "http://www.website.com/dexpi/");
- prefixManager.setPrefix("lis", "http://rds.posccaesar.org/ontology/lis14/rdl/");
- prefixManager.setPrefix("swrlb", "http://www.w3.org/2003/11/swrlb#");
- System.out.println(" Axioms: " + ontology.getAxiomCount() + "\n Format:"
- + ontologyManager.getOntologyFormat(ontology) + "\n Classes:" + ontology.getClassesInSignature().size()
- + "\n Object Properties:" + ontology.getObjectPropertiesInSignature().size() + "\n DataProperties:"
- + ontology.getDataPropertiesInSignature().size() + "\n Individuals: "
- + ontology.getIndividualsInSignature().size());
- System.out.println();
- OWLDataFactory factory = ontologyManager.getOWLDataFactory();
- // "dexpi:Pump(?p)^ lis:datumValue(?p,?v) ^ swrlb:greaterThan(?v,10)-> dexpi:EjectorPump(?p)");
- OWLClass clsPump = factory.getOWLClass(prefixManager.getIRI("dexpi:Pump"));
- OWLClass clsRotaryPump = factory.getOWLClass(prefixManager.getIRI("dexpi:EjectorPump"));
- OWLDataProperty datumProperty = factory.getOWLDataProperty(prefixManager.getIRI("lis:datumValue"));
- SWRLVariable pump = factory.getSWRLVariable(prefixManager.getIRI("dexpi:pump"));
- SWRLVariable value = factory.getSWRLVariable(prefixManager.getIRI("dexpi:value"));
- Set<SWRLAtom> antecedant = new HashSet<SWRLAtom>();
- antecedant.add(factory.getSWRLClassAtom(clsPump, pump));
- antecedant.add(factory.getSWRLDataPropertyAtom(datumProperty, pump, value));
- List<SWRLDArgument> args1 = new ArrayList<SWRLDArgument>(2);
- args1.add(value);
- OWLLiteral arg = factory.getOWLLiteral(5.0);
- args1.add(factory.getSWRLLiteralArgument(arg));
- antecedant.add(factory.getSWRLBuiltInAtom(prefixManager.getIRI("swrlb:greaterThan"), args1));
- Set<SWRLAtom> consequent = new HashSet<SWRLAtom>();
- consequent.add(factory.getSWRLClassAtom(clsRotaryPump, pump));
- SWRLRule rule1 = factory.getSWRLRule(antecedant, consequent);
- ontologyManager.applyChange(new AddAxiom(ontology, rule1));
- OpenlletReasoner reasoner = OpenlletReasonerFactory.getInstance().createReasoner(ontology);
- // Ask the reasoner to do all the necessary work now
- reasoner.precomputeInferences(InferenceType.values());
- List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<>();
- gens.add(new InferredEntityAxiomGenerator<OWLClass, OWLClassAssertionAxiom>() {
- @Override
- protected Stream<OWLClass> getEntities(OWLOntology ont) {
- return Stream.of(factory.getOWLClass(IRI.create("dexpi:EjectorPump")));
- }
- @Override
- protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory,
- Set<OWLClassAssertionAxiom> result) {
- reasoner.getInstances(entity).entities().map(x -> factory.getOWLClassAssertionAxiom(entity, x))
- .forEach(result::add);
- }
- @Override
- public String getLabel() {
- // TODO Auto-generated method stub
- return "Instances of EjectorPump";
- }
- });
- InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,gens);
- iog.fillOntology(factory, ontology);
- String answer = "";
- if (reasoner.isConsistent()) {
- if (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() > 0) {
- answer = "Ontology FAILED satisfiability test. Unsatisfiable classes detected: "
- + reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size();
- }
- answer = "Ontology PASSED the consistency test";
- ontologyManager.saveOntology(ontology);
- } else {
- answer = "Ontology FAILED the consistency test, please review the Axioms or debug using Protege";
- }
- System.out.println(answer);
- System.out.println(prefixManager.getIRI("dexpi:pump"));
- System.out.println("Hello");
- }
- }
- -----------------------------------------Sample test.rdf----------------------------------------------
- <?xml version="1.0"?>
- <rdf:RDF xmlns="http://www.website.com/dexpi"
- xml:base="http://www.website.com/dexpi"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:owl="http://www.w3.org/2002/07/owl#"
- xmlns:pav="http://purl.org/pav/"
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
- xmlns:rdl="http://rds.posccaesar.org/ontology/lis14/rdl/"
- xmlns:xml="http://www.w3.org/XML/1998/namespace"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
- xmlns:foaf="http://xmlns.com/foaf/0.1/"
- xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
- xmlns:skos="http://www.w3.org/2004/02/skos/core#"
- xmlns:dexpi="http://www.website.com/dexpi#"
- xmlns:terms="http://purl.org/dc/terms/">
- <owl:Ontology rdf:about="http://www.website.com/dexpi">
- <owl:imports rdf:resource="http://rds.posccaesar.org/ontology/lis14/ont/core/1.0"/>
- </owl:Ontology>
- <!--
- ///////////////////////////////////////////////////////////////////////////////////////
- //
- // Classes
- //
- ///////////////////////////////////////////////////////////////////////////////////////
- -->
- <!-- http://www.website.com/dexpi/EjectorPump -->
- <owl:Class rdf:about="http://www.website.com/dexpi/EjectorPump">
- <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/Pump"/>
- </owl:Class>
- <!-- http://www.website.com/dexpi/Equipment -->
- <owl:Class rdf:about="http://www.website.com/dexpi/Equipment">
- <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/NozzleOwner"/>
- </owl:Class>
- <!-- http://www.website.com/dexpi/NozzleOwner -->
- <owl:Class rdf:about="http://www.website.com/dexpi/NozzleOwner"/>
- <!-- http://www.website.com/dexpi/Pump -->
- <owl:Class rdf:about="http://www.website.com/dexpi/Pump">
- <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/Equipment"/>
- </owl:Class>
- <!--
- ///////////////////////////////////////////////////////////////////////////////////////
- //
- // Individuals
- //
- ///////////////////////////////////////////////////////////////////////////////////////
- -->
- <!-- http://www.website.com/dexpi/satya-pump -->
- <owl:NamedIndividual rdf:about="http://www.website.com/dexpi/satya-pump">
- <rdf:type rdf:resource="http://www.website.com/dexpi/Pump"/>
- <rdl:datumValue rdf:datatype="http://www.w3.org/2001/XMLSchema#double">10.0</rdl:datumValue>
- </owl:NamedIndividual>
- </rdf:RDF>
- <!-- Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi -->
Advertisement
Add Comment
Please, Sign In to add comment