Commit f70e72ab authored by Javier Baliosian's avatar Javier Baliosian
Browse files

intersection of tffsts looks good


Signed-off-by: Javier Baliosian's avatarbaliosian <javierba@fing.edu.uy>
parent 37e61ba1
......@@ -19,7 +19,7 @@ import uy.edu.fing.mina.fsa.utils.Utils;
* @author Javier Baliosian &lt; <a
* href="mailto:jbaliosian@tsc.upc.es">jbaliosian@tsc.upc.es </a>&gt;
*/
public class TffstComposition3 {
public class TffstComposition2 {
/**
* composition test 1
......
......@@ -21,71 +21,64 @@ import uy.edu.fing.mina.fsa.utils.Utils;
*/
public class TffstIntersection0 {
/**
* composition test 1
*
* @param args
*/
public static void main(String[] args) {
Tffst.setMinimizeAlways(false);
// tffst1 :
// ┌───┐ A/A ┌───┐ B/e ┌───┐ C/e ┌───┐ D/D ╔═══╗
// initial ──▶ │ 0 │ ─────▶ │ 1 │ ─────▶ │ 2 │ ─────▶ │ 3 │ ─────▶║ 4 ║
// └───┘ └───┘ └───┘ └───┘ ╚═══╝
Tffst tffst1 = new Tffst();
State s0 = new State();
tffst1.setInitialState(s0);
State s1 = new State();
State s2 = new State();
State s3 = new State();
State s4 = new State();
s4.setAccept(true);
s0.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("A"), s1));
s1.addOutTran(new Transition(new SimpleTf("B"), SimpleTf.Epsilon(), s2));
s2.addOutTran(new Transition(new SimpleTf("C"), SimpleTf.Epsilon(), s3));
s3.addOutTran(new Transition(new SimpleTf("D"), new SimpleTf("D"), s4));
Utils.showDot(tffst1.toDot("tffst1"));
// tffst1 :
// ┌───┐ A/D ┌───┐ e/E ┌───┐ D/A ╔═══╗
// initial ──▶ │ 0 │ ─────▶ │ 1 │ ─────▶ │ 2 │ ─────▶║ 3 ║
// └───┘ └───┘ └───┘ ╚═══╝
Tffst tffst2 = new Tffst();
State s20 = new State();
tffst2.setInitialState(s20);
State s21 = new State();
State s22 = new State();
State s23 = new State();
s23.setAccept(true);
s20.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("D"), s21));
s21.addOutTran(new Transition(SimpleTf.Epsilon(), new SimpleTf("E"), s22));
s22.addOutTran(new Transition(new SimpleTf("D"), new SimpleTf("A"), s23));
Utils.showDot(tffst2.toDot("tffst2"));
Tffst tffstComposition = tffst1.composition(tffst2);
Utils.showDot(tffstComposition.toDot("tffst1 o tffst2"));
tffstComposition.setDeterministic(false);
tffstComposition.determinize();
Utils.showDot(tffstComposition.toDot("tffst1 o tffst2 determinize"));
}
/**
* composition test 1
*
* @param args
*/
public static void main(String[] args) {
Tffst.setMinimizeAlways(false);
// tffst1 :
// ┌───┐ A/B ╔═══╗
// initial ──▶ │ 0 │ ─────▶ ║ 1 ║
// └───┘ ╚═══╝
// ─────▶
// C/D
Tffst tffst1 = new Tffst();
State s0 = new State();
tffst1.setInitialState(s0);
State s1 = new State();
s1.setAccept(true);
s0.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s1));
s0.addOutTran(new Transition(new SimpleTf("C"), new SimpleTf("D"), s1));
Utils.showDot(tffst1.toDot("tffst1"));
// tffst1 :
// ┌───┐ A/B ╔═══╗
// initial ──▶ │ 0 │ ─────▶ ║ 1 ║
// └───┘ ╚═══╝
// ─────▶
// E/F
Tffst tffst2 = new Tffst();
State s20 = new State();
tffst2.setInitialState(s20);
State s21 = new State();
s21.setAccept(true);
s20.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s21));
s20.addOutTran(new Transition(new SimpleTf("E"), new SimpleTf("F"), s21));
Utils.showDot(tffst1.toDot("tffst1"));
Utils.showDot(tffst2.toDot("tffst2"));
Tffst tffstintersection = tffst1.intersection(tffst2);
Utils.showDot(tffstintersection.toDot("tffst1 o tffst2"));
tffstintersection.setDeterministic(false);
tffstintersection.determinize();
Utils.showDot(tffstintersection.toDot("tffst1 int tffst2 determinize"));
}
}
\ No newline at end of file
/*
* Created on 12-Aug-2004
*
* Copyright (C) 2004 Javier Baliosian
* All rights reserved.
* */
package uy.edu.fing.mina.fsa.test.operations;
import uy.edu.fing.mina.fsa.tf.SimpleTf;
import uy.edu.fing.mina.fsa.tf.TfI;
import uy.edu.fing.mina.fsa.tf.TfString;
import uy.edu.fing.mina.fsa.tffsr.Tffsr;
import uy.edu.fing.mina.fsa.tffst.State;
import uy.edu.fing.mina.fsa.tffst.Tffst;
import uy.edu.fing.mina.fsa.tffst.Transition;
import uy.edu.fing.mina.fsa.utils.Utils;
/**
* @author Javier Baliosian &lt; <a
* href="mailto:jbaliosian@tsc.upc.es">jbaliosian@tsc.upc.es </a>&gt;
*/
public class TffstIntersection1 {
/**
* composition test 1
*
* @param args
*/
public static void main(String[] args) {
Tffst.setMinimizeAlways(false);
// tffst1 : ┌───┐A/B
// ┌───┐ A/B ╔═══╗ │
// initial ──▶ │ 0 │ ─────▶ ║ 1 ║ ┘
// └───┘ ╚═══╝
// ─────▶
// C/D
Tffst tffst1 = new Tffst();
State s0 = new State();
tffst1.setInitialState(s0);
State s1 = new State();
s1.setAccept(true);
s0.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s1));
s1.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s1));
s0.addOutTran(new Transition(new SimpleTf("C"), new SimpleTf("D"), s1));
Utils.showDot(tffst1.toDot("tffst1"));
// tffst2 : ┌───┐A/B
// ┌───┐ A/B ╔═══╗ │
// initial ──▶ │ 0 │ ─────▶ ║ 1 ║ ┘
// └───┘ ╚═══╝
// ─────▶
// E/F
Tffst tffst2 = new Tffst();
State s20 = new State();
tffst2.setInitialState(s20);
State s21 = new State();
s21.setAccept(true);
s20.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s21));
s21.addOutTran(new Transition(new SimpleTf("A"), new SimpleTf("B"), s21));
s20.addOutTran(new Transition(new SimpleTf("E"), new SimpleTf("F"), s21));
Utils.showDot(tffst1.toDot("tffst1"));
Utils.showDot(tffst2.toDot("tffst2"));
Tffst tffstintersection = tffst1.intersection(tffst2);
Utils.showDot(tffstintersection.toDot("tffst1 o tffst2"));
tffstintersection.setDeterministic(false);
tffstintersection.determinize();
Utils.showDot(tffstintersection.toDot("tffst1 int tffst2 determinize"));
}
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ public class State implements Serializable, Comparable<State> {
/**
* @uml.property name="transitions"
*/
private Set<Transition> transitions = new HashSet<Transition>();
private Set<Transition> departingTransitions = new HashSet<Transition>();
private Set<Transition> arrivingTransitions = new HashSet<Transition>();
......@@ -98,7 +98,7 @@ public class State implements Serializable, Comparable<State> {
State s = new State();
for (Iterator<Transition> iter = getTransitionsIterator(); iter.hasNext();) {
Transition t = (Transition) iter.next();
s.transitions.add(t);
s.departingTransitions.add(t);
}
s.setAccept(isAccept());
return s;
......@@ -109,7 +109,7 @@ public class State implements Serializable, Comparable<State> {
Iterator<Transition> i = to.getTransitionsIterator();
while (i.hasNext()) {
Transition t = (Transition) i.next();
transitions.add(t);
departingTransitions.add(t);
}
}
......@@ -128,7 +128,7 @@ public class State implements Serializable, Comparable<State> {
* @uml.property name="transitions"
*/
public void setTransitions(HashSet<Transition> transitions) {
this.transitions = transitions;
this.departingTransitions = transitions; //FIXME it may be a problem with the arriving transitions at the destination nodes.
}
/** Resets transition set. */
......@@ -143,9 +143,9 @@ public class State implements Serializable, Comparable<State> {
* transition
*/
public void addOutTran(Transition t) {
if (!transitions.contains(t)) {
if (!departingTransitions.contains(t)) {
t.setFrom(this);
transitions.add(t);
departingTransitions.add(t);
}
}
......@@ -153,8 +153,8 @@ public class State implements Serializable, Comparable<State> {
* @param transition
*/
public void remOutTran(Transition transition) {
if (transitions.contains(transition)) {
transitions.remove(transition);
if (departingTransitions.contains(transition)) {
departingTransitions.remove(transition);
transition.setFrom(null);
}
}
......@@ -196,7 +196,7 @@ public class State implements Serializable, Comparable<State> {
* @return the transitions
*/
public Set<Transition> getTransitions() {
return transitions;
return departingTransitions;
}
/**
......@@ -208,7 +208,7 @@ public class State implements Serializable, Comparable<State> {
* @uml.property name="transitions"
*/
public Iterator<Transition> getTransitionsIterator() {
return transitions.iterator();
return departingTransitions.iterator();
}
......@@ -233,7 +233,7 @@ public class State implements Serializable, Comparable<State> {
/** Returns transitions sorted by (min, reverse max, to) or (to, min, reverse max) */
Transition[] getSortedTransitionArray(boolean to_first)
{
Transition[] e = (Transition[]) transitions.toArray(new Transition[0]);
Transition[] e = (Transition[]) departingTransitions.toArray(new Transition[0]);
Arrays.sort(e);
return e;
}
......
......@@ -567,10 +567,10 @@ public class Tffsr implements Serializable {
* Complexity: quadratic in number of states (if already deterministic).
*/
public Tffsr intersection(Tffsr a) {
determinizeTF();
a.determinizeTF();
Transition[][] transitions1 = getSortedTransitions(getStates());
Transition[][] transitions2 = getSortedTransitions(a.getStates());
determinizeSymbols();
a.determinizeSymbols();
Map<Integer, Set<Transition>> transitions1 = getTransitions(getStates());
Map<Integer, Set<Transition>> transitions2 = getTransitions(a.getStates());
Tffsr c = new Tffsr();
LinkedList<StatePair> worklist = new LinkedList<StatePair>();
HashMap<StatePair, StatePair> newstates = new HashMap<StatePair, StatePair>();
......@@ -582,20 +582,21 @@ public class Tffsr implements Serializable {
while (worklist.size() > 0) {
p = worklist.removeFirst();
p.s.setAccept(p.s1.isAccept() && p.s2.isAccept());
Transition[] t1 = transitions1[p.s1.getNumber()];
Transition[] t2 = transitions2[p.s2.getNumber()];
for (int n1 = 0, n2 = 0; n1 < t1.length && n2 < t2.length;) {
StatePair q = new StatePair(t1[n1].getTo(), t2[n2].getTo());
StatePair r = newstates.get(q);
if (r == null) {
q.s = new State();
worklist.add(q);
newstates.put(q, q);
r = q;
}
p.s.addOutTran(new Transition(new TfString(t1[n1].label.get(0).and(t2[n2].label.get(0))), r.s)); //FIXME has to simplified
}
Set<Transition> ts1 = transitions1.get(Integer.valueOf(p.s1.getNumber()));
Set<Transition> ts2 = transitions2.get(Integer.valueOf(p.s2.getNumber()));
for (Transition t1 : ts1)
for (Transition t2 : ts2) {
StatePair q = new StatePair(t1.getTo(), t2.getTo());
StatePair r = newstates.get(q);
if (r == null) {
q.s = new State();
worklist.add(q);
newstates.put(q, q);
r = q;
}
p.s.addOutTran(new Transition(new TfString(t1.label.get(0).andSimple(t2.label.get(0))), r.s));
}
}
c.deterministic = true;
c.removeDeadTransitions();
......@@ -1284,7 +1285,7 @@ public class Tffsr implements Serializable {
}
/**
* Returns sorted array of transitions for each state, by state "numinimber". (and
* Returns sorted array of transitions for each state, by state "number". (and
* sets state numbers).
*/
Transition[][] getSortedTransitions(Set<State> states) {
......@@ -1293,11 +1294,25 @@ public class Tffsr implements Serializable {
Iterator<State> i = states.iterator();
while (i.hasNext()) {
State s = i.next();
transitions[s.getNumber()] = (Transition[]) s.getTransitions().toArray(); //TODO take arrays out of this
transitions[s.getNumber()] = s.getTransitions().toArray(transitions[s.getNumber()]);
}
return transitions;
}
/**
* Returns sorted array of transitions for each state, by state "number". (and
* sets state numbers).
*/
Map<Integer,Set<Transition>> getTransitions(Set<State> states) {
setStateNumbers(states);
Map<Integer,Set<Transition>> transitions = new HashMap<Integer, Set<Transition>>();
for (State state : states) {
transitions.put(state.getNumber(), state.getTransitions());
}
return transitions;
}
/**
* Checks whether there is a loop containing s. (This is sufficient since
* there are never transitions to dead states.)
......
......@@ -400,8 +400,9 @@ public class Tffst implements Serializable {
}
}
out.removeInputEpsilonLabel();
out.removeDeadTransitions();
out.removeInputEpsilonLabel();
out.removeDeadTransitions();
// out.checkMinimizeAlways(); //TODO need this?
//out.setDeterministic(false);
//out.determinize();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment