Exemple #1
0
  private static void xmlExample(InputStream input, int nbrSteps) {
    XmlHandler handler = new XmlHandler();
    try {
      handler.readStream(input);
    } catch (ParserConfigurationException e) {

      e.printStackTrace();
    } catch (SAXException e) {

      e.printStackTrace();
    } catch (IOException e) {

      e.printStackTrace();
    }

    System.out.println("Input: " + handler.getRoot());

    Set<AssertionInterface> valid = handler.getRoot().getGivenAssertions();

    InferenceMaps maps = handler.getRoot().getMaps();

    Set<ConstrainedAssertionFilter> invalidity = handler.getRoot().getInvalidityFilters();
    Set<ConstrainedAssertionFilter> triviality = handler.getRoot().getTrivialityFilters();

    /** use equivalence classes to speed up the process */
    AssertionEquivalenceClasses eq_classes =
        new AssertionEquivalenceClasses(new HashSet<ArrayList<Integer>>(), new StringIds());

    eq_classes.addNewAssertions(valid);

    /** do some inference */
    int step = 0;
    int size = 0;

    boolean cancel_further_steps = false;

    ExcessLimit limit = new ExcessLimit(30);

    while (step <= nbrSteps) {
      size = eq_classes.getClasses().size();

      System.out.println("  ++++ Step " + step + " ++++\n\n");
      if (step > 0) {
        Set<AssertionInterface> new_assertions = maps.inferNew(eq_classes.getClasses(), limit);

        eq_classes.markAllOld();

        for (ConstrainedAssertionFilter filter : invalidity) {
          Set<AssertionInterface> invalid = filter.filter(new_assertions);
          if (invalid.isEmpty() == false) {

            System.out.println(" #### The following inferred assertions are invalid #### ");

            TreeSet<String> ordered = new TreeSet<String>();
            for (AssertionInterface a : invalid) {
              ordered.add(
                  "!! "
                      + a.getSubject()
                      + "·"
                      + a.getPredicate()
                      + "·"
                      + a.getObject()
                      + "\n"
                      + a.toString().replaceAll("\n", "\n!! "));
            }

            for (String s : ordered) {
              System.out.println(s);
            }

            cancel_further_steps = true;
          }
        }

        if (cancel_further_steps) {
          break;
        }

        int dropped_trivial = 0;

        for (ConstrainedAssertionFilter filter : triviality) {
          Set<AssertionInterface> trivial = filter.filter(new_assertions);
          dropped_trivial += trivial.size();

          TreeSet<String> ordered = new TreeSet<String>();
          for (AssertionInterface a : trivial) {
            ordered.add(
                "  ++ trivial ++ "
                    + a.getSubject()
                    + "·"
                    + a.getPredicate()
                    + "·"
                    + a.getObject()
                    + "\n"
                    + a.toString().replaceAll("\n", "\n  ++ "));
          }

          for (String s : ordered) {
            System.out.println(s);
          }

          new_assertions.removeAll(trivial);
        }

        System.out.println("   ++ dropped " + dropped_trivial + " trivial assertions ++ ");

        eq_classes.addNewAssertions(new_assertions);
      }

      TreeSet<String> ordered = new TreeSet<String>();
      for (AssertionInterface a : eq_classes.getClasses()) {
        ordered.add(a.toString());
      }

      for (String s : ordered) {
        System.out.println(s);
      }

      step++;
      System.out.println(
          "\n   +++ premise assertions " + size + " -> " + eq_classes.getClasses().size());

      if ((size == eq_classes.getClasses().size()) && (step > 1)) {
        break; // nothing new
      }
    }

    if (cancel_further_steps) {
      System.out.println(
          "\n\nIt was possible to infer invalid assertions from the given \n"
              + "initial set of assertions and the given inference rules! Please check!");

      return;
    }

    System.out.println("\n\n The following non-trivial assertions can be inferred\n\n");

    TreeSet<String> ordered = new TreeSet<String>();
    for (AssertionInterface a : eq_classes.getClasses()) {
      ordered.add(a.getSubject() + " " + a.getPredicate() + " " + a.getObject());
    }

    int count = 0;

    for (String s : ordered) {
      ++count;

      System.out.println(String.format("%6d", count) + "    " + s);
    }

    if (limit.exceeded()) {
      System.out.println("Stopped inference after time limit exceeded.");
    }
  }
