|
| 1 | +/* Copyright (C) 2013-2025 TU Dortmund University |
| 2 | + * This file is part of LearnLib <https://learnlib.de>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package de.learnlib.algorithm.lsharp; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Random; |
| 20 | + |
| 21 | +import de.learnlib.driver.simulator.MealySimulatorSUL; |
| 22 | +import de.learnlib.oracle.AdaptiveMembershipOracle; |
| 23 | +import de.learnlib.oracle.equivalence.MealySimulatorEQOracle; |
| 24 | +import de.learnlib.oracle.membership.SULAdaptiveOracle; |
| 25 | +import de.learnlib.query.AdaptiveQuery; |
| 26 | +import de.learnlib.query.DefaultQuery; |
| 27 | +import net.automatalib.alphabet.Alphabet; |
| 28 | +import net.automatalib.alphabet.impl.Alphabets; |
| 29 | +import net.automatalib.automaton.transducer.MealyMachine; |
| 30 | +import net.automatalib.automaton.transducer.impl.CompactMealy; |
| 31 | +import net.automatalib.util.automaton.Automata; |
| 32 | +import net.automatalib.util.automaton.random.RandomAutomata; |
| 33 | +import net.automatalib.word.Word; |
| 34 | +import org.testng.Assert; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +public class LSharpMealyTest { |
| 38 | + |
| 39 | + /** |
| 40 | + * Checks that {@link LSharpMealy#getHypothesisModel()} is free of side-effects. For details, see <a |
| 41 | + * href="https://github.com/LearnLib/learnlib/issues/145">issue 145</a>. |
| 42 | + */ |
| 43 | + @Test |
| 44 | + public void testIssue145() { |
| 45 | + Alphabet<Integer> alphabet = Alphabets.integers(0, 1); |
| 46 | + CompactMealy<Integer, Character> mealy = |
| 47 | + RandomAutomata.randomMealy(new Random(42), 20, alphabet, Alphabets.characters('a', 'c')); |
| 48 | + |
| 49 | + ValidatingOracle<Integer, Character> mqo = |
| 50 | + new ValidatingOracle<>(new SULAdaptiveOracle<>(new MealySimulatorSUL<>(mealy))); |
| 51 | + MealySimulatorEQOracle<Integer, Character> eqo = new MealySimulatorEQOracle<>(mealy); |
| 52 | + LSharpMealy<Integer, Character> learner = new LSharpMealyBuilder<Integer, Character>().withAlphabet(alphabet) |
| 53 | + .withOracle(mqo) |
| 54 | + .withRandom(new Random(42)) |
| 55 | + .create(); |
| 56 | + |
| 57 | + mqo.mutable = true; |
| 58 | + learner.startLearning(); |
| 59 | + mqo.mutable = false; |
| 60 | + MealyMachine<?, Integer, ?, Character> hyp = learner.getHypothesisModel(); |
| 61 | + DefaultQuery<Integer, Word<Character>> cex; |
| 62 | + |
| 63 | + while ((cex = eqo.findCounterExample(hyp, alphabet)) != null) { |
| 64 | + mqo.mutable = true; |
| 65 | + learner.refineHypothesis(cex); |
| 66 | + mqo.mutable = false; |
| 67 | + hyp = learner.getHypothesisModel(); |
| 68 | + } |
| 69 | + |
| 70 | + Assert.assertTrue(Automata.testEquivalence(mealy, learner.getHypothesisModel(), alphabet)); |
| 71 | + } |
| 72 | + |
| 73 | + private static final class ValidatingOracle<I, O> implements AdaptiveMembershipOracle<I, O> { |
| 74 | + |
| 75 | + private final AdaptiveMembershipOracle<I, O> delegate; |
| 76 | + private boolean mutable; |
| 77 | + |
| 78 | + ValidatingOracle(AdaptiveMembershipOracle<I, O> delegate) { |
| 79 | + this.delegate = delegate; |
| 80 | + this.mutable = false; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void processQueries(Collection<? extends AdaptiveQuery<I, O>> adaptiveQueries) { |
| 85 | + Assert.assertTrue(mutable); |
| 86 | + this.delegate.processQueries(adaptiveQueries); |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments