|
| 1 | +/* Copyright (C) 2013-2026 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.example.reactive; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.Random; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | + |
| 22 | +import de.learnlib.algorithm.ttt.mealy.TTTLearnerMealy; |
| 23 | +import de.learnlib.driver.simulator.MealySimulatorSUL; |
| 24 | +import de.learnlib.oracle.equivalence.KWayStateCoverEQOracleBuilder; |
| 25 | +import de.learnlib.oracle.equivalence.RandomWMethodEQOracle; |
| 26 | +import de.learnlib.oracle.parallelism.ParallelOracleBuilders; |
| 27 | +import de.learnlib.query.DefaultQuery; |
| 28 | +import io.smallrye.mutiny.Multi; |
| 29 | +import net.automatalib.alphabet.impl.Alphabets; |
| 30 | +import net.automatalib.automaton.transducer.MealyMachine; |
| 31 | +import net.automatalib.util.automaton.Automata; |
| 32 | +import net.automatalib.util.automaton.random.RandomAutomata; |
| 33 | +import net.automatalib.word.Word; |
| 34 | + |
| 35 | +/** |
| 36 | + * An example of constructing a learn-loop using reactive streams (from SmallRye) to compute counterexamples. |
| 37 | + */ |
| 38 | +// allow println and vars in examples, ExecutorService does not implement AutoClosable until Java 19+ |
| 39 | +@SuppressWarnings("PMD") |
| 40 | +public final class MutinyExample { |
| 41 | + |
| 42 | + private static final int SEED = 42; |
| 43 | + private static final int SIZE = 10; |
| 44 | + private static final int NUM_INPUTS = 4; |
| 45 | + private static final int RND_LENGTH = 4; |
| 46 | + private static final int LIMIT = 1000; |
| 47 | + |
| 48 | + private MutinyExample() { |
| 49 | + // prevent instantiation |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + // setup symbols |
| 54 | + var inputs = Alphabets.integers(0, NUM_INPUTS); |
| 55 | + var outputs = Alphabets.characters('a', 'd'); |
| 56 | + |
| 57 | + // setup membership oracle |
| 58 | + var mealy = RandomAutomata.randomMealy(new Random(SEED), SIZE, inputs, outputs); |
| 59 | + var sul = new MealySimulatorSUL<>(mealy); |
| 60 | + // IMPORTANT: make sure to use parallel-aware oracle (with thread local instances) |
| 61 | + // because it will be called from different threads in the reactive environment |
| 62 | + var mqo = ParallelOracleBuilders.newDynamicParallelOracle(sul).create(); |
| 63 | + |
| 64 | + // setup equivalence oracles |
| 65 | + var eqo = new RandomWMethodEQOracle<>(mqo, SIZE / 2, RND_LENGTH); |
| 66 | + var eqo2 = |
| 67 | + new KWayStateCoverEQOracleBuilder<MealyMachine<?, Integer, ?, Character>, Integer, Word<Character>>().withOracle( |
| 68 | + mqo).withRandom(new Random(SEED)).create(); |
| 69 | + |
| 70 | + // setup learner |
| 71 | + var learner = new TTTLearnerMealy<>(inputs, mqo); |
| 72 | + |
| 73 | + // setup thread pools |
| 74 | + var pool = Executors.newFixedThreadPool(1); |
| 75 | + var pool2 = Executors.newFixedThreadPool(1); |
| 76 | + var pool3 = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 77 | + |
| 78 | + // learning loop |
| 79 | + learner.startLearning(); |
| 80 | + var hyp = learner.getHypothesisModel(); |
| 81 | + |
| 82 | + while (true) { |
| 83 | + // since we only access the hypothesis in read-only fashion it is fine to share the reference across threads |
| 84 | + final var finalHyp = hyp; |
| 85 | + var m1 = Multi.createFrom() |
| 86 | + // create a (cold) publisher from first quivalence oracle |
| 87 | + .items(() -> eqo.generateTestWords(finalHyp, inputs)) |
| 88 | + // sample on own thread |
| 89 | + .runSubscriptionOn(pool) |
| 90 | + // limit to 1000 elements |
| 91 | + .select().first(LIMIT); |
| 92 | + var m2 = Multi.createFrom() |
| 93 | + // create a (cold) publisher from second quivalence oracle |
| 94 | + .items(() -> eqo2.generateTestWords(finalHyp, inputs)) |
| 95 | + // sample on own thread |
| 96 | + .runSubscriptionOn(pool2); |
| 97 | + |
| 98 | + var ce = Multi.createBy() |
| 99 | + // merge elements from both oracles in interleaving fashion |
| 100 | + .merging().streams(m1, m2) |
| 101 | + // run processing in parallel |
| 102 | + .runSubscriptionOn(pool3) |
| 103 | + // filter for counterexamples |
| 104 | + .filter(w -> !Objects.equals(mqo.answerQuery(w), finalHyp.computeOutput(w))) |
| 105 | + // toUni implicitly fetches the first element |
| 106 | + .toUni() |
| 107 | + // use a blocking call to extract the final counterexample |
| 108 | + .await().indefinitely(); |
| 109 | + |
| 110 | + if (ce != null) { |
| 111 | + learner.refineHypothesis(new DefaultQuery<>(ce, mqo.answerQuery(ce))); |
| 112 | + hyp = learner.getHypothesisModel(); |
| 113 | + } else { |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // cleanup |
| 119 | + mqo.shutdown(); |
| 120 | + pool.shutdown(); |
| 121 | + pool2.shutdown(); |
| 122 | + pool3.shutdown(); |
| 123 | + |
| 124 | + // process results |
| 125 | + hyp = learner.getHypothesisModel(); |
| 126 | + |
| 127 | + System.out.println("Final hypothesis size " + hyp.size()); |
| 128 | + System.out.println("Is equivalent? " + Automata.testEquivalence(mealy, hyp, inputs)); |
| 129 | + } |
| 130 | +} |
0 commit comments