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

rationals.Automaton Maven / Gradle / Ivy

Go to download

HermiT is reasoner for ontologies written using the Web Ontology Language (OWL). Given an OWL file, HermiT can determine whether or not the ontology is consistent, identify subsumption relationships between classes, and much more. This is the maven build of HermiT and is designed for people who wish to use HermiT from within the OWL API. It is now versioned in the main HermiT version repository, although not officially supported by the HermiT developers. The version number of this package is a composite of the HermiT version and a value representing the OWLAPI release it is compatible with. Note that the group id for the upstream HermiT is com.hermit-reasoner, while this fork is released under net.sourceforge.owlapi. This fork exists to allow HermiT users to use newer OWLAPI versions than the ones supported by the original HermiT codebase. This package includes the Jautomata library (http://jautomata.sourceforge.net/), and builds with it directly. This library appears to be no longer under active development, and so a "fork" seems appropriate. No development is intended or anticipated on this code base.

The newest version!
package rationals;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import rationals.transformations.TransformationsToolBox;

/**
 * A class defining Automaton objects
 * 
 * This class defines the notion of automaton. Following notations are used to
 * describe this class.
 * 

* An automaton is a 5-uple A = (X , Q , I , T , D) where *

    *
  • X is a finite set of labels named alphabet , *
  • Q is a finite set of states, *
  • I, included in Q, is the set of initial states, *
  • T, included in Q, is the set of terminal states *
  • and D is the set of transitions, which is included in * Q times X times Q (transitions are triples (q , l , q') * where q, q' are states and l a label). *
* The empty word, usually denoted by epsilon will be denoted here by * the symbol @. *

* In this implementation of automaton, any object may be a label, states are * instance of class {@code State} and transitions are intances of class * {@code Transition}. Only automata should create instances of states through * {@code Automaton} method {@code newState}. * * @author [email protected] * @author [email protected] * @version $Id: Automaton.java 15 2008-09-20 13:29:35Z oqube $ * @see Transition State */ public class Automaton implements Acceptor, StateMachine, Rational, Cloneable { // The set of all objects which are labels of // transitions of this automaton. protected final Set alphabet; // The set of all states of this automaton. private final Set states; // the set of initial states private final Set initials; // the set of terminale states private final Set terminals; // Allows acces to transitions of this automaton // starting from a given state and labelled by // a given object. The keys of this map are instances // of class Key and // values are sets of transitions. private final Map> transitions; // Allows acces to transitions of this automaton // arriving to a given state and labelled by // a given object. The keys of this map are instances // of class Key and // values are sets of transitions. private final Map> reverse; // bonte private final StateFactory stateFactory; private final Map labels = new HashMap<>(); @Override public StateFactory getStateFactory() { return this.stateFactory; } /** * Returns an automaton which recognizes the regular language associated * with the regular expression @, where @ denotes the * empty word. * * @return an automaton which recognizes @ */ public static Automaton epsilonAutomaton() { Automaton v = new Automaton(); v.addState(true, true); return v; } /** * Returns an automaton which recognizes the regular language associated * with the regular expression l, where l is a given * label. * * @param label * any object that will be used as a label. * @return an automaton which recognizes label */ public static Automaton labelAutomaton(Object label) { Automaton v = new Automaton(); State start = v.addState(true, false); State end = v.addState(false, true); v.addTransition(new Transition(start, label, end), null); return v; } /** * Creates a new empty automaton which contains no state and no transition. * An empty automaton recognizes the empty language. */ public Automaton() { this(null); } /** * Create a new empty automaton with given state factory. * * @param sf * the StateFactory object to use for creating new states. May be * null. */ public Automaton(StateFactory sf) { this.stateFactory = sf == null ? new DefaultStateFactory(this) : sf; alphabet = new HashSet<>(); states = stateFactory.stateSet(); initials = stateFactory.stateSet(); terminals = stateFactory.stateSet(); transitions = new HashMap<>(); reverse = new HashMap<>(); } @Override public State addState(boolean initial, boolean terminal) { State state = stateFactory.create(initial, terminal); if (initial) initials.add(state); if (terminal) terminals.add(state); states.add(state); return state; } @Override public Set alphabet() { return alphabet; } @Override public Set states() { return states; } @Override public Set initials() { return initials; } @Override public Set terminals() { return terminals; } // Computes and return the set of all accessible states, starting // from a given set of states and using transitions // contained in a given Map protected Set access(Set start, Map> map) { Set current = start; Set old; do { old = current; current = stateFactory.stateSet(); Iterator i = old.iterator(); while (i.hasNext()) { State e = i.next(); current.add(e); Iterator j = alphabet.iterator(); while (j.hasNext()) { Iterator k = find(map, e, j.next()).iterator(); while (k.hasNext()) { current.add(k.next().end()); } } } } while (current.size() != old.size()); return current; } @Override public Set accessibleStates() { return access(initials, transitions); } @Override public Set coAccessibleStates() { return access(terminals, reverse); } @Override public Set accessibleAndCoAccessibleStates() { Set ac = accessibleStates(); ac.retainAll(coAccessibleStates()); return ac; } // Computes and return the set of all transitions, starting // from a given state and labelled by a given label // contained in a given Map protected Set find(Map> m, State e, Object l) { Key n = new Key(e, l); if (!m.containsKey(n)) return new HashSet<>(); return m.get(n); } // add a given transition in a given Map protected void add(Map> m, Transition t) { Key n = new Key(t.start(), t.label()); Set s; if (!m.containsKey(n)) { s = new HashSet<>(); m.put(n, s); } else s = m.get(n); s.add(t); } @Override public Set delta() { Set s = new HashSet<>(); for (Set tr : transitions.values()) s.addAll(tr); return s; } @Override public Set delta(State state, Object label) { return find(transitions, state, label); } @Override public Set deltaFrom(State from, State to) { Set t = delta(from); for (Iterator i = t.iterator(); i.hasNext();) { Transition tr = i.next(); if (!to.equals(tr.end())) i.remove(); } return t; } @Override public Set delta(State state) { Set s = new HashSet<>(); for (Object lt : alphabet) s.addAll(delta(state, lt)); return s; } @Override public Set delta(Set s) { Set ds = new HashSet<>(); for (State st : s) ds.addAll(delta(st)); return ds; } /** * Returns the set of all transitions of the reverse of this automaton * * @return the set of all transitions of the reverse of this automaton. A * reverse of an automaton A = (X , Q , I , T , D) is the * automaton A' = (X , Q , T , I , D') where D' is * the set { (q , l , q') | (q' , l , q) in D}. Objects * which are contained in this set are instances of class * {@code Transition}. * @see Transition */ @Override public Set deltaMinusOne(State state, Object label) { return find(reverse, state, label); } @Override public boolean addTransition(Transition transition) { if (!alphabet.contains(transition.label())) { alphabet.add(transition.label()); } add(transitions, transition); add(reverse, new Transition(transition.end(), transition.label(), transition.start())); return true; } @Override public boolean validTransition(Transition transition) { return transition == null || states.contains(transition.start()) && states.contains(transition.end()); } @Override public boolean addTransition(Transition transition, String ifInvalid) { if (validTransition(transition)) { return addTransition(transition); } if (ifInvalid != null) { throw new IllegalArgumentException(ifInvalid); } return false; } /** * returns a textual representation of this automaton. * * @return a textual representation of this automaton based on the converter * {@code toAscii}. * @see rationals.converters.toAscii */ @Override public String toString() { return new rationals.converters.toAscii().toString(this); } /** * returns a copy of this automaton. * * @return a copy of this automaton with new instances of states and * transitions. */ @Override public Object clone() { Automaton b = new Automaton(); Map map = new HashMap<>(); for (State e : states) map.put(e, b.addState(e.isInitial(), e.isTerminal())); for (Transition t : delta()) { b.addTransition(new Transition(map.get(t.start()), t.label(), map.get(t.end())), null); } return b; } private class Key { State s; Object l; protected Key(State s, Object l) { this.s = s; this.l = l; } @Override public boolean equals(Object o) { if (o == this) { return true; } if (o == null || !(o instanceof Key)) return false; Key t = (Key) o; return (l == null ? t.l == null : l.equals(t.l)) && (s == null ? t.s == null : s.equals(t.s)); } @Override public int hashCode() { int x, y; if (s == null) x = 0; else x = s.hashCode(); if (l == null) y = 0; else y = l.hashCode(); return y << 16 | x; } } @Override public Set steps(List word) { Set s = TransformationsToolBox.epsilonClosure(initials(), this); return steps(s, word); } @Override public Set steps(Set _s, List word) { Set s = _s; for(Object o: word) { s = step(s, o); if (s.isEmpty()) return s; } return s; } @Override public Set step(Set s, Object o) { Set ns = stateFactory.stateSet(); Set ec = TransformationsToolBox.epsilonClosure(s, this); Iterator it = ec.iterator(); while (it.hasNext()) { State st = it.next(); Iterator it2 = delta(st).iterator(); while (it2.hasNext()) { Transition tr = it2.next(); if (tr.label() != null && tr.label().equals(o)) ns.add(tr.end()); } } return ns; } @Override public Set deltaMinusOne(State st) { Set s = new HashSet<>(); Iterator alphit = alphabet().iterator(); while (alphit.hasNext()) { s.addAll(deltaMinusOne(st, alphit.next())); } return s; } /** * Create a new state with given label. The state is created with as neither * initial nor terminal. * * @param label * the state's label. May not be null. * @return the newly created state. */ public State state(Object label) { State s = labels.get(label); if (s == null) { s = stateFactory.create(false, false); states.add(s); labels.put(label, s); } return s; } }