Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create states from AUT header #52

Merged
merged 6 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
class InternalAUTParser {

private int initialState;
private int numStates;
private final Set<String> alphabetSymbols = new HashSet<>();
private final Map<Integer, Map<String, Integer>> transitionMap = new HashMap<>();

Expand All @@ -63,9 +64,9 @@ public <I> InputModelData<I, SimpleAutomaton<Integer, I>> parse(Function<String,
final Map<String, I> inputMap = Maps.asMap(alphabetSymbols, inputTransformer::apply);
final Alphabet<I> alphabet = Alphabets.fromCollection(inputMap.values());

final CompactNFA<I> result = new CompactNFA<>(alphabet, transitionMap.size());
final CompactNFA<I> result = new CompactNFA<>(alphabet, numStates);

for (int i = 0; i < transitionMap.size(); i++) {
for (int i = 0; i < numStates; i++) {
result.addState();
}

Expand Down Expand Up @@ -98,9 +99,12 @@ private void parseHeader(BufferedReader reader) throws IOException {
verifyLBracketAndShift();
initialState = parseNumberAndShift();
verifyCommaAndShift();
parseNumberAndShift(); // ignore number of states
verifyCommaAndShift();
parseNumberAndShift(); // ignore number of transitions
verifyCommaAndShift();
numStates = parseNumberAndShift(); // store number of states
if (numStates < 1) {
throw new IllegalArgumentException("Number of states must be >= 1");
}
verifyRBracketAndShift();
}

Expand Down Expand Up @@ -194,6 +198,10 @@ private int parseNumberAndShift() {
sym = currentLineContent[currentPos];
}

if (sb.length() == 0){
throw new IllegalArgumentException(buildErrorMessage("Expected a positive number"));
}

// forward pointer
shiftToNextNonWhitespace();
return Integer.parseInt(sb.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.automatalib.util.automata.random.RandomAutomata;
import net.automatalib.words.Alphabet;
import net.automatalib.words.impl.Alphabets;
import net.automatalib.words.impl.ArrayAlphabet;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -65,6 +66,28 @@ public void quotationLabelTest() throws Exception {
}
}

@Test
public void sinkStateTest() throws Exception {
try (InputStream is = AUTSerializationTest.class.getResourceAsStream("/sinkStateTest.aut")) {
final SimpleAutomaton<Integer, String> automaton = AUTParser.readAutomaton(is).model;
Assert.assertEquals(3, automaton.size());

final Alphabet<String> alphabet = new ArrayAlphabet<String>("input", "output");

final Set<Integer> s0 = automaton.getInitialStates();
final Set<Integer> t1 = automaton.getSuccessors(s0, Collections.singletonList("input"));
final Set<Integer> t2 = automaton.getSuccessors(s0, Collections.singletonList("output"));
final Set<Integer> t3 = automaton.getSuccessors(s0, Arrays.asList("input", "output"));
final Set<Integer> t4 = automaton.getSuccessors(s0, Arrays.asList("input", "output", "output"));

Assert.assertEquals(Collections.singleton(0), s0);
Assert.assertEquals(Collections.singleton(1), t1);
Assert.assertEquals(Collections.singleton(2), t2);
Assert.assertEquals(Collections.singleton(0), t3);
Assert.assertEquals(Collections.singleton(2), t4);
}
}

@Test
public void serializationTest() throws Exception {
final Alphabet<Integer> alphabet = Alphabets.integers(0, 2);
Expand Down Expand Up @@ -92,10 +115,14 @@ private <S, I> void equalityTest(SimpleAutomaton<S, I> src, SimpleAutomaton<S, I
public void errorTest() throws IOException {
try (InputStream e1 = AUTSerializationTest.class.getResourceAsStream("/error1.aut");
InputStream e2 = AUTSerializationTest.class.getResourceAsStream("/error2.aut");
InputStream e3 = AUTSerializationTest.class.getResourceAsStream("/error3.aut")) {
InputStream e3 = AUTSerializationTest.class.getResourceAsStream("/error3.aut");
InputStream e4 = AUTSerializationTest.class.getResourceAsStream("/error4.aut");
InputStream e5 = AUTSerializationTest.class.getResourceAsStream("/error5.aut")) {
Assert.assertThrows(() -> AUTParser.readAutomaton(e1));
Assert.assertThrows(() -> AUTParser.readAutomaton(e2));
Assert.assertThrows(() -> AUTParser.readAutomaton(e3));
Assert.assertThrows(() -> AUTParser.readAutomaton(e4));
Assert.assertThrows(() -> AUTParser.readAutomaton(e5));
}
}

Expand Down
4 changes: 4 additions & 0 deletions serialization/aut/src/test/resources/error4.aut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
des(0, 3, 0)
(0, a, 1)
(1, b, 0)
(1, c, 1)
2 changes: 2 additions & 0 deletions serialization/aut/src/test/resources/error5.aut
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
des(0, 1, 1)
(1, z , -2)
5 changes: 5 additions & 0 deletions serialization/aut/src/test/resources/sinkStateTest.aut
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
des(0,4,3)
(0,input,1)
(0,output,2)
(1,input,2)
(1,output,0)