ActionTransitions.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.vis.actions;
00002 
00003 import java.util.ArrayList;
00004 import java.util.Collection;
00005 import java.util.Collections;
00006 import java.util.HashMap;
00007 import java.util.HashSet;
00008 import java.util.Iterator;
00009 import java.util.List;
00010 import java.util.Map;
00011 import java.util.Set;
00012 import java.util.Vector;
00013 
00014 
00015 public class ActionTransitions implements Iterable<ActionTransition> {
00016 
00017         List<ActionTransition> transitions;
00018         Action startAction = null;
00019 
00020         public ActionTransitions() {
00021                 this.transitions = Collections.synchronizedList(new ArrayList<ActionTransition>());
00022         }
00023         
00024         
00025         public synchronized boolean add(ActionTransition t) {
00026                 
00027                 if(!transitions.contains(t))
00028                         transitions.add(t);
00029                 
00030                 return true;
00031         }
00032         
00033         public synchronized boolean addAll(Collection<ActionTransition> ts) {
00034                 return transitions.addAll(ts);
00035         }
00036         
00037         public synchronized boolean addAll(ActionTransitions ts) {
00038                 return transitions.addAll(ts.transitions);
00039         }
00040         
00041         
00042         public synchronized void remove(ActionTransition t) {
00043                 transitions.remove(t);
00044         }
00045 
00046         
00047         public synchronized void removeAll(Collection<ActionTransition> ts) {
00048                 transitions.removeAll(ts);
00049         }
00050         
00051         
00052         public synchronized Set<ActionTransition> getTransitionsFrom(Action a) {
00053                 
00054                 Set<ActionTransition> res = new HashSet<ActionTransition>();
00055                 
00056                 for(ActionTransition t: transitions) {
00057                         
00058                         if(t.from.equals(a))
00059                                 res.add(t);
00060                 }
00061                 return res;
00062         }
00063         
00064         
00065         public synchronized Set<ActionTransition> getTransitionsTo(Action a) {
00066                 
00067                 Set<ActionTransition> res = new HashSet<ActionTransition>();
00068                 
00069                 for(ActionTransition t: transitions) {
00070                         
00071                         if(t.to.equals(a))
00072                                 res.add(t);
00073                 }
00074                 return res;
00075         }
00076         
00077         
00078         public synchronized int getTreeLevel(Action a) {
00079 
00080                 int level = -1;
00081                 if(startAction != null) {
00082                         level = getTreeLevelRecursive(a, 0, new HashSet<Action>());
00083                 }
00084                 return level;
00085         }
00086         
00087         
00088         private synchronized int getTreeLevelRecursive(Action a, int level, Set<Action> visited) {
00089                 
00090                 if(a.equals(startAction)) {
00091                         return level;
00092                         
00093                 } else {
00094                         
00095                         ArrayList<Integer> levels = new ArrayList<Integer>();
00096                         for(ActionTransition trans : getTransitionsTo(a)) {
00097                                 
00098                                 if(!visited.contains(trans.from)) {
00099                                         visited.add(trans.from);
00100                                         levels.add(getTreeLevelRecursive(trans.from, ++level, visited));
00101                                 }
00102                         }
00103                         
00104                         if(levels.size()>0)
00105                                 return Collections.min(levels);
00106                         
00107                         // if no parent can be found in this branch, return large value
00108                         else return Integer.MAX_VALUE;
00109                 }
00110         }
00111         
00112         
00113         public synchronized Map<Integer, Vector<Action>> getTreeLevels() {
00114                 
00115                 HashMap<Integer, Vector<Action>> levels = new HashMap<Integer, Vector<Action>>();
00116                 for(ActionTransition t : transitions) {
00117                         
00118                         for(Action a : new Action[]{t.from, t.to}) {
00119                                 
00120                                 int l = getTreeLevel(a);
00121                                 if(!levels.containsKey(l)) {
00122                                         levels.put(l, new Vector<Action>());
00123                                 }
00124                                 if(!levels.get(l).contains(a))
00125                                         levels.get(l).add(a);
00126                         }
00127                 }
00128                 return levels;
00129         }
00130         
00135         public synchronized Map<Integer, Float> getLevelWidths() {
00136                 
00137                 Map<Integer, Float> levelWidths = new HashMap<Integer, Float>();
00138                 Map<Integer, Vector<Action>> levels = getTreeLevels();
00139                 
00140                 for(int l : levels.keySet()) {
00141                         
00142                         Vector<Action> acts = levels.get(l);
00143                         
00144                         float width = 2* ActionDrawInformation.SEQUENCE_BOX_PADDING;
00145                         for(Action a : acts) {
00146                                 width += a.getDrawInfo().getSimpleBoxDimension().x + ActionDrawInformation.SEQUENCE_BOX_PADDING;
00147                         }
00148                         levelWidths.put(l, width);
00149                 }
00150                 return levelWidths;
00151         }
00152         
00153 
00154         
00155         public Action getStartAction() {
00156                 return startAction;
00157         }
00158 
00159 
00160         public void setStartAction(Action startAction) {
00161                 this.startAction = startAction;
00162         }
00163 
00164         @Override
00165         public synchronized Iterator<ActionTransition> iterator() {
00166                 return transitions.iterator();
00167         }
00168 }


mod_vis
Author(s): Moritz Tenorth, Jakob Engel
autogenerated on Sat Dec 28 2013 17:09:49