All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.automatalib.brics.AbstractBricsAutomaton Maven / Gradle / Ivy

Go to download

This artifact contains adapter classes for treating the Automaton objects from the BRICS library (http://www.brics.dk/automaton) as AutomataLib models.

The newest version!
/* Copyright (C) 2013-2023 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.brics;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import dk.brics.automaton.Automaton;
import dk.brics.automaton.State;
import dk.brics.automaton.Transition;
import net.automatalib.automaton.fsa.FiniteStateAcceptor;
import net.automatalib.automaton.graph.AbstractAutomatonGraphView;
import net.automatalib.graph.UniversalGraph;
import net.automatalib.graph.concept.GraphViewable;
import net.automatalib.visualization.VisualizationHelper;

/**
 * Base class for Brics automata adapters.
 */
public abstract class AbstractBricsAutomaton implements FiniteStateAcceptor, GraphViewable {

    protected final Automaton automaton;

    /**
     * Constructor.
     *
     * @param automaton
     *         the Brics automaton to wrap.
     * @param totalize
     *         flag, indicating whether the automaton should have a total transition function.
     *
     * @see Automaton#totalize()
     */
    public AbstractBricsAutomaton(Automaton automaton, boolean totalize) {
        this.automaton = automaton;

        if (totalize) {
            State s = new State();
            s.addTransition(new Transition(Character.MIN_VALUE, Character.MAX_VALUE, s));
            for (State p : automaton.getStates()) {
                int maxi = Character.MIN_VALUE;
                for (Transition t : p.getSortedTransitions(false)) {
                    if (t.getMin() > maxi) {
                        p.addTransition(new Transition((char) maxi, (char) (t.getMin() - 1), s));
                    }
                    if (t.getMin() + 1 > maxi) {
                        maxi = t.getMax() + 1;
                    }
                }
                if (maxi <= Character.MAX_VALUE) {
                    p.addTransition(new Transition((char) maxi, Character.MAX_VALUE, s));
                }
            }
        }
    }

    /**
     * Retrieves the Brics automaton object.
     *
     * @return the brics automaton object
     */
    public Automaton getBricsAutomaton() {
        return automaton;
    }

    @Override
    public boolean isAccepting(State state) {
        return state.isAccept();
    }

    @Override
    public Collection getTransitions(State state, Character input) {
        Collection transitions = state.getSortedTransitions(false);

        Set result = new HashSet<>();

        for (Transition t : transitions) {
            char min = t.getMin();
            if (input < min) {
                break;
            }
            char max = t.getMax();
            if (input > max) {
                continue;
            }
            result.add(t.getDest());
        }
        return result;
    }

    @Override
    public Set getInitialStates() {
        return Collections.singleton(automaton.getInitialState());
    }

    @Override
    public Collection getStates() {
        return automaton.getStates();
    }

    @Override
    public GraphView graphView() {
        return new GraphView();
    }

    public class GraphView extends AbstractAutomatonGraphView
            implements UniversalGraph {

        public GraphView() {
            super(AbstractBricsAutomaton.this);
        }

        @Override
        public Collection getOutgoingEdges(State node) {
            return node.getTransitions();
        }

        @Override
        public State getTarget(Transition edge) {
            return edge.getDest();
        }

        @Override
        public VisualizationHelper getVisualizationHelper() {
            return new BricsVisualizationHelper(AbstractBricsAutomaton.this);
        }

        @Override
        public Boolean getNodeProperty(State node) {
            return node.isAccept();
        }

        @Override
        public BricsTransitionProperty getEdgeProperty(Transition edge) {
            return new BricsTransitionProperty(edge);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy