Advertisement
alejandrorg

Untitled

Apr 22nd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.41 KB | None | 0 0
  1.     private Result loadInputOntologyOWLAPI() {
  2.         try {
  3.             inputNamespace = IRI.create(this.outputInputPair.getInput()
  4.                     .getObject());
  5.             logger.log("inputNamespace created.. OWL API [ " + inputNamespace.toString() + " ]");
  6.             inputOntology = manager
  7.                     .loadOntologyFromOntologyDocument(inputNamespace);
  8.             logger.log("Input ontology loaded in the object.. OWL API");
  9.         } catch (Exception e) {
  10.             return new Result(false, e.getMessage());
  11.         }
  12.         return new Result(true);
  13.     }
  14.  
  15.     /**
  16.      * Method to load the output ontology using OWLAPI.
  17.      *
  18.      * @return Return the result of the operation.
  19.      * @throws Exception
  20.      *             It can throw an exception.
  21.      */
  22.     private Result loadOutputOntologyOWLAPI() {
  23.         try {
  24.             outputNamespace = IRI.create(this.outputInputPair.getOutput()
  25.                     .getObject());
  26.             logger.log("inputNamespace created.. OWL API [ " + outputNamespace.toString() + " ]");
  27.             outputOntology = manager
  28.                     .loadOntologyFromOntologyDocument(outputNamespace);
  29.             logger.log("Output ontology loaded in the object.. OWL API");
  30.         } catch (Exception e) {
  31.             return new Result(false, e.getMessage());
  32.         }
  33.         return new Result(true);
  34.     }
  35.     /**
  36.      * Method to create the instances using Jena.
  37.      *
  38.      * @param mode
  39.      *            Receives the mode (create the instace on input or output class
  40.      *            and subclasses)
  41.      */
  42.     private void createInstancesUsingOWLAPI() {
  43.         /*
  44.          * First, we create the list of individuals.
  45.          */
  46.         individualsCreatedInputOrOutputClass = new LinkedList<IndividualAndClass>();
  47.         logger.log("Processing I/O..");
  48.  
  49.         /*
  50.          * We get the main class (the input or output class)
  51.          */
  52.         OWLClass oc = null;
  53.         String param = "";
  54.         switch (mode) {
  55.         case Constants.FROM_INPUT_TO_OUTPUT:
  56.             oc = dataFactory.getOWLClass(IRI.create(this.outputInputPair
  57.                     .getInput().getObject()));
  58.             param = this.outputInputPair.getInput().getObject();
  59.             break;
  60.         case Constants.FROM_OUTPUT_TO_INPUT:
  61.             oc = dataFactory.getOWLClass(IRI.create(this.outputInputPair
  62.                     .getOutput().getObject()));
  63.             param = this.outputInputPair.getOutput().getObject();
  64.             break;
  65.         }
  66.        
  67.         if (oc != null) {
  68.             /*
  69.              * If we can find it without problems in the model.. we create the
  70.              * individual name
  71.              */
  72.             String indName = oc.getIRI().getStart() + "STI"
  73.                     + oc.getIRI().getFragment();
  74.             logger.log("Main class. Individual: " + indName);
  75.             /*
  76.              * We create the individual inside the class.
  77.              */
  78.             OWLNamedIndividual ind = this.dataFactory.getOWLNamedIndividual(IRI
  79.                     .create(indName));
  80.             OWLClassAssertionAxiom classAssertion = dataFactory
  81.                     .getOWLClassAssertionAxiom(oc, ind);
  82.             switch (mode) {
  83.             case Constants.FROM_INPUT_TO_OUTPUT:
  84.                 manager.addAxiom(this.inputOntology, classAssertion);
  85.                 break;
  86.             case Constants.FROM_OUTPUT_TO_INPUT:
  87.                 manager.addAxiom(this.outputOntology, classAssertion);
  88.                 break;
  89.             }
  90.             /*
  91.              * We add it to the model.
  92.              */
  93.             individualsCreatedInputOrOutputClass.add(new IndividualAndClass(
  94.                     ind, oc));
  95.             /*
  96.              * We get the subclassess of the input or output class (the tree)
  97.              */
  98.             Set<OWLClassExpression> subTree = null;
  99.             switch (mode) {
  100.             case Constants.FROM_INPUT_TO_OUTPUT:
  101.                 subTree = oc.getSubClasses(this.inputOntology);
  102.                 break;
  103.             case Constants.FROM_OUTPUT_TO_INPUT:
  104.                 subTree = oc.getSubClasses(this.outputOntology);
  105.                 break;
  106.             }
  107.  
  108.             /*
  109.              * To avoid concurrency, we store the tree on a list.
  110.              */
  111.             LinkedList<OWLClassExpression> toModify = new LinkedList<OWLClassExpression>();
  112.             Object[] classess = subTree.toArray();
  113.             for (int i = 0; i < classess.length; i++) {
  114.                 toModify.add((OWLClassExpression) classess[i]);
  115.             }
  116.             /*
  117.              * Once is stored, we move over the list and create the individuals
  118.              * in each corresponding class.
  119.              */
  120.  
  121.             for (int i = 0; i < toModify.size(); i++) {
  122.                 OWLClassExpression oce = toModify.get(i);
  123.                 OWLClass ocs = oce.asOWLClass();
  124.                 indName = ocs.getIRI().getStart() + "STI"
  125.                         + ocs.getIRI().getFragment();
  126.                 logger.log("Sub class. Individual: " + indName);
  127.                 OWLNamedIndividual indi = this.dataFactory
  128.                         .getOWLNamedIndividual(IRI.create(indName));
  129.                 OWLClassAssertionAxiom classAsser = dataFactory
  130.                         .getOWLClassAssertionAxiom(ocs, indi);
  131.                 switch (mode) {
  132.                 case Constants.FROM_INPUT_TO_OUTPUT:
  133.                     manager.addAxiom(this.inputOntology, classAsser);
  134.                     break;
  135.                 case Constants.FROM_OUTPUT_TO_INPUT:
  136.                     manager.addAxiom(this.outputOntology, classAsser);
  137.                     break;
  138.                 }
  139.                 individualsCreatedInputOrOutputClass
  140.                         .add(new IndividualAndClass(indi, ocs));
  141.             }
  142.         } else {
  143.            
  144.             logger.log("Error loading class to populate with individuals: "
  145.                     + param);
  146.         }
  147.     }
  148.  
  149.    
  150. private Result mergeOntologiesOnASingleOneAndPerformReasoningOWLAPI() {
  151.         try {
  152.             // saveOntology(this.inputOntology, "C:\\inputontowlapi.owl");
  153.             // saveOntology(this.outputOntology,
  154.             // "C:\\outputontologyowlapi.owl");
  155.             IRI mergedOntologyIRI = IRI.create("myOntologySADI");
  156.             OWLOntologyMerger merger = new OWLOntologyMerger(manager);
  157.             mergedOntologies = merger.createMergedOntology(manager,
  158.                     mergedOntologyIRI);
  159.  
  160.             // saveOntology(this.mergedOntologies, "C:\\mergedowlapi.owl");
  161.  
  162.             /*
  163.              * Create the inference model using HermiT.
  164.              */
  165.             Reasoner reasoner = new Reasoner(mergedOntologies);
  166.  
  167.             /*
  168.              * We get output class.
  169.              */
  170.  
  171.             OWLClass outputClass = dataFactory.getOWLClass(IRI
  172.                     .create(this.outputInputPair.getOutput().getObject()));
  173.             /*
  174.              * List their instances (infered and asserted)
  175.              */
  176.             NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(
  177.                     outputClass, true);
  178.  
  179.             /*
  180.              * For each instance, we check if each instance returned by the
  181.              * listInstances is one of the created in the input class/output class. If it is,
  182.              * it means that we found an equivalence.
  183.              */
  184.  
  185.             Object[] ic = instances.getNodes().toArray();
  186.             for (int i = 0; i < ic.length; i++) {
  187.                 @SuppressWarnings("unchecked")
  188.                 Node<OWLNamedIndividual> ind = (Node<OWLNamedIndividual>) ic[i];
  189.                 if (isInputOutputIndividual(ind.getRepresentativeElement())) {
  190.                     this.inferenceResult = Constants.POSITIVE_RESULT_IN_REASONING;
  191.                     int nm = Integer
  192.                             .parseInt(ConfigManager.getConfig(Constants.NUMBER_MATCHES_FOUND)) + 1;
  193.                     writeOutput(
  194.                             " --------------- Match found [" + nm + " - Pair: " + this.pair + "]-----------------",
  195.                             "matches.txt");
  196.                     writeOutput(
  197.                             "\t[!] Service output: "
  198.                                     + this.outputInputPair
  199.                                             .getServiceOutputName(),
  200.                             "matches.txt");
  201.                     writeOutput(
  202.                             "\t[!] Service input: "
  203.                                     + this.outputInputPair
  204.                                             .getServiceInputName(),
  205.                             "matches.txt");
  206.                     writeOutput("\n", "matches.txt");
  207.                     writeOutput(
  208.                             "\t[!] Class output: "
  209.                                     + this.outputInputPair.getOutput(),
  210.                             "matches.txt");
  211.                     writeOutput(
  212.                             "\t[!] Class input: "
  213.                                     + this.outputInputPair.getInput(),
  214.                             "matches.txt");
  215.                     writeOutput("\t[!] Individual: "
  216.                             + ind.getRepresentativeElement().getIRI()
  217.                                     .toString(), "matches.txt");
  218.                     writeOutput(
  219.                             " --------------- End of match found -----------------",
  220.                             "matches.txt");
  221.                     ConfigManager.setConfig(Constants.NUMBER_MATCHES_FOUND,
  222.                             (Integer.toString(nm)));
  223.                 }
  224.                 reasoner.dispose();
  225.             }
  226.         } catch (Exception e) {
  227.             return new Result(false, e.getMessage());
  228.         }
  229.         return new Result(true);
  230.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement