Skip to content

feat: Adaptive Mealy Tree Builder #56

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

Merged
merged 18 commits into from
Oct 14, 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
@@ -0,0 +1,49 @@
/* Copyright (C) 2013-2022 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.incremental.mealy;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import net.automatalib.incremental.mealy.MealyBuilder.GraphView;
import net.automatalib.visualization.DefaultVisualizationHelper;
import net.automatalib.visualization.VisualizationHelper;

public abstract class AbstractGraphView<I, O, N, E> implements GraphView<I, O, N, E> {

@Override
public VisualizationHelper<N, E> getVisualizationHelper() {
return new DefaultVisualizationHelper<N, E>() {

@Override
public Collection<N> initialNodes() {
return Collections.singleton(getInitialNode());
}

@Override
public boolean getEdgeProperties(N src, E edge, N tgt, Map<String, String> properties) {
if (!super.getEdgeProperties(src, edge, tgt, properties)) {
return false;
}
I input = getInputSymbol(edge);
O output = getOutputSymbol(edge);
properties.put(EdgeAttrs.LABEL, input + " / " + output);
return true;
}
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright (C) 2013-2022 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.incremental.mealy;

import net.automatalib.words.Word;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A variation of the {@link IncrementalMealyBuilder} interface that allows one to override previously inserted traces.
*
* @param <I>
* input symbol type
* @param <O>
* output symbol type
*
* @author ferreira
* @author frohme
*/
public interface AdaptiveMealyBuilder<I, O> extends MealyBuilder<I, O> {

/**
* Incorporates a pair of input/output words into the stored information.
*
* @param inputWord
* the input word
* @param outputWord
* the corresponding output word
*
* @return {@code true} if the inserted output word has overridden existing information, {@code false} otherwise.
*/
boolean insert(Word<? extends I> inputWord, Word<? extends O> outputWord);

/**
* Returns the oldest, non-overridden input that has been introduced and persisted.
*
* @return the {@code Word} representing the oldest stored input, {@code null} if the cache is empty.
*/
@Nullable Word<I> getOldestInput();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,10 @@
*/
package net.automatalib.incremental.mealy;

import java.util.List;

import net.automatalib.SupportsGrowingAlphabet;
import net.automatalib.automata.transducers.MealyMachine;
import net.automatalib.graphs.Graph;
import net.automatalib.incremental.ConflictException;
import net.automatalib.incremental.IncrementalConstruction;
import net.automatalib.ts.output.MealyTransitionSystem;
import net.automatalib.words.Word;

public interface IncrementalMealyBuilder<I, O>
extends IncrementalConstruction<MealyMachine<?, I, ?, O>, I>, SupportsGrowingAlphabet<I> {

Word<O> lookup(Word<? extends I> inputWord);

/**
* Retrieves the output word for the given input word. If no definitive information for the input word exists, the
* output for the longest known prefix will be returned.
*
* @param inputWord
* the input word
* @param output
* a consumer for constructing the output word
*
* @return {@code true} if the information contained was complete (in this case, {@code word.length() ==
* output.size()} will hold), {@code false} otherwise.
*/
boolean lookup(Word<? extends I> inputWord, List<? super O> output);
public interface IncrementalMealyBuilder<I, O> extends MealyBuilder<I, O> {

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

@Override
GraphView<I, O, ?, ?> asGraph();

@Override
MealyTransitionSystem<?, I, ?, O> asTransitionSystem();

interface GraphView<I, O, N, E> extends Graph<N, E> {

I getInputSymbol(E edge);

O getOutputSymbol(E edge);

N getInitialNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Copyright (C) 2013-2022 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.incremental.mealy;

import java.util.ArrayList;
import java.util.List;

import net.automatalib.SupportsGrowingAlphabet;
import net.automatalib.automata.transducers.MealyMachine;
import net.automatalib.graphs.Graph;
import net.automatalib.incremental.IncrementalConstruction;
import net.automatalib.ts.output.MealyTransitionSystem;
import net.automatalib.words.Word;
import net.automatalib.words.WordBuilder;

/**
* A utility interface to share functionality between {@link IncrementalMealyBuilder}s and
* {@link AdaptiveMealyBuilder}s.
*
* @param <I>
* input symbol type
* @param <O>
* output symbol type
*/
public interface MealyBuilder<I, O>
extends IncrementalConstruction<MealyMachine<?, I, ?, O>, I>, SupportsGrowingAlphabet<I> {

/**
* Retrieves the output word for the given input word. If no definitive information for the input word exists, the
* output for the longest known prefix will be returned.
*
* @param inputWord
* the input word
* @param output
* a consumer for constructing the output word
*
* @return {@code true} if the information contained was complete (in this case,
* {@code word.length() == output.size()} will hold), {@code false} otherwise.
*/
boolean lookup(Word<? extends I> inputWord, List<? super O> output);

default Word<O> lookup(Word<? extends I> inputWord) {
WordBuilder<O> wb = new WordBuilder<>(inputWord.size());
lookup(inputWord, wb);
return wb.toWord();
}

@Override
default boolean hasDefinitiveInformation(Word<? extends I> word) {
return lookup(word, new ArrayList<>(word.length()));
}

@Override
GraphView<I, O, ?, ?> asGraph();

@Override
MealyTransitionSystem<?, I, ?, O> asTransitionSystem();

interface GraphView<I, O, N, E> extends Graph<N, E> {

I getInputSymbol(E edge);

O getOutputSymbol(E edge);

N getInitialNode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import net.automatalib.commons.util.IntDisjointSets;
import net.automatalib.commons.util.UnionFind;
import net.automatalib.incremental.ConflictException;
import net.automatalib.incremental.mealy.AbstractIncrementalMealyBuilder;
import net.automatalib.incremental.mealy.AbstractGraphView;
import net.automatalib.incremental.mealy.IncrementalMealyBuilder;
import net.automatalib.ts.output.MealyTransitionSystem;
import net.automatalib.visualization.VisualizationHelper;
import net.automatalib.visualization.helper.DelegateVisualizationHelper;
Expand All @@ -53,8 +54,7 @@
*
* @author Malte Isberner
*/
public class IncrementalMealyDAGBuilder<I, O> extends AbstractIncrementalMealyBuilder<I, O>
implements InputAlphabetHolder<I> {
public class IncrementalMealyDAGBuilder<I, O> implements IncrementalMealyBuilder<I, O>, InputAlphabetHolder<I> {

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