From a5661e4b440820de00a71fffbc9aed66fb283d90 Mon Sep 17 00:00:00 2001 From: Markus Frohme Date: Wed, 2 Oct 2024 23:21:06 +0200 Subject: [PATCH] oml: add tests for CE management --- algorithms/active/oml/pom.xml | 31 ++++++- .../oml/AbstractCounterexampleQueueTest.java | 92 +++++++++++++++++++ ...ptimalLStarDFACounterexampleQueueTest.java | 30 ++++++ .../OptimalTTTDFACounterexampleQueueTest.java | 29 ++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/AbstractCounterexampleQueueTest.java create mode 100644 algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/lstar/dfa/OptimalLStarDFACounterexampleQueueTest.java create mode 100644 algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/ttt/dfa/OptimalTTTDFACounterexampleQueueTest.java diff --git a/algorithms/active/oml/pom.xml b/algorithms/active/oml/pom.xml index 59054262d0..35f0c9f26d 100644 --- a/algorithms/active/oml/pom.xml +++ b/algorithms/active/oml/pom.xml @@ -65,6 +65,16 @@ limitations under the License. + + de.learnlib + learnlib-equivalence-oracles + test + + + de.learnlib + learnlib-membership-oracles + test + de.learnlib.testsupport learnlib-learner-it-support @@ -83,11 +93,30 @@ limitations under the License. automata-serialization-dot test + + net.automatalib + automata-util + test + org.testng testng - + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + @{argLine} --add-reads=de.learnlib.algorithm.oml=net.automatalib.util + + + + + diff --git a/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/AbstractCounterexampleQueueTest.java b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/AbstractCounterexampleQueueTest.java new file mode 100644 index 0000000000..d47bb31d14 --- /dev/null +++ b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/AbstractCounterexampleQueueTest.java @@ -0,0 +1,92 @@ +/* Copyright (C) 2013-2024 TU Dortmund University + * This file is part of LearnLib, http://www.learnlib.de/. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.learnlib.algorithm.oml; + +import de.learnlib.algorithm.LearningAlgorithm.DFALearner; +import de.learnlib.oracle.MembershipOracle.DFAMembershipOracle; +import de.learnlib.oracle.equivalence.SampleSetEQOracle; +import de.learnlib.oracle.membership.DFASimulatorOracle; +import de.learnlib.util.Experiment.DFAExperiment; +import net.automatalib.alphabet.Alphabet; +import net.automatalib.alphabet.impl.Alphabets; +import net.automatalib.automaton.fsa.DFA; +import net.automatalib.automaton.fsa.impl.CompactDFA; +import net.automatalib.util.automaton.Automata; +import net.automatalib.util.automaton.builder.AutomatonBuilders; +import net.automatalib.word.Word; +import net.automatalib.word.WordBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * Test cases for testing counterexample management. + */ +public abstract class AbstractCounterexampleQueueTest { + + private static final CompactDFA DFA; + + static { + final Alphabet alphabet = Alphabets.characters('a', 'b'); + // @formatter:off + DFA = AutomatonBuilders.newDFA(alphabet) + .withInitial("q0") + .from("q0") + .on('a').to("q1") + .on('b').to("q1'") + .from("q1") + .on('a').to("q2") + .on('b').loop() + .from("q1'") + .on('a').loop() + .on('b').to("q2'") + .from("q2") + .on('a').to("q3") + .on('b').loop() + .from("q2'") + .on('a', 'b').loop() + .from("q3") + .on('a').to("q0") + .on('b').loop() + .withAccepting("q0", "q2'") + .create(); + //@formatter:on + } + + /** + * Simulates a learning setup in which intermediate counterexamples are discarded (popped from the stack) and the + * main counterexample needs to be re-evaluated twice. + */ + @Test + public void testPop() { + final Alphabet alphabet = DFA.getInputAlphabet(); + final DFAMembershipOracle mqOracle = new DFASimulatorOracle<>(DFA); + final DFALearner learner = getLearner(alphabet, mqOracle); + + final SampleSetEQOracle eqOracle = new SampleSetEQOracle<>(false); + final Word a = Word.fromLetter('a'); + final Word b = new WordBuilder<>('b', 9).toWord(); + eqOracle.addAll(mqOracle, Word.fromWords(b, a, b, a, b, a, b, a)); + + final DFAExperiment experiment = new DFAExperiment<>(learner, eqOracle, alphabet); + experiment.run(); + + final DFA result = experiment.getFinalHypothesis(); + + Assert.assertTrue(Automata.testEquivalence(DFA, result, alphabet)); + } + + protected abstract DFALearner getLearner(Alphabet alphabet, DFAMembershipOracle oracle); +} diff --git a/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/lstar/dfa/OptimalLStarDFACounterexampleQueueTest.java b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/lstar/dfa/OptimalLStarDFACounterexampleQueueTest.java new file mode 100644 index 0000000000..3144c95fd1 --- /dev/null +++ b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/lstar/dfa/OptimalLStarDFACounterexampleQueueTest.java @@ -0,0 +1,30 @@ +/* Copyright (C) 2013-2024 TU Dortmund University + * This file is part of LearnLib, http://www.learnlib.de/. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.learnlib.algorithm.oml.lstar.dfa; + +import de.learnlib.algorithm.LearningAlgorithm.DFALearner; +import de.learnlib.algorithm.oml.AbstractCounterexampleQueueTest; +import de.learnlib.algorithm.oml.lstar.OptimalLStarDFA; +import de.learnlib.oracle.MembershipOracle.DFAMembershipOracle; +import net.automatalib.alphabet.Alphabet; + +public class OptimalLStarDFACounterexampleQueueTest extends AbstractCounterexampleQueueTest { + + @Override + protected DFALearner getLearner(Alphabet alphabet, DFAMembershipOracle oracle) { + return new OptimalLStarDFA<>(alphabet, oracle); + } +} diff --git a/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/ttt/dfa/OptimalTTTDFACounterexampleQueueTest.java b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/ttt/dfa/OptimalTTTDFACounterexampleQueueTest.java new file mode 100644 index 0000000000..de3712fae1 --- /dev/null +++ b/algorithms/active/oml/src/test/java/de/learnlib/algorithm/oml/ttt/dfa/OptimalTTTDFACounterexampleQueueTest.java @@ -0,0 +1,29 @@ +/* Copyright (C) 2013-2024 TU Dortmund University + * This file is part of LearnLib, http://www.learnlib.de/. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.learnlib.algorithm.oml.ttt.dfa; + +import de.learnlib.algorithm.LearningAlgorithm.DFALearner; +import de.learnlib.algorithm.oml.AbstractCounterexampleQueueTest; +import de.learnlib.oracle.MembershipOracle.DFAMembershipOracle; +import net.automatalib.alphabet.Alphabet; + +public class OptimalTTTDFACounterexampleQueueTest extends AbstractCounterexampleQueueTest { + + @Override + protected DFALearner getLearner(Alphabet alphabet, DFAMembershipOracle oracle) { + return new OptimalTTTDFA<>(alphabet, oracle); + } +}