Package statechum.analysis.learning

Examples of statechum.analysis.learning.RPNIUniversalLearner


  {
    Configuration testConfig = Configuration.getDefaultConfiguration().copy();
    testConfig.setGdFailOnDuplicateNames(false);
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);

    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          @SuppressWarnings("unused"List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here


    // now sanity checking on the plus and minus sets
    for(String [] path:plus)
      assert AbstractOracle.USER_ACCEPTED == expected.paths.tracePathPrefixClosed(AbstractLearnerGraph.buildList(Arrays.asList(path),testConfig,converter));
    for(String [] path:minus)
      assert AbstractOracle.USER_ACCEPTED != expected.paths.tracePathPrefixClosed(AbstractLearnerGraph.buildList(Arrays.asList(path),testConfig,converter));
    Learner l = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
          @SuppressWarnings("unused") List<Boolean> acceptedElements,
          @SuppressWarnings("unused") PairScore pairBeingMerged,
          @SuppressWarnings("unused"final Object [] moreOptions)
      {
        return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
      }
    };
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);
    ByteArrayOutputStream logStream = new ByteArrayOutputStream();
    RecordProgressDecorator recorder = new RecordProgressDecorator(l,logStream,1,testConfig,useZip);
    Collection<List<Label>> testSet = new LinkedList<List<Label>>();
    recorder.writeLearnerEvaluationData(new LearnerEvaluationConfiguration(expected, testSet, testConfig, null, null));
    LearnerGraph learntStructureA = recorder.learnMachine(buildSet(plus,testConfig,converter), buildSet(minus,testConfig,converter));
   
    //System.out.println("compression rate: "+recorder.getCompressionRate());
    //System.out.println(logStream.toString()+"============");
    //System.out.println(logStream.toByteArray().length);
    LearnerGraph learntMachineNoRejects = new LearnerGraph(testConfig);
    AbstractPathRoutines.removeRejectStates(learntStructureA,learntMachineNoRejects);
    Assert.assertNull(WMethod.checkM(learntMachineNoRejects, expected));
   
    switch(kind)
    {
      case RECORDERTEST_SS:
      {// matching two simulators
        final LearnerSimulator
          simulator = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip,converter),
          simulator2 = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip,converter);
       
        LearnerEvaluationConfiguration eval1 = simulator.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval1.graph));
        Assert.assertEquals(testSet, eval1.testSet);
        Assert.assertEquals(expected.config, testConfig);
        LearnerEvaluationConfiguration eval2 = simulator2.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval2.graph));
        Assert.assertEquals(testSet, eval2.testSet);
        Assert.assertEquals(expected.config, testConfig);
       
        new Test_LearnerComparator(simulator,simulator2,true).learnMachine(buildSet(plus,testConfig,converter), buildSet(minus,testConfig,converter));
        break;
      }
     
      case RECORDERTEST_SL:
      {// now a simulator to a learner
        final LearnerSimulator simulator = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip,converter);
        LearnerEvaluationConfiguration eval1 = simulator.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval1.graph));
        Assert.assertEquals(testSet, eval1.testSet);
        Assert.assertEquals(expected.config, testConfig);
 
        Learner learner2 = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,expected.config,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
              @SuppressWarnings("unused") List<Boolean> acceptedElements,
              @SuppressWarnings("unused") PairScore pairBeingMerged,
              @SuppressWarnings("unused"final Object [] moreOptions)
          {
            return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
          }
        };
        new Test_LearnerComparator(learner2,simulator,true).learnMachine(buildSet(plus,testConfig,converter), buildSet(minus,testConfig,converter));
        break;
      }

      case RECORDERTEST_LL:
      {// now two learners
        Learner learnerA = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
              @SuppressWarnings("unused") List<Boolean> acceptedElements,
              @SuppressWarnings("unused") PairScore pairBeingMerged,
              @SuppressWarnings("unused"final Object [] moreOptions)
          {
            return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
          }
        };
        Learner learnerB = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

    // now sanity checking on the plus and minus sets
    for(String [] path:plus)
      assert AbstractOracle.USER_ACCEPTED == expected.paths.tracePathPrefixClosed(AbstractLearnerGraph.buildList(Arrays.asList(path),testConfig));
    for(String [] path:minus)
      assert AbstractOracle.USER_ACCEPTED != expected.paths.tracePathPrefixClosed(AbstractLearnerGraph.buildList(Arrays.asList(path),testConfig));
    Learner l = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
          @SuppressWarnings("unused") List<Boolean> acceptedElements,
          @SuppressWarnings("unused") PairScore pairBeingMerged,
          @SuppressWarnings("unused"final Object [] moreOptions)
      {
        return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
      }
    };
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);
    ByteArrayOutputStream logStream = new ByteArrayOutputStream();
    RecordProgressDecorator recorder = new RecordProgressDecorator(l,logStream,1,testConfig,useZip);
    Collection<List<Label>> testSet = new LinkedList<List<Label>>();
    recorder.writeLearnerEvaluationData(new LearnerEvaluationConfiguration(expected, testSet, testConfig, null, null));
    LearnerGraph learntStructureA = recorder.learnMachine(buildSet(plus,testConfig), buildSet(minus,testConfig));
   
    //System.out.println("compression rate: "+recorder.getCompressionRate());
    //System.out.println(logStream.toString()+"============");
    //System.out.println(logStream.toByteArray().length);
    LearnerGraph learntMachineNoRejects = new LearnerGraph(testConfig);
    AbstractPathRoutines.removeRejectStates(learntStructureA,learntMachineNoRejects);
    Assert.assertNull(WMethod.checkM(learntMachineNoRejects, expected));
   
    switch(kind)
    {
      case RECORDERTEST_SS:
      {// matching two simulators
        final LearnerSimulator
          simulator = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip),
          simulator2 = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip);
       
        LearnerEvaluationConfiguration eval1 = simulator.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval1.graph));
        Assert.assertEquals(testSet, eval1.testSet);
        Assert.assertEquals(expected.config, testConfig);
        LearnerEvaluationConfiguration eval2 = simulator2.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval2.graph));
        Assert.assertEquals(testSet, eval2.testSet);
        Assert.assertEquals(expected.config, testConfig);
       
        new Test_LearnerComparator(simulator,simulator2,true).learnMachine(buildSet(plus,testConfig), buildSet(minus,testConfig));
        break;
      }
     
      case RECORDERTEST_SL:
      {// now a simulator to a learner
        final LearnerSimulator simulator = new LearnerSimulator(new ByteArrayInputStream(logStream.toByteArray()),useZip);
        LearnerEvaluationConfiguration eval1 = simulator.readLearnerConstructionData(testConfig);
        Assert.assertNull(WMethod.checkM(expected, eval1.graph));
        Assert.assertEquals(testSet, eval1.testSet);
        Assert.assertEquals(expected.config, testConfig);
 
        Learner learner2 = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,expected.config,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
              @SuppressWarnings("unused") List<Boolean> acceptedElements,
              @SuppressWarnings("unused") PairScore pairBeingMerged,
              @SuppressWarnings("unused"final Object [] moreOptions)
          {
            return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
          }
        };
        new Test_LearnerComparator(learner2,simulator,true).learnMachine(buildSet(plus,testConfig), buildSet(minus,testConfig));
        break;
      }

      case RECORDERTEST_LL:
      {// now two learners
        Learner learnerA = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
              @SuppressWarnings("unused") List<Boolean> acceptedElements,
              @SuppressWarnings("unused") PairScore pairBeingMerged,
              @SuppressWarnings("unused"final Object [] moreOptions)
          {
            return new Pair<Integer,String>(expected.paths.tracePathPrefixClosed(question),null);
          }
        };
        Learner learnerB = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
        {
          @Override
          public Pair<Integer,String> CheckWithEndUser(
              @SuppressWarnings("unused"LearnerGraph model,
              List<Label> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

    //"B-e->A\nC-e->A\nD-e->A\nE-e->A\nF-e->A\nG-e->A\n","testAutoAnswers0"),Configuration.getDefaultConfiguration()),null);
    Configuration testConfig = Configuration.getDefaultConfiguration().copy();
    testConfig.setGdFailOnDuplicateNames(false);
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);

    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          List<String> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

    final AutoAnswers semiUser = new AutoAnswers(null);
    semiUser.loadAnswers(new StringReader(
        partA
    ));
   
    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          List<String> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

  {
    Configuration testConfig = Configuration.getDefaultConfiguration().copy();
    testConfig.setGdFailOnDuplicateNames(false);
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);

    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          @SuppressWarnings("unused"List<String> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

  {
    Configuration testConfig = Configuration.getDefaultConfiguration().copy();
    testConfig.setGdFailOnDuplicateNames(false);
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);

    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          @SuppressWarnings("unused"List<String> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

  {
    Configuration testConfig = Configuration.getDefaultConfiguration().copy();
    testConfig.setGdFailOnDuplicateNames(false);
    testConfig.setLearnerIdMode(IDMode.POSITIVE_NEGATIVE);

    RPNILearner learner = new RPNIUniversalLearner(null,new LearnerEvaluationConfiguration(null,null,testConfig,null,null))
    {
      @Override
      public Pair<Integer,String> CheckWithEndUser(
          @SuppressWarnings("unused"LearnerGraph model,
          @SuppressWarnings("unused"List<String> question, @SuppressWarnings("unused") int responseForNoRestart,
View Full Code Here

                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            final AtomicLong counter = new AtomicLong();
                            RPNIUniversalLearner learner = new RPNIUniversalLearner(null, learnerInitConfiguration) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                // send the notification if necessary
                                if (message.arity() > 2 && message.elementAt(2) instanceof OtpErlangPid)
                                  sendProgress((OtpErlangPid)message.elementAt(2), ref, graph, null, counter);
                               
                                // check if we were asked to terminate
                                try {
                                  OtpErlangObject messageReceived = mbox.receive(0);// do not wait, return null if anything received
                                  if (messageReceived != null && messageReceived instanceof OtpErlangTuple && ((OtpErlangTuple)messageReceived).arity() == 2)
                                  {
                                    OtpErlangTuple cmd = ((OtpErlangTuple)messageReceived);
                                    if (cmd.elementAt(0).equals(ref) && cmd.elementAt(1).equals(msgStop))
                                      throw new AskedToTerminateException();
                                  }
                                } catch (OtpErlangExit e) {
                                  Helper.throwUnchecked("node exited", e);
                                } catch (OtpErlangDecodeException e) {
                                  Helper.throwUnchecked("decode exception", e);
                                }
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }

                              @Override
                              public Pair<Integer, String> CheckWithEndUser(
                                  LearnerGraph model,
                                  List<Label> question,
                                  int expectedForNoRestart,
                                  List<Boolean> consistentFacts,
                                  PairScore pairBeingMerged,
                                  Object[] moreOptions) {

                                return super.CheckWithEndUser(model, question, expectedForNoRestart,
                                    consistentFacts, pairBeingMerged, moreOptions);
                              }
                             
                            };
                            learner.init(sPlus, sMinus);
                            if (learnerInitConfiguration.graph != null)
                            {
                              learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                              LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                            }
                            LearnerGraph graphLearnt = learner.learnMachine();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                          }
                          catch(AskedToTerminateException e)
                          {
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                         
                        }
                        else
                        // Args: Ref,addTypeInformation,list of pairs containing method names and types.
                        if (command.equals(msgAddTypeInformation) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            updateFrom((OtpErlangList)message.elementAt(2), overrides);
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  typeMapToList(overrides)});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,purgeModuleInformation
                        // Since using addTypeInformation followed by learnErlang causes changes to the alphabet modules we are dealing with, independence of tests requires the collection of loaded modules to be purged. This is the purpose of this function.
                        if (command.equals(msgPurgeModuleInformation) && message.arity() >= 2)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.flushRegistry();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,extractTypeInformation,moduleFileFullPath.
                        // Returns a list of pairs of method names and types.
                        if (command.equals(msgExtractTypeInformation) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.setupErlangConfiguration(learnerInitConfiguration.config, new File(((OtpErlangAtom)message.elementAt(2)).atomValue()));
                           
                            // we start a separate Erlang node to run the questions
                            ErlangRunner erlangRunner = ErlangRuntime.getDefaultRuntime().createNewRunner();
                            learnerInitConfiguration.config.setErlangMboxName(erlangRunner.getRunnerName());
                            final ErlangModule mod = ErlangModule.loadModule(learnerInitConfiguration.config);
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  typeMapToList(mod.sigTypes)});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,learnErlang,moduleFileFullPath, pid
                        // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                        // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                        // Response: Ref,ok,fsm
                        // on error: Ref,failure,text_of_the_error (as string)
                        if (command.equals(msgLearnErlang) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.setupErlangConfiguration(learnerInitConfiguration.config, new File(((OtpErlangAtom)message.elementAt(2)).atomValue()));
                           
                            // we start a separate Erlang node to run the questions
                            ErlangRunner erlangRunner = ErlangRuntime.getDefaultRuntime().createNewRunner();
                            learnerInitConfiguration.config.setErlangMboxName(erlangRunner.getRunnerName());
                            final AtomicLong counter = new AtomicLong();
                            final ErlangModule mod = ErlangModule.loadModule(learnerInitConfiguration.config);
                            mod.rebuildSigs(learnerInitConfiguration.config, overrides);mod.behaviour.generateAlphabet(learnerInitConfiguration.config);
                           
                            ErlangOracleLearner learner = new ErlangOracleLearner(null, learnerInitConfiguration) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                // check if we were asked to terminate
                                try {
                                  if (message.arity() > 3 && message.elementAt(3) instanceof OtpErlangPid)
                                    // send the notification if necessary
                                    sendProgress((OtpErlangPid)message.elementAt(3), ref, graph, mod, counter);

                                  OtpErlangObject messageReceived = mbox.receive(0);// do not wait, return null if anything received
                                  if (messageReceived != null && messageReceived instanceof OtpErlangTuple && ((OtpErlangTuple)messageReceived).arity() == 2)
                                  {
                                    OtpErlangTuple cmd = ((OtpErlangTuple)messageReceived);
                                    if (cmd.elementAt(0).equals(ref) && cmd.elementAt(1).equals(msgStop))
                                      throw new AskedToTerminateException();
                                  }
                                } catch (OtpErlangExit e) {
                                  Helper.throwUnchecked("node exited", e);
                                } catch (OtpErlangDecodeException e) {
                                  Helper.throwUnchecked("decode exception", e);
                                }
                               
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }

                              @Override
                              public Pair<Integer, String> CheckWithEndUser(
                                  LearnerGraph model,
                                  List<Label> question,
                                  int expectedForNoRestart,
                                  List<Boolean> consistentFacts,
                                  PairScore pairBeingMerged,
                                  Object[] moreOptions) {
                                return super.CheckWithEndUser(model, question, expectedForNoRestart,consistentFacts, pairBeingMerged, moreOptions);
                              }
                             
                            };
                            if (learnerInitConfiguration.config.getAskQuestions()) // only generate initial traces if we are permited to ask questions.
                              learner.init(learner.GenerateInitialTraces(learnerInitConfiguration.config.getErlangInitialTraceLength()),0,0);
                            System.out.println("random trace generation complete");
                            LearnerGraph graphLearnt = learner.learnMachine(),
                                graphWithTrimmedLabels = new LearnerGraph(learnerInitConfiguration.config);
                           
                            if (learnerInitConfiguration.config.getErlangStripModuleNamesFromFunctionsInNonGenModules())
                              convertLabelsToStrings(graphLearnt,graphWithTrimmedLabels);
                            else
View Full Code Here

                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            final AtomicLong counter = new AtomicLong();
                            RPNIUniversalLearner learner = new RPNIUniversalLearner(null, learnerInitConfiguration) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                reportLearningProgress(graph,message,ref,counter);
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }
                            };
                            learner.init(sPlus, sMinus);
                            if (learnerInitConfiguration.graph != null)
                            {
                              learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                              LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                            }
                            LearnerGraph graphLearnt = learner.learnMachine();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                          }
                          catch(AskedToTerminateException e)
                          {
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                         
                        }
                        else
                          // Args: Ref,learn, pid
                          // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                          // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                          // Response: Ref,ok,fsm
                          // on error: Ref,failure,text_of_the_error (as string)
                          if (command.equals(msgLearnEDSMMARKOV) && message.arity() >= 2)
                          {
                            OtpErlangObject outcome = null;
                            try
                            {
                              final AtomicLong counter = new AtomicLong();
                              learnerInitConfiguration.config.setLearnerScoreMode(ScoreMode.ONLYOVERRIDE);
                              LearnerGraph pta=new LearnerGraph(learnerInitConfiguration.config);
                              for(List<Label> seq:sPlus)
                                pta.paths.augmentPTA(seq,true,false,null);
                              for(List<Label> seq:sMinus)
                                pta.paths.augmentPTA(seq,false,false,null);
                              final MarkovModel m= new MarkovModel(3,true,true,false);

                              new MarkovClassifier(m, pta).updateMarkov(false);// construct Markov chain if asked for.
                              final ConsistencyChecker checker = new MarkovClassifier.DifferentPredictionsInconsistencyNoBlacklistingIncludeMissingPrefixes();
                           
                              pta.clearColours();
                              EDSM_MarkovLearner learner = new EDSM_MarkovLearner(learnerInitConfiguration,pta,0) {

                                @Override
                                public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                                {
                                  reportLearningProgress(graph,message,ref,counter);
                                  // resume learning.
                                  return super.ChooseStatePairs(graph);
                                }
                              };
                              learner.setMarkov(m);learner.setChecker(checker);
                              learner.setUseNewScoreNearRoot(false);learner.setUseClassifyPairs(false);
                              learner.setDisableInconsistenciesInMergers(false);
                             
                              if (learnerInitConfiguration.graph != null)
                              {
                                learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                                LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                              }
                              LearnerGraph graphLearnt = learner.learnMachine(new LinkedList<List<Label>>(),new LinkedList<List<Label>>());
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                            }
                            catch(AskedToTerminateException e)
                            {
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                            }
                            catch(Throwable ex)
                            {
                              ex.printStackTrace();
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                            }
                            mbox.send(erlangPartner,outcome);
                           
                          }
                        else
                          // Args: Ref,learn, pid
                          // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                          // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                          // Response: Ref,ok,fsm
                          // on error: Ref,failure,text_of_the_error (as string)
                          if (command.equals(msgLearnEDSMMARKOVcentre) && message.arity() >= 2)
                          {
                            OtpErlangObject outcome = null;
                            try
                            {
                              final AtomicLong counter = new AtomicLong();
                              learnerInitConfiguration.config.setLearnerScoreMode(ScoreMode.ONLYOVERRIDE);
                              LearnerGraph ptaInitial=new LearnerGraph(learnerInitConfiguration.config);
                              for(List<Label> seq:sPlus)
                                ptaInitial.paths.augmentPTA(seq,true,false,null);
                              for(List<Label> seq:sMinus)
                                ptaInitial.paths.augmentPTA(seq,false,false,null);
                              final MarkovModel m= new MarkovModel(3,true,true,false);

                              final MarkovClassifier ptaClassifier = new MarkovClassifier(m, ptaInitial);ptaClassifier.updateMarkov(false);
                              LearnerGraph ptaToUseForInference = ptaInitial;
                              final ConsistencyChecker checker = new MarkovClassifier.DifferentPredictionsInconsistencyNoBlacklistingIncludeMissingPrefixes();
                              {
                                Collection<Set<CmpVertex>> verticesToMergeBasedOnInitialPTA=null;
                                final List<List<Label>> pathsToMerge=ptaClassifier.identifyPathsToMerge(checker);
                                // These vertices are merged first and then the learning start from the root as normal.
                                // The reason to learn from the root is a memory cost. if we learn from the middle, we can get a better results
                                verticesToMergeBasedOnInitialPTA=ptaClassifier.buildVerticesToMergeForPaths(pathsToMerge);
                               
                                List<StatePair> pairsListInitialMerge = ptaClassifier.buildVerticesToMergeForPath(pathsToMerge);
                                LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>> verticesToMergeInitialMerge = new LinkedList<AMEquivalenceClass<CmpVertex,LearnerGraphCachedData>>();
                                int scoreInitialMerge = ptaInitial.pairscores.computePairCompatibilityScore_general(null, pairsListInitialMerge, verticesToMergeInitialMerge);
                                assert scoreInitialMerge >= 0;
                                ptaToUseForInference = MergeStates.mergeCollectionOfVertices(ptaInitial, null, verticesToMergeInitialMerge);
                                final CmpVertex vertexWithMostTransitions = MarkovPassivePairSelection.findVertexWithMostTransitions(ptaToUseForInference,MarkovClassifier.computeInverseGraph(ptaInitial));
                                ptaToUseForInference.clearColours();ptaToUseForInference.getInit().setColour(null);vertexWithMostTransitions.setColour(JUConstants.RED);
                                LearnerGraphND inverseOfPtaAfterInitialMerge = MarkovClassifier.computeInverseGraph(ptaToUseForInference);
                                System.out.println("Centre vertex: "+vertexWithMostTransitions+" number of transitions: "+MarkovPassivePairSelection.countTransitions(ptaToUseForInference, inverseOfPtaAfterInitialMerge, vertexWithMostTransitions));
                               
                              }
                              ptaToUseForInference.clearColours();
                              EDSM_MarkovLearner learner = new EDSM_MarkovLearner(learnerInitConfiguration,ptaToUseForInference,0) {

                                @Override
                                public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                                {
                                  reportLearningProgress(graph,message,ref,counter);
                                  // resume learning.
                                  return super.ChooseStatePairs(graph);
                                }
                              };
                              learner.setMarkov(m);learner.setChecker(checker);
                              learner.setUseNewScoreNearRoot(false);learner.setUseClassifyPairs(false);
                              learner.setDisableInconsistenciesInMergers(false);
                             
                              if (learnerInitConfiguration.graph != null)
                              {
                                learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                                LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                              }
                              LearnerGraph graphLearnt = learner.learnMachine(new LinkedList<List<Label>>(),new LinkedList<List<Label>>());
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                            }
                            catch(AskedToTerminateException e)
                            {
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                            }
                            catch(Throwable ex)
                            {
                              ex.printStackTrace();
                              outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                            }
                            mbox.send(erlangPartner,outcome);
                           
                          }
                        else
                        // Args: Ref,
                        // Arguments: ref,compareWithOthers,fsmA,fsmB
                        // Response: ref,ok,results of comparison
                        // on error, ref,failure,text_of_the_error (as string)
                        if (command.equals(msgCompareWithOthers) && message.arity() == 4)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            LearnerGraph grA = new LearnerGraph(learnerInitConfiguration.config), grB = new LearnerGraph(learnerInitConfiguration.config);
                            Synapse.StatechumProcess.parseStatemachine(message.elementAt(2),grA,null,true);
                            Synapse.StatechumProcess.parseStatemachine(message.elementAt(3),grB,null,true);
                            OtpErlangObject difference  = computeBCR_and_structuralDifference(grA, grB, learnerInitConfiguration.config);
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,difference});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,learn, pid
                        // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                        // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                        // Response: Ref,ok,fsm
                        // on error: Ref,failure,text_of_the_error (as string)
                        if (command.equals(msgLearnSicco) && message.arity() >= 2)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            final AtomicLong counter = new AtomicLong();
                            learnerInitConfiguration.config.setLearnerScoreMode(ScoreMode.ONLYOVERRIDE);
                            LearnerGraph pta=new LearnerGraph(learnerInitConfiguration.config);
                            for(List<Label> seq:sPlus)
                              pta.paths.augmentPTA(seq,true,false,null);
                            for(List<Label> seq:sMinus)
                              pta.paths.augmentPTA(seq,false,false,null);
                            pta.clearColours();
                            ReferenceLearner learner = new ReferenceLearner(learnerInitConfiguration,null,pta,false) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                reportLearningProgress(graph,message,ref,counter);
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }

                              @Override
                              public Pair<Integer, String> CheckWithEndUser(
                                  LearnerGraph model,
                                  List<Label> question,
                                  int expectedForNoRestart,
                                  List<Boolean> consistentFacts,
                                  PairScore pairBeingMerged,
                                  Object[] moreOptions) {

                                return super.CheckWithEndUser(model, question, expectedForNoRestart,
                                    consistentFacts, pairBeingMerged, moreOptions);
                              }
                             
                            };

                            if (learnerInitConfiguration.graph != null)
                            {
                              learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                              LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                            }
                            LearnerGraph graphLearnt = learner.learnMachine(new LinkedList<List<Label>>(),new LinkedList<List<Label>>());
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                          }
                          catch(AskedToTerminateException e)
                          {
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                         
                        }
                        else
                        // Args: Ref,learn, pid
                        // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                        // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                        // Response: Ref,ok,fsm
                        // on error: Ref,failure,text_of_the_error (as string)
                        if (command.equals(msgLearnKTails) && message.arity() >= 2)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            final AtomicLong counter = new AtomicLong();
                            learnerInitConfiguration.config.setLearnerScoreMode(ScoreMode.ONLYOVERRIDE);
                            LearnerGraph pta=new LearnerGraph(learnerInitConfiguration.config);
                            for(List<Label> seq:sPlus)
                              pta.paths.augmentPTA(seq,true,false,null);
                            for(List<Label> seq:sMinus)
                              pta.paths.augmentPTA(seq,false,false,null);
                            pta.clearColours();
                            KTailsReferenceLearner learner = new KTailsReferenceLearner(learnerInitConfiguration,pta,true,1) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                reportLearningProgress(graph,message,ref,counter);
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }

                            };

                            if (learnerInitConfiguration.graph != null)
                            {
                              learnerInitConfiguration.graph.clearColours();learnerInitConfiguration.graph.getInit().setColour(JUConstants.RED);
                              LearnerGraph.copyGraphs(learnerInitConfiguration.graph,learner.getTentativeAutomaton());
                            }
                            LearnerGraph graphLearnt = learner.learnMachine(new LinkedList<List<Label>>(),new LinkedList<List<Label>>());
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  constructFSM(graphLearnt)});
                          }
                          catch(AskedToTerminateException e)
                          {
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgTerminate});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                         
                        }
                        else
                        // Args: Ref,addTypeInformation,list of pairs containing method names and types.
                        if (command.equals(msgAddTypeInformation) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            updateFrom((OtpErlangList)message.elementAt(2), overrides);
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  typeMapToList(overrides)});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,purgeModuleInformation
                        // Since using addTypeInformation followed by learnErlang causes changes to the alphabet modules we are dealing with, independence of tests requires the collection of loaded modules to be purged. This is the purpose of this function.
                        if (command.equals(msgPurgeModuleInformation) && message.arity() >= 2)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.flushRegistry();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,extractTypeInformation,moduleFileFullPath.
                        // Returns a list of pairs of method names and types.
                        if (command.equals(msgExtractTypeInformation) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.setupErlangConfiguration(learnerInitConfiguration.config, new File(((OtpErlangAtom)message.elementAt(2)).atomValue()));
                           
                            // we start a separate Erlang node to run the questions
                            ErlangRunner erlangRunner = ErlangRuntime.getDefaultRuntime().createNewRunner();
                            learnerInitConfiguration.config.setErlangMboxName(erlangRunner.getRunnerName());
                            final ErlangModule mod = ErlangModule.loadModule(learnerInitConfiguration.config);
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgOk,  typeMapToList(mod.sigTypes)});
                          }
                          catch(Throwable ex)
                          {
                            ex.printStackTrace();
                            outcome = new OtpErlangTuple(new OtpErlangObject[]{ref,msgFailure,new OtpErlangList(ex.getMessage())});
                          }
                          mbox.send(erlangPartner,outcome);
                        }
                        else
                        // Args: Ref,learnErlang,moduleFileFullPath, pid
                        // pid is optional, where provided, progress messages are reported in a form of {Ref,'status',step}
                        // in the course of learning, the learner is receptive to messages directed at its normal PID, a {Ref,terminate} command will kill it and the response will be {Ref,terminate}.
                        // Response: Ref,ok,fsm
                        // on error: Ref,failure,text_of_the_error (as string)
                        if (command.equals(msgLearnErlang) && message.arity() >= 3)
                        {
                          OtpErlangObject outcome = null;
                          try
                          {
                            ErlangModule.setupErlangConfiguration(learnerInitConfiguration.config, new File(((OtpErlangAtom)message.elementAt(2)).atomValue()));
                           
                            // we start a separate Erlang node to run the questions
                            ErlangRunner erlangRunner = ErlangRuntime.getDefaultRuntime().createNewRunner();
                            learnerInitConfiguration.config.setErlangMboxName(erlangRunner.getRunnerName());
                            final AtomicLong counter = new AtomicLong();
                            final ErlangModule mod = ErlangModule.loadModule(learnerInitConfiguration.config);
                            mod.rebuildSigs(learnerInitConfiguration.config, overrides);mod.behaviour.generateAlphabet(learnerInitConfiguration.config);
                           
                            ErlangOracleLearner learner = new ErlangOracleLearner(null, learnerInitConfiguration) {

                              @Override
                              public Stack<PairScore> ChooseStatePairs(LearnerGraph graph)
                              {
                                // check if we were asked to terminate
                                try {
                                  if (message.arity() > 3 && message.elementAt(3) instanceof OtpErlangPid)
                                    // send the notification if necessary
                                    sendProgress((OtpErlangPid)message.elementAt(3), ref, graph, mod, counter);

                                  OtpErlangObject messageReceived = mbox.receive(0);// do not wait, return null if anything received
                                  if (messageReceived != null && messageReceived instanceof OtpErlangTuple && ((OtpErlangTuple)messageReceived).arity() == 2)
                                  {
                                    OtpErlangTuple cmd = ((OtpErlangTuple)messageReceived);
                                    if (cmd.elementAt(0).equals(ref) && cmd.elementAt(1).equals(msgStop))
                                      throw new AskedToTerminateException();
                                  }
                                } catch (OtpErlangExit e) {
                                  Helper.throwUnchecked("node exited", e);
                                } catch (OtpErlangDecodeException e) {
                                  Helper.throwUnchecked("decode exception", e);
                                }
                               
                                // resume learning.
                                return super.ChooseStatePairs(graph);
                              }

                              @Override
                              public Pair<Integer, String> CheckWithEndUser(
                                  LearnerGraph model,
                                  List<Label> question,
                                  int expectedForNoRestart,
                                  List<Boolean> consistentFacts,
                                  PairScore pairBeingMerged,
                                  Object[] moreOptions) {
                                return super.CheckWithEndUser(model, question, expectedForNoRestart,consistentFacts, pairBeingMerged, moreOptions);
                              }
                             
                            };
                            if (learnerInitConfiguration.config.getAskQuestions()) // only generate initial traces if we are permited to ask questions.
                              learner.init(learner.GenerateInitialTraces(learnerInitConfiguration.config.getErlangInitialTraceLength()),0,0);
                            System.out.println("random trace generation complete");
                            LearnerGraph graphLearnt = learner.learnMachine(),
                                graphWithTrimmedLabels = new LearnerGraph(learnerInitConfiguration.config);
                           
                            if (learnerInitConfiguration.config.getErlangStripModuleNamesFromFunctionsInNonGenModules())
                              convertLabelsToStrings(graphLearnt,graphWithTrimmedLabels);
                            else
View Full Code Here

TOP

Related Classes of statechum.analysis.learning.RPNIUniversalLearner

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.