Skip to content

How to print the intermediate observation table (especially when the create the hypothesis) #115

@VishalPatel43

Description

@VishalPatel43

import com.google.common.collect.Lists;
import de.learnlib.algorithms.lstar.closing.ClosingStrategies;
import de.learnlib.algorithms.lstar.dfa.ClassicLStarDFA;
import de.learnlib.algorithms.lstar.dfa.ClassicLStarDFABuilder;
import de.learnlib.algorithms.malerpnueli.MalerPnueliDFA;
import de.learnlib.algorithms.rivestschapire.RivestSchapireDFA;
import de.learnlib.algorithms.rivestschapire.RivestSchapireDFABuilder;
import de.learnlib.api.oracle.MembershipOracle;
import de.learnlib.datastructure.observationtable.writer.ObservationTableASCIIWriter;
import de.learnlib.filter.statistic.oracle.DFACounterOracle;
import de.learnlib.oracle.equivalence.DFAWMethodEQOracle;
import de.learnlib.oracle.equivalence.SampleSetEQOracle;
import de.learnlib.oracle.membership.SimulatorOracle;
import de.learnlib.util.Experiment;
import dk.brics.automaton.Automaton;
import dk.brics.automaton.RegExp;
import net.automatalib.automata.fsa.DFA;
import net.automatalib.brics.BricsDFA;
import net.automatalib.util.automata.conformance.WMethodTestsIterator;
import net.automatalib.visualization.Visualization;
import net.automatalib.words.Alphabet;
import net.automatalib.words.impl.Alphabets;

public class Driver {

    private static final int EXPLORATION_DEPTH = 5;

    public static void main(String[] args) {
        String regex = "a|b|.{3,}";

        final Automaton automaton = new RegExp(regex).toAutomaton();
        final Alphabet<Character> alphabet = Alphabets.characters('a', 'b');
        final BricsDFA dfa = new BricsDFA(automaton, true);

        MembershipOracle.DFAMembershipOracle<Character> sul = new SimulatorOracle.DFASimulatorOracle<>(dfa);

        lstarAlgorithm(dfa, alphabet, sul);
        System.out.println("-------------------------------------------------------");
        rsAlgorithm(dfa, alphabet, sul);
        System.out.println("-------------------------------------------------------");
        colAlgorithm(dfa, alphabet, sul);
    }

    private static void lstarAlgorithm(BricsDFA dfa, Alphabet<Character> alphabet, MembershipOracle.DFAMembershipOracle<Character> sul) {
        DFACounterOracle<Character> lstarCounter = new DFACounterOracle<>(sul, "Membership Queries");
        ClassicLStarDFA<Character> lstar = new ClassicLStarDFABuilder<Character>().withAlphabet(alphabet)
                .withOracle(lstarCounter)
                .create();

        final SampleSetEQOracle<Character, Boolean> eqo = new SampleSetEQOracle<>(false);
        eqo.addAll(sul, Lists.newArrayList(new WMethodTestsIterator<>(dfa, alphabet, EXPLORATION_DEPTH)));
        Experiment.DFAExperiment<Character> lstarExperiment = new Experiment.DFAExperiment<>(lstar, eqo, alphabet);

        System.out.println("Initial observation table of LSTAR:");
        new ObservationTableASCIIWriter<>().write(lstar.getObservationTable(), System.out);
        System.out.println();

        long startTime = System.currentTimeMillis();
        lstarExperiment.run();
        long endTime = System.currentTimeMillis();

        printResults("LStar", lstarExperiment, lstarCounter, endTime - startTime);
        System.out.println("Final observation table of LSTAR:");
        new ObservationTableASCIIWriter<>().write(lstar.getObservationTable(), System.out);
    }

    private static void rsAlgorithm(BricsDFA dfa, Alphabet<Character> alphabet, MembershipOracle.DFAMembershipOracle<Character> sul) {
        DFACounterOracle<Character> rsCounter = new DFACounterOracle<>(sul, "Membership Queries");
        RivestSchapireDFA<Character> rs = new RivestSchapireDFABuilder<Character>()
                .withAlphabet(alphabet)
                .withOracle(rsCounter)
                .withClosingStrategy(ClosingStrategies.CLOSE_FIRST)
                .create();

        final SampleSetEQOracle<Character, Boolean> eqo = new SampleSetEQOracle<>(false);
        eqo.addAll(sul, Lists.newArrayList(new WMethodTestsIterator<>(dfa, alphabet, EXPLORATION_DEPTH)));
        Experiment.DFAExperiment<Character> rsExperiment = new Experiment.DFAExperiment<>(rs, eqo, alphabet);

        System.out.println("Initial observation table of L_RS:");
        new ObservationTableASCIIWriter<>().write(rs.getObservationTable(), System.out);
        System.out.println();

        long startTime = System.currentTimeMillis();
        rsExperiment.run();
        long endTime = System.currentTimeMillis();

        printResults("L_RS", rsExperiment, rsCounter, endTime - startTime);
        System.out.println("Final observation table of L_RS:");
        new ObservationTableASCIIWriter<>().write(rs.getObservationTable(), System.out);
    }

    private static void colAlgorithm(BricsDFA dfa, Alphabet<Character> alphabet, MembershipOracle.DFAMembershipOracle<Character> sul) {
        DFACounterOracle<Character> colCounter = new DFACounterOracle<>(sul, "Membership Queries");
        MalerPnueliDFA<Character> col = new MalerPnueliDFA<>(alphabet, colCounter);

        final SampleSetEQOracle<Character, Boolean> eqo = new SampleSetEQOracle<>(false);
        eqo.addAll(sul, Lists.newArrayList(new WMethodTestsIterator<>(dfa, alphabet, EXPLORATION_DEPTH)));
        Experiment.DFAExperiment<Character> colExperiment = new Experiment.DFAExperiment<>(col, eqo, alphabet);

        System.out.println("Initial observation table of L_COL:");
        new ObservationTableASCIIWriter<>().write(col.getObservationTable(), System.out);
        System.out.println();

        long startTime = System.currentTimeMillis();
        colExperiment.run();
        long endTime = System.currentTimeMillis();

        printResults("L_Col", colExperiment, colCounter, endTime - startTime);
        System.out.println("Final observation table of L_COL:");
        new ObservationTableASCIIWriter<>().write(col.getObservationTable(), System.out);
    }

    private static void printResults(String algorithmName, Experiment.DFAExperiment<Character> experiment,
                                     DFACounterOracle<Character> counterOracle, long timeTaken) {
        System.out.println("----------------" + algorithmName + "-------------");
        System.out.println("EQ: " + experiment.getRounds().getCount());
        System.out.println("MQ: " + counterOracle.getCount());
        System.out.println("Time: " + timeTaken);
    }
}

"I provide the code, I want to print the intermediate observation table when hypothesis created or when the each membership queries ask to the teacher (SUL)"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions