Ejemplo n.º 1
0
  /**
   * Serializes a batch of triples corresponding to a predicate into one file. Upper bound:
   * TRIPLE_LIMIT_PER_FILE.
   */
  private static int serializeTripleBatch(
      OWLOntology ontology,
      QuestOWLIndividualAxiomIterator iterator,
      String filePrefix,
      String predicateName,
      int fileCount,
      String format)
      throws Exception {
    String fileName = filePrefix + fileCount + ".owl";

    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

    // Main buffer
    OWLOntology aBox = manager.createOntology(IRI.create(predicateName));

    // Add the signatures
    for (OWLDeclarationAxiom axiom : ontology.getAxioms(AxiomType.DECLARATION)) {
      manager.addAxiom(aBox, axiom);
    }

    int tripleCount = 0;
    while (iterator.hasNext() && (tripleCount < TRIPLE_LIMIT_PER_FILE)) {
      manager.addAxiom(aBox, iterator.next());
      tripleCount++;
    }

    // BufferedOutputStream output = new BufferedOutputStream(new
    // FileOutputStream(outputPath.toFile()));
    // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    manager.saveOntology(aBox, getOntologyFormat(format), new WriterDocumentTarget(writer));

    return tripleCount;
  }
  ////////////////////////////////
  // INDIVIDUAL ASSERTION METHODS//
  ////////////////////////////////
  public boolean assertSiteIndividual(String siteNameHash, String siteNameAnnotation) {
    OWLClassExpression tmpCE = OWLFactory.getOWLClass(":Site", topIxPrefixManager);
    OWLIndividual tmpInd = OWLFactory.getOWLNamedIndividual(':' + siteNameHash, topIxPrefixManager);

    manager.addAxiom(topIxOnt, OWLFactory.getOWLClassAssertionAxiom(tmpCE, tmpInd));
    manager.addAxiom(
        topIxOnt,
        OWLFactory.getOWLAnnotationAssertionAxiom(
            OWLFactory.getOWLAnnotationProperty(
                IRI.create(
                    topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + "individualName")),
            IRI.create(topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + siteNameHash),
            OWLFactory.getOWLLiteral(siteNameAnnotation)));
    return true;
  }
  public boolean assertHouseIndividual(String houseEntryHash, String houseEntryAnnotation) {
    OWLClassExpression tempClassExpression = OWLFactory.getOWLClass(":House", topIxPrefixManager);
    OWLIndividual tempIndividual =
        OWLFactory.getOWLNamedIndividual(':' + houseEntryHash, topIxPrefixManager);

    manager.addAxiom(
        topIxOnt, OWLFactory.getOWLClassAssertionAxiom(tempClassExpression, tempIndividual));
    manager.addAxiom(
        topIxOnt,
        OWLFactory.getOWLAnnotationAssertionAxiom(
            OWLFactory.getOWLAnnotationProperty(
                IRI.create(
                    topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + "individualName")),
            IRI.create(topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + houseEntryHash),
            OWLFactory.getOWLLiteral(houseEntryAnnotation)));
    return true;
  }
 public boolean assertDataPropertyInstance(String ind, String prop, int value) {
   OWLDataProperty dataProp =
       OWLFactory.getOWLDataProperty(
           IRI.create(topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + prop));
   OWLDataPropertyAssertionAxiom tmpAx =
       OWLFactory.getOWLDataPropertyAssertionAxiom(
           dataProp,
           OWLFactory.getOWLNamedIndividual(
               IRI.create(topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + ind)),
           OWLFactory.getOWLLiteral(value));
   manager.addAxiom(topIxOnt, tmpAx);
   return true;
 }
 // asserts a hasHouse property instance between a house and its corresponding
 // site
 public boolean assertHasHousePropertyInstance(String houseEntryHash, String siteNameHash) {
   OWLObjectProperty objProp =
       OWLFactory.getOWLObjectProperty(
           IRI.create(topIxOnt.getOntologyID().getOntologyIRI() + "#hasHouse"));
   OWLIndividual houseIndividual =
       OWLFactory.getOWLNamedIndividual(':' + houseEntryHash, topIxPrefixManager);
   OWLIndividual siteIndividual =
       OWLFactory.getOWLNamedIndividual(':' + siteNameHash, topIxPrefixManager);
   manager.addAxiom(
       topIxOnt,
       OWLFactory.getOWLObjectPropertyAssertionAxiom(objProp, siteIndividual, houseIndividual));
   return true;
 }
 //////////////////////////////
 // PROPERTY ASSERTION METHODS//
 //////////////////////////////
 public boolean assertPropertyInstance(
     String comboBoxObjectPropertyEntry, String room1Hash, String room2Hash) {
   OWLObjectProperty objProp =
       OWLFactory.getOWLObjectProperty(
           IRI.create(propEntryNametoPropEntryIRI.get(comboBoxObjectPropertyEntry)));
   OWLIndividual room1Individual =
       OWLFactory.getOWLNamedIndividual(':' + room1Hash, topIxPrefixManager);
   OWLIndividual room2Individual =
       OWLFactory.getOWLNamedIndividual(':' + room2Hash, topIxPrefixManager);
   manager.addAxiom(
       topIxOnt,
       OWLFactory.getOWLObjectPropertyAssertionAxiom(objProp, room1Individual, room2Individual));
   return true;
 }
 public boolean assertHasRoomPropertyInstance(
     String houseIndividualHash, String roomIndividualHash) {
   OWLObjectProperty tempOWLbjectProperty =
       OWLFactory.getOWLObjectProperty(
           IRI.create(topIxOnt.getOntologyID().getOntologyIRI() + "#hasRoom"));
   OWLIndividual houseIndividual =
       OWLFactory.getOWLNamedIndividual(':' + houseIndividualHash, topIxPrefixManager);
   OWLIndividual roomIndividual =
       OWLFactory.getOWLNamedIndividual(':' + roomIndividualHash, topIxPrefixManager);
   manager.addAxiom(
       topIxOnt,
       OWLFactory.getOWLObjectPropertyAssertionAxiom(
           tempOWLbjectProperty, houseIndividual, roomIndividual));
   return true;
 }
  public boolean assertRoomIndividual(
      String roomName, String roomIndividualHash, String roomIndividualAnnotation) {
    OWLClassExpression tempClassExpression =
        OWLFactory.getOWLClass(
            IRI.create(
                roomToIRI.get(
                    roomName))); // retrieves the Room Class IRI fron the roomToIRI map, using
                                 // roomID as a key.
    OWLIndividual tempIndividual =
        OWLFactory.getOWLNamedIndividual(':' + roomIndividualHash, topIxPrefixManager);

    manager.addAxiom(
        topIxOnt, OWLFactory.getOWLClassAssertionAxiom(tempClassExpression, tempIndividual));
    manager.addAxiom(
        topIxOnt,
        OWLFactory.getOWLAnnotationAssertionAxiom(
            OWLFactory.getOWLAnnotationProperty(
                IRI.create(
                    topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + "individualName")),
            IRI.create(
                topIxOnt.getOntologyID().getOntologyIRI().toString() + '#' + roomIndividualHash),
            OWLFactory.getOWLLiteral(String.format(roomIndividualAnnotation))));
    return true;
  }
