00001 package de.tum.in.fipm.kipm.gui.visualisation.base;
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.Radio;
00015 import controlP5.Textarea;
00016 import controlP5.Textfield;
00017 import controlP5.ControlListener;
00018 import controlP5.ControlEvent;
00019 import controlP5.Textlabel;
00020 import de.tum.in.fipm.kipm.gui.visualisation.applets.ActionVisApplet;
00021 import de.tum.in.fipm.kipm.gui.visualisation.applets.StandaloneKitchenVisApplet;
00022 import de.tum.in.fipm.kipm.gui.visualisation.applets.ImageViewerApplet;
00023
00024 import processing.core.*;
00025 import jpl.Query;
00026 import jpl.fli.Prolog;
00027
00028 public class PrologVisualizationCanvas extends PApplet implements MouseListener, MouseMotionListener {
00029
00030 private static final long serialVersionUID = 4575739930038583994L;
00031 public enum Part {KITCHEN_VIS, ACTION_VIS, VIDEO, ALL};
00032
00036 private boolean debugMode = false;
00037
00038
00039
00040 protected ActionVisApplet AVObject;
00041 protected StandaloneKitchenVisApplet KVObject;
00042 public ControlP5 controlP5;
00043 protected ControlWindow controlWindow;
00044
00045 public JFrame img_window;
00046
00047
00048 public PrologVisualizationCanvas() {
00049
00050 img_window = null;
00051
00052
00053
00054 this.init();
00055 this.setSize(560, 620);
00056
00057 AVObject = new ActionVisApplet();
00058 AVObject.init();
00059 AVObject.setSize(550, 600);
00060 AVObject.setPrologVisCanvas(this);
00061
00062
00063 KVObject = new StandaloneKitchenVisApplet();
00064 KVObject.init();
00065 KVObject.setSize(720, 600);
00066 KVObject.setPrologVisCanvas(this);
00067
00068
00069 this.add(KVObject);
00070 this.add(AVObject);
00071
00072 initControlP5();
00073
00074
00075 this.draw();
00076 this.setVisible(true);
00077 this.setSize(1270, 620);
00078
00079 if(debugMode) {
00080 (new Thread(new testDataLoader(this))).start();
00081 }
00082
00083 }
00084
00085 public void draw() {
00086 background(20, 20, 20);
00087 }
00088
00089
00094 public void displayFixed(String identifier) {
00095 KVObject.displayActionFixedIdent(addSingleQuotes(identifier));
00096 KVObject.redraw();
00097 }
00098
00103 public void displayAction(String identifier) {
00104 KVObject.displayAction(addSingleQuotes(identifier));
00105 KVObject.redraw();
00106 }
00107
00112 public void displayEyeTrajectory(String identifier) {
00113 KVObject.displayEyeTrajectory(addSingleQuotes(identifier));
00114 KVObject.redraw();
00115 }
00116
00117 public void displayHumanTrajectory(String identifier, String handUsed){
00118 KVObject.displayHumanTrajectory(addSingleQuotes(identifier), addSingleQuotes(handUsed));
00119 KVObject.redraw();
00120 }
00121
00128 public void setActionInformation(String[][][] pl_list, String hand, int level) {
00129 if(AVObject != null)
00130 AVObject.setActionInformation(pl_list, hand, level);
00131 KVObject.redraw();
00132 }
00133
00137 public void clear(){
00138 KVObject.clear();
00139 KVObject.redraw();
00140 }
00141
00147 public void drawBackground(){
00148 KVObject.drawBackground();
00149 KVObject.redraw();
00150 }
00151
00152
00157 public void addObject(String identifier){
00158 KVObject.addObject(addSingleQuotes(identifier));
00159 KVObject.redraw();
00160 }
00161
00168 public void addObjectWithChildren(String identifier) {
00169 KVObject.addObjectWithChildren(addSingleQuotes(identifier));
00170 KVObject.redraw();
00171 }
00172
00177 public void addTrajectory(String[] identifiers){
00178
00179 for(String identifier : identifiers) {
00180
00181 addObject(identifier);
00182
00183
00184 KVObject.highlightItem(addSingleQuotes(identifier), true, StandaloneKitchenVisApplet.convertColor(255, 221, 0, 255));
00185
00186 }
00187 KVObject.redraw();
00188 }
00189
00194 public void removeObject(String identifier){
00195 KVObject.removeObject(addSingleQuotes(identifier));
00196 KVObject.redraw();
00197 }
00198
00199
00206 public void removeObjectWithChildren(String identifier) {
00207 KVObject.removeObjectWithChildren(addSingleQuotes(identifier));
00208 KVObject.redraw();
00209 }
00210
00216 public void highlight(String identifier, boolean highlight) {
00217 KVObject.highlightItem(addSingleQuotes(identifier),highlight);
00218 KVObject.redraw();
00219 }
00220 public void highlight(String identifier, boolean highlight, int color) {
00221 KVObject.highlightItem(addSingleQuotes(identifier),highlight, color);
00222 KVObject.redraw();
00223 }
00224 public void highlight(String identifier, boolean highlight, int r, int g, int b) {
00225
00226 int c = (((((255 << 8) + r) << 8) + g) << 8) + b;
00227 KVObject.highlightItem(addSingleQuotes(identifier),highlight, c);
00228 KVObject.redraw();
00229
00230 }
00231 public void highlight(String identifier, boolean highlight, int r, int g, int b, String prob) {
00232
00233
00234 int alpha = (int) (255 * Float.valueOf(prob));
00235 int c = (((((alpha << 8) + r) << 8) + g) << 8) + b;
00236
00237
00238
00239
00240 KVObject.highlightItem(addSingleQuotes(identifier),highlight, c);
00241 KVObject.redraw();
00242 }
00247 int HSVtoRGB (float h, float s, float v) {
00248
00249 float rr = 0, gg = 0, bb = 0;
00250 float hh = (6 * h) % 6;
00251 int c1 = (int) hh;
00252 float c2 = hh - c1;
00253 float x = (1 - s) * v;
00254 float y = (1 - (s * c2)) * v;
00255 float z = (1 - (s * (1 - c2))) * v;
00256 switch (c1) {
00257 case 0: rr=v; gg=z; bb=x; break;
00258 case 1: rr=y; gg=v; bb=x; break;
00259 case 2: rr=x; gg=v; bb=z; break;
00260 case 3: rr=x; gg=y; bb=v; break;
00261 case 4: rr=z; gg=x; bb=v; break;
00262 case 5: rr=v; gg=x; bb=y; break;
00263 }
00264 int N = 256;
00265 int r = Math.min(Math.round(rr*N),N-1);
00266 int g = Math.min(Math.round(gg*N),N-1);
00267 int b = Math.min(Math.round(bb*N),N-1);
00268
00269 int rgb = ((r&0xff)<<16) | ((g&0xff)<<8) | b&0xff;
00270 return rgb;
00271 }
00272
00278 public void highlightWithChildren(String identifier, boolean highlight) {
00279 KVObject.highlightReachable(addSingleQuotes(identifier),highlight);
00280 KVObject.redraw();
00281 }
00282
00283
00287 public void clearHighlight() {
00288 KVObject.clearHighlights();
00289 KVObject.redraw();
00290 }
00291
00292
00297 public void displayInfoFor(String identifier) {
00298 displayInformationForEntity(addSingleQuotes(identifier));
00299 KVObject.redraw();
00300 }
00301
00302
00303 public void setViewParameters(float xShift, float yShift,float xRot, float yRot, float zoom) {
00304 KVObject.setViewParameters(xShift, yShift, xRot, yRot, zoom);
00305 KVObject.redraw();
00306 }
00307
00308 public void showImagesInNewWindow(String[] imgs) {
00309
00310 img_window = new JFrame();
00311 img_window.setLayout(new GridLayout(1,imgs.length));
00312
00313 int frameWidth = 0;
00314 int frameHeight = 30;
00315
00316 int totalWidth = frameWidth;
00317 int totalHeight = frameHeight;
00318
00319 for(String img : imgs) {
00320 img = img.replaceAll( "\"", "" );
00321
00322 ImageViewerApplet iviewer = new ImageViewerApplet(img);
00323 iviewer.init();
00324
00325 img_window.getContentPane().add(iviewer);
00326 iviewer.setSize(iviewer.width,iviewer.height);
00327 totalWidth += iviewer.width;
00328 totalHeight = max(iviewer.height + frameHeight, totalHeight);
00329 }
00330
00331 img_window.setSize(totalWidth, totalHeight);
00332 img_window.pack();
00333 img_window.setVisible(true);
00334 img_window.setResizable(true);
00335 }
00336
00337
00341 protected static void initProlog() {
00342 try {
00343 Vector<String> args= new Vector<String>(Arrays.asList(Prolog.get_default_init_args()));
00344 args.add( "-G256M" );
00345 args.add( "-q" );
00346 args.add( "-nosignals" );
00347 Prolog.set_default_init_args( args.toArray( new String[0] ) );
00348
00349 } catch(Exception e) {
00350 e.printStackTrace();
00351 }
00352 }
00353
00354
00358
00359
00360
00361
00362 @SuppressWarnings("unused")
00363 private ArrayList<String[]> readInformationForEntity(String entity) {
00364
00365
00366 ArrayList<String[]> bindings = new ArrayList<String[]>();
00367 HashMap<String, Vector<Object>> qres = executeQuery("findall([P|O], (rdf_has("+entity+", P, O)), Cs)", "/home/tenorth/work/owl/gram_ias.pl");
00368
00369 for(String k : qres.keySet()) {
00370
00371 Vector<Object> res = qres.get(k);
00372
00373 for(Object o : res) {
00374
00375 String[] list = o.toString().split("'\\.'", 2);
00376 if(list.length<2) continue;
00377 String rest = list[1];
00378
00379 bindings.addAll(dottedPairsToArrayList(rest));
00380 }
00381 }
00382 return bindings;
00383 }
00384
00385 public void actionsInActivity() {
00386 HashMap<String, Vector<Object>> qres = executeQuery("rdf_has(Plan, rdfs:label, literal(type('http://www.w3.org/2001/XMLSchema#string', 'set a table'))), " +
00387 "comp_ehow:matching_actions(Plan, Act)", null);
00388 Vector<Object> act = qres.get("Act");
00389
00390 if(act != null) {
00391
00392
00393 this.AVObject.setActionsInActivity(act.toArray());
00394 }
00395 }
00396
00397 private void displayInformationForEntity(String entity) {
00398
00399 HashMap<String, Vector<Object>> qres = executeQuery("rdf_has("+entity+", P, O)", null);
00400 Vector<Object> P = qres.get("P");
00401 Vector<Object> O = qres.get("O");
00402 String info = "";
00403 if(P != null && O != null)
00404 for(int i=0;i<P.size() && i<O.size();i++) {
00405 info += printKey(P.get(i).toString()) + ": " + printValue(O.get(i).toString()) + "\n";
00406 }
00407
00408 if(controlP5.controller("CurrentAction")!=null)
00409 ((Textfield) controlP5.controller("CurrentAction")).setValue(printKey(entity));
00410 if(controlP5.getGroup("CurrentAttributes")!=null)
00411 ((Textarea) controlP5.getGroup("CurrentAttributes")).setText(info);
00412
00413 System.out.println(info);
00414
00415 }
00416
00417 private String printKey(String k) {
00418 String[] ks = k.split("#");
00419 if(ks.length>1) {
00420 String res = ks[1].replaceAll("'", "");
00421 return res;
00422 }
00423 else return k;
00424 }
00425
00426 private String printValue(String v) {
00427 if (v.contains("#")) {
00428 String[]vs=v.split("#");
00429 return vs[1].replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", "");
00430
00431 } else if(!v.startsWith("literal")) {
00432 return v.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", "");
00433
00434 } else {
00435
00436 String v1 = v.replaceFirst("literal\\(type\\('http://www\\.w3\\.org/2001/XMLSchema", "");
00437 return v1.replaceAll("'", "").replaceAll("\\(", "").replaceAll("\\)", "");
00438 }
00439 }
00440
00441 private void initControlP5() {
00442
00443 controlP5 = new ControlP5(AVObject);
00444 controlWindow = controlP5.addControlWindow("controlP5window",660,230,400,350);
00445 controlWindow.setBackground(color(40));
00446 controlWindow.setUpdateMode(ControlWindow.NORMAL);
00447 controlWindow.frameRate(5);
00448
00449 Textlabel act_label = controlP5.addTextlabel("act_label","Abstraction Level",80,40);
00450 act_label.setColorValue(0xffffffff);
00451 act_label.setFont(3);
00452 act_label.moveTo(controlWindow);
00453
00454 Radio act_radio = controlP5.addRadio("act_radio", 80, 60);
00455 act_radio.addItem("activity",3);
00456 act_radio.addItem("2",2);
00457 act_radio.addItem("1",1);
00458 act_radio.addItem("0",0);
00459 act_radio.moveTo(controlWindow);
00460 act_radio.setId(100);
00461
00462 Textlabel color_label = controlP5.addTextlabel("color_label","Colors",220,40);
00463 color_label.setColorValue(0xffffffff);
00464 color_label.setFont(3);
00465 color_label.moveTo(controlWindow);
00466
00467 Radio color_radio = controlP5.addRadio("color_radio",220,60);
00468 color_radio.addItem("by action type",0);
00469 color_radio.addItem("by object",1);
00470 color_radio.addItem("missing in activity",2);
00471 color_radio.moveTo(controlWindow);
00472 color_radio.setId(101);
00473 color_radio.addListener(new RadioButtonListener(this));
00474
00475 controlP5.addTextfield("CurrentAction",80,120,200,20).moveTo(controlWindow);
00476 Textarea text = controlP5.addTextarea("CurrentAttributes","", 80,160,200,130);
00477 text.setColorBackground(0xFF000000);
00478 text.moveTo(controlWindow);
00479
00480 }
00481
00482
00483 private ArrayList<String[]> dottedPairsToArrayList(String rest) {
00484 ArrayList<String[]> bindings = new ArrayList<String[]>();
00485 while(rest.length()>0) {
00486 String[] l = rest.split("'\\.'", 2);
00487
00488 if((l[0].equals("")) || (l[0].equals("("))) {
00489 if(l[0].startsWith("(")) {
00490 rest=l[1]; continue;
00491
00492 } else break;
00493 } else {
00494 bindings.add(l[0].substring(1, l[0].length()-2).split(", "));
00495 if(l.length>1) {
00496 rest=l[1]; continue;
00497 } else break;
00498 }
00499
00500 }
00501 return bindings;
00502 }
00503
00504
00505 public static String removeSingleQuotes(String str) {
00506 if(str.startsWith("'"))
00507 str = str.substring(1);
00508
00509 if(str.endsWith("'"))
00510 str = str.substring(0, str.length()-1);
00511 return str;
00512 }
00513
00514 public static String addSingleQuotes(String str) {
00515 return "'"+removeSingleQuotes(str)+"'";
00516 }
00517
00518
00519 public StandaloneKitchenVisApplet getKitchenVisApplet() {
00520 return this.KVObject;
00521 }
00522
00523
00529 @SuppressWarnings("rawtypes")
00530 public static HashMap<String, Vector<Object>> executeQuery(String query, String plFile) {
00531
00532 HashMap<String, Vector<Object>> result = new HashMap< String, Vector<Object> >();
00533 Hashtable[] solutions;
00534
00535 Query q = new Query( "expand_goal(("+query+"),_9), call(_9)" );
00536
00537
00538 if (!q.hasMoreSolutions())
00539 return new HashMap<String, Vector<Object>>();
00540 Hashtable oneSolution = q.nextSolution();
00541 if (oneSolution.isEmpty())
00542 return new HashMap<String, Vector<Object>>();
00543
00544
00545 q.rewind();
00546 solutions = q.allSolutions();
00547
00548
00549
00550 for (Object key: solutions[0].keySet()) {
00551 result.put(key.toString(), new Vector<Object>());
00552 }
00553
00554
00555 for (int i=0; i<solutions.length; i++) {
00556 Hashtable solution = solutions[i];
00557 for (Object key: solution.keySet()) {
00558 String keyStr = key.toString();
00559 if (!result.containsKey( keyStr )) {
00560
00561
00562 Vector<Object> resultVector = new Vector<Object>();
00563 resultVector.add( i, solution.get( key ).toString() );
00564 result.put(keyStr, resultVector);
00565
00566 }
00567
00568 Vector<Object> resultVector = result.get( keyStr );
00569 resultVector.add( i, solution.get( key ).toString() );
00570 }
00571 }
00572
00573 return result;
00574 }
00575
00576 public static void main(String args[]) {
00577 PApplet.main(new String[] { "de.tum.in.fipm.kipm.gui.visualisation.base.PrologVisualizationCanvas" });
00578 }
00579 }
00580
00581 class RadioButtonListener implements ControlListener {
00582
00583 PrologVisualizationCanvas pvc;
00584
00585 RadioButtonListener(PrologVisualizationCanvas p) {
00586 this.pvc = p;
00587 }
00588
00589 public void controlEvent(ControlEvent e) {
00590
00591 if(e.controller().id()==101 && e.value()==2.0)
00592 pvc.actionsInActivity();
00593
00594 }
00595 }
00596
00597 class testDataLoader implements Runnable {
00598
00599 PrologVisualizationCanvas c;
00600 public testDataLoader(PrologVisualizationCanvas c) {
00601 this.c = c;
00602 }
00603
00604 @SuppressWarnings("unchecked")
00605 public void run() {
00606
00607 try{
00608 Thread.sleep(20000);
00609 }catch(Exception e){};
00610 new Query("initCmdProlog").oneSolution();
00611 new Query("ensure_loaded('C:/Projects/Eclipse2/kipm/demo.pl')").oneSolution();
00612
00613 new Query("ddLeft").oneSolution();
00614 try{
00615 FileInputStream fos = new FileInputStream("C:/Projects/Eclipse2/kipm/tmpDataLeft.ser");
00616 ObjectInputStream oos = new ObjectInputStream(fos);
00617
00618 ArrayList<Object[]> o = (ArrayList<Object[]>)oos.readObject();
00619
00620 for(Object[] obj : o){
00621 c.setActionInformation((String[][][])obj[0], (String)obj[1], (Integer)obj[2]);
00622 }
00623
00624 oos.close();
00625 }catch(Exception e){}
00626
00627
00628 new Query("ddRight").oneSolution();
00629 try{
00630 FileInputStream fos = new FileInputStream("C:/Projects/Eclipse2/kipm/tmpDataRight.ser");
00631 ObjectInputStream oos = new ObjectInputStream(fos);
00632
00633 ArrayList<Object[]> o = (ArrayList<Object[]>)oos.readObject();
00634
00635 for(Object[] obj : o){
00636 c.setActionInformation((String[][][])obj[0], (String)obj[1], (Integer)obj[2]);
00637 }
00638
00639 oos.close();
00640 }catch(Exception e){}
00641
00642
00643 }
00644
00645 }