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.Collections;
00005 import java.util.HashMap;
00006 import java.util.HashSet;
00007 import java.util.LinkedHashMap;
00008 import java.util.LinkedList;
00009 import java.util.List;
00010 import java.util.Map;
00011 import java.util.Set;
00012 import java.util.Vector;
00013
00014
00015 import edu.tum.cs.ias.knowrob.owl.OWLClass;
00016 import edu.tum.cs.ias.knowrob.owl.ObjectInstance;
00017 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00018 import edu.tum.cs.ias.knowrob.prolog.PrologQueryUtils;
00019
00020
00021 public class Action extends OWLClass {
00022
00026 private List<Action> sub_actions;
00027
00031 private ActionTransitions transitions;
00032
00036 private Action parentOfSequence = null;
00037
00041 private Action expandedSequence = null;
00042
00043
00048 private ActionDrawInformation drawInfo;
00049
00050
00051
00052
00059 protected Action(String iri, String label) {
00060
00061 super(iri, label);
00062 this.drawInfo = new ActionDrawInformation(this);
00063 this.sub_actions = Collections.synchronizedList(new ArrayList<Action>());
00064 this.transitions = new ActionTransitions();
00065
00066 }
00067
00068
00074 protected Action(OWLClass ind) {
00075
00076 this(ind.getIRI(), ind.getLabel());
00077
00078
00079 some_values_from.putAll(ind.getSomeValuesFrom());
00080 all_values_from.putAll(ind.getAllValuesFrom());
00081 has_value.putAll(ind.getHasValue());
00082 }
00083
00084
00094 public static Action getAction(String iri, String label) {
00095
00096
00097 if(identifiers.containsKey(iri) && identifiers.get(iri) instanceof Action) {
00098 return (Action) identifiers.get(iri);
00099 }
00100
00101
00102 Action res = new Action(OWLClass.getOWLClass(iri, label));
00103 identifiers.put(iri, res);
00104 return res;
00105 }
00106
00115 public static Action getAction(String iri) {
00116 return getAction(iri, null);
00117 }
00118
00119
00120
00121
00130 public Map<String, Vector<String>> getProperties() {
00131
00132 Map<String, Vector<String>> res = new LinkedHashMap<String, Vector<String>>();
00133
00134
00135 if(getSuperClasses()!=null && getSuperClasses().size() > 0) {
00136 res.put("type", new Vector<String>());
00137 for(OWLClass cl : getSuperClasses()) {
00138 res.get("type").add(cl.getIRI());
00139 }
00140 }
00141
00142 res.putAll(some_values_from);
00143 res.putAll(has_value);
00144
00145 return res;
00146 }
00147
00155 public List<String> getProperty(String key) {
00156
00157 if(some_values_from.containsKey(key)){
00158 return some_values_from.get(key);
00159 } else if(has_value.containsKey(key)) {
00160 return has_value.get(key);
00161 } else if (key.equals("type")){
00162
00163 Vector<String> res = new Vector<String>();
00164 for(OWLClass cl : superclasses) {
00165 res.add(cl.getLabel());
00166 }
00167 return res;
00168 } else {
00169 return null;
00170 }
00171
00172 }
00173
00174
00175
00180 public ActionDrawInformation getDrawInfo() {
00181 return drawInfo;
00182 }
00183
00184
00190 public List<Action> getSubActions() {
00191 return sub_actions;
00192 }
00193
00194 public List<Action> getSubActionsRecursive() {
00195
00196 List<Action> res = new ArrayList<Action>();
00197 res.addAll(getSubActions());
00198
00199 for(Action sub : getSubActions()) {
00200 res.addAll(sub.getSubActionsRecursive());
00201 }
00202 return res;
00203 }
00204
00205
00211 public int getSubActionsCount() {
00212 return sub_actions.size();
00213 }
00214
00220 public void addSubAction(Action sub_action) {
00221
00222 synchronized(sub_actions) {
00223 this.sub_actions.add(sub_action);
00224 }
00225 sub_action.parentOfSequence = this;
00226 drawInfo.notifyModified();
00227 }
00228
00234 public void addSubActions(LinkedList<Action> sub_actions) {
00235
00236 synchronized(sub_actions) {
00237 this.sub_actions.addAll(sub_actions);
00238 }
00239
00240 for (Action s : sub_actions) {
00241 s.parentOfSequence = this;
00242 }
00243
00244 drawInfo.notifyModified();
00245 }
00246
00247
00253 public void removeSubAction(Action sub_action) {
00254
00255
00256 if(sub_actions.contains(sub_action)) {
00257
00258 sub_actions.remove(sub_action);
00259
00260
00261 if(has_value.containsKey("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState") &&
00262 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState").contains(sub_action.getIRI())) {
00263
00264
00265 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState").remove(sub_action.getIRI());
00266
00267 if(sub_action.getTransitions()!=null &&
00268 sub_action.getTransitions().getTransitionsFrom(sub_action)!=null) {
00269
00270 Set<ActionTransition> trans = sub_action.getTransitions().getTransitionsFrom(sub_action);
00271
00272
00273 for(ActionTransition t : trans) {
00274 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState").add(t.to.getIRI());
00275 break;
00276 }
00277 }
00278 }
00279
00280 if(has_value.containsKey("http://ias.cs.tum.edu/kb/knowrob.owl#taskEndState") &&
00281 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskEndState").contains(sub_action.getIRI())) {
00282
00283
00284 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskEndState").remove(sub_action.getIRI());
00285
00286
00287 if(getTransitionsRecursive()!=null &&
00288 getTransitionsRecursive().getTransitionsTo(sub_action)!=null) {
00289
00290 Set<ActionTransition> trans = getTransitionsRecursive().getTransitionsTo(sub_action);
00291
00292
00293 for(ActionTransition t : trans) {
00294 has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskEndState").add(t.from.getIRI());
00295 }
00296 }
00297 }
00298
00299 } else {
00300
00301 for(Action sub : sub_actions) {
00302 sub.removeSubAction(sub_action);
00303 }
00304 }
00305
00306 drawInfo.notifyModified();
00307 }
00308
00309
00310 public void addTransition(ActionTransition t) {
00311 this.transitions.add(t);
00312 }
00313
00314 public void removeTransition(ActionTransition t) {
00315 this.transitions.remove(t);
00316 }
00317
00318 public ActionTransitions getTransitions() {
00319 return transitions;
00320 }
00321
00322
00323 public ActionTransitions getTransitionsRecursive() {
00324
00325 ActionTransitions res = new ActionTransitions();
00326
00327 res.addAll(transitions);
00328
00329 synchronized (sub_actions) {
00330 for(Action sub : sub_actions) {
00331 res.addAll(sub.getTransitionsRecursive());
00332 }
00333 }
00334 res.startAction = transitions.startAction;
00335 return res;
00336 }
00337
00342 public boolean isExpanded() {
00343 if(parentOfSequence!=null)
00344 return (parentOfSequence.expandedSequence == this);
00345 else
00346 return false;
00347 }
00348
00353 public Action getExpandedSequence() {
00354 return expandedSequence;
00355 }
00356
00361 public void setExpandedSequence(Action sequence) {
00362 if (this.sub_actions.contains(sequence))
00363 {
00364 this.expandedSequence = sequence;
00365 if (parentOfSequence != null)
00366 parentOfSequence.drawInfo.notifyModified();
00367 }
00368 }
00369
00373 public void toggleExpand()
00374 {
00375 if (parentOfSequence.expandedSequence == this)
00376 parentOfSequence.expandedSequence = null;
00377 else
00378 parentOfSequence.expandedSequence = this;
00379 parentOfSequence.drawInfo.notifyModified();
00380 }
00381
00382
00383
00387 public void readFromProlog() {
00388
00389 if(isReadFromProlog())
00390 return;
00391
00392
00393 super.readFromProlog();
00394
00395
00396 some_values_from.remove("http://ias.cs.tum.edu/kb/knowrob.owl#subAction");
00397 has_value.remove("http://ias.cs.tum.edu/kb/knowrob.owl#stateTransition");
00398
00399
00400
00401
00402 if(has_value.containsKey("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState")) {
00403 transitions.setStartAction(Action.getAction(has_value.get("http://ias.cs.tum.edu/kb/knowrob.owl#taskStartState").firstElement()));
00404 }
00405
00406
00407 try {
00408
00409 HashMap<String, Vector<String>> qSub = PrologInterface.executeQuery("plan_subevents('"+iri+"',List)");
00410
00411 if(qSub!=null) {
00412
00413 Vector<String> list = qSub.get("List");
00414
00415
00416 HashSet<String> alreadyAdded = new HashSet<String>();
00417
00418 if(list != null) {
00419
00420 for(String o : list) {
00421 for (String sArr[] : PrologInterface.dottedPairsToArrayList(o)) {
00422 for (String s : sArr) {
00423
00424 s = PrologInterface.removeSingleQuotes(s);
00425
00426 if (alreadyAdded.contains(s))
00427 continue;
00428 alreadyAdded.add(s);
00429
00430 Action sub = Action.getAction(s);
00431 sub.readFromProlog();
00432 this.addSubAction(sub);
00433 }
00434 }
00435 }
00436 }
00437 }
00438
00439 } catch (Exception e) {
00440 e.printStackTrace();
00441 }
00442
00443
00444
00445
00446 try {
00447
00448 HashMap<String, Vector<String>> qTrans =
00449 PrologInterface.executeQuery("class_properties_value('"+iri+"', 'http://ias.cs.tum.edu/kb/knowrob.owl#stateTransition', Trans)");
00450
00451 if(qTrans!=null && qTrans.get("Trans").size()>0) {
00452
00453 for(String t : qTrans.get("Trans")) {
00454
00455 t = PrologInterface.removeSingleQuotes(t);
00456
00457 ActionTransition trans = ActionTransition.getActionTransition(t);
00458 trans.readFromProlog();
00459 trans.getFrom().addTransition(trans);
00460 }
00461 }
00462
00463 } catch (Exception e) {
00464 e.printStackTrace();
00465 }
00466
00467
00468 this.setReadFromProlog(true);
00469 }
00470
00471
00472
00476 public void writeToProlog() {
00477
00478 if(this.needsSaveToProlog()) {
00479
00480
00481
00482 PrologQueryUtils.clearActionStateTransitions(iri);
00483
00484
00485
00486 super.writeToProlog();
00487
00488
00489 for(Action sub : sub_actions) {
00490 for(ActionTransition t : sub.getTransitions()) {
00491
00492
00493 String trans = PrologQueryUtils.createStateTransition(t.from.getIRI(), t.to.getIRI(), t.getCause());
00494 PrologQueryUtils.createRestriction(iri, "http://ias.cs.tum.edu/kb/knowrob.owl#stateTransition", trans, "http://www.w3.org/2002/07/owl#hasValue", "knowrob_java");
00495 }
00496 }
00497
00498 this.setSaveToProlog(false);
00499 }
00500
00501
00502 for(Action sub : sub_actions) {
00503 sub.writeToProlog();
00504
00505
00506 PrologQueryUtils.createRestriction(iri, "http://ias.cs.tum.edu/kb/knowrob.owl#subAction", sub.getIRI(), "http://www.w3.org/2002/07/owl#someValuesFrom", "knowrob_java");
00507 }
00508 }
00509 }