$search
00001 package edu.tum.cs.ias.knowrob.vis.applets; 00002 00003 import java.awt.GridLayout; 00004 import java.awt.event.MouseListener; 00005 import java.awt.event.MouseMotionListener; 00006 import java.io.FileInputStream; 00007 import java.io.ObjectInputStream; 00008 import java.util.*; 00009 00010 import javax.swing.JFrame; 00011 00012 import controlP5.ControlP5; 00013 import controlP5.ControlWindow; 00014 import controlP5.Textarea; 00015 import controlP5.Textfield; 00016 import controlP5.ControlEvent; 00017 import edu.tum.cs.ias.knowrob.prolog.PrologInterface; 00018 00019 import processing.core.*; 00020 import jpl.Query; 00021 public class PrologVisualizationCanvas extends PApplet implements MouseListener, MouseMotionListener, MapObjClickListener { 00022 00023 private static final long serialVersionUID = 4575739930038583994L; 00024 public enum Part {KITCHEN_VIS, ACTION_VIS, VIDEO, ALL}; 00025 00029 private boolean debugMode = false; 00030 00031 00032 // components 00033 protected ActionVisApplet AVObject; 00034 protected SemanticMapVisApplet KVObject; 00035 public ControlP5 controlP5; 00036 protected ControlWindow controlWindow; 00037 //public ImageViewerApplet iviewer; 00038 public JFrame img_window; 00039 00040 00041 public PrologVisualizationCanvas() { 00042 00043 img_window = null; 00044 00045 // not required when calling from Prolog 00046 //initProlog(); 00047 this.init(); 00048 this.setSize(560, 620); 00049 00050 AVObject = new ActionVisApplet(); 00051 AVObject.init(); 00052 AVObject.setSize(550, 600); 00053 AVObject.setPrologVisCanvas(this); 00054 00055 00056 KVObject = new SemanticMapVisApplet(); 00057 KVObject.init(); 00058 KVObject.setSize(720, 600); 00059 KVObject.setMapObjClickListener(this); 00060 00061 00062 this.add(KVObject); 00063 //this.add(AVObject); 00064 00065 // deactivated control window (used for outdated action visualization only) 00066 // initControlP5(); 00067 00068 00069 this.draw(); 00070 this.setVisible(true); 00071 this.setSize(1270, 620); 00072 00073 if(debugMode) { 00074 (new Thread(new testDataLoader(this))).start(); 00075 } 00076 00077 } 00078 00079 public void draw() { 00080 background(20, 20, 20); 00081 } 00082 00083 00088 public void displayFixed(String identifier) { 00089 KVObject.displayActionFixedIdent(addSingleQuotes(identifier)); 00090 KVObject.redraw(); 00091 } 00092 00097 public void displayAction(String identifier) { 00098 KVObject.displayAction(addSingleQuotes(identifier)); 00099 KVObject.redraw(); 00100 } 00101 00106 public void displayEyeTrajectory(String identifier) { 00107 KVObject.displayEyeTrajectory(addSingleQuotes(identifier)); 00108 KVObject.redraw(); 00109 } 00110 00111 public void displayHumanTrajectory(String identifier, String handUsed){ 00112 KVObject.displayHumanTrajectory(addSingleQuotes(identifier), addSingleQuotes(handUsed)); 00113 KVObject.redraw(); 00114 } 00115 00122 public void setActionInformation(String[][][] pl_list, String hand, int level) { 00123 if(AVObject != null) 00124 AVObject.setActionInformation(pl_list, hand, level); 00125 KVObject.redraw(); 00126 } 00127 00131 public void clear(){ 00132 KVObject.clear(); 00133 KVObject.redraw(); 00134 } 00135 00141 public void drawBackground(){ 00142 KVObject.drawBackground(); 00143 KVObject.redraw(); 00144 } 00145 00146 00151 public void addObject(String identifier){ 00152 KVObject.addObject(addSingleQuotes(identifier)); 00153 KVObject.redraw(); 00154 } 00155 00162 public void addObjectWithChildren(String identifier) { 00163 KVObject.addObjectWithChildren(addSingleQuotes(identifier)); 00164 KVObject.redraw(); 00165 } 00166 00171 public void addTrajectory(String[] identifiers){ 00172 00173 for(String identifier : identifiers) { 00174 00175 addObject(identifier); 00176 00177 // draw in yellow 00178 KVObject.highlightItem(addSingleQuotes(identifier), true, SemanticMapVisApplet.convertColor(255, 221, 0, 255)); 00179 00180 } 00181 KVObject.redraw(); 00182 } 00183 00188 public void removeObject(String identifier){ 00189 KVObject.removeObject(addSingleQuotes(identifier)); 00190 KVObject.redraw(); 00191 } 00192 00193 00200 public void removeObjectWithChildren(String identifier) { 00201 KVObject.removeObjectWithChildren(addSingleQuotes(identifier)); 00202 KVObject.redraw(); 00203 } 00204 00210 public void highlight(String identifier, boolean highlight) { 00211 KVObject.highlightItem(addSingleQuotes(identifier),highlight); 00212 KVObject.redraw(); 00213 } 00214 public void highlight(String identifier, boolean highlight, int color) { 00215 KVObject.highlightItem(addSingleQuotes(identifier),highlight, color); 00216 KVObject.redraw(); 00217 } 00218 public void highlight(String identifier, boolean highlight, int r, int g, int b) { 00219 00220 int c = (((((255 << 8) + r) << 8) + g) << 8) + b; 00221 KVObject.highlightItem(addSingleQuotes(identifier),highlight, c); 00222 KVObject.redraw(); 00223 00224 } 00225 public void highlight(String identifier, boolean highlight, int r, int g, int b, String prob) { 00226 00227 // show prob by alpha channel 00228 int alpha = (int) (255 * Float.valueOf(prob)); 00229 int c = (((((alpha << 8) + r) << 8) + g) << 8) + b; 00230 00231 // show prob by hue 00232 //int c = HSVtoRGB (1-Float.valueOf(prob), 1.0f, 1.0f)+ 0xFF000000; 00233 00234 KVObject.highlightItem(addSingleQuotes(identifier),highlight, c); 00235 KVObject.redraw(); 00236 } 00241 int HSVtoRGB (float h, float s, float v) { 00242 // h,s,v in [0,1] 00243 float rr = 0, gg = 0, bb = 0; 00244 float hh = (6 * h) % 6; 00245 int c1 = (int) hh; 00246 float c2 = hh - c1; 00247 float x = (1 - s) * v; 00248 float y = (1 - (s * c2)) * v; 00249 float z = (1 - (s * (1 - c2))) * v; 00250 switch (c1) { 00251 case 0: rr=v; gg=z; bb=x; break; 00252 case 1: rr=y; gg=v; bb=x; break; 00253 case 2: rr=x; gg=v; bb=z; break; 00254 case 3: rr=x; gg=y; bb=v; break; 00255 case 4: rr=z; gg=x; bb=v; break; 00256 case 5: rr=v; gg=x; bb=y; break; 00257 } 00258 int N = 256; 00259 int r = Math.min(Math.round(rr*N),N-1); 00260 int g = Math.min(Math.round(gg*N),N-1); 00261 int b = Math.min(Math.round(bb*N),N-1); 00262 // create int-packed RGB-color: 00263 int rgb = ((r&0xff)<<16) | ((g&0xff)<<8) | b&0xff; 00264 return rgb; 00265 } 00266 00272 public void highlightWithChildren(String identifier, boolean highlight) { 00273 KVObject.highlightReachable(addSingleQuotes(identifier),highlight); 00274 KVObject.redraw(); 00275 } 00276 00277 00281 public void clearHighlight() { 00282 KVObject.clearHighlights(); 00283 KVObject.redraw(); 00284 } 00285 00286 00291 public void mapObjectClicked(String identifier) { 00292 displayInformationForEntity(addSingleQuotes(identifier)); 00293 KVObject.redraw(); 00294 } 00295 00296 00297 public void setViewParameters(float xShift, float yShift,float xRot, float yRot, float zoom) { 00298 KVObject.setViewParameters(xShift, yShift, xRot, yRot, zoom); 00299 KVObject.redraw(); 00300 } 00301 00302 public void showImagesInNewWindow(String[] imgs) { 00303 00304 img_window = new JFrame(); 00305 img_window.setLayout(new GridLayout(1,imgs.length)); 00306 00307 int frameWidth = 0; 00308 int frameHeight = 30; 00309 00310 int totalWidth = frameWidth; 00311 int totalHeight = frameHeight; 00312 00313 for(String img : imgs) { 00314 img = img.replaceAll( "\"", "" ); //remove "" 00315 00316 ImageViewerApplet iviewer = new ImageViewerApplet(img); 00317 iviewer.init(); 00318 //iviewer.setImage(img); 00319 img_window.getContentPane().add(iviewer); 00320 iviewer.setSize(iviewer.width,iviewer.height); 00321 totalWidth += iviewer.width; 00322 totalHeight = max(iviewer.height + frameHeight, totalHeight); 00323 } 00324 00325 img_window.setSize(totalWidth, totalHeight); 00326 img_window.pack(); 00327 img_window.setVisible(true); 00328 img_window.setResizable(true); 00329 } 00330 00331 00335 // 00336 // USER INTERFACE 00337 // 00338 00339 @SuppressWarnings("unused") 00340 private ArrayList<String[]> readInformationForEntity(String entity) { 00341 00342 // read list of attributes from Prolog 00343 ArrayList<String[]> bindings = new ArrayList<String[]>(); 00344 HashMap<String, Vector<String>> qres = PrologInterface.executeQuery("findall([P|O], (rdf_has("+entity+", P, O)), Cs)"); 00345 00346 if(qres!=null) { 00347 for(String k : qres.keySet()) { 00348 00349 Vector<String> res = qres.get(k); 00350 00351 for(String o : res) { 00352 00353 String[] list = o.split("'\\.'", 2); 00354 if(list.length<2) continue; 00355 String rest = list[1]; 00356 00357 bindings.addAll(dottedPairsToArrayList(rest)); 00358 } 00359 } 00360 } 00361 return bindings; 00362 } 00363 00364 public void actionsInActivity() { 00365 HashMap<String, Vector<String>> qres = PrologInterface.executeQuery("rdf_has(Plan, rdfs:label, literal(type('http://www.w3.org/2001/XMLSchema#string', 'set a table'))), " + 00366 "comp_ehow:matching_actions(Plan, Act)"); 00367 if(qres!=null) { 00368 Vector<String> act = qres.get("Act"); 00369 00370 if(act != null) { 00371 00372 //System.out.println(tmp.toString()); 00373 this.AVObject.setActionsInActivity(act.toArray()); 00374 } 00375 } 00376 } 00377 00378 private void displayInformationForEntity(String entity) { 00379 00380 HashMap<String, Vector<String>> qres = PrologInterface.executeQuery("rdf_has("+entity+", P, O)"); 00381 00382 if(qres!=null) { 00383 Vector<String> P = qres.get("P"); 00384 Vector<String> O = qres.get("O"); 00385 String info = ""; 00386 if(P != null && O != null) 00387 for(int i=0;i<P.size() && i<O.size();i++) { 00388 info += printKey(P.get(i)) + ": " + printValue(O.get(i)) + "\n"; 00389 } 00390 00391 if(controlP5!=null && controlP5.getController("CurrentAction")!=null) 00392 ((Textfield) controlP5.getController("CurrentAction")).setValue(printKey(entity)); 00393 if(controlP5!=null && controlP5.getGroup("CurrentAttributes")!=null) 00394 ((Textarea) controlP5.getGroup("CurrentAttributes")).setText(info); 00395 00396 System.out.println(info); 00397 } 00398 00399 } 00400 00401 private String printKey(String k) { 00402 String[] ks = k.split("#"); 00403 if(ks.length>1) { 00404 String res = ks[1].replaceAll("'", ""); 00405 return res; 00406 } 00407 else return k; 00408 } 00409 00410 private String printValue(String v) { 00411 if (v.contains("#")) { 00412 String[]vs=v.split("#"); 00413 return vs[1].replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", ""); 00414 00415 } else if(!v.startsWith("literal")) { 00416 return v.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", ""); 00417 00418 } else { 00419 // extract the literal string 00420 String v1 = v.replaceFirst("literal\\(type\\('http://www\\.w3\\.org/2001/XMLSchema", ""); 00421 return v1.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", ""); 00422 } 00423 } 00424 00425 // 00426 // initialize control window for showing action/object properties (currently unused) 00427 // 00428 // private void initControlP5() { 00429 // 00430 // controlP5 = new ControlP5(KVObject); 00431 // controlWindow = controlP5.addControlWindow("controlP5window",660,230,400,350); 00432 // controlWindow.setBackground(color(40)); 00433 // controlWindow.setUpdateMode(ControlWindow.NORMAL); 00434 // controlWindow.frameRate(5); 00435 // 00436 // Textlabel act_label = controlP5.addTextlabel("act_label","Abstraction Level",80,40); 00438 // act_label.setFont(3); 00439 // act_label.moveTo(controlWindow); 00440 // 00441 // RadioButton act_radio = controlP5.addRadioButton("act_radio", 80, 60); 00442 // act_radio.addItem("activity",3); 00443 // act_radio.addItem("2",2); 00444 // act_radio.addItem("1",1); 00445 // act_radio.addItem("0",0); 00446 // act_radio.moveTo(controlWindow); 00447 // act_radio.setId(100); 00448 // 00449 // Textlabel color_label = controlP5.addTextlabel("color_label","Colors",220,40); 00451 // color_label.setFont(3); 00452 // color_label.moveTo(controlWindow); 00453 // 00454 // RadioButton color_radio = controlP5.addRadioButton("color_radio",220,60); 00455 // color_radio.addItem("by action type",0); 00456 // color_radio.addItem("by object",1); 00457 // color_radio.addItem("missing in activity",2); 00458 // color_radio.moveTo(controlWindow); 00459 // color_radio.setId(101); 00460 // 00461 // controlP5.addTextfield("CurrentAction",80,120,200,20).moveTo(controlWindow); 00462 // Textarea text = controlP5.addTextarea("CurrentAttributes","", 80,160,200,130); 00463 // text.setColorBackground(0xFF000000); 00464 // text.moveTo(controlWindow); 00465 // 00466 // controlWindow.hide(); 00467 // 00468 // } 00469 00470 public void controlEvent(ControlEvent e) { 00471 00472 if(e.getController().getId()==101 && e.getValue()==2.0) 00473 actionsInActivity(); 00474 00475 } 00476 00477 00478 private ArrayList<String[]> dottedPairsToArrayList(String rest) { 00479 ArrayList<String[]> bindings = new ArrayList<String[]>(); 00480 while(rest.length()>0) { 00481 String[] l = rest.split("'\\.'", 2); 00482 00483 if((l[0].equals("")) || (l[0].equals("("))) { 00484 if(l[0].startsWith("(")) { 00485 rest=l[1]; continue; 00486 00487 } else break; 00488 } else { 00489 bindings.add(l[0].substring(1, l[0].length()-2).split(", ")); 00490 if(l.length>1) { 00491 rest=l[1]; continue; 00492 } else break; 00493 } 00494 00495 } 00496 return bindings; 00497 } 00498 00499 00500 public static String removeSingleQuotes(String str) { 00501 if(str.startsWith("'")) 00502 str = str.substring(1); 00503 00504 if(str.endsWith("'")) 00505 str = str.substring(0, str.length()-1); 00506 return str; 00507 } 00508 00509 public static String addSingleQuotes(String str) { 00510 return "'"+removeSingleQuotes(str)+"'"; 00511 } 00512 00513 00514 public SemanticMapVisApplet getKitchenVisApplet() { 00515 return this.KVObject; 00516 } 00517 00518 public static void main(String args[]) { 00519 PApplet.main(new String[] { "de.tum.in.fipm.kipm.gui.visualisation.base.PrologVisualizationCanvas" }); 00520 } 00521 } 00522 00523 class testDataLoader implements Runnable { 00524 00525 PrologVisualizationCanvas c; 00526 public testDataLoader(PrologVisualizationCanvas c) { 00527 this.c = c; 00528 } 00529 00530 @SuppressWarnings("unchecked") 00531 public void run() { 00532 00533 try{ 00534 Thread.sleep(20000); 00535 }catch(Exception e){}; 00536 new Query("initCmdProlog").oneSolution(); 00537 new Query("ensure_loaded('C:/Projects/Eclipse2/kipm/demo.pl')").oneSolution(); 00538 00539 new Query("ddLeft").oneSolution(); 00540 try{ 00541 FileInputStream fos = new FileInputStream("C:/Projects/Eclipse2/kipm/tmpDataLeft.ser"); 00542 ObjectInputStream oos = new ObjectInputStream(fos); 00543 00544 ArrayList<Object[]> o = (ArrayList<Object[]>)oos.readObject(); 00545 00546 for(Object[] obj : o){ 00547 c.setActionInformation((String[][][])obj[0], (String)obj[1], (Integer)obj[2]); 00548 } 00549 00550 oos.close(); 00551 }catch(Exception e){} 00552 00553 00554 new Query("ddRight").oneSolution(); 00555 try{ 00556 FileInputStream fos = new FileInputStream("C:/Projects/Eclipse2/kipm/tmpDataRight.ser"); 00557 ObjectInputStream oos = new ObjectInputStream(fos); 00558 00559 ArrayList<Object[]> o = (ArrayList<Object[]>)oos.readObject(); 00560 00561 for(Object[] obj : o){ 00562 c.setActionInformation((String[][][])obj[0], (String)obj[1], (Integer)obj[2]); 00563 } 00564 00565 oos.close(); 00566 }catch(Exception e){} 00567 00568 00569 } 00570 00571 }