00001 package de.tum.in.fipm.kipm.gui.visualisation.applets;
00002
00003 import java.awt.event.MouseEvent;
00004 import java.awt.event.MouseListener;
00005 import java.awt.event.MouseMotionListener;
00006 import java.io.Serializable;
00007 import java.util.ArrayList;
00008 import java.util.HashMap;
00009 import java.util.Hashtable;
00010 import java.util.Vector;
00011
00012 import jpl.Query;
00013 import processing.core.PApplet;
00014 import processing.core.PFont;
00015 import processing.core.PGraphics;
00016 import controlP5.ControlEvent;
00017 import controlP5.Radio;
00018 import de.tum.in.fipm.kipm.gui.visualisation.base.PrologVisualizationCanvas;
00019
00020 public class ActionVisApplet extends PApplet implements MouseListener, MouseMotionListener {
00021
00022 static final long serialVersionUID=0;
00023
00024 PGraphics buffer;
00025 private PrologVisualizationCanvas prologVisCanvas = null;
00026
00028
00029
00030
00031
00032 float leftMouseX=-1.0f, leftMouseY=-1.0f, rightMouseX=-1.0f, rightMouseY=-1.0f, centerMouseY=-1.0f;
00033 float xRotDisplay=-2.549f, yRotDisplay=86.79f, xShiftDisplay=410f, yShiftDisplay=15f, zoomDisplay=1f;
00034
00035 PFont verdana;
00036 PFont verdanaBold;
00037 PFont dejavu;
00038
00039 ArrayList<String> actionTypes;
00040 ArrayList<String> objectTypes;
00041
00043
00044 ArrayList<Integer> colors = new ArrayList<Integer>(12);
00045
00046
00047 private ArrayList<ArrayList<ActionInformation>> leftHandActions = new ArrayList<ArrayList<ActionInformation>>();
00048 private ArrayList<ArrayList<ActionInformation>> rightHandActions = new ArrayList<ArrayList<ActionInformation>>();
00049
00050 private HashMap<Integer, String> color2id = new HashMap<Integer, String>();
00051
00053
00054
00055 public int actionIndex = 0;
00056 private int idColor=0xFFFF0000;
00057
00059
00060
00061 int maxLevel = 1000;
00062 boolean initialize = false;
00063
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 public void setup() {
00086
00087 actionTypes= new ArrayList<String>();
00088 objectTypes= new ArrayList<String>();
00089
00090 size(550, 600, P3D);
00091 buffer = createGraphics(width, height, P3D);
00092 lights();
00093
00094 verdana = createFont("Verdana", 11);
00095 verdanaBold = createFont("Verdana-Bold", 18);
00096 dejavu = createFont("DejaVu Sans",13);
00097
00098 textFont(verdana);
00099 textFont(verdanaBold);
00100 textFont(dejavu);
00101 hint(ENABLE_ACCURATE_TEXTURES);
00102
00103
00104 ellipseMode(RADIUS);
00105 frameRate(20);
00106
00107 setColors();
00108
00109 noLoop();
00110 draw();
00111
00112 if(prologVisCanvas != null) prologVisCanvas.validate();
00113
00114 }
00115
00116
00117 public void draw() {
00118
00119 background(20, 20, 20);
00120
00121 cursor(CROSS);
00122 ortho(-width/2, width/2, -height/2, height/2, -10, 10);
00123
00124 pushMatrix();
00125
00126 translate(xShiftDisplay, yShiftDisplay, 0.0f);
00127
00128 rotateX( radians(xRotDisplay) );
00129 rotateY( radians( yRotDisplay) );
00130
00131 translate(2000f, height, -400.0f);
00132
00133
00134
00135
00136 lights();
00137 pushMatrix();
00138
00139 textFont(verdana);
00140 textMode(SCREEN);
00141 textAlign(CENTER);
00142 fill(0xFFFFFFFF);stroke(0xFFFFFFFF);
00143
00144 text("left hand", screenX(0, 25, -50), screenY(0, 25, -50));
00145 text("right hand", screenX(0, 25, 50), screenY(0, 25, 50));
00146
00147 text("abstraction level", screenX(0, -200, 0), screenY(0, -200, 0));
00148
00149 strokeWeight(2);
00150 line(0, 0, 0, 1000, 0, 0);
00151 line(0, 0, 0, 0, -180, 0);
00152 line(0, -180, 0, 0, -170, -5);
00153 line(0, -180, 0, 0, -170, 5);
00154
00155 line(0, 0, -100, 0, 0, 100);
00156
00157 popMatrix();
00158
00159 pushMatrix();
00160 drawActionSequence(this.g, false);
00161 popMatrix();
00162
00163 popMatrix();
00164
00165
00166 }
00167
00168
00172
00173
00174
00175
00176 public void drawActionSequence(PGraphics g, boolean isBuffer) {
00177
00178 g.noStroke();
00179 int i=0;
00180
00181 if(leftHandActions.size()<1) return;
00182 if(rightHandActions.size()<1) return;
00183
00184 int level=0;
00185 while(level <= maxLevel) {
00186
00187 ArrayList<ArrayList<ActionInformation>> actions;
00188 for(String side: new String[]{"left", "right"}) {
00189
00190 if(side.equals("left")) actions=leftHandActions;
00191 else actions=rightHandActions;
00192
00193
00194 if(level>=actions.size()) continue;
00195 for (i=0; i< actions.get(level).size(); i++) {
00196
00197 if(i>=actions.get(level).size()) continue;
00198
00199 ActionInformation action = actions.get(level).get(i);
00200
00201
00202 float actionLengthFactor = 1.0f;
00203 float start = actionLengthFactor*action.getStarttime();
00204 float end = actionLengthFactor*action.getEndtime();
00205 if(end==-1) end=start;
00206
00207 float yshift = -40 * level;
00208 float zshift = (side.equals("left"))?(-40):(40);
00209
00210 g.pushMatrix();
00211 g.fill(this.getColor(actions.get(level).get(i), isBuffer));
00212
00213 g.translate(100*start
00214 +50*(end-start)
00215
00216 -(100*actionLengthFactor*actions.get(0).get(actionIndex).getTime()),yshift,zshift);
00217 g.box(((end==start)?(1):(100*(end-start))), 20, 40);
00218 g.popMatrix();
00219
00220
00221 }
00222
00223 }
00224
00225 level++;
00226 }
00227 }
00228
00229
00230 private int getColor(ActionInformation item, boolean isBuffer) {
00231
00232
00233 if(isBuffer)
00234 return(item.getIdColor());
00235
00236
00237 if(item.endtime==-1) {
00238 return (0xFF000000 + (((int)(80*item.starttime++)%200)<< 16));
00239 }
00240
00241
00242 if(prologVisCanvas != null && (prologVisCanvas.controlP5.controller("color_radio")!=null) && ((Radio) prologVisCanvas.controlP5.controller("color_radio")).value()==0) {
00243
00244
00245 String[] act = item.getTypes();
00246 if(!actionTypes.contains(act[0])) {actionTypes.add(act[0]);}
00247 int idx = actionTypes.indexOf(act[0]);
00248 if(idx<0 || idx>this.colors.size()) {System.out.println("Missing color for " + act[0]); return 0;}
00249 return this.colors.get(idx);
00250
00251 } else if(prologVisCanvas != null && (prologVisCanvas.controlP5.controller("color_radio")!=null) && ((Radio) prologVisCanvas.controlP5.controller("color_radio")).value()==1) {
00252
00253
00254 String obj = item.getObjecttype();
00255 if(!objectTypes.contains(obj)) {objectTypes.add(obj);}
00256 int idx = objectTypes.indexOf(obj);
00257
00258 if(idx<0 || idx>this.colors.size()) {System.out.println("Missing color for " + obj); return 0;}
00259
00260 return this.colors.get(idx);
00261
00262 } else if(prologVisCanvas != null && (prologVisCanvas.controlP5.controller("color_radio")!=null) && ((Radio) prologVisCanvas.controlP5.controller("color_radio")).value()==2) {
00263
00264
00265 if(item.isInActivity()) {
00266 return 0xFF00CC00;
00267 }
00268 else return 0xFFCC0000;
00269 }
00270 return 0;
00271 }
00272
00273
00274
00275
00276
00280
00281
00282
00283
00284 public void setActionInformation(String[][][] pl_list, String hand, int level) {
00285
00286 try{
00287
00288
00289 ArrayList<ActionInformation> actionlist = new ArrayList<ActionInformation>();
00290 for(String[][] infos : pl_list) {
00291
00292 String label = infos[0][0];
00293
00294 float starttime=0.0f;
00295 try{
00296 starttime=Float.valueOf(infos[1][0]);
00297 } catch(NumberFormatException e) {
00298 starttime=Float.valueOf((infos[1][0].split("#"))[1]);
00299 }
00300
00301 float endtime=0.0f;
00302 try{
00303 endtime=Float.valueOf(infos[2][0]);
00304 } catch(NumberFormatException e) {
00305 endtime=Float.valueOf((infos[2][0].split("#"))[1]);
00306 }
00307
00308 String[] types = infos[3];
00309 String objecttype = infos[4][0];
00310
00311 color2id.put(idColor, label);
00312 actionlist.add(new ActionInformation(label, starttime, endtime, types, objecttype, idColor++));
00313
00314 }
00315
00316 int l = Integer.valueOf(level);
00317
00318 while(l>=leftHandActions.size()) {
00319 leftHandActions.add(new ArrayList<ActionInformation>());
00320 }
00321
00322 while(l>=rightHandActions.size()) {
00323 rightHandActions.add(new ArrayList<ActionInformation>());
00324 }
00325
00326 if(hand.equals("left"))
00327 this.leftHandActions.set(l, actionlist);
00328 else
00329 this.rightHandActions.set(l, actionlist);
00330
00331 } catch(Exception e) {
00332 e.printStackTrace();
00333 }
00334
00335 this.redraw();
00336 }
00337
00338
00339 public void setActionsInActivity(Object[] actions) {
00340 System.out.println(actions.toString());
00341 for(Object act : actions) {
00342 System.out.println(act.toString());
00343 for(ArrayList<ActionInformation> alist : this.leftHandActions) {
00344 for(ActionInformation ainfo : alist) {
00345 if(("'"+ainfo.getLabel()+"'").equals(act.toString())) {
00346 ainfo.setInActivity(true);
00347 }
00348 }
00349 }
00350 }
00351 for(Object act : actions) {
00352 for(ArrayList<ActionInformation> alist : this.rightHandActions) {
00353 for(ActionInformation ainfo : alist) {
00354 if(("'"+ainfo.getLabel()+"'").equals(act.toString())) {
00355 ainfo.setInActivity(true);
00356 }
00357 }
00358 }
00359 }
00360 }
00361
00362
00363
00367
00368
00369
00370
00371
00372 public void mousePressed(MouseEvent e) {
00373
00374
00375 if(e.getButton()==MouseEvent.BUTTON1) {
00376 leftMouseX = e.getX();
00377 leftMouseY = e.getY();
00378 } else if(e.getButton()==MouseEvent.BUTTON3) {
00379 rightMouseX = e.getX();
00380 rightMouseY = e.getY();
00381 } else if (e.getButton()==MouseEvent.BUTTON2) {
00382 centerMouseY = e.getY();
00383 }
00384
00385
00386 }
00387
00388
00389 public void mouseReleased(MouseEvent e) {
00390
00391 if(e.getButton()==MouseEvent.BUTTON1) {
00392 leftMouseX = -1.0f;
00393 leftMouseY = -1.0f;
00394 } else if(e.getButton()==MouseEvent.BUTTON3) {
00395 rightMouseX = -1.0f;
00396 rightMouseY = -1.0f;
00397 } else if (e.getButton()==MouseEvent.BUTTON2) {
00398 centerMouseY = -1.0f;
00399 }
00400
00401 }
00402
00403 public void mouseDragged(MouseEvent e) {
00404
00405
00406 if(leftMouseX != -1.0f) {
00407 xRotDisplay+= (e.getY()-leftMouseY) * 0.05;
00408 yRotDisplay-= (e.getX()-leftMouseX) * 0.05;
00409 leftMouseX = e.getX();
00410 leftMouseY = e.getY();
00411
00412 }else if(rightMouseX != -1.0f) {
00413 yShiftDisplay+= (e.getY()-rightMouseY) * 0.5;
00414 xShiftDisplay+= (e.getX()-rightMouseX) * 0.5;
00415 rightMouseX = e.getX();
00416 rightMouseY = e.getY();
00417
00418 } else if (centerMouseY != -1.0f) {
00419 zoomDisplay+= (e.getY()-centerMouseY) * 0.02;
00420 if(zoomDisplay<0.01){zoomDisplay=0.01f;}
00421 centerMouseY = e.getY();
00422 }
00423
00424 redraw();
00425 }
00426
00427 public void mouseClicked() {
00428
00429
00430 buffer.beginDraw();
00431 buffer.background(0);
00432
00433 buffer.ortho(-width/2, width/2, -height/2, height/2, -10, 10);
00434
00435 buffer.pushMatrix();
00436
00437 buffer.translate(xShiftDisplay, yShiftDisplay, 0.0f);
00438
00439 buffer.rotateX( radians(xRotDisplay) );
00440 buffer.rotateY( radians( yRotDisplay) );
00441
00442 buffer.translate(2000f, height, -400.0f);
00443 drawActionSequence(buffer, true);
00444
00445 buffer.popMatrix();
00446 buffer.endDraw();
00447
00448
00449 int pick = buffer.get(mouseX, mouseY);
00450
00451 String id = this.color2id.get(pick);
00452 if(id!=null && prologVisCanvas != null) {
00453 prologVisCanvas.displayAction(id);
00454 prologVisCanvas.displayInfoFor(id);
00455 }
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 void controlEvent(ControlEvent theEvent) {
00490
00491
00492 if(prologVisCanvas != null && theEvent.controller().id()==100) {
00493
00494 this.maxLevel =(int)prologVisCanvas.controlP5.controller("act_radio").value();
00495
00496 } else if(theEvent.controller().id()==101) {
00497
00498
00499
00500 }
00501 redraw();
00502 }
00503
00504
00505 public int makeColor(int r,int g,int b){
00506 int col = 0xFF000000 + 0x0000FF00 * r + 0x000000FF * g + b;
00507 return col;
00508 }
00509
00510 public void setPrologVisCanvas(PrologVisualizationCanvas c){
00511 prologVisCanvas = c;
00512 }
00513
00514
00515 public void setColors(){
00516
00517 for(int[] c : new int[][]{
00518
00519 {255,255,153}, {255,153,153}, {153,0,102},
00520 {51,0,51}, {102,102,102}, {102,51,153},
00521 {255,204,0}, {255,51,0}, {153,204,0},
00522 {204,153,0}, {255,255,255}, {255,153,102},
00523 {102,0,0}, {204,255,102}, {102,102,51},
00524 {204,204,204}, {255,255,51}, {153,102,102},
00525 {153,0,51}, {153,153,153}, {51,153,0},
00526 {102,153,204}, {51,51,204}, {0,153,102},
00527 {102,255,204}, {0,0,204},
00528 {102,153,204}, {51,51,204}, {0,153,102},
00529 {255,255,153}, {255,153,153}, {153,0,102},
00530 {153,0,51}, {153,153,153}, {51,153,0},
00531 {51,0,51}, {102,102,102}, {102,51,153},
00532 {204,204,204}, {255,255,51}, {153,102,102},
00533 {102,0,0}, {204,255,102}, {102,102,51},
00534 {204,153,0}, {255,255,255}, {255,153,102},
00535 {255,204,0}, {255,51,0}, {153,204,0},
00536 {102,255,204}, {0,0,204}}) {
00537 colors.add(makeColor(c[0], c[1], c[2]));
00538 }
00539
00540
00541 }
00542
00543
00544 private class ActionInformation implements Serializable {
00545
00546 private static final long serialVersionUID = -1005928838950117046L;
00547 private String label;
00548 private float starttime;
00549 private float endtime=-1;
00550 private String[] types;
00551 private String objecttype;
00552 private int idColor;
00553 private boolean inActivity;
00554
00555
00556 public ActionInformation(String label, float starttime, float endtime, String[] types, String objecttype, int idColor) {
00557 super();
00558 this.label = label;
00559 this.starttime = starttime;
00560 this.endtime = endtime;
00561 this.types = types;
00562 this.objecttype = objecttype;
00563 this.idColor = idColor;
00564 this.inActivity = false;
00565 }
00566
00567
00568 @SuppressWarnings("unused")
00569 public ActionInformation(String label, float time, String[] types, String objecttype, int idColor) {
00570 super();
00571 this.label = label;
00572 this.starttime = time;
00573 this.types = types;
00574 this.objecttype = objecttype;
00575 this.idColor = idColor;
00576 this.inActivity = false;
00577 }
00578
00579
00580 public String getLabel() {
00581 return label;
00582 }
00583
00584
00585 @SuppressWarnings("unused")
00586 public void setLabel(String label) {
00587 this.label = label;
00588 }
00589
00590
00591 public String getObjecttype() {
00592 return objecttype;
00593 }
00594
00595
00596 @SuppressWarnings("unused")
00597 public void setObjecttype(String objecttype) {
00598 this.objecttype = objecttype;
00599 }
00600
00601
00602 public float getTime() {
00603 return starttime;
00604 }
00605
00606
00607 public int getIdColor() {
00608 return idColor;
00609 }
00610
00611
00612 @SuppressWarnings("unused")
00613 public void setIdColor(int idColor) {
00614 this.idColor = idColor;
00615 }
00616
00617
00618 @SuppressWarnings("unused")
00619 public void setTime(float time) {
00620 this.starttime = time;
00621 }
00622
00623
00624 public String[] getTypes() {
00625 return types;
00626 }
00627
00628
00629 @SuppressWarnings("unused")
00630 public void setTypes(String[] types) {
00631 this.types = types;
00632 }
00633
00634
00635 public float getEndtime() {
00636 return endtime;
00637 }
00638
00639
00640 @SuppressWarnings("unused")
00641 public void setEndtime(float endtime) {
00642 this.endtime = endtime;
00643 }
00644
00645
00646 public float getStarttime() {
00647 return starttime;
00648 }
00649
00650
00651 @SuppressWarnings("unused")
00652 public void setStarttime(float starttime) {
00653 this.starttime = starttime;
00654 }
00655
00656 public boolean isInActivity() {
00657 return inActivity;
00658 }
00659
00660
00661 public void setInActivity(boolean inActivity) {
00662 this.inActivity = inActivity;
00663 }
00664
00665 }
00666
00672 @SuppressWarnings("rawtypes")
00673 public static HashMap<String, Vector<Object>> executeQuery(String query, String plFile) {
00674
00675
00676 HashMap<String, Vector<Object>> result = new HashMap< String, Vector<Object> >();
00677 Hashtable[] solutions;
00678
00679 Query q = new Query( "expand_goal(("+query+"),_9), call(_9)" );
00680
00681
00682 if (!q.hasMoreSolutions())
00683 return new HashMap<String, Vector<Object>>();
00684 Hashtable oneSolution = q.nextSolution();
00685 if (oneSolution.isEmpty())
00686 return new HashMap<String, Vector<Object>>();
00687
00688
00689 q.rewind();
00690 solutions = q.allSolutions();
00691
00692
00693
00694 for (Object key: solutions[0].keySet()) {
00695 result.put(key.toString(), new Vector<Object>());
00696 }
00697
00698
00699 for (int i=0; i<solutions.length; i++) {
00700 Hashtable solution = solutions[i];
00701 for (Object key: solution.keySet()) {
00702 String keyStr = key.toString();
00703 if (!result.containsKey( keyStr )) {
00704
00705
00706 Vector<Object> resultVector = new Vector<Object>();
00707 resultVector.add( i, solution.get( key ).toString() );
00708 result.put(keyStr, resultVector);
00709
00710 }
00711
00712 Vector<Object> resultVector = result.get( keyStr );
00713 resultVector.add( i, solution.get( key ).toString() );
00714 }
00715 }
00716
00717 return result;
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740 public static void main(String args[]) {
00741 PApplet.main(new String[] { "de.tum.in.fipm.kipm.gui.visualisation.applets.ActionVisApplet" });
00742 }
00743 }
00744