• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.92
/serialization/aut/src/main/java/net/automatalib/serialization/aut/AUTWriter.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://automatalib.net>.
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 net.automatalib.serialization.aut;
17

18
import java.io.IOException;
19
import java.io.OutputStream;
20
import java.io.Writer;
21
import java.util.HashSet;
22
import java.util.Set;
23
import java.util.function.Function;
24

25
import net.automatalib.alphabet.Alphabet;
26
import net.automatalib.automaton.concept.StateIDs;
27
import net.automatalib.automaton.simple.SimpleAutomaton;
28
import net.automatalib.common.util.IOUtil;
29
import net.automatalib.serialization.InputModelSerializer;
30

31
/**
32
 * A writer that exports automata to the AUT format. For further information about the AUT format, see <a
33
 * href="http://cadp.inria.fr/man/aut.html">http://cadp.inria.fr/man/aut.html</a>.
34
 */
35
public final class AUTWriter<I> implements InputModelSerializer<I, SimpleAutomaton<?, I>> {
2✔
36

37
    @Override
38
    public void writeModel(OutputStream os, SimpleAutomaton<?, I> model, Alphabet<I> alphabet) throws IOException {
39
        writeAutomaton(model, alphabet, str -> "\"" + str + "\"", os);
2✔
40
    }
2✔
41

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

47
        final Set<TransitionTriple<S, I>> transitions = new HashSet<>();
2✔
48

49
        for (S s : automaton.getStates()) {
2✔
50
            for (I i : alphabet) {
2✔
51
                final Set<S> succs = automaton.getSuccessors(s, i);
2✔
52

53
                if (!succs.isEmpty()) {
2✔
54
                    for (S succ : succs) {
2✔
55
                        transitions.add(new TransitionTriple<>(s, i, succ));
2✔
56
                    }
2✔
57

58
                }
59
            }
2✔
60
        }
2✔
61

62
        try (Writer w = IOUtil.asNonClosingUTF8Writer(os)) {
2✔
63
            writeHeader(automaton, transitions, w);
2✔
64
            writeTransitions(automaton, transitions, inputTransformer, w);
2✔
65
        }
66
    }
2✔
67

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

72
        final Set<S> inits = automaton.getInitialStates();
2✔
73

74
        if (inits.size() != 1) {
2✔
75
            throw new IllegalArgumentException("Automaton needs to exactly specify a single initial state");
×
76
        }
77

78
        final S init = inits.iterator().next();
2✔
79

80
        final StateIDs<S> stateIds = automaton.stateIDs();
2✔
81

82
        appendable.append("des (");
2✔
83
        appendable.append(Integer.toString(stateIds.getStateId(init)));
2✔
84
        appendable.append(", ");
2✔
85
        appendable.append(Integer.toString(transitions.size()));
2✔
86
        appendable.append(", ");
2✔
87
        appendable.append(Integer.toString(automaton.size()));
2✔
88
        appendable.append(')');
2✔
89
        appendable.append(System.lineSeparator());
2✔
90
    }
2✔
91

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

97
        final StateIDs<S> stateIds = automaton.stateIDs();
2✔
98

99
        for (TransitionTriple<S, I> trans : transitions) {
2✔
100
            appendable.append('(');
2✔
101
            appendable.append(Integer.toString(stateIds.getStateId(trans.src)));
2✔
102
            appendable.append(", ");
2✔
103
            appendable.append(inputTransformer.apply(trans.input));
2✔
104
            appendable.append(", ");
2✔
105
            appendable.append(Integer.toString(stateIds.getStateId(trans.dest)));
2✔
106
            appendable.append(')');
2✔
107
            appendable.append(System.lineSeparator());
2✔
108
        }
2✔
109
    }
2✔
110

111
    private static class TransitionTriple<S, I> {
112

113
        final S src;
114
        final I input;
115
        final S dest;
116

117
        TransitionTriple(S src, I input, S dest) {
2✔
118
            this.src = src;
2✔
119
            this.input = input;
2✔
120
            this.dest = dest;
2✔
121
        }
2✔
122
    }
123

124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc