Exemplo n.º 1
0
 private Set<OWLLogicalAxiom> validateMinimality(
     Set<OWLLogicalAxiom> diagnosis, OWLTheory faultyTheory) throws SolverException {
   Set<OWLLogicalAxiom> rem = new HashSet<OWLLogicalAxiom>();
   faultyTheory.reset();
   assertTrue(faultyTheory.testDiagnosis(diagnosis));
   for (OWLLogicalAxiom o : diagnosis) {
     Set<OWLLogicalAxiom> cp = new HashSet<OWLLogicalAxiom>(diagnosis);
     cp.remove(o);
     faultyTheory.reset();
     if (faultyTheory.testDiagnosis(cp)) rem.add(o);
   }
   return rem;
 }
Exemplo n.º 2
0
 protected String computeHSShortLog(
     TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search,
     OWLTheory theory,
     FormulaSet<OWLLogicalAxiom> diagnoses,
     List<Double> queries,
     QSSType type,
     String message,
     OWLTheory queryAnswerTheory) {
   SimulatedSession session = new SimulatedSession();
   session.setShowElRates(false);
   TableList entry = new TableList();
   session.setEntry(entry);
   session.setMessage(message);
   session.setTargetD(diagnoses);
   session.setScoringFunct(type);
   session.setTheory(theory);
   session.setQueryAnswerTheory(queryAnswerTheory);
   session.setSearch(search);
   String out = session.simulateQuerySession();
   FormulaSet<OWLLogicalAxiom> diag = getMostProbable(search.getDiagnoses());
   logger.info("found Diag: " + CalculateDiagnoses.renderAxioms(diag));
   boolean foundCorrectD =
       diagnoses.containsAll(
           diag); // with ConflictTreeTest only a subset of the diagnosis could be returned
   if (this.getClass() != ConflictTreeTest.class) {
     foundCorrectD = diag.equals(diagnoses);
     theory.getKnowledgeBase().clearTestCases(); // don't clear if called from ConflictTreeTest
   }
   search.reset();
   // Assert.assertTrue(foundCorrectD);
   queries.add(entry.getMeanQuery());
   return out;
 }
Exemplo n.º 3
0
  /**
   * Sets up theory and search
   *
   * @return search
   * @throws SolverException
   * @throws InconsistentTheoryException
   * @throws NoConflictException
   * @param strOntologyFile
   */
  private TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> testSetup(String strOntologyFile)
      throws SolverException, InconsistentTheoryException, NoConflictException {
    // create theory
    OWLOntology ontology = getOntologySimple(strOntologyFile);
    ctTheory = getSimpleTheory(ontology, false);
    // set query parameters
    //        ctTheory.setIncludeSubClassOfAxioms(true);
    //        ctTheory.setIncludeClassAssertionAxioms(true);
    //        ctTheory.setIncludeEquivalentClassAxioms(true);
    //        ctTheory.setIncludePropertyAssertAxioms(true);
    //        ctTheory.setIncludeDisjointClassAxioms(true);

    origTheory = (OWLTheory) ctTheory.copy();

    // setup search
    TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search =
        new HsTreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom>();
    search.setSearchStrategy(new UniformCostSearchStrategy<OWLLogicalAxiom>());
    search.setSearcher(new QuickXplain<OWLLogicalAxiom>());
    search.setSearchable(ctTheory);
    search.setCostsEstimator(new OWLAxiomKeywordCostsEstimator(ctTheory));

    // set for all found diagnoses during search
    foundDiagnoses = new LinkedHashSet<OWLLogicalAxiom>();

    return search;
  }
Exemplo n.º 4
0
  @Ignore
  @Test
  public void testNormalSimulatedSession()
      throws SolverException, InconsistentTheoryException, NoConflictException,
          OWLOntologyCreationException {
    logger.info("NormalSimulatedSession");

    TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search =
        testSetup("ontologies/ecai2010.owl");
    Set<? extends FormulaSet<OWLLogicalAxiom>> diagnoses = getDiagnoses(search);

    Map<QSSType, DurationStat> nTimes = new HashMap<QSSType, DurationStat>();
    Map<QSSType, List<Double>> nQueries = new HashMap<QSSType, List<Double>>();

    for (QSSType type : QSSType.values()) { // run for each scoring function
      logger.info("QSSType: " + type);

      nTimes.put(type, new DurationStat());
      nQueries.put(type, new LinkedList<Double>());
      for (FormulaSet<OWLLogicalAxiom> targetDiagnosis :
          diagnoses) { // run for each possible target diagnosis
        logger.info("targetD: " + CalculateDiagnoses.renderAxioms(targetDiagnosis));
        long completeTime = System.currentTimeMillis();

        computeHS(search, ctTheory, targetDiagnosis, nQueries.get(type), type);
        ctTheory.getKnowledgeBase().removeFormulas(targetDiagnosis);
        completeTime = System.currentTimeMillis() - completeTime;
        nTimes.get(type).add(completeTime);

        foundDiagnoses.addAll(targetDiagnosis);
        ctTheory.getReasoner().addFormulasToCache(ctTheory.getKnowledgeBase().getFaultyFormulas());
        assertTrue(ctTheory.verifyConsistency());
        ctTheory.reset();

        resetTheoryTests(ctTheory);
        search.reset();
      }
      logStatistics(nQueries, nTimes, type, "normal");
      logger.info("found Diagnoses: " + foundDiagnoses.size());
      logger.info("found Diagnosis: " + CalculateDiagnoses.renderAxioms(foundDiagnoses));
      logger.info(
          "found all target diagnoses: "
              + (foundDiagnoses.size() > 0 && foundDiagnoses.containsAll(diagnoses)));
    }
  }
Exemplo n.º 5
0
  private OWLTheory loadTheory(OWLOntology ontology, String ontologyDir, String o1, String o2) {
    OWLTheory theory = getExtendTheory(ontology, false);

    LinkedHashSet<OWLLogicalAxiom> bx = new LinkedHashSet<OWLLogicalAxiom>();
    OWLOntology ontology1 = getOntologySimple(ontologyDir, o1 + ".owl");
    OWLOntology ontology2 = getOntologySimple(ontologyDir, o2 + ".owl");
    Set<OWLLogicalAxiom> onto1Axioms =
        createIntersection(ontology.getLogicalAxioms(), ontology1.getLogicalAxioms());
    Set<OWLLogicalAxiom> onto2Axioms =
        createIntersection(ontology.getLogicalAxioms(), ontology2.getLogicalAxioms());
    int modOnto1Size = onto1Axioms.size();
    int modOnto2Size = onto2Axioms.size();
    int modMappingSize = ontology.getLogicalAxioms().size() - modOnto1Size - modOnto2Size;
    bx.addAll(onto1Axioms);
    bx.addAll(onto2Axioms);
    theory.getKnowledgeBase().addBackgroundFormulas(bx);

    logger.info(
        "ontology axiom sizes: " + modOnto1Size + "," + modOnto2Size + "," + modMappingSize);
    return theory;
  }
Exemplo n.º 6
0
  public Set<FormulaSet<OWLLogicalAxiom>> getRandomDiagSet(
      String matchingsDir,
      String ontologyDir,
      Map<File, String> map,
      File file,
      String o1,
      String o2,
      String n,
      OAEI11ConferenceSession conferenceSession)
      throws SolverException, InconsistentTheoryException {
    OWLOntology preOntology =
        loadOntology(matchingsDir, ontologyDir, map, conferenceSession, file, o1, o2, n);
    OWLTheory preTheory = loadTheory(preOntology, ontologyDir, o1, o2);
    TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search =
        getUniformCostSearch(preTheory, false);

    Map<OWLLogicalAxiom, BigDecimal> map1 =
        conferenceSession.readRdfMapping(matchingsDir + map.get(file), n + ".rdf");
    OWLAxiomCostsEstimator es = new OWLAxiomCostsEstimator(preTheory, map1);
    search.setCostsEstimator(es);
    preTheory.getReasoner().addFormulasToCache(preTheory.getKnowledgeBase().getFaultyFormulas());
    assertFalse(preTheory.verifyConsistency());
    search.reset();

    try {
      search.setMaxDiagnosesNumber(30);
      search.start();
    } catch (NoConflictException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    Set<FormulaSet<OWLLogicalAxiom>> diagnoses =
        new TreeSet<FormulaSet<OWLLogicalAxiom>>(search.getDiagnoses());

    search.reset();

    return diagnoses;
  }
Exemplo n.º 7
0
  private Set<? extends FormulaSet<OWLLogicalAxiom>> getDiagnoses(
      TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search)
      throws SolverException, InconsistentTheoryException, NoConflictException {
    // cost estimator
    SimpleCostsEstimator<OWLLogicalAxiom> es = new SimpleCostsEstimator<OWLLogicalAxiom>();
    search.setCostsEstimator(es);

    // searching diagnoses
    search.start();
    Set<? extends FormulaSet<OWLLogicalAxiom>> diagnoses = search.getDiagnoses();

    // resets history
    ctTheory.getKnowledgeBase().clearTestCases();
    ctTheory.getKnowledgeBase().clearTestsList();

    search.reset();

    return diagnoses;
  }
Exemplo n.º 8
0
 private void resetTheoryTests(OWLTheory theory) {
   theory.getKnowledgeBase().addFormulas(origTheory.getKnowledgeBase().getKnowledgeBase());
   theory.getKnowledgeBase().clearTestCases();
   theory.getKnowledgeBase().clearTestsList();
 }
Exemplo n.º 9
0
  @Ignore
  @Test
  public void testCompareSearchMethods()
      throws SolverException, InconsistentTheoryException, NoConflictException,
          OWLOntologyCreationException {
    logger.info("NormalSimulatedSession compared to ConflictTreeSimulatedSession");

    TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search =
        testSetup("ontologies/ecai2010.owl");
    Set<? extends FormulaSet<OWLLogicalAxiom>> diagnoses = getDiagnoses(search);

    // setup second search
    TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> ctSearch =
        new HsTreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom>();
    ctSearch.setSearchStrategy(new UniformCostSearchStrategy<OWLLogicalAxiom>());
    ctSearch.setSearcher(new QuickXplain<OWLLogicalAxiom>());
    ctSearch.setSearchable(ctTheory);
    ctSearch.setCostsEstimator(new OWLAxiomKeywordCostsEstimator(ctTheory));

    Map<QSSType, DurationStat> nTimes = new HashMap<QSSType, DurationStat>();
    Map<QSSType, List<Double>> nQueries = new HashMap<QSSType, List<Double>>();
    Map<QSSType, DurationStat> ctTimes = new HashMap<QSSType, DurationStat>();
    Map<QSSType, List<Double>> ctQueries = new HashMap<QSSType, List<Double>>();

    for (QSSType type : QSSType.values()) { // run for each scoring function
      logger.info("QSSType: " + type);

      // run normal simulated session
      logger.info("NormalSimulatedSession");
      nTimes.put(type, new DurationStat());
      nQueries.put(type, new LinkedList<Double>());
      for (FormulaSet<OWLLogicalAxiom> targetDiagnosis :
          diagnoses) { // run for each possible target diagnosis
        long completeTime = System.currentTimeMillis();

        computeHS(search, ctTheory, targetDiagnosis, nQueries.get(type), type);
        ctTheory.getKnowledgeBase().removeFormulas(targetDiagnosis);
        completeTime = System.currentTimeMillis() - completeTime;
        nTimes.get(type).add(completeTime);

        foundDiagnoses.addAll(targetDiagnosis);
        ctTheory.getReasoner().addFormulasToCache(ctTheory.getKnowledgeBase().getFaultyFormulas());
        assertTrue(ctTheory.verifyConsistency());
        ctTheory.reset();

        resetTheoryTests(ctTheory);
        search.reset();
      }
      logger.info("found Diagnoses: " + foundDiagnoses.size());
      logger.info("found Diagnosis: " + CalculateDiagnoses.renderAxioms(foundDiagnoses));
      logger.info(
          "found all target diagnoses: "
              + (foundDiagnoses.size() > 0 && foundDiagnoses.containsAll(diagnoses)));
      // end (run normal simulated session)

      foundDiagnoses.clear();

      // run conflict tree simulated session
      logger.info("ConflictTreeSimulatedSession");
      ctTimes.put(type, new DurationStat());
      ctQueries.put(type, new LinkedList<Double>());
      for (FormulaSet<OWLLogicalAxiom> targetDiagnosis :
          diagnoses) { // run for each possible target diagnosis
        logger.info("targetD: " + CalculateDiagnoses.renderAxioms(targetDiagnosis));

        ConflictTreeSession conflictTreeSearch = new ConflictTreeSession(this, ctTheory, ctSearch);
        long completeTime = conflictTreeSearch.search(targetDiagnosis, ctQueries, type);
        ctTimes.get(type).add(completeTime);

        foundDiagnoses.addAll(conflictTreeSearch.getDiagnosis());
        ctTheory.getReasoner().addFormulasToCache(ctTheory.getKnowledgeBase().getFaultyFormulas());
        assertTrue(ctTheory.verifyConsistency());
        ctTheory.reset();

        resetTheoryTests(ctTheory);
        ctSearch.reset();
      }
      logger.info("found Diagnoses: " + foundDiagnoses.size());
      logger.info("found Diagnosis: " + CalculateDiagnoses.renderAxioms(foundDiagnoses));
      logger.info(
          "found all target diagnoses: "
              + (foundDiagnoses.size() > 0 && foundDiagnoses.containsAll(diagnoses)));
      // end (run conflict tree simulated session)

      // print time statistics
      logStatistics(nQueries, nTimes, type, "normal");
      logStatistics(ctQueries, ctTimes, type, "treeSearch");

      foundDiagnoses.clear();
    }
  }
Exemplo n.º 10
0
  /**
   * Runs ConflictTreeSimulatedSession with calculated diagnoses
   *
   * @param matchingsDir
   * @param ontologyDir
   * @param files
   * @param map
   * @throws SolverException
   * @throws InconsistentTheoryException
   * @throws OWLOntologyCreationException
   */
  private void runOaeiConferenceTests(
      String matchingsDir,
      String ontologyDir,
      Set<File> files,
      Map<File, String> map,
      int numberOfConflicts)
      throws SolverException, InconsistentTheoryException, OWLOntologyCreationException {
    OAEI11ConferenceSession conferenceSession = new OAEI11ConferenceSession();

    QSSType[] qssTypes = new QSSType[] {QSSType.MINSCORE};
    // QSSType[] qssTypes = new QSSType[]{QSSType.MINSCORE, QSSType.SPLITINHALF,
    // QSSType.DYNAMICRISK};

    TargetSource targetSource = TargetSource.FROM_30_DIAGS;
    for (File file : files) {
      logger.info("processing " + file.getName());
      String out = "STAT, " + file;

      // file parameter
      String fileName = file.getName();
      StringTokenizer t = new StringTokenizer(fileName, "-");
      String matcher = t.nextToken();
      String o1 = t.nextToken();
      String o2 = t.nextToken();
      o2 = o2.substring(0, o2.length() - 4);
      String n = file.getName().substring(0, file.getName().length() - 4);

      Set<FormulaSet<OWLLogicalAxiom>> targetDgSet =
          getRandomDiagSet(matchingsDir, ontologyDir, map, file, o1, o2, n, conferenceSession);
      logger.info("number of found diagnoses (max. 30): " + targetDgSet.size());
      /*
      TODO: for each ontology always the same "randomly" chosen diagnosis is calculated, dependent
          from the size of the targetDgSet -> change to a real random selection
       */
      Random random = new Random(12311);
      int randomNr = conferenceSession.chooseRandomNum(targetDgSet, random);
      Set<OWLLogicalAxiom> targetDg =
          conferenceSession.chooseRandomDiag(targetDgSet, file, randomNr);

      OWLOntology inconsOntology =
          loadOntology(matchingsDir, ontologyDir, map, conferenceSession, file, o1, o2, n);
      //                OWLTheory faultyTheory = getExtendTheory(inconsOntology, false);
      OWLTheory faultyTheory = loadTheory(inconsOntology, ontologyDir, o1, o2);
      Set<OWLLogicalAxiom> rem = validateMinimality(targetDg, faultyTheory);
      if (!rem.isEmpty()) {
        targetDg.removeAll(rem);
        assertTrue(validateMinimality(targetDg, faultyTheory).isEmpty());
      }

      Map<QSSType, DurationStat> ctTimes = new HashMap<QSSType, DurationStat>();
      Map<QSSType, List<Double>> ctQueries = new HashMap<QSSType, List<Double>>();

      for (QSSType qssType : qssTypes) {
        String message =
            "act,"
                + file.getName()
                + ","
                + map.get(file)
                + ","
                + targetSource
                + ","
                + qssType
                + ","
                + randomNr;
        long preprocessModulExtract = System.currentTimeMillis();
        OWLOntology ontology =
            loadOntology(matchingsDir, ontologyDir, map, conferenceSession, file, o1, o2, n);
        ctTheory = loadTheory(ontology, ontologyDir, o1, o2);
        preprocessModulExtract = System.currentTimeMillis() - preprocessModulExtract;
        message += "," + preprocessModulExtract;

        // Define Treesearch here
        TreeSearch<FormulaSet<OWLLogicalAxiom>, OWLLogicalAxiom> search =
            getUniformCostSearch(ctTheory, false);

        Map<OWLLogicalAxiom, BigDecimal> map1 =
            conferenceSession.readRdfMapping(matchingsDir + map.get(file), n + ".rdf");
        OWLAxiomCostsEstimator es = new OWLAxiomCostsEstimator(ctTheory, map1);
        search.setCostsEstimator(es);
        search.reset();

        // set for all found diagnoses during search
        foundDiagnoses = new LinkedHashSet<OWLLogicalAxiom>();

        ctTimes.put(qssType, new DurationStat());
        ctQueries.put(qssType, new LinkedList<Double>());

        // calculation part
        ConflictTreeSession conflictTreeSearch = new ConflictTreeSession(this, ctTheory, search);
        if (numberOfConflicts > 0) {
          conflictTreeSearch.setMaximumNumberOfConflicts(numberOfConflicts);
        }
        conflictTreeSearch.setOutputString(out);
        conflictTreeSearch.setMessageString(message);
        FormulaSet<OWLLogicalAxiom> targetD =
            new FormulaSetImpl<OWLLogicalAxiom>(new BigDecimal(1), targetDg, null);
        long completeTime = conflictTreeSearch.search(targetD, ctQueries, qssType, true);
        ctTimes.get(qssType).add(completeTime);
        out += conflictTreeSearch.getOutputString();

        foundDiagnoses = conflictTreeSearch.getDiagnosis();
        logger.info("targetD: " + targetD.size() + ", " + CalculateDiagnoses.renderAxioms(targetD));
        logger.info(
            "foundD: "
                + foundDiagnoses.size()
                + ", "
                + CalculateDiagnoses.renderAxioms(foundDiagnoses));

        faultyTheory
            .getReasoner()
            .addFormulasToCache(faultyTheory.getKnowledgeBase().getFaultyFormulas());
        assertFalse(faultyTheory.verifyConsistency());
        Set<OWLLogicalAxiom> removedFound = validateMinimality(foundDiagnoses, faultyTheory);
        // assertTrue(removed.isEmpty());
        Set<OWLLogicalAxiom> removedTarget = validateMinimality(targetD, faultyTheory);
        assertTrue(removedTarget.isEmpty());

        assertTrue(targetD.containsAll(foundDiagnoses));
        // TODO uncomment later
        // assertTrue(foundDiagnoses.containsAll(targetD));

        resetTheoryTests(ctTheory);
        search.reset();
      }
      logger.info(out);
    }
  }