/** Make sure that we can augment a graph with a single state which is a reject-state. */
 @Test
 public void testPTAconstruction_singleRejectState() {
   Configuration config = mainConfiguration.copy();
   RPNIUniversalLearner l =
       new RPNIUniversalLearner(
           null, new LearnerEvaluationConfiguration(null, null, config, null, null));
   config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
   // set the initial state to be reject
   l.getTentativeAutomaton().initPTA();
   l.getTentativeAutomaton().getVertex(new LinkedList<Label>()).setAccept(false);
   // and check how augmentPTA works with such a PTA
   for (boolean maxAutomaton : new boolean[] {true, false}) {
     for (List<Label> sequence : buildSet(new String[][] {new String[] {}}, config, converter))
       l.getTentativeAutomaton().paths.augmentPTA(sequence, false, maxAutomaton, null);
     for (List<Label> sequence : buildSet(new String[][] {}, config, converter))
       l.getTentativeAutomaton().paths.augmentPTA(sequence, true, maxAutomaton, null);
     DirectedSparseGraph actualC = l.getTentativeAutomaton().pathroutines.getGraph();
     Assert.assertEquals(1, actualC.getVertices().size());
     Assert.assertEquals(
         false,
         DeterministicDirectedSparseGraph.isAccept(
             ((Vertex) actualC.getVertices().iterator().next())));
     Assert.assertEquals(0, ((CmpVertex) (actualC.getVertices().iterator().next())).getDepth());
     Assert.assertEquals(0, actualC.getEdges().size());
   }
 }
예제 #2
0
 public LearnerEvaluationConfiguration(Configuration defaultCnf) {
   // Mostly rely on defaults above.
   if (defaultCnf == null)
     config =
         Configuration.getDefaultConfiguration()
             .copy(); // making a clone is important because the configuration may later be
                      // modified and we do not wish to mess up the default one.
   else config = defaultCnf.copy();
 }
예제 #3
0
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null) return false;
      if (!(obj instanceof LearnerEvaluationConfiguration)) return false;
      final LearnerEvaluationConfiguration other = (LearnerEvaluationConfiguration) obj;
      assert config != null && other.config != null;
      if (!config.equals(other.config)) return false;

      assert graph != null && other.graph != null;
      if (graph == null) {
        if (other.graph != null) return false;
      } else if (!graph.equals(other.graph)) return false;
      if (ifthenSequences == null) {
        if (other.ifthenSequences != null) return false;
      } else if (!ifthenSequences.equals(other.ifthenSequences)) return false;

      if (testSet == null) {
        if (other.testSet != null) return false;
      } else if (!testSet.equals(other.testSet)) return false;

      if (labelDetails == null) {
        if (other.labelDetails != null) return false;
      } else if (!labelDetails.equals(other.labelDetails)) return false;
      return true;
    }
  /**
   * Checks that if we add a positive path over a negative, we get an exception regardless of
   * whether we add this to a maximal automaton or to a PTA.
   */
  @Test
  public void testAddPositive() {
    final Configuration conf = mainConfiguration;
    Set<List<Label>>
        plusStrings =
            buildSet(
                new String[][] {
                  new String[] {"a", "b", "c"},
                  new String[] {"a", "b"},
                  new String[] {"a", "d", "c"}
                },
                conf,
                converter),
        minusStrings =
            buildSet(new String[][] {new String[] {"a", "b", "c", "d"}}, conf, converter);

    for (boolean max : new boolean[] {true, false}) {
      final boolean maxAutomaton = max;
      Configuration config = mainConfiguration.copy();
      final RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.getTentativeAutomaton().initPTA();
      l.getTentativeAutomaton().paths.augmentPTA(minusStrings, false, maxAutomaton);
      l.getTentativeAutomaton().paths.augmentPTA(plusStrings, true, maxAutomaton);

      checkForCorrectException(
          new whatToRun() {
            public @Override void run() throws NumberFormatException {
              l.getTentativeAutomaton()
                  .paths
                  .augmentPTA(
                      buildSet(new String[][] {new String[] {"a", "b", "c", "d"}}, conf, converter),
                      true,
                      maxAutomaton);
            }
          },
          IllegalArgumentException.class,
          "incompatible ");
    }
  }
