Skip to content

Commit

Permalink
examples: remove (breaking) dependency on test-support module
Browse files Browse the repository at this point in the history
Includes fixes for the (also broken) ResumableExample

See the discussion of #128
  • Loading branch information
mtf90 committed Jun 20, 2024
1 parent 0e257cf commit 97cc60e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
10 changes: 4 additions & 6 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ limitations under the License.
<groupId>de.learnlib</groupId>
<artifactId>learnlib-statistics</artifactId>
</dependency>
<dependency>
<groupId>de.learnlib.testsupport</groupId>
<artifactId>learnlib-test-support</artifactId>
<scope>compile</scope>
<!-- Override! -->
</dependency>
<dependency>
<groupId>de.learnlib</groupId>
<artifactId>learnlib-ttt</artifactId>
Expand Down Expand Up @@ -162,6 +156,10 @@ limitations under the License.
<artifactId>automata-serialization-dot</artifactId>
</dependency>

<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import de.learnlib.oracle.equivalence.DFASimulatorEQOracle;
import de.learnlib.oracle.membership.DFASimulatorOracle;
import de.learnlib.query.DefaultQuery;
import de.learnlib.testsupport.ResumeUtils;
import net.automatalib.alphabet.Alphabet;
import net.automatalib.alphabet.impl.Alphabets;
import net.automatalib.alphabet.impl.GrowingMapAlphabet;
import net.automatalib.automaton.fsa.impl.CompactDFA;
import net.automatalib.util.automaton.random.RandomAutomata;
import org.apache.fury.Fury;
import org.apache.fury.logging.LogLevel;
import org.apache.fury.logging.LoggerFactory;

/**
* An example to demonstrate the {@link Resumable} feature of LearnLib to continue learning setups from previously
Expand All @@ -43,13 +45,18 @@ public final class ResumableExample {

private static final CompactDFA<Character> TARGET;
private static final Alphabet<Character> INITIAL_ALPHABET;
private static final Fury FURY;

static {
LoggerFactory.useSlf4jLogging(true);
LoggerFactory.setLogLevel(LogLevel.ERROR_LEVEL);

final int seed = 42;
final int size = 100;

TARGET = RandomAutomata.randomDFA(new Random(seed), size, Alphabets.characters('a', 'd'));
INITIAL_ALPHABET = Alphabets.characters('a', 'b');
FURY = Fury.builder().withRefTracking(true).requireClassRegistration(false).build();
}

private ResumableExample() {
Expand All @@ -71,8 +78,8 @@ public static void main(String[] args) {
printStats(setup);

// serialize the current state of the learning setup which may be stored somewhere external
final byte[] learnerData = ResumeUtils.toBytes(setup.learner.suspend());
final byte[] cacheData = ResumeUtils.toBytes(setup.cache.suspend());
final byte[] learnerData = toBytes(setup.learner.suspend());
final byte[] cacheData = toBytes(setup.cache.suspend());

// continue exploring the previous snapshot with new input symbol 'c'
continueExploring(learnerData, cacheData, 'c');
Expand All @@ -87,10 +94,10 @@ private static void continueExploring(byte[] learnerData, byte[] cacheData, char
final Setup setup = new Setup();

// resume from previous states and add new symbol
setup.cache.resume(ResumeUtils.fromBytes(cacheData));
setup.cache.resume(fromBytes(cacheData));
setup.cache.addAlphabetSymbol(newSymbol);

setup.learner.resume(ResumeUtils.fromBytes(learnerData));
setup.learner.resume(fromBytes(learnerData));
setup.learner.addAlphabetSymbol(newSymbol);

// continue learning-loop
Expand All @@ -103,6 +110,15 @@ private static void continueExploring(byte[] learnerData, byte[] cacheData, char
printStats(setup);
}

private static byte[] toBytes(Object state) {
return FURY.serialize(state);
}

@SuppressWarnings("unchecked")
private static <T> T fromBytes(byte[] bytes) {
return (T) FURY.deserialize(bytes);
}

private static void printStats(Setup setup) {
System.out.println("Hypothesis size: " + setup.learner.getHypothesisModel().size());
System.out.println(setup.counter.getStatisticalData().getSummary());
Expand Down
6 changes: 5 additions & 1 deletion examples/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
requires de.learnlib.oracle.membership;
requires de.learnlib.oracle.parallelism;
requires de.learnlib.oracle.property;
requires de.learnlib.testsupport;
requires de.learnlib.testsupport.example;
requires net.automatalib.api;
requires net.automatalib.common.util;
Expand All @@ -50,6 +49,11 @@
requires net.automatalib.util;
requires net.automatalib.serialization.dot;
requires net.automatalib.visualization.dot;
requires org.apache.fury.core;

// required by Fury
requires jdk.unsupported;
requires java.sql;

// make non-static once https://github.com/typetools/checker-framework/issues/4559 is implemented
requires static org.checkerframework.checker.qual;
Expand Down
1 change: 0 additions & 1 deletion test-support/test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<scope>compile</scope>
<!-- Override! -->
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private ResumeUtils() {
// prevent instantiation
}

public static <T extends Object> byte[] toBytes(T state) {
public static byte[] toBytes(Object state) {
return FURY.serialize(state);
}

Expand Down

0 comments on commit 97cc60e

Please sign in to comment.