Exemple #2
0
  private static void hardcodedExample() {
    Set<InferenceMap> mapset = new HashSet<InferenceMap>();
    mapset.add(new Phi1to2());
    mapset.add(new Phi2_3to2());
    mapset.add(new Phi2Combine());
    mapset.add(new Phi2Neg());
    mapset.add(new Phi3());
    InferenceMaps maps = new InferenceMaps(mapset);

    String[] premises = {
      "bulb A·is connected in parallel with·bulbchain BC",
      "bulb B·is serial connected with·bulb C",
      "the voltage of bulbchain BC·is bigger than·the voltage of bulb B",
      "a bigger voltage·means·a bigger current",
      "a bigger voltage·means·a bigger luminosity",
      "the current through bulb A·is bigger than·the current through bulbchain BC"
    };

    Set<AssertionInterface> valid = new HashSet<AssertionInterface>();
    for (String premise : premises) {
      valid.add(new Assertion(premise));
    }

    /** use equivalence classes to speed up the process */
    AssertionEquivalenceClasses eq_classes =
        new AssertionEquivalenceClasses(new HashSet<ArrayList<Integer>>(), new StringIds());

    eq_classes.addNewAssertions(valid);

    /** do some inference */
    int step = 0;
    int size = 0;

    while (step <= 100) {
      size = eq_classes.getClasses().size();

      System.out.println("  ++++ Step " + step + " ++++\n\n");
      if (step > 0) {
        Set<AssertionInterface> new_assertions =
            InferredAssertion.nonTrivial(maps.inferNew(eq_classes.getClasses(), null));
        eq_classes.markAllOld();
        eq_classes.addNewAssertions(new_assertions);
      }

      TreeSet<String> ordered = new TreeSet<String>();
      for (AssertionInterface a : eq_classes.getClasses()) {
        ordered.add(a.toString());
      }

      for (String s : ordered) {
        System.out.println(s);
      }

      step++;
      System.out.println(
          "\n   +++ premise assertions " + size + " -> " + eq_classes.getClasses().size());

      if ((size == eq_classes.getClasses().size()) && (step > 1)) {
        break; // nothing new
      }
    }
  }
