Skip to content

Commit

Permalink
serialization: add serializers for AUT format (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtf90 committed Sep 4, 2017
1 parent db01d1a commit a7aa0f2
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 5 deletions.
56 changes: 56 additions & 0 deletions serialization/aut/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2013-2017 TU Dortmund
This file is part of AutomataLib, http://www.automatalib.net/.
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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>automata-serialization-aut</artifactId>
<packaging>jar</packaging>

<name>AutomataLib :: Serialization :: AUT</name>
<description>(De-)Serializers for the AUT Format</description>

<parent>
<groupId>net.automatalib</groupId>
<artifactId>automata-serialization-parent</artifactId>
<version>0.6.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>net.automatalib</groupId>
<artifactId>automata-api</artifactId>
</dependency>
<dependency>
<groupId>net.automatalib</groupId>
<artifactId>automata-core</artifactId>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>net.automatalib</groupId>
<artifactId>automata-util</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (C) 2013-2017 TU Dortmund
* This file is part of AutomataLib, http://www.automatalib.net/.
*
* 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 net.automatalib.serialization.aut;

import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;

import net.automatalib.automata.simple.SimpleAutomaton;

/**
* A parser for automata specified in the AUT format (see http://cadp.inria.fr/man/aut.html for further information).
*
* @author frohme
*/
public final class AUTParser {

private AUTParser() {
// prevent instantiation
}

public static SimpleAutomaton<Integer, String> readAutomaton(InputStream is) throws IOException {
return readAutomaton(is, Function.identity());
}

public static <I> SimpleAutomaton<Integer, I> readAutomaton(InputStream is, Function<String, I> inputTransformer)
throws IOException {
return new InternalAUTParser(is).parse(inputTransformer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Copyright (C) 2013-2017 TU Dortmund
* This file is part of AutomataLib, http://www.automatalib.net/.
*
* 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 net.automatalib.serialization.aut;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import net.automatalib.automata.concepts.StateIDs;
import net.automatalib.automata.simple.SimpleAutomaton;
import net.automatalib.words.Alphabet;

/**
* A utility class that exports automata to the AUT format (see http://cadp.inria.fr/man/aut.html for further
* information).
*
* @author frohme
*/
public final class AUTWriter {

private AUTWriter() {
// prevent instantiation
}

public static <S, I> void writeAutomaton(SimpleAutomaton<S, I> automaton, Alphabet<I> alphabet, OutputStream os)
throws IOException {
writeAutomaton(automaton, alphabet, str -> "\"" + str + "\"", os);

}

public static <S, I> void writeAutomaton(SimpleAutomaton<S, I> automaton,
Alphabet<I> alphabet,
Function<I, String> inputTransformer,
OutputStream os) throws IOException {

final Set<TransitionTriple<S, I>> transitions = new HashSet<>();

for (final S s : automaton.getStates()) {
for (final I i : alphabet) {
final Set<? extends S> succs = automaton.getSuccessors(s, i);

if (succs != null && !succs.isEmpty()) {
for (final S succ : succs) {
transitions.add(new TransitionTriple<>(s, i, succ));
}

}
}
}

try (OutputStreamWriter osw = new OutputStreamWriter(os)) {
writeHeader(automaton, transitions, osw);
writeTransitions(automaton, transitions, inputTransformer, osw);
}
}

private static <S, I> void writeHeader(SimpleAutomaton<S, I> automaton,
Set<TransitionTriple<S, I>> transitions,
Appendable appendable) throws IOException {

final Set<? extends S> inits = automaton.getInitialStates();

if (inits == null || inits.size() != 1) {
throw new IllegalArgumentException("Automaton needs to exactly specify a single initial state");
}

final S init = inits.iterator().next();

final StateIDs<S> stateIds = automaton.stateIDs();

appendable.append("des (");
appendable.append(Integer.toString(stateIds.getStateId(init)));
appendable.append(", ");
appendable.append(Integer.toString(transitions.size()));
appendable.append(", ");
appendable.append(Integer.toString(automaton.size()));
appendable.append(")");
appendable.append(System.lineSeparator());
}

private static <S, I> void writeTransitions(SimpleAutomaton<S, I> automaton,
Set<TransitionTriple<S, I>> transitions,
Function<I, String> inputTransformer,
Appendable appendable) throws IOException {

final StateIDs<S> stateIds = automaton.stateIDs();

for (final TransitionTriple<S, I> trans : transitions) {
appendable.append('(');
appendable.append(Integer.toString(stateIds.getStateId(trans.src)));
appendable.append(", ");
appendable.append(inputTransformer.apply(trans.input));
appendable.append(", ");
appendable.append(Integer.toString(stateIds.getStateId(trans.dest)));
appendable.append(")");
appendable.append(System.lineSeparator());
}
}

private static class TransitionTriple<S, I> {

final S src;
final I input;
final S dest;

TransitionTriple(S src, I input, S dest) {
this.src = src;
this.input = input;
this.dest = dest;
}
}

}
Loading

0 comments on commit a7aa0f2

Please sign in to comment.