Ejemplo n.º 9
0
  public void runWithSingleFile() {
    BufferedOutputStream output = null;
    BufferedWriter writer = null;

    try {
      final long startTime = System.currentTimeMillis();

      if (outputFile != null) {
        output = new BufferedOutputStream(new FileOutputStream(outputFile));
      } else {
        output = new BufferedOutputStream(System.out);
      }
      writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));

      OBDAModel obdaModel = loadMappingFile(mappingFile);

      OWLOntology ontology = null;
      OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
      OWLAPI3Materializer materializer = null;

      if (owlFile != null) {
        // Loading the OWL ontology from the file as with normal OWLReasoners
        ontology = manager.loadOntologyFromOntologyDocument((new File(owlFile)));

        if (disableReasoning) {
          /*
           * when reasoning is disabled, we extract only the declaration assertions for the vocabulary
           */
          ontology = extractDeclarations(manager, ontology);
        }

        Ontology onto = OWLAPI3TranslatorUtility.translate(ontology);
        obdaModel.declareAll(onto.getVocabulary());
        materializer = new OWLAPI3Materializer(obdaModel, onto, DO_STREAM_RESULTS);
      } else {
        ontology = manager.createOntology();
        materializer = new OWLAPI3Materializer(obdaModel, DO_STREAM_RESULTS);
      }

      // OBDAModelSynchronizer.declarePredicates(ontology, obdaModel);

      QuestOWLIndividualAxiomIterator iterator = materializer.getIterator();

      while (iterator.hasNext()) manager.addAxiom(ontology, iterator.next());

      OWLOntologyFormat ontologyFormat = getOntologyFormat(format);

      manager.saveOntology(ontology, ontologyFormat, new WriterDocumentTarget(writer));

      System.err.println("NR of TRIPLES: " + materializer.getTriplesCount());
      System.err.println("VOCABULARY SIZE (NR of QUERIES): " + materializer.getVocabularySize());

      materializer.disconnect();
      if (outputFile != null) output.close();

      final long endTime = System.currentTimeMillis();
      final long time = endTime - startTime;
      System.out.println("Elapsed time to materialize: " + time + " {ms}");

    } catch (Exception e) {
      System.out.println("Error materializing ontology:");
      e.printStackTrace();
    }
  }
  public boolean storeSolutions(String siteNameHash) {
    for (OwlSolution tempSolution : solutionsList) {
      // assert Solution individual and assert Site->hasSolution
      OWLClassExpression solutionClassExpression =
          OWLFactory.getOWLClass(":Solution", topIxPrefixManager);
      String tempSolutionID =
          String.format(siteNameHash + "_S_%1$02d", tempSolution.getSolutionID().intValue());
      OWLIndividual tempSolutionIndividual =
          OWLFactory.getOWLNamedIndividual(":" + tempSolutionID, topIxPrefixManager);
      OWLClassAssertionAxiom tempClassAssertionAxiom =
          OWLFactory.getOWLClassAssertionAxiom(solutionClassExpression, tempSolutionIndividual);
      manager.addAxiom(topIxOnt, tempClassAssertionAxiom);

      OWLObjectProperty tempSolutionObjectProperty =
          OWLFactory.getOWLObjectProperty(":hasSolution", topIxPrefixManager);
      OWLIndividual tempSiteIndividual =
          OWLFactory.getOWLNamedIndividual(":" + siteNameHash, topIxPrefixManager);
      OWLObjectPropertyAssertionAxiom tempObjPropAssAxiom =
          OWLFactory.getOWLObjectPropertyAssertionAxiom(
              tempSolutionObjectProperty, tempSiteIndividual, tempSolutionIndividual);
      manager.addAxiom(topIxOnt, tempObjPropAssAxiom);

      // assert SolvedHouse individuals and assert Solution->hasSolvedHouse objProperty. also assert
      // the DATA PROPERTIES for each solvedHouse indvidual
      for (OwlSolvedHouse tempSolvedHouse : tempSolution.getSolvedHouses()) {
        // logger.info("373");
        // logger.info(tempSolution.getSolvedHouses().toString());
        OWLClassExpression solvedHouseClassExpression =
            OWLFactory.getOWLClass(":SolvedHouse", topIxPrefixManager);
        OWLIndividual tempSolvedHouseIndividual =
            OWLFactory.getOWLNamedIndividual(
                ":" + tempSolutionID + "_SH_" + tempSolvedHouse.getSolvedHouseHash(),
                topIxPrefixManager);
        OWLClassAssertionAxiom tempClassAssAx =
            OWLFactory.getOWLClassAssertionAxiom(
                solvedHouseClassExpression, tempSolvedHouseIndividual);
        manager.addAxiom(topIxOnt, tempClassAssAx);

        OWLObjectProperty tempSolvedHouseObjectProperty =
            OWLFactory.getOWLObjectProperty(":hasSolvedHouse", topIxPrefixManager);
        OWLObjectPropertyAssertionAxiom tempHouseObjPropAssAx =
            OWLFactory.getOWLObjectPropertyAssertionAxiom(
                tempSolvedHouseObjectProperty, tempSolutionIndividual, tempSolvedHouseIndividual);
        manager.addAxiom(topIxOnt, tempHouseObjPropAssAx);

        OWLDataProperty tempDataProperty;
        OWLDataPropertyAssertionAxiom tempDataPropertyAssertionAxiom;

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasLiteral", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty,
                tempSolvedHouseIndividual,
                tempSolvedHouse.getSolvedHouseLiteral());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasL", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty,
                tempSolvedHouseIndividual,
                tempSolvedHouse.getSolvedHouseLength());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasW", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedHouseIndividual, tempSolvedHouse.getSolvedHouseWidth());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasX", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedHouseIndividual, tempSolvedHouse.getSolvedHouseX());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasY", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedHouseIndividual, tempSolvedHouse.getSolvedHouseY());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);
        // logger.info("asserts the house");
        // logger.info(tempSolvedHouse.getSolvedHouseLiteral());
      }

      // assert SolvedRoom individuals and assert Solution->hasSolvedRoom objProperty. also assert
      // the DATA PROPERTIES for each solvedRoom indvidual
      for (OwlSolvedRoom tempSolvedRoom : tempSolution.getSolvedRooms()) {
        OWLClassExpression solvedRoomClassExpression =
            OWLFactory.getOWLClass(":SolvedRoom", topIxPrefixManager);
        OWLIndividual tempSolvedRoomIndividual =
            OWLFactory.getOWLNamedIndividual(
                ":" + tempSolutionID + "_SR_" + tempSolvedRoom.getSolvedRoomHash(),
                topIxPrefixManager);
        OWLClassAssertionAxiom tempClassAssAx =
            OWLFactory.getOWLClassAssertionAxiom(
                solvedRoomClassExpression, tempSolvedRoomIndividual);
        manager.addAxiom(topIxOnt, tempClassAssAx);

        OWLObjectProperty tempSolvedRoomObjectProperty =
            OWLFactory.getOWLObjectProperty(":hasSolvedRoom", topIxPrefixManager);
        OWLObjectPropertyAssertionAxiom tempRoomObjPropAssAx =
            OWLFactory.getOWLObjectPropertyAssertionAxiom(
                tempSolvedRoomObjectProperty, tempSolutionIndividual, tempSolvedRoomIndividual);
        manager.addAxiom(topIxOnt, tempRoomObjPropAssAx);

        OWLDataProperty tempDataProperty;
        OWLDataPropertyAssertionAxiom tempDataPropertyAssertionAxiom;

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasLiteral", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomLiteral());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasL", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomLength());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasW", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomWidth());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasH", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomHeight());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasX", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomX());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasY", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomY());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);

        tempDataProperty = OWLFactory.getOWLDataProperty(":hasZ", topIxPrefixManager);
        tempDataPropertyAssertionAxiom =
            OWLFactory.getOWLDataPropertyAssertionAxiom(
                tempDataProperty, tempSolvedRoomIndividual, tempSolvedRoom.getSolvedRoomZ());
        manager.addAxiom(topIxOnt, tempDataPropertyAssertionAxiom);
      }
    }
    return true;
  }