Exemple #3
0
  private static void regexpExample() {
    Set<InferenceMap> mapset = new HashSet<InferenceMap>();

    /** fill the map set */
    RegExpInferenceMap phi3 = new RegExpInferenceMap("trans", false);
    phi3.addPremiseForm(".*", "means", ".*");
    phi3.addPremiseForm(".*", "means", ".*");
    phi3.addPremiseConstraint(0, AssertionPart.object, 1, AssertionPart.subject);
    phi3.addConclusion(
        0, AssertionPart.subject, 0, AssertionPart.predicate, 1, AssertionPart.object);

    mapset.add(phi3);

    RegExpInferenceMap phi2neg = new RegExpInferenceMap("neg", false);
    phi2neg.addPremiseForm(".*", "is (as.*as|(bigger|smaller) than)", ".*");
    phi2neg.addConclusion(
        0,
        AssertionPart.object,
        ".*→»1",
        0,
        AssertionPart.predicate,
        "is b.*→is smaller than¶is s.*→is bigger than¶is as.*as→»1",
        0,
        AssertionPart.subject,
        ".*→»1");

    mapset.add(phi2neg);

    RegExpInferenceMap phi2monotone = new RegExpInferenceMap("monotone", false);
    phi2monotone.addPremiseForm(".*", "is ((bigger|smaller) than|as big as)", ".*");
    phi2monotone.addPremiseForm(".*", "is ((bigger|smaller) than|as big as)", ".*");

    phi2monotone.addPremiseConstraint(
        0, AssertionPart.predicate, 1, AssertionPart.predicate, "is (b|s).*→»1¶.*→is as big as");

    phi2monotone.addPremiseConstraint(0, AssertionPart.object, 1, AssertionPart.subject);

    phi2monotone.addConclusion(
        0, AssertionPart.subject, 0, AssertionPart.predicate, 1, AssertionPart.object);

    mapset.add(phi2monotone);

    RegExpInferenceMap phi1to2s = new RegExpInferenceMap("serial", false);
    phi1to2s.addPremiseForm(".*", "is serial connected with", ".*");
    phi1to2s.addConclusion(
        0,
        AssertionPart.subject,
        ".*→the current through ·»1",
        0,
        AssertionPart.predicate,
        ".*→is as big as",
        0,
        AssertionPart.object,
        ".*→the current through ·»1");

    mapset.add(phi1to2s);

    RegExpInferenceMap phi1to2p = new RegExpInferenceMap("parallel", false);
    phi1to2p.addPremiseForm(".*", "is connected in parallel with", ".*");
    phi1to2p.addConclusion(
        0,
        AssertionPart.subject,
        ".*→the voltage of ·»1",
        0,
        AssertionPart.predicate,
        ".*→is as big as",
        0,
        AssertionPart.object,
        ".*→the voltage of ·»1");

    mapset.add(phi1to2p);

    /** monotone relation */
    RegExpInferenceMap phi2_3to3m1 = new RegExpInferenceMap("combine-monotone", false);
    phi2_3to3m1.addPremiseForm(
        "the.*(of|through).*", "is (as big as|(small|bigg)er than)", "the.*(of|through).*");
    phi2_3to3m1.addPremiseForm(".*", "means", ".*");

    /** there are constraints that cannot be represented by addPremiseConstraint */
    phi2_3to3m1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            0,
            AssertionPart.subject,
            new SplittedStringRelation("the ·.*· (of|through).*→»2"),
            0,
            AssertionPart.object,
            new SplittedStringRelation("the ·.*· (of|through).*→»2")));

    phi2_3to3m1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            0,
            AssertionPart.subject,
            new SplittedStringRelation("the ·.*· (of|through).*→»2"),
            1,
            AssertionPart.subject,
            new SplittedStringRelation("a (bigg|small)er ·.*→»2")));

    phi2_3to3m1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            1,
            AssertionPart.subject,
            new SplittedStringRelation("a (bigg|small)er ·.*→»1"),
            1,
            AssertionPart.object,
            new SplittedStringRelation("a (bigg|small)er ·.*→»1")));

    /** this rule cannot be represented with addConclusion -> need to do it manually */
    RegExpInferenceMap.AdvancedPremiseCombinator conclusion =
        new RegExpInferenceMap.AdvancedPremiseCombinator(phi2_3to3m1);

    conclusion.addSubjectPart(
        new StringRelationJoin(
            "a (bigg|small)er current→the current through ¶a (bigg|small)er ·[^c].*→the ·»2· of "),
        1,
        AssertionPart.object);
    conclusion.addObjectPart(
        new StringRelationJoin(
            "a (bigg|small)er current→the current through ¶a (bigg|small)er ·[^c].*→the ·»2· of "),
        1,
        AssertionPart.object);

    conclusion.addSubjectPart(
        new SplittedStringRelation("the.*(of|through) ·.*→»2"), 0, AssertionPart.subject);
    conclusion.addObjectPart(
        new SplittedStringRelation("the.*(of|through) ·.*→»2"), 0, AssertionPart.object);

    conclusion.addPredicatePart(new SplittedStringRelation(".*→»1"), 0, AssertionPart.predicate);

    phi2_3to3m1.addPremiseCombinator(conclusion);

    mapset.add(phi2_3to3m1);

    /** antitone relation */
    RegExpInferenceMap phi2_3to3a1 = new RegExpInferenceMap("combine-antitone", false);
    phi2_3to3a1.addPremiseForm(
        "the.*(of|through).*", "is (as big as|(small|bigg)er than)", "the.*(of|through).*");
    phi2_3to3a1.addPremiseForm(".*", "means", ".*");

    /** there are constraints that cannot be represented by addPremiseConstraint */
    phi2_3to3a1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            0,
            AssertionPart.subject,
            new SplittedStringRelation("the ·.*· (of|through).*→»2"),
            0,
            AssertionPart.object,
            new SplittedStringRelation("the ·.*· (of|through).*→»2")));

    phi2_3to3a1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            0,
            AssertionPart.subject,
            new SplittedStringRelation("the ·.*· (of|through).*→»2"),
            1,
            AssertionPart.subject,
            new SplittedStringRelation("a (bigg|small)er ·.*→»2")));

    phi2_3to3a1.addConstraint(
        new RegExpInferenceMap.AdvancedCompatibleChecker(
            1,
            AssertionPart.subject,
            new StringRelationJoin("a bigger.*→big¶a smaller.*→small"),
            1,
            AssertionPart.object,
            new StringRelationJoin("a bigger.*→small¶a smaller.*→big")));

    /** this rule cannot be represented with addConclusion -> need to do it manually */
    RegExpInferenceMap.AdvancedPremiseCombinator conclusion2 =
        new RegExpInferenceMap.AdvancedPremiseCombinator(phi2_3to3a1);

    conclusion2.addSubjectPart(
        new StringRelationJoin(
            "a (bigg|small)er current→the current through ¶a (bigg|small)er ·[^c].*→the ·»2· of "),
        1,
        AssertionPart.object);
    conclusion2.addObjectPart(
        new StringRelationJoin(
            "a (bigg|small)er current→the current through ¶a (bigg|small)er ·[^c].*→the ·»2· of "),
        1,
        AssertionPart.object);

    conclusion2.addSubjectPart(
        new SplittedStringRelation("the.*(of|through) ·.*→»2"), 0, AssertionPart.subject);
    conclusion2.addObjectPart(
        new SplittedStringRelation("the.*(of|through) ·.*→»2"), 0, AssertionPart.object);

    conclusion2.addPredicatePart(
        new StringRelationJoin("is bigger than→is smaller than¶is smaller than→is bigger than"),
        0,
        AssertionPart.predicate);

    phi2_3to3a1.addPremiseCombinator(conclusion2);

    mapset.add(phi2_3to3a1);

    /** initial premises */
    String[] premises = {
      "bulb A·is connected in parallel with·bulbchain BC",
      "bulb B·is serial connected with·bulb C",
      "the voltage of bulbchain BC·is bigger than·the voltage of bulb B",
      "a bigger voltage·means·a bigger current",
      "a bigger voltage·means·a bigger luminosity",
      "the current through bulb A·is bigger than·the current through bulbchain BC"

      /*
       * "bulb A is connected in parallel with bulbchain BC",
       * "bulb B is serial connected with bulb C",
       * "the voltage of bulbchain BC is bigger than the voltage of bulb B",
       * "a bigger voltage means a smaller current",
       * "a bigger voltage means a bigger luminosity",
       * "the current through bulb A is bigger than the current through bulbchain BC"
       * , "my left leg is as crooked as my right leg", "X is as big as Y",
       * "Y is smaller than Z", "V is bigger than X", "X is as big as X2",
       * "Q means Q2", "Q2 means Q3", "Q4 means Q3"
       */

    };

    /** chop into subject·predicate·object */
    SubjectPredicateObjectMatcher matchStrings =
        new SubjectPredicateObjectMatcher(
            // subject starts with non-whitespace
            "\\S.*",
            // predicate is of either form: is ... with, is ... than, is as
            // ... as, means
            "(means|is(\\s.*\\swith|\\s.*\\sthan|\\s+as.*\\sas))",
            // object starts with non-whitespace
            "\\S.*");

    Set<AssertionInterface> valid = new HashSet<AssertionInterface>();
    for (String premise : premises) {
      valid.addAll(matchStrings.match((premise)));
    }

    InferenceMaps maps = new InferenceMaps(mapset);

    /** use equivalence classes to speed up the process */
    AssertionEquivalenceClasses eq_classes =
        new AssertionEquivalenceClasses(new HashSet<ArrayList<Integer>>(), new StringIds());

    eq_classes.addNewAssertions(valid);

    /** do some inference */
    int step = 0;
    int size = 0;

    while (step <= 100) {
      size = eq_classes.getClasses().size();

      System.out.println("  ++++ Step " + step + " ++++\n\n");
      if (step > 0) {
        Set<AssertionInterface> new_assertions =
            InferredAssertion.nonTrivial(
                maps.inferNew(eq_classes.getClasses(), new ExcessLimit(30)));
        eq_classes.markAllOld();
        eq_classes.addNewAssertions(new_assertions);
      }

      TreeSet<String> ordered = new TreeSet<String>();
      for (AssertionInterface a : eq_classes.getClasses()) {
        ordered.add(a.toString());
      }

      for (String s : ordered) {
        System.out.println(s);
      }

      step++;
      System.out.println(
          "\n   +++ premise assertions " + size + " -> " + eq_classes.getClasses().size());

      if ((size == eq_classes.getClasses().size()) && (step > 1)) {
        break; // nothing new
      }
    }
  }