Skip to content

Commit e1e198b

Browse files
tiferreimtf90
andauthored
feat: Adaptive Mealy Tree Builder (#56)
* feat: Adaptive PTT * feat: unit tests * feat: conflicts() * feat: LimitException * chore: cleanup * feat: cleanup * feat: return conflicted subtree in conflicts method * feat: make net.automatalib.incremental.mealy.tree.Node public * feat: cleanup * add AdaptiveMealyBuilder interface and adjust incremental class hierarchy * add intermediate abstraction layer to separate incremental and adaptive behavior * feat: implement new interfcae * Revert "feat: make net.automatalib.incremental.mealy.tree.Node public" This reverts commit 2ba1ae0. * feat: extend interface * cleanup and fix validations * refactor adaptive impl * minor cleanups + renamings Co-authored-by: Markus Frohme <[email protected]>
1 parent 8f93c89 commit e1e198b

13 files changed

+668
-211
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright (C) 2013-2022 TU Dortmund
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.incremental.mealy;
17+
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import net.automatalib.incremental.mealy.MealyBuilder.GraphView;
23+
import net.automatalib.visualization.DefaultVisualizationHelper;
24+
import net.automatalib.visualization.VisualizationHelper;
25+
26+
public abstract class AbstractGraphView<I, O, N, E> implements GraphView<I, O, N, E> {
27+
28+
@Override
29+
public VisualizationHelper<N, E> getVisualizationHelper() {
30+
return new DefaultVisualizationHelper<N, E>() {
31+
32+
@Override
33+
public Collection<N> initialNodes() {
34+
return Collections.singleton(getInitialNode());
35+
}
36+
37+
@Override
38+
public boolean getEdgeProperties(N src, E edge, N tgt, Map<String, String> properties) {
39+
if (!super.getEdgeProperties(src, edge, tgt, properties)) {
40+
return false;
41+
}
42+
I input = getInputSymbol(edge);
43+
O output = getOutputSymbol(edge);
44+
properties.put(EdgeAttrs.LABEL, input + " / " + output);
45+
return true;
46+
}
47+
};
48+
}
49+
}

incremental/src/main/java/net/automatalib/incremental/mealy/AbstractIncrementalMealyBuilder.java

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright (C) 2013-2022 TU Dortmund
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.incremental.mealy;
17+
18+
import net.automatalib.words.Word;
19+
import org.checkerframework.checker.nullness.qual.Nullable;
20+
21+
/**
22+
* A variation of the {@link IncrementalMealyBuilder} interface that allows one to override previously inserted traces.
23+
*
24+
* @param <I>
25+
* input symbol type
26+
* @param <O>
27+
* output symbol type
28+
*
29+
* @author ferreira
30+
* @author frohme
31+
*/
32+
public interface AdaptiveMealyBuilder<I, O> extends MealyBuilder<I, O> {
33+
34+
/**
35+
* Incorporates a pair of input/output words into the stored information.
36+
*
37+
* @param inputWord
38+
* the input word
39+
* @param outputWord
40+
* the corresponding output word
41+
*
42+
* @return {@code true} if the inserted output word has overridden existing information, {@code false} otherwise.
43+
*/
44+
boolean insert(Word<? extends I> inputWord, Word<? extends O> outputWord);
45+
46+
/**
47+
* Returns the oldest, non-overridden input that has been introduced and persisted.
48+
*
49+
* @return the {@code Word} representing the oldest stored input, {@code null} if the cache is empty.
50+
*/
51+
@Nullable Word<I> getOldestInput();
52+
53+
}

incremental/src/main/java/net/automatalib/incremental/mealy/IncrementalMealyBuilder.java

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,10 @@
1515
*/
1616
package net.automatalib.incremental.mealy;
1717

18-
import java.util.List;
19-
20-
import net.automatalib.SupportsGrowingAlphabet;
21-
import net.automatalib.automata.transducers.MealyMachine;
22-
import net.automatalib.graphs.Graph;
2318
import net.automatalib.incremental.ConflictException;
24-
import net.automatalib.incremental.IncrementalConstruction;
25-
import net.automatalib.ts.output.MealyTransitionSystem;
2619
import net.automatalib.words.Word;
2720

28-
public interface IncrementalMealyBuilder<I, O>
29-
extends IncrementalConstruction<MealyMachine<?, I, ?, O>, I>, SupportsGrowingAlphabet<I> {
30-
31-
Word<O> lookup(Word<? extends I> inputWord);
32-
33-
/**
34-
* Retrieves the output word for the given input word. If no definitive information for the input word exists, the
35-
* output for the longest known prefix will be returned.
36-
*
37-
* @param inputWord
38-
* the input word
39-
* @param output
40-
* a consumer for constructing the output word
41-
*
42-
* @return {@code true} if the information contained was complete (in this case, {@code word.length() ==
43-
* output.size()} will hold), {@code false} otherwise.
44-
*/
45-
boolean lookup(Word<? extends I> inputWord, List<? super O> output);
21+
public interface IncrementalMealyBuilder<I, O> extends MealyBuilder<I, O> {
4622

4723
/**
4824
* Incorporates a pair of input/output words into the stored information.
@@ -57,18 +33,4 @@ public interface IncrementalMealyBuilder<I, O>
5733
*/
5834
void insert(Word<? extends I> inputWord, Word<? extends O> outputWord);
5935

60-
@Override
61-
GraphView<I, O, ?, ?> asGraph();
62-
63-
@Override
64-
MealyTransitionSystem<?, I, ?, O> asTransitionSystem();
65-
66-
interface GraphView<I, O, N, E> extends Graph<N, E> {
67-
68-
I getInputSymbol(E edge);
69-
70-
O getOutputSymbol(E edge);
71-
72-
N getInitialNode();
73-
}
7436
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright (C) 2013-2022 TU Dortmund
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.incremental.mealy;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import net.automatalib.SupportsGrowingAlphabet;
22+
import net.automatalib.automata.transducers.MealyMachine;
23+
import net.automatalib.graphs.Graph;
24+
import net.automatalib.incremental.IncrementalConstruction;
25+
import net.automatalib.ts.output.MealyTransitionSystem;
26+
import net.automatalib.words.Word;
27+
import net.automatalib.words.WordBuilder;
28+
29+
/**
30+
* A utility interface to share functionality between {@link IncrementalMealyBuilder}s and
31+
* {@link AdaptiveMealyBuilder}s.
32+
*
33+
* @param <I>
34+
* input symbol type
35+
* @param <O>
36+
* output symbol type
37+
*/
38+
public interface MealyBuilder<I, O>
39+
extends IncrementalConstruction<MealyMachine<?, I, ?, O>, I>, SupportsGrowingAlphabet<I> {
40+
41+
/**
42+
* Retrieves the output word for the given input word. If no definitive information for the input word exists, the
43+
* output for the longest known prefix will be returned.
44+
*
45+
* @param inputWord
46+
* the input word
47+
* @param output
48+
* a consumer for constructing the output word
49+
*
50+
* @return {@code true} if the information contained was complete (in this case,
51+
* {@code word.length() == output.size()} will hold), {@code false} otherwise.
52+
*/
53+
boolean lookup(Word<? extends I> inputWord, List<? super O> output);
54+
55+
default Word<O> lookup(Word<? extends I> inputWord) {
56+
WordBuilder<O> wb = new WordBuilder<>(inputWord.size());
57+
lookup(inputWord, wb);
58+
return wb.toWord();
59+
}
60+
61+
@Override
62+
default boolean hasDefinitiveInformation(Word<? extends I> word) {
63+
return lookup(word, new ArrayList<>(word.length()));
64+
}
65+
66+
@Override
67+
GraphView<I, O, ?, ?> asGraph();
68+
69+
@Override
70+
MealyTransitionSystem<?, I, ?, O> asTransitionSystem();
71+
72+
interface GraphView<I, O, N, E> extends Graph<N, E> {
73+
74+
I getInputSymbol(E edge);
75+
76+
O getOutputSymbol(E edge);
77+
78+
N getInitialNode();
79+
}
80+
}

incremental/src/main/java/net/automatalib/incremental/mealy/dag/IncrementalMealyDAGBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
import net.automatalib.commons.util.IntDisjointSets;
3434
import net.automatalib.commons.util.UnionFind;
3535
import net.automatalib.incremental.ConflictException;
36-
import net.automatalib.incremental.mealy.AbstractIncrementalMealyBuilder;
36+
import net.automatalib.incremental.mealy.AbstractGraphView;
37+
import net.automatalib.incremental.mealy.IncrementalMealyBuilder;
3738
import net.automatalib.ts.output.MealyTransitionSystem;
3839
import net.automatalib.visualization.VisualizationHelper;
3940
import net.automatalib.visualization.helper.DelegateVisualizationHelper;
@@ -53,8 +54,7 @@
5354
*
5455
* @author Malte Isberner
5556
*/
56-
public class IncrementalMealyDAGBuilder<I, O> extends AbstractIncrementalMealyBuilder<I, O>
57-
implements InputAlphabetHolder<I> {
57+
public class IncrementalMealyDAGBuilder<I, O> implements IncrementalMealyBuilder<I, O>, InputAlphabetHolder<I> {
5858

5959
private final Map<@Nullable StateSignature<O>, State<O>> register = new HashMap<>();
6060
private final Alphabet<I> inputAlphabet;

0 commit comments

Comments
 (0)