예제 #5
0
 /* (non-Javadoc)
  * @see java.lang.Object#hashCode()
  */
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((config == null) ? 0 : config.hashCode());
   result = prime * result + ((graph == null) ? 0 : graph.hashCode());
   result = prime * result + ((ifthenSequences == null) ? 0 : ifthenSequences.hashCode());
   result = prime * result + ((testSet == null) ? 0 : testSet.hashCode());
   result = prime * result + ((labelDetails == null) ? 0 : labelDetails.hashCode());
   return result;
 }
예제 #6
0
 /**
  * Performs initialization if necessary of the io routines aimed at loading/saving sequences of
  * labels.
  */
 protected void initIO(Document document, Configuration configuration) {
   if (configuration.getLegacyXML()) {
     if (labelio == null)
       labelio =
           new StatechumXML.LEGACY_StringLabelSequenceWriter(
               document, configuration, getLabelConverter());
     if (stringio == null) stringio = new StatechumXML.LEGACY_StringSequenceWriter(document);
   } else { // current
     if (labelio == null)
       labelio =
           new StatechumXML.LabelSequenceWriter(document, configuration, getLabelConverter());
     if (stringio == null) stringio = new StatechumXML.StringSequenceWriter(document);
   }
 }
예제 #7
0
  public AnySignature(Configuration config, OtpErlangList attributes) {
    super();
    if (attributes.arity() != 0)
      throw new IllegalArgumentException("AnySignature does not accept attributes");

    List<OtpErlangObject> result = new ArrayList<OtpErlangObject>();
    switch (config.getErlangAlphabetAnyElements()) {
      case ANY_INT:
        result.add(new OtpErlangInt(10));
        result.add(new OtpErlangInt(11));
        result.add(new OtpErlangInt(12));
        break;
      case ANY_WIBBLE:
        result.add(new OtpErlangAtom("AnyWibble"));
        break;
      case ANY_WITHLIST:
        // We are allowed anything so lets try a few things...
        if (config.getUseANumberOfValues()) {
          result.add(new OtpErlangAtom("JustAnythingA"));
          result.add(new OtpErlangList(new OtpErlangObject[] {}));
          result.add(new OtpErlangList(new OtpErlangObject[] {new OtpErlangAtom("WibbleA")}));
          result.add(
              new OtpErlangList(
                  new OtpErlangObject[] {
                    new OtpErlangAtom("WibbleA"), new OtpErlangAtom("WobbleA")
                  }));
        } else
          result.add(
              new OtpErlangAtom(
                  "A")); // the motivation for a shorter label is that longer ones would look really
                         // long on large graphs.
        break;
    }
    values = Collections.unmodifiableList(result);
    erlangTermForThisType = erlangTypeToString(attributes, null);
  }
 @Test
 public void testAugmentPTA_Simple() // only two traces, both accept
     {
   Configuration config = mainConfiguration.copy();
   config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
   Set<List<Label>> plusStrings =
       buildSet(
           new String[][] {new String[] {"a", "b", "c"}, new String[] {"a", "d", "c"}},
           config,
           converter);
   DirectedSparseGraph actualA =
       Test_Orig_RPNIBlueFringeLearner.augmentPTA(
           DeterministicDirectedSparseGraph.initialise(), plusStrings, true);
   DeterministicDirectedSparseGraph.numberVertices(
       actualA); // Numbering is necessary to ensure uniqueness of labels used by LearnerGraph
   // constructor.
   config.setAllowedToCloneNonCmpVertex(true);
   LearnerGraph l = new LearnerGraph(config);
   LearnerGraph actualC = l.paths.augmentPTA(plusStrings, true, false);
   DeterministicDirectedSparseGraph.numberVertices(actualA);
   String expectedPTA = "A-a->B--b->C-c->Z1\nB--d->C2-c->Z2";
   checkM(expectedPTA, new LearnerGraph(actualA, config), config, converter);
   checkM(expectedPTA, actualC, config, converter);
 }
  /**
   * Builds a PTA from the supplied arguments using two different methods. If any of them throws,
   * checks that another one throws too and then rethrows the exception.
   *
   * @param arrayPlusStrings allowed sequences
   * @param arrayMinusStrings sequences ending at a reject state
   * @param expectedPTA a textual representation of a PTA which should be built.
   * @param expectMaxAutomataToBeTheSameAsPTA whether we expect augmentation of a maximal automaton
   *     to yield the same result as that of a normal PTA.
   */
  private void checkPTAconstruction(
      String[][] arrayPlusStrings,
      String[][] arrayMinusStrings,
      String expectedPTA,
      boolean expectMaxAutomataToBeTheSameAsPTA) {
    Configuration conf = mainConfiguration.copy();
    Set<List<Label>> plusStrings = buildSet(arrayPlusStrings, conf, converter),
        minusStrings = buildSet(arrayMinusStrings, conf, converter);
    LearnerGraph actualA = null, actualC = null, actualD = null, actualE = null, actualF = null;
    IllegalArgumentException eA = null, eC = null, eD = null, eE = null, eF = null;
    try {
      actualA =
          new LearnerGraph(
              Test_Orig_RPNIBlueFringeLearner.createAugmentedPTA(plusStrings, minusStrings), conf);
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eA = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.init(plusStrings, minusStrings);
      actualC = l.getTentativeAutomaton();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eC = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      PTASequenceEngine engine = buildPTA(plusStrings, minusStrings);
      checkPTAConsistency(engine, plusStrings, true);
      if (engine.numberOfLeafNodes() > 0) checkPTAConsistency(engine, minusStrings, false);
      l.init(engine, 0, 0);
      actualD = l.getTentativeAutomaton();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eD = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.init(buildPTA(plusStrings, buildSet(new String[][] {}, config, converter)), 0, 0);
      for (List<Label> seq : minusStrings) {
        Set<List<Label>> negativeSeq = new HashSet<List<Label>>();
        negativeSeq.add(seq);
        l.getTentativeAutomaton()
            .paths
            .augmentPTA(buildPTA(buildSet(new String[][] {}, config, converter), negativeSeq));
      }
      actualE = l.getTentativeAutomaton();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eE = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.getTentativeAutomaton().initPTA();
      l.getTentativeAutomaton().paths.augmentPTA(minusStrings, false, true);
      l.getTentativeAutomaton().paths.augmentPTA(plusStrings, true, true);
      actualF = l.getTentativeAutomaton();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eF = e;
    }

    if (eA != null) {
      Assert.assertNotNull(eC);
      Assert.assertNotNull(eD);
      Assert.assertNotNull(eE);
      if (expectMaxAutomataToBeTheSameAsPTA) Assert.assertNotNull(eF);
      throw eA;
    }

    Assert.assertNull(eA);
    Assert.assertNull(eC);
    Assert.assertNull(eD);
    Assert.assertNull(eE);
    if (expectMaxAutomataToBeTheSameAsPTA) Assert.assertNull(eF);

    Configuration config = mainConfiguration.copy();
    config.setAllowedToCloneNonCmpVertex(true);
    checkM(expectedPTA, actualA, config, converter);
    checkM(expectedPTA, actualC, config, converter);
    checkDepthLabelling(actualC);
    // Visualiser.updateFrame(actualE,FsmParser.buildGraph(expectedPTA,"expected
    // graph"));Visualiser.waitForKey();
    checkM(expectedPTA, actualD, config, converter);
    checkDepthLabelling(actualD);
    checkM(expectedPTA, actualE, config, converter);
    checkDepthLabelling(actualE);

    if (expectMaxAutomataToBeTheSameAsPTA) checkM(expectedPTA, actualF, config, converter);
    checkDepthLabelling(actualF);
  }
  private void checkEmptyPTA(
      String[][] arrayPlusStrings,
      String[][] arrayMinusStrings,
      boolean expectMaxAutomataToBeTheSameAsPTA) {
    Configuration conf = mainConfiguration.copy();
    Set<List<Label>> plusStrings = buildSet(arrayPlusStrings, conf, converter),
        minusStrings = buildSet(arrayMinusStrings, conf, converter);
    DirectedSparseGraph actualA = null,
        actualC = null,
        actualD = null,
        actualE = null,
        actualF = null;
    IllegalArgumentException eA = null, eC = null, eD = null, eE = null, eF = null;
    try {
      actualA = Test_Orig_RPNIBlueFringeLearner.createAugmentedPTA(plusStrings, minusStrings);
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eA = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.init(plusStrings, minusStrings);
      actualC = l.getTentativeAutomaton().pathroutines.getGraph();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eC = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      PTASequenceEngine engine = buildPTA(plusStrings, minusStrings);
      checkPTAConsistency(engine, plusStrings, true);
      if (engine.numberOfLeafNodes() > 0) checkPTAConsistency(engine, minusStrings, false);
      l.init(engine, 0, 0);
      actualD = l.getTentativeAutomaton().pathroutines.getGraph();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eD = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.init(buildPTA(plusStrings, buildSet(new String[][] {}, config, converter)), 0, 0);
      for (List<Label> seq : minusStrings) {
        Set<List<Label>> negativeSeq = new HashSet<List<Label>>();
        negativeSeq.add(seq);
        l.getTentativeAutomaton()
            .paths
            .augmentPTA(buildPTA(buildSet(new String[][] {}, config, converter), negativeSeq));
      }
      actualE = l.getTentativeAutomaton().pathroutines.getGraph();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eE = e;
    }

    try {
      Configuration config = mainConfiguration.copy();
      RPNIUniversalLearner l =
          new RPNIUniversalLearner(
              null, new LearnerEvaluationConfiguration(null, null, config, null, null));
      config.setLearnerIdMode(Configuration.IDMode.POSITIVE_NEGATIVE);
      l.getTentativeAutomaton().initPTA();
      l.getTentativeAutomaton().paths.augmentPTA(minusStrings, false, true);
      l.getTentativeAutomaton().paths.augmentPTA(plusStrings, true, true);
      actualF = l.getTentativeAutomaton().pathroutines.getGraph();
    } catch (IllegalArgumentException e) {
      // ignore this - it might be expected.
      eF = e;
    }

    if (eA != null) { // an exception has been thrown, hence verify that all way of PTA construction
      // have thrown too.
      Assert.assertNotNull(eC);
      Assert.assertNotNull(eD);
      Assert.assertNotNull(eE);
      if (expectMaxAutomataToBeTheSameAsPTA) Assert.assertNotNull(eF);
      throw eA;
    }

    Assert.assertNull(eA);
    Assert.assertNull(eC);
    Assert.assertNull(eD);
    Assert.assertNull(eE);
    if (expectMaxAutomataToBeTheSameAsPTA) Assert.assertNull(eF);

    Assert.assertEquals(1, actualA.getVertices().size());
    Assert.assertEquals(
        true,
        DeterministicDirectedSparseGraph.isAccept(
            ((Vertex) actualA.getVertices().iterator().next())));
    Assert.assertEquals(0, actualA.getEdges().size());

    Assert.assertEquals(1, actualC.getVertices().size());
    Assert.assertEquals(
        true,
        DeterministicDirectedSparseGraph.isAccept(
            ((Vertex) actualC.getVertices().iterator().next())));
    Assert.assertEquals(0, ((CmpVertex) (actualC.getVertices().iterator().next())).getDepth());
    Assert.assertEquals(0, actualC.getEdges().size());

    Assert.assertEquals(1, actualD.getVertices().size());
    Assert.assertEquals(
        true,
        DeterministicDirectedSparseGraph.isAccept(
            ((Vertex) actualD.getVertices().iterator().next())));
    Assert.assertEquals(0, ((CmpVertex) (actualD.getVertices().iterator().next())).getDepth());
    Assert.assertEquals(0, actualD.getEdges().size());

    Assert.assertEquals(1, actualE.getVertices().size());
    Assert.assertEquals(
        true,
        DeterministicDirectedSparseGraph.isAccept(
            ((Vertex) actualE.getVertices().iterator().next())));
    Assert.assertEquals(0, ((CmpVertex) (actualE.getVertices().iterator().next())).getDepth());
    Assert.assertEquals(0, actualE.getEdges().size());

    if (expectMaxAutomataToBeTheSameAsPTA) {
      Assert.assertEquals(1, actualF.getVertices().size());
      Assert.assertEquals(
          true,
          DeterministicDirectedSparseGraph.isAccept(
              ((Vertex) actualF.getVertices().iterator().next())));
      Assert.assertEquals(0, ((CmpVertex) (actualF.getVertices().iterator().next())).getDepth());
      Assert.assertEquals(0, actualF.getEdges().size());
    }
  }