00001 package edu.tum.cs.ias.knowrob.vis.actions;
00002
00003 import java.util.HashMap;
00004 import java.util.HashSet;
00005 import java.util.Vector;
00006
00007 import javax.vecmath.Vector2f;
00008 import processing.core.PApplet;
00009
00010 import edu.tum.cs.ias.knowrob.owl.OWLClass;
00011 import edu.tum.cs.ias.knowrob.owl.OWLIndividual;
00012 import edu.tum.cs.ias.knowrob.owl.OWLThing;
00013 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00014 import edu.tum.cs.ias.knowrob.vis.applets.PlanVisAppletFsm;
00015
00016 public class ActionTransition extends OWLIndividual {
00017
00018 Action from;
00019 Action to;
00020 String caused_by;
00021
00022 Vector2f fromPosition;
00023 Vector2f toPosition;
00024
00025
00026
00027
00032 boolean active = false;
00033
00034
00041 protected ActionTransition(String iri, String label) {
00042
00043 super(iri, label);
00044
00045 }
00046
00047
00053 protected ActionTransition(OWLIndividual ind) {
00054
00055 this(ind.getIRI(), ind.getLabel());
00056 this.types.addAll(ind.getTypes());
00057 this.data_props.putAll(ind.getDataProperties());
00058 this.obj_props.putAll(ind.getObjProperties());
00059 }
00060
00061
00071 public static ActionTransition getActionTransition(String iri, String label) {
00072
00073
00074 if(identifiers.containsKey(iri) && identifiers.get(iri) instanceof ActionTransition) {
00075 return (ActionTransition) identifiers.get(iri);
00076 }
00077
00078
00079 ActionTransition res = new ActionTransition(OWLIndividual.getOWLIndividual(iri, label));
00080 identifiers.put(iri, res);
00081 return res;
00082 }
00083
00092 public static ActionTransition getActionTransition(String iri) {
00093 return getActionTransition(iri, null);
00094 }
00095
00104 public static ActionTransition getActionTransition(String iri, Action from, Action to, String type) {
00105 ActionTransition res = getActionTransition(iri, null);
00106 res.setFrom(from);
00107 res.setTo(to);
00108 res.setCause(type);
00109 return res;
00110 }
00111
00112
00119 public void readFromProlog() {
00120
00121
00122 try {
00123
00124 HashMap<String, Vector<String>> qProp =
00125 PrologInterface.executeQuery("owl_has('" + iri + "', Prop, Val)");
00126
00127
00128 if(qProp != null) {
00129
00130 Vector<String> prop = qProp.get("Prop");
00131 Vector<String> val = qProp.get("Val");
00132
00133
00134
00135
00136
00137 HashSet<String> alreadyAdded = new HashSet<String>();
00138 if(prop != null && val != null)
00139
00140 for(int i=0;i<prop.size() && i<val.size();i++) {
00141
00142 if (alreadyAdded.contains(prop.get(i)+val.get(i)))
00143 continue;
00144
00145 alreadyAdded.add(prop.get(i)+val.get(i));
00146 String p = OWLThing.removeSingleQuotes(prop.get(i));
00147 String v = OWLThing.removeSingleQuotes(val.get(i));
00148
00149 if(p.endsWith("fromState")) {
00150
00151 if(!obj_props.containsKey(p))
00152 obj_props.put(p, new Vector<String>());
00153 this.obj_props.get(p).add(v);
00154
00155 from = Action.getAction(v);
00156
00157 }else if(p.endsWith("toState")) {
00158
00159 if(!obj_props.containsKey(p))
00160 obj_props.put(p, new Vector<String>());
00161 this.obj_props.get(p).add(v);
00162
00163 to = Action.getAction(v);
00164
00165
00166 } else if(p.endsWith("causedBy")) {
00167 this.caused_by=v;
00168
00169 } else if(p.endsWith("type")) {
00170 this.types.add(OWLClass.getOWLClass(v));
00171 }
00172
00173 }
00174 }
00175
00176 } catch (Exception e) {
00177 e.printStackTrace();
00178 }
00179
00180
00181 }
00182
00183
00184
00185 public Action getFrom() {
00186 return from;
00187 }
00188
00189
00190 public void setFrom(Action from) {
00191 this.from = from;
00192 }
00193
00194
00195 public Action getTo() {
00196 return to;
00197 }
00198
00199
00200 public void setTo(Action to) {
00201 this.to = to;
00202 }
00203
00204
00205 public String getType() {
00206
00207 if(!types.isEmpty())
00208 return types.firstElement().getShortName();
00209
00210 else return null;
00211 }
00212
00213
00214 public void setType(String type) {
00215
00216 if(type.startsWith("http")) {
00217 this.types.add(OWLClass.getOWLClass(type));
00218 } else {
00219 if(OWLThing.getOWLThingByShortname(type) != null)
00220 this.types.add(OWLClass.getOWLClass(OWLThing.getOWLThingByShortname(type).getIRI()));
00221 }
00222 }
00223
00224
00225 public void drawConnection(PApplet app) {
00226
00227
00228 if(this.caused_by != null) {
00229
00230 if(this.caused_by.endsWith("OK")) {
00231
00232 if(active) {
00233 app.stroke(app.color(60, 140, 60));
00234 app.fill(app.color(80, 180, 80));
00235 } else {
00236 app.stroke(app.color(60, 100, 60));
00237 app.fill(app.color(80, 120, 80));
00238 }
00239
00240 } else if(this.caused_by.endsWith("CONDITION_TRUE")) {
00241 if(active) {
00242 app.stroke(app.color(20, 140, 140));
00243 app.fill(app.color(80, 180, 190));
00244 } else {
00245 app.stroke(app.color(60, 100, 100));
00246 app.fill(app.color(80, 120, 130));
00247 }
00248
00249 } else if(this.caused_by.endsWith("CONDITION_FALSE")) {
00250 if(active) {
00251 app.stroke(app.color(10, 120, 120));
00252 app.fill(app.color(40, 140, 150));
00253 } else {
00254 app.stroke(app.color(30, 60, 60));
00255 app.fill(app.color(40, 90, 100));
00256 }
00257
00258 } else if(this.caused_by.endsWith("ERROR")) {
00259 if(active) {
00260 app.stroke(app.color(130, 30, 30));
00261 app.fill(app.color(140, 70, 70));
00262 } else {
00263 app.stroke(app.color(90, 30, 30));
00264 app.fill(app.color(100, 50, 50));
00265 }
00266
00267 } else if(this.caused_by.endsWith("OUT_OF_RESOURCES")) {
00268 if(active) {
00269 app.stroke(app.color(140, 20, 20));
00270 app.fill(app.color(180, 80, 80));
00271 } else {
00272 app.stroke(app.color(100, 60, 60));
00273 app.fill(app.color(120, 80, 80));
00274 }
00275
00276 } else if(this.caused_by.endsWith("ABORT")) {
00277 if(active) {
00278 app.stroke(app.color(140, 20, 140));
00279 app.fill(app.color(190, 80, 180));
00280 } else {
00281 app.stroke(app.color(100, 60, 100));
00282 app.fill(app.color(130, 80, 120));
00283 }
00284
00285 } else if(this.caused_by.endsWith("TIMEOUT")) {
00286 if(active) {
00287 app.stroke(app.color(160, 140, 20));
00288 app.fill(app.color(220, 180, 80));
00289 } else {
00290 app.stroke(app.color(120, 100, 60));
00291 app.fill(app.color(160, 120, 80));
00292 }
00293 }
00294 } else {
00295 if(active) {
00296 app.stroke(app.color(120));
00297 app.fill(app.color(150));
00298 } else {
00299 app.stroke(app.color(80));
00300 app.fill(app.color(100));
00301 }
00302 }
00303
00304
00305 this.fromPosition = this.from.getDrawInfo().getOutboundConnectorPos();
00306 this.toPosition = new Vector2f(to.getDrawInfo().getInboundConnectorPos().x,
00307 to.getDrawInfo().getInboundConnectorPos().y-5);
00308
00309 PlanVisAppletFsm.arrowFromTo(app, fromPosition, toPosition, 5, -1);
00310
00311 }
00312
00313 public boolean checkPosInArrow(Vector2f pos, PApplet app) {
00314
00315 Vector2f[] vertices = PlanVisAppletFsm.getArrowVertices(app, fromPosition, toPosition, 5f, -1f).toArray(new Vector2f[]{});
00316
00317 int i, j=vertices.length-1;
00318 int sides = vertices.length;
00319 boolean oddNodes = false;
00320
00321
00322 for (i=0; i<sides; i++) {
00323
00324 if ((vertices[i].y < pos.y && vertices[j].y >= pos.y ||
00325 vertices[j].y < pos.y && vertices[i].y >= pos.y) && (vertices[i].x <= pos.x ||
00326 vertices[j].x <= pos.x)) {
00327
00328 oddNodes^=(vertices[i].x + (pos.y-vertices[i].y)/(vertices[j].y - vertices[i].y)*(vertices[j].x-vertices[i].x)<pos.x);
00329 }
00330 j=i;
00331 }
00332 return oddNodes;
00333
00334 }
00335
00336 public boolean isActive() {
00337 return active;
00338 }
00339
00340 public void setActive(boolean b) {
00341 active = b;
00342 }
00343
00344 public void toggleActive() {
00345 active = !active;
00346 }
00347
00348 public String getCause() {
00349 return caused_by;
00350 }
00351 public void setCause(String cause) {
00352 caused_by = cause;
00353 }
00354
00355
00356 }