Package dk.brics.automaton

Examples of dk.brics.automaton.Automaton


     * @param a input automaton
     * @return resulting automaton
     */
    @Override
    public Automaton op(Automaton a) {
        Automaton b = a.clone();
        b.setDeterministic(false);
        Map<State, Set<State>> normal_prevs = new HashMap<State, Set<State>>();
        Map<State, Set<State>> special_prevs = new HashMap<State, Set<State>>();
        findPrevs(b, normal_prevs, special_prevs);
        Set<State> pre = findPreSet(b);
        Set<State> post = findPostSet(b, special_prevs);
        boolean initial_accept = post.contains(b.getInitialState());
        State initial = new State();
        b.setInitialState(initial);
        for (State s : pre) {
            for (Transition t : new ArrayList<Transition>(s.getTransitions())) {
                char min = t.getMin();
                char max = t.getMax();
                if (min <= '\u0020') {
                    min = '\u0021';
                }
                if (min <= max) {
                    initial.addTransition(new Transition(min, max, t.getDest()));
                }
            }
        }
        State accept = new State();
        accept.setAccept(true);
        for (State s : b.getAcceptStates()) {
            s.setAccept(false);
        }
        if (initial_accept) {
            initial.setAccept(true);
        }
        for (State s : post) {
            Set<State> prevset = normal_prevs.get(s);
            if (prevset != null) {
                for (State p : prevset) {
                    for (Transition t : new ArrayList<Transition>(p.getTransitions())) {
                        if (t.getDest() == s) {
                            char min = t.getMin();
                            char max = t.getMax();
                            if (min <= '\u0020') {
                                min = '\u0021';
                            }
                            if (min <= max) {
                                p.addTransition(new Transition(min, max, accept));
                            }
                        }
                    }
                }
            }
        }
        b.minimize();
        return b;
    }
View Full Code Here


    public Automaton op(Automaton a, Automaton b) {
        return pop(a, b);
    }

    static Automaton pop(Automaton a, Automaton b) {
        Automaton a1 = a.clone();
        Automaton a2 = a.clone();
        Automaton bb = b.clone();
        Set<StatePair> epsilons = new HashSet<StatePair>();
        for (State s : a1.getStates()) {
            s.setAccept(false);
            epsilons.add(new StatePair(s, bb.getInitialState()));
        }
        for (State s : bb.getAcceptStates()) {
            s.setAccept(false);
            for (State p : a2.getStates()) {
                epsilons.add(new StatePair(s, p));
            }
        }
View Full Code Here

    @Override
    public Automaton op(Automaton a1, Automaton a2) {
        if (a1.isEmpty() || a2.isEmpty())
            return Automaton.makeEmpty();
       
        Automaton anyStringContainingArgument =
            Automaton.makeAnyString().concatenate(a2).concatenate(Automaton.makeAnyString());

        // if no string in a1 contains any of the possible arguments, then
        // the answer is always false
        Automaton intersection = a1.intersection(anyStringContainingArgument);
        if (intersection.isEmpty()) {
            return Basic.makeBinaryBoolean(false);
        }
       
        // if an infinite number of arguments are possible, we can't deduce anything
        // simple proof: for any callee X there exists an argument Y that is longer than X, and therefore X cannot contain Y
        if (!a2.isFinite())
            return Basic.getBinaryBooleans();
       
        // note: 30 is an arbitrary limit to avoid time explosion
        Set<String> finite = a2.getFiniteStrings(30);
        if (finite == null)
            return Basic.getBinaryBooleans();
       
        // try every finite string in the language
        for (String s : finite) {
            Automaton a = Automaton.makeAnyString().concatenate(Automaton.makeString(s)).concatenate(Automaton.makeAnyString());
            if (!a1.subsetOf(a)) {
                return Basic.getBinaryBooleans();
            }
        }
       
View Full Code Here

    /**
     * Returns automaton for the given constant string.
     */
    public static Automaton makeConstString(String s) {
        Automaton a = Automaton.makeString(s);
        a.setInfo(escapeString(s));
        return a;
    }
View Full Code Here

     * <p/>
     * The resulting automaton will be deterministic if and only if the input automaton
     * was deterministic.
     */
    public static Automaton getPrefixesOf(Automaton automaton) {
        Automaton result = automaton.clone();
        for (State state : result.getLiveStates()) {
            state.setAccept(true);
        }
        result.restoreInvariant();
        if (result.isDeterministic()) {
            result.minimize();
        }
        return result;
    }
View Full Code Here

     * <p/>
     * The resulting automaton will be deterministic if and only if the input automaton
     * was deterministic.
     */
    public static Automaton getSuffixesOf(Automaton automaton) {
        Automaton result = automaton.clone();
        Collection<StatePair> epsilons = new ArrayList<StatePair>();
        for (State state : result.getLiveStates()) {
            if (state != result.getInitialState()) {
                epsilons.add(new StatePair(result.getInitialState(), state));
            }
        }
        result.addEpsilons(epsilons);
        if (automaton.isDeterministic()) {
            result.determinize();
            result.minimize();
        }
        return result;
    }
View Full Code Here

    /**
     * Returns an automaton accepting every substring of every string accepted by the
     * specified automaton.
     */
    public static Automaton getSubstringsOf(Automaton automaton) {
        Automaton result = automaton.clone();
        result.removeDeadTransitions();
        Collection<StatePair> epsilons = new ArrayList<StatePair>();
        for (State state : result.getStates()) {
            state.setAccept(true);
            if (state != result.getInitialState()) {
                epsilons.add(new StatePair(result.getInitialState(), state));
            }
        }
        result.restoreInvariant(); // accept states have been modified
        result.addEpsilons(epsilons);
        result.determinize();
        result.minimize();
        return result;
    }
View Full Code Here

     * @param a input automaton
     * @return resulting automaton
     */
    @Override
    public Automaton op(Automaton a) {
        Automaton b = a.clone();
        State initial = new State();
        Set<StatePair> epsilons = new HashSet<StatePair>();
        for (State s : b.getStates()) {
            epsilons.add(new StatePair(initial, s));
        }
        b.setInitialState(initial);
        b.addEpsilons(epsilons);
        b.minimize();
        return b;
    }
View Full Code Here

     * @param a input automaton
     * @return resulting automaton
     */
    @Override
    public Automaton op(Automaton a) {
        Automaton b = a.clone();
        for (State s : b.getStates()) {
            Set<Transition> transitions = s.getTransitions();
            for (Transition t : new ArrayList<Transition>(transitions)) {
                char min = t.getMin();
                char max = t.getMax();
                State dest = t.getDest();
                if (min <= c && c <= max) {
                    transitions.remove(t);
                    transitions.add(new Transition(d, dest));
                    if (min < c) {
                        transitions.add(new Transition(min, (char) (c - 1), dest));
                    }
                    if (c < max) {
                        transitions.add(new Transition((char) (c + 1), max, dest));
                    }
                }
            }
        }
        b.setDeterministic(false);
        b.reduce();
        b.minimize();
        return b;
    }
View Full Code Here

     * @param a input automaton
     * @return resulting automaton
     */
    @Override
    public Automaton op(Automaton a) {
        Automaton a1 = a.clone();
        Map<State, State> map = new HashMap<State, State>();
        Set<State> a1s = a1.getStates();
        for (State s : a1s) {
            State p = new State();
            map.put(s, p);
            if (s.isAccept()) {
                p.setAccept(true);
                s.setAccept(false);
            }
        }
        for (State s : a1s) {
            State p = map.get(s);
            for (Transition t : s.getTransitions()) {
                p.addTransition(new Transition(t.getMin(), t.getMax(), map.get(t.getDest())));
            }
        }
        a1.setDeterministic(false);
        for (State s : a1s) {
            for (Transition t : map.get(s).getTransitions()) {
                s.addTransition(new Transition(Character.MIN_VALUE, Character.MAX_VALUE, t.getDest()));
            }
        }
        a1.minimize();
        return a1;
    }
View Full Code Here

TOP

Related Classes of dk.brics.automaton.Automaton

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.