Guest User

Code and Ontology Sample

a guest
Aug 2nd, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. --------------------------------Code---------------------------------------
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
  6.  
  7.         // Create OWLOntology instance using the OWLAPI
  8.         OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
  9.         // Load the ontology
  10.         OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(
  11.                 new File("/home/satya/owlapi/owlapi_swrl/src/main/resources/test.rdf"));
  12.  
  13. //      Prefix manager
  14.         PrefixManager prefixManager = ontology.getOWLOntologyManager().getOntologyFormat(ontology).asPrefixOWLDocumentFormat();
  15.  
  16.         // Define prefixes
  17.         prefixManager.setPrefix("dexpi", "http://www.website.com/dexpi/");
  18.         prefixManager.setPrefix("lis", "http://rds.posccaesar.org/ontology/lis14/rdl/");
  19.         prefixManager.setPrefix("swrlb", "http://www.w3.org/2003/11/swrlb#");
  20.  
  21.         System.out.println(" Axioms: " + ontology.getAxiomCount() + "\n Format:"
  22.                 + ontologyManager.getOntologyFormat(ontology) + "\n Classes:" + ontology.getClassesInSignature().size()
  23.                 + "\n Object Properties:" + ontology.getObjectPropertiesInSignature().size() + "\n DataProperties:"
  24.                 + ontology.getDataPropertiesInSignature().size() + "\n Individuals: "
  25.                 + ontology.getIndividualsInSignature().size());
  26.         System.out.println();
  27.  
  28.         OWLDataFactory factory = ontologyManager.getOWLDataFactory();
  29.  
  30. //  "dexpi:Pump(?p)^ lis:datumValue(?p,?v) ^ swrlb:greaterThan(?v,10)-> dexpi:EjectorPump(?p)");
  31.  
  32.         OWLClass clsPump = factory.getOWLClass(prefixManager.getIRI("dexpi:Pump"));
  33.         OWLClass clsRotaryPump = factory.getOWLClass(prefixManager.getIRI("dexpi:EjectorPump"));
  34.         OWLDataProperty datumProperty = factory.getOWLDataProperty(prefixManager.getIRI("lis:datumValue"));
  35.  
  36.         SWRLVariable pump = factory.getSWRLVariable(prefixManager.getIRI("dexpi:pump"));
  37.         SWRLVariable value = factory.getSWRLVariable(prefixManager.getIRI("dexpi:value"));
  38.  
  39.         Set<SWRLAtom> antecedant = new HashSet<SWRLAtom>();
  40.         antecedant.add(factory.getSWRLClassAtom(clsPump, pump));
  41.         antecedant.add(factory.getSWRLDataPropertyAtom(datumProperty, pump, value));
  42.  
  43.         List<SWRLDArgument> args1 = new ArrayList<SWRLDArgument>(2);
  44.         args1.add(value);
  45.         OWLLiteral arg = factory.getOWLLiteral(5.0);
  46.         args1.add(factory.getSWRLLiteralArgument(arg));
  47.         antecedant.add(factory.getSWRLBuiltInAtom(prefixManager.getIRI("swrlb:greaterThan"), args1));
  48.  
  49.         Set<SWRLAtom> consequent = new HashSet<SWRLAtom>();
  50.         consequent.add(factory.getSWRLClassAtom(clsRotaryPump, pump));
  51.  
  52.         SWRLRule rule1 = factory.getSWRLRule(antecedant, consequent);
  53.         ontologyManager.applyChange(new AddAxiom(ontology, rule1));
  54.  
  55.         OpenlletReasoner reasoner = OpenlletReasonerFactory.getInstance().createReasoner(ontology);
  56. //      Ask the reasoner to do all the necessary work now
  57.         reasoner.precomputeInferences(InferenceType.values());
  58.        
  59.         List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<>();
  60.         gens.add(new InferredEntityAxiomGenerator<OWLClass, OWLClassAssertionAxiom>() {
  61.             @Override
  62.             protected Stream<OWLClass> getEntities(OWLOntology ont) {
  63.                 return Stream.of(factory.getOWLClass(IRI.create("dexpi:EjectorPump")));
  64.             }
  65.  
  66.             @Override
  67.             protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory,
  68.                     Set<OWLClassAssertionAxiom> result) {
  69.                 reasoner.getInstances(entity).entities().map(x -> factory.getOWLClassAssertionAxiom(entity, x))
  70.                         .forEach(result::add);
  71.             }
  72.  
  73.             @Override
  74.             public String getLabel() {
  75.                 // TODO Auto-generated method stub
  76.                 return "Instances of EjectorPump";
  77.             }
  78.  
  79.         });
  80.  
  81.         InferredOntologyGenerator iog = new  InferredOntologyGenerator(reasoner,gens);
  82.         iog.fillOntology(factory, ontology);
  83.    
  84.         String answer = "";
  85.         if (reasoner.isConsistent()) {
  86.             if (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() > 0) {
  87.                 answer = "Ontology FAILED satisfiability test. Unsatisfiable classes detected: "
  88.                         + reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size();
  89.             }
  90.             answer = "Ontology PASSED the consistency test";
  91.             ontologyManager.saveOntology(ontology);
  92.         } else {
  93.             answer = "Ontology FAILED the consistency test, please review the Axioms or debug using Protege";
  94.         }
  95.         System.out.println(answer);
  96.         System.out.println(prefixManager.getIRI("dexpi:pump"));
  97.         System.out.println("Hello");
  98.     }
  99.  
  100. }
  101.  
  102.  
  103. -----------------------------------------Sample test.rdf----------------------------------------------
  104. <?xml version="1.0"?>
  105. <rdf:RDF xmlns="http://www.website.com/dexpi"
  106.      xml:base="http://www.website.com/dexpi"
  107.      xmlns:dc="http://purl.org/dc/elements/1.1/"
  108.      xmlns:owl="http://www.w3.org/2002/07/owl#"
  109.      xmlns:pav="http://purl.org/pav/"
  110.      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  111.      xmlns:rdl="http://rds.posccaesar.org/ontology/lis14/rdl/"
  112.      xmlns:xml="http://www.w3.org/XML/1998/namespace"
  113.      xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
  114.      xmlns:foaf="http://xmlns.com/foaf/0.1/"
  115.      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  116.      xmlns:skos="http://www.w3.org/2004/02/skos/core#"
  117.      xmlns:dexpi="http://www.website.com/dexpi#"
  118.      xmlns:terms="http://purl.org/dc/terms/">
  119.     <owl:Ontology rdf:about="http://www.website.com/dexpi">
  120.         <owl:imports rdf:resource="http://rds.posccaesar.org/ontology/lis14/ont/core/1.0"/>
  121.     </owl:Ontology>
  122.    
  123.  
  124.  
  125.     <!--
  126.     ///////////////////////////////////////////////////////////////////////////////////////
  127.     //
  128.     // Classes
  129.     //
  130.     ///////////////////////////////////////////////////////////////////////////////////////
  131.      -->
  132.  
  133.    
  134.  
  135.  
  136.     <!-- http://www.website.com/dexpi/EjectorPump -->
  137.  
  138.     <owl:Class rdf:about="http://www.website.com/dexpi/EjectorPump">
  139.         <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/Pump"/>
  140.     </owl:Class>
  141.    
  142.  
  143.  
  144.     <!-- http://www.website.com/dexpi/Equipment -->
  145.  
  146.     <owl:Class rdf:about="http://www.website.com/dexpi/Equipment">
  147.         <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/NozzleOwner"/>
  148.     </owl:Class>
  149.    
  150.  
  151.  
  152.     <!-- http://www.website.com/dexpi/NozzleOwner -->
  153.  
  154.     <owl:Class rdf:about="http://www.website.com/dexpi/NozzleOwner"/>
  155.    
  156.  
  157.  
  158.     <!-- http://www.website.com/dexpi/Pump -->
  159.  
  160.     <owl:Class rdf:about="http://www.website.com/dexpi/Pump">
  161.         <rdfs:subClassOf rdf:resource="http://www.website.com/dexpi/Equipment"/>
  162.     </owl:Class>
  163.    
  164.  
  165.  
  166.     <!--
  167.     ///////////////////////////////////////////////////////////////////////////////////////
  168.     //
  169.     // Individuals
  170.     //
  171.     ///////////////////////////////////////////////////////////////////////////////////////
  172.      -->
  173.  
  174.    
  175.  
  176.  
  177.     <!-- http://www.website.com/dexpi/satya-pump -->
  178.  
  179.     <owl:NamedIndividual rdf:about="http://www.website.com/dexpi/satya-pump">
  180.         <rdf:type rdf:resource="http://www.website.com/dexpi/Pump"/>
  181.         <rdl:datumValue rdf:datatype="http://www.w3.org/2001/XMLSchema#double">10.0</rdl:datumValue>
  182.     </owl:NamedIndividual>
  183. </rdf:RDF>
  184.  
  185.  
  186.  
  187. <!-- Generated by the OWL API (version 4.5.25.2023-02-15T19:15:49Z) https://github.com/owlcs/owlapi -->
  188.  
  189.  
Advertisement
Add Comment
Please, Sign In to add comment