package de.dlr.sl.reasoner; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.SimpleIRIMapper; import org.swrlapi.core.SWRLRuleEngine; import org.swrlapi.factory.SWRLAPIFactory; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Stack; public class InferenceEngine { public static void main(String[] args) throws IOException { // File inputFile = new File("D:/GitRepository/my-awesome-project/production/src/databases/reasoner_input.txt"); File inputFile = new File(args[0]); BufferedReader reader = new BufferedReader(new FileReader(inputFile)); Stack filesToImport = new Stack<>(); String line; while((line = reader.readLine()) != null) { filesToImport.add(new File(line)); } reader.close(); File ontologyFile = filesToImport.pop(); OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = manager.getOWLDataFactory(); OWLOntology ontology; List ontologies = new ArrayList<>(); List imports = new ArrayList<>(); try { for (File file: filesToImport){ OWLOntology tempOntology = manager.loadOntologyFromOntologyDocument(file); IRI tempIRI = tempOntology.getOntologyID().getOntologyIRI().get(); manager.getIRIMappers().add(new SimpleIRIMapper(tempIRI, IRI.create(file))); ontologies.add(tempOntology); imports.add(dataFactory.getOWLImportsDeclaration(tempIRI)); } ontology = manager.loadOntologyFromOntologyDocument(ontologyFile); List importsOntology = new ArrayList<>(); for (OWLImportsDeclaration importDeclaration: imports){ importsOntology.add(new AddImport(ontology, importDeclaration)); } manager.saveOntology(ontology); SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology); // System.out.println("Rule engine created"); ruleEngine.infer(); manager.saveOntology(ontology); System.out.println("Rule engine worked and the ontology has been updated!"); } catch (OWLOntologyCreationException exception) { exception.printStackTrace(); } catch (OWLOntologyStorageException e) { e.printStackTrace(); } } }