Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Result loadInputOntologyOWLAPI() {
- try {
- inputNamespace = IRI.create(this.outputInputPair.getInput()
- .getObject());
- logger.log("inputNamespace created.. OWL API [ " + inputNamespace.toString() + " ]");
- inputOntology = manager
- .loadOntologyFromOntologyDocument(inputNamespace);
- logger.log("Input ontology loaded in the object.. OWL API");
- } catch (Exception e) {
- return new Result(false, e.getMessage());
- }
- return new Result(true);
- }
- /**
- * Method to load the output ontology using OWLAPI.
- *
- * @return Return the result of the operation.
- * @throws Exception
- * It can throw an exception.
- */
- private Result loadOutputOntologyOWLAPI() {
- try {
- outputNamespace = IRI.create(this.outputInputPair.getOutput()
- .getObject());
- logger.log("inputNamespace created.. OWL API [ " + outputNamespace.toString() + " ]");
- outputOntology = manager
- .loadOntologyFromOntologyDocument(outputNamespace);
- logger.log("Output ontology loaded in the object.. OWL API");
- } catch (Exception e) {
- return new Result(false, e.getMessage());
- }
- return new Result(true);
- }
- /**
- * Method to create the instances using Jena.
- *
- * @param mode
- * Receives the mode (create the instace on input or output class
- * and subclasses)
- */
- private void createInstancesUsingOWLAPI() {
- /*
- * First, we create the list of individuals.
- */
- individualsCreatedInputOrOutputClass = new LinkedList<IndividualAndClass>();
- logger.log("Processing I/O..");
- /*
- * We get the main class (the input or output class)
- */
- OWLClass oc = null;
- String param = "";
- switch (mode) {
- case Constants.FROM_INPUT_TO_OUTPUT:
- oc = dataFactory.getOWLClass(IRI.create(this.outputInputPair
- .getInput().getObject()));
- param = this.outputInputPair.getInput().getObject();
- break;
- case Constants.FROM_OUTPUT_TO_INPUT:
- oc = dataFactory.getOWLClass(IRI.create(this.outputInputPair
- .getOutput().getObject()));
- param = this.outputInputPair.getOutput().getObject();
- break;
- }
- if (oc != null) {
- /*
- * If we can find it without problems in the model.. we create the
- * individual name
- */
- String indName = oc.getIRI().getStart() + "STI"
- + oc.getIRI().getFragment();
- logger.log("Main class. Individual: " + indName);
- /*
- * We create the individual inside the class.
- */
- OWLNamedIndividual ind = this.dataFactory.getOWLNamedIndividual(IRI
- .create(indName));
- OWLClassAssertionAxiom classAssertion = dataFactory
- .getOWLClassAssertionAxiom(oc, ind);
- switch (mode) {
- case Constants.FROM_INPUT_TO_OUTPUT:
- manager.addAxiom(this.inputOntology, classAssertion);
- break;
- case Constants.FROM_OUTPUT_TO_INPUT:
- manager.addAxiom(this.outputOntology, classAssertion);
- break;
- }
- /*
- * We add it to the model.
- */
- individualsCreatedInputOrOutputClass.add(new IndividualAndClass(
- ind, oc));
- /*
- * We get the subclassess of the input or output class (the tree)
- */
- Set<OWLClassExpression> subTree = null;
- switch (mode) {
- case Constants.FROM_INPUT_TO_OUTPUT:
- subTree = oc.getSubClasses(this.inputOntology);
- break;
- case Constants.FROM_OUTPUT_TO_INPUT:
- subTree = oc.getSubClasses(this.outputOntology);
- break;
- }
- /*
- * To avoid concurrency, we store the tree on a list.
- */
- LinkedList<OWLClassExpression> toModify = new LinkedList<OWLClassExpression>();
- Object[] classess = subTree.toArray();
- for (int i = 0; i < classess.length; i++) {
- toModify.add((OWLClassExpression) classess[i]);
- }
- /*
- * Once is stored, we move over the list and create the individuals
- * in each corresponding class.
- */
- for (int i = 0; i < toModify.size(); i++) {
- OWLClassExpression oce = toModify.get(i);
- OWLClass ocs = oce.asOWLClass();
- indName = ocs.getIRI().getStart() + "STI"
- + ocs.getIRI().getFragment();
- logger.log("Sub class. Individual: " + indName);
- OWLNamedIndividual indi = this.dataFactory
- .getOWLNamedIndividual(IRI.create(indName));
- OWLClassAssertionAxiom classAsser = dataFactory
- .getOWLClassAssertionAxiom(ocs, indi);
- switch (mode) {
- case Constants.FROM_INPUT_TO_OUTPUT:
- manager.addAxiom(this.inputOntology, classAsser);
- break;
- case Constants.FROM_OUTPUT_TO_INPUT:
- manager.addAxiom(this.outputOntology, classAsser);
- break;
- }
- individualsCreatedInputOrOutputClass
- .add(new IndividualAndClass(indi, ocs));
- }
- } else {
- logger.log("Error loading class to populate with individuals: "
- + param);
- }
- }
- private Result mergeOntologiesOnASingleOneAndPerformReasoningOWLAPI() {
- try {
- // saveOntology(this.inputOntology, "C:\\inputontowlapi.owl");
- // saveOntology(this.outputOntology,
- // "C:\\outputontologyowlapi.owl");
- IRI mergedOntologyIRI = IRI.create("myOntologySADI");
- OWLOntologyMerger merger = new OWLOntologyMerger(manager);
- mergedOntologies = merger.createMergedOntology(manager,
- mergedOntologyIRI);
- // saveOntology(this.mergedOntologies, "C:\\mergedowlapi.owl");
- /*
- * Create the inference model using HermiT.
- */
- Reasoner reasoner = new Reasoner(mergedOntologies);
- /*
- * We get output class.
- */
- OWLClass outputClass = dataFactory.getOWLClass(IRI
- .create(this.outputInputPair.getOutput().getObject()));
- /*
- * List their instances (infered and asserted)
- */
- NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(
- outputClass, true);
- /*
- * For each instance, we check if each instance returned by the
- * listInstances is one of the created in the input class/output class. If it is,
- * it means that we found an equivalence.
- */
- Object[] ic = instances.getNodes().toArray();
- for (int i = 0; i < ic.length; i++) {
- @SuppressWarnings("unchecked")
- Node<OWLNamedIndividual> ind = (Node<OWLNamedIndividual>) ic[i];
- if (isInputOutputIndividual(ind.getRepresentativeElement())) {
- this.inferenceResult = Constants.POSITIVE_RESULT_IN_REASONING;
- int nm = Integer
- .parseInt(ConfigManager.getConfig(Constants.NUMBER_MATCHES_FOUND)) + 1;
- writeOutput(
- " --------------- Match found [" + nm + " - Pair: " + this.pair + "]-----------------",
- "matches.txt");
- writeOutput(
- "\t[!] Service output: "
- + this.outputInputPair
- .getServiceOutputName(),
- "matches.txt");
- writeOutput(
- "\t[!] Service input: "
- + this.outputInputPair
- .getServiceInputName(),
- "matches.txt");
- writeOutput("\n", "matches.txt");
- writeOutput(
- "\t[!] Class output: "
- + this.outputInputPair.getOutput(),
- "matches.txt");
- writeOutput(
- "\t[!] Class input: "
- + this.outputInputPair.getInput(),
- "matches.txt");
- writeOutput("\t[!] Individual: "
- + ind.getRepresentativeElement().getIRI()
- .toString(), "matches.txt");
- writeOutput(
- " --------------- End of match found -----------------",
- "matches.txt");
- ConfigManager.setConfig(Constants.NUMBER_MATCHES_FOUND,
- (Integer.toString(nm)));
- }
- reasoner.dispose();
- }
- } catch (Exception e) {
- return new Result(false, e.getMessage());
- }
- return new Result(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement