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

LearnLib / automatalib / 10220057657

02 Aug 2024 06:06PM UTC coverage: 90.366% (+0.3%) from 90.072%
10220057657

push

github

mtf90
overhaul serizalition code

* unify access to Input(De)Serializers behind facades to leverage the default methods for various input/output channels
* drop implicit buffering/decompressing as this should be decided where the stream are constructed (user-land)
* remove/cleanup the (now) unused code

300 of 349 new or added lines in 33 files covered. (85.96%)

9 existing lines in 3 files now uncovered.

15927 of 17625 relevant lines covered (90.37%)

1.67 hits per line

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

95.51
/serialization/saf/src/main/java/net/automatalib/serialization/saf/AbstractSAFInput.java
1
/* Copyright (C) 2013-2024 TU Dortmund University
2
 * This file is part of AutomataLib, http://www.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.saf;
17

18
import java.io.DataInput;
19
import java.io.DataInputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.util.ArrayList;
23
import java.util.List;
24

25
import net.automatalib.alphabet.Alphabet;
26
import net.automatalib.automaton.AutomatonCreator;
27
import net.automatalib.automaton.MutableAutomaton;
28
import net.automatalib.common.util.io.NonClosingInputStream;
29
import net.automatalib.exception.FormatException;
30
import net.automatalib.serialization.InputModelData;
31
import net.automatalib.serialization.InputModelDeserializer;
32

33
/**
34
 * Abstract deserializer for the SAF (simple automaton format).
35
 */
36
abstract class AbstractSAFInput<S, I, T, SP, TP, A extends MutableAutomaton<S, I, T, SP, TP>>
37
        implements InputModelDeserializer<I, A> {
38

39
    private static final AutomatonType[] TYPES = AutomatonType.values();
2✔
40

41
    private final AutomatonType expectedType;
42
    private final AutomatonCreator<? extends A, I> creator;
43
    private final BlockPropertyDecoder<? extends SP> spDecoder;
44
    private final SinglePropertyDecoder<? extends TP> tpDecoder;
45

46
    AbstractSAFInput(AutomatonType expectedType,
47
                     AutomatonCreator<? extends A, I> creator,
48
                     BlockPropertyDecoder<? extends SP> spDecoder,
49
                     SinglePropertyDecoder<? extends TP> tpDecoder) {
2✔
50
        this.expectedType = expectedType;
2✔
51
        this.creator = creator;
2✔
52
        this.spDecoder = spDecoder;
2✔
53
        this.tpDecoder = tpDecoder;
2✔
54
    }
2✔
55

56
    protected abstract Alphabet<I> getAlphabet(int size);
57

58
    @Override
59
    public InputModelData<I, A> readModel(InputStream is) throws IOException, FormatException {
60
        try (DataInputStream in = new DataInputStream(new NonClosingInputStream(is))) {
2✔
61
            final AutomatonType type = readHeader(in);
2✔
62

63
            if (type != expectedType) {
2✔
NEW
64
                throw new FormatException();
×
65
            }
66

67
            final int alphabetSize = in.readInt();
2✔
68

69
            if (alphabetSize <= 0) {
2✔
NEW
70
                throw new FormatException();
×
71
            }
72

73
            final Alphabet<I> alphabet = getAlphabet(alphabetSize);
2✔
74
            final A automaton = readAutomatonBody(in, alphabet, type.isDeterministic(), creator, spDecoder, tpDecoder);
2✔
75

76
            return new InputModelData<>(automaton, alphabet);
2✔
77
        }
78
    }
79

80
    private AutomatonType readHeader(DataInput in) throws IOException, FormatException {
81
        final int headerSize = 4;
2✔
82
        byte[] header = new byte[headerSize];
2✔
83
        in.readFully(header);
2✔
84
        if (header[0] != 'S' || header[1] != 'A' || header[2] != 'F') {
2✔
NEW
85
            throw new FormatException();
×
86
        }
87
        byte type = header[3];
2✔
88
        if (type < 0 || type >= TYPES.length) {
2✔
NEW
89
            throw new FormatException();
×
90
        }
91
        return TYPES[type];
2✔
92
    }
93

94
    private A readAutomatonBody(DataInput in,
95
                                Alphabet<I> alphabet,
96
                                boolean deterministic,
97
                                AutomatonCreator<? extends A, I> creator,
98
                                BlockPropertyDecoder<? extends SP> spDecoder,
99
                                SinglePropertyDecoder<? extends TP> tpDecoder) throws IOException {
100
        int numStates = in.readInt();
2✔
101
        A result = creator.createAutomaton(alphabet, numStates);
2✔
102

103
        if (deterministic) {
2✔
104
            decodeBodyDet(in, result, alphabet, numStates, spDecoder, tpDecoder);
2✔
105
        } else {
106
            decodeBodyNondet(in, result, alphabet, numStates, spDecoder, tpDecoder);
2✔
107
        }
108

109
        return result;
2✔
110
    }
111

112
    private void decodeBodyDet(DataInput in,
113
                               MutableAutomaton<S, I, ?, SP, TP> result,
114
                               Alphabet<I> alphabet,
115
                               int numStates,
116
                               BlockPropertyDecoder<? extends SP> spDecoder,
117
                               SinglePropertyDecoder<? extends TP> tpDecoder) throws IOException {
118

119
        List<S> stateList = decodeStatesDet(in, result, numStates, spDecoder);
2✔
120
        decodeTransitionsDet(in, result, stateList, alphabet, tpDecoder);
2✔
121
    }
2✔
122

123
    private void decodeBodyNondet(DataInput in,
124
                                  MutableAutomaton<S, I, ?, SP, TP> result,
125
                                  Alphabet<I> alphabet,
126
                                  int numStates,
127
                                  BlockPropertyDecoder<? extends SP> spDecoder,
128
                                  SinglePropertyDecoder<? extends TP> tpDecoder) throws IOException {
129

130
        List<S> stateList = decodeStatesNondet(in, result, numStates, spDecoder);
2✔
131
        decodeTransitionsNondet(in, result, stateList, alphabet, tpDecoder);
2✔
132
    }
2✔
133

134
    private List<S> decodeStatesDet(DataInput in,
135
                                    MutableAutomaton<S, ?, ?, SP, ?> result,
136
                                    int numStates,
137
                                    BlockPropertyDecoder<? extends SP> decoder) throws IOException {
138
        int initStateId = in.readInt();
2✔
139

140
        List<S> stateList = decodeStateProperties(in, result, numStates, decoder);
2✔
141

142
        S initState = stateList.get(initStateId);
2✔
143

144
        result.setInitial(initState, true);
2✔
145

146
        return stateList;
2✔
147
    }
148

149
    private void decodeTransitionsDet(DataInput in,
150
                                      MutableAutomaton<S, I, ?, ?, TP> result,
151
                                      List<S> stateList,
152
                                      Alphabet<I> alphabet,
153
                                      SinglePropertyDecoder<? extends TP> tpDecoder) throws IOException {
154
        int numStates = stateList.size();
2✔
155
        assert result.size() == numStates;
2✔
156

157
        int numInputs = alphabet.size();
2✔
158

159
        for (S state : stateList) {
2✔
160
            for (int j = 0; j < numInputs; j++) {
2✔
161
                int tgt = in.readInt();
2✔
162
                if (tgt != -1) {
2✔
163
                    I sym = alphabet.getSymbol(j);
2✔
164
                    S tgtState = stateList.get(tgt);
2✔
165
                    TP prop = tpDecoder.readProperty(in);
2✔
166
                    result.addTransition(state, sym, tgtState, prop);
2✔
167
                }
168
            }
169
        }
2✔
170
    }
2✔
171

172
    private List<S> decodeStatesNondet(DataInput in,
173
                                       MutableAutomaton<S, ?, ?, SP, ?> result,
174
                                       int numStates,
175
                                       BlockPropertyDecoder<? extends SP> decoder) throws IOException {
176
        int[] initStates = readInts(in);
2✔
177

178
        List<S> stateList = decodeStateProperties(in, result, numStates, decoder);
2✔
179

180
        for (int initId : initStates) {
2✔
181
            S initState = stateList.get(initId);
2✔
182
            result.setInitial(initState, true);
2✔
183
        }
184

185
        return stateList;
2✔
186
    }
187

188
    private void decodeTransitionsNondet(DataInput in,
189
                                         MutableAutomaton<S, I, ?, ?, TP> result,
190
                                         List<S> stateList,
191
                                         Alphabet<I> alphabet,
192
                                         SinglePropertyDecoder<? extends TP> tpDecoder) throws IOException {
193
        int numStates = stateList.size();
2✔
194
        assert result.size() == numStates;
2✔
195

196
        int numInputs = alphabet.size();
2✔
197

198
        for (S state : stateList) {
2✔
199
            for (int j = 0; j < numInputs; j++) {
2✔
200
                int numTgts = in.readInt();
2✔
201
                I sym = alphabet.getSymbol(j);
2✔
202
                for (int k = 0; k < numTgts; k++) {
2✔
203
                    int tgt = in.readInt();
2✔
204
                    TP prop = tpDecoder.readProperty(in);
2✔
205
                    S tgtState = stateList.get(tgt);
2✔
206
                    result.addTransition(state, sym, tgtState, prop);
2✔
207
                }
208
            }
209
        }
2✔
210
    }
2✔
211

212
    private List<S> decodeStateProperties(DataInput in,
213
                                          MutableAutomaton<S, ?, ?, SP, ?> result,
214
                                          int numStates,
215
                                          BlockPropertyDecoder<? extends SP> decoder) throws IOException {
216
        List<S> stateList = new ArrayList<>(numStates);
2✔
217

218
        decoder.start(in);
2✔
219
        for (int i = 0; i < numStates; i++) {
2✔
220
            SP prop = decoder.readProperty(in);
2✔
221
            S state = result.addState(prop);
2✔
222
            stateList.add(state);
2✔
223
        }
224
        decoder.finish(in);
2✔
225

226
        return stateList;
2✔
227
    }
228

229
    private static int[] readInts(DataInput in) throws IOException {
230
        int n = in.readInt();
2✔
231
        int[] result = new int[n];
2✔
232
        for (int i = 0; i < n; i++) {
2✔
233
            result[i] = in.readInt();
2✔
234
        }
235
        return result;
2✔
236
    }
237
}
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