2-APL UPC project.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

added blockworld sources

fdesan 6428efbe 662d7074

+2887
+127
code/2apl_platform/environments/blockworld/Agent.java
··· 1 + package blockworld; 2 + 3 + /* 4 + 3APL Blockworld program by Jelle Herold, copyright 2003. 5 + Written for the Intelligent Systems group at Utrecht University. 6 + This is LGPL software. 7 + 8 + $Id: Agent.java,v 1.6 2004/09/14 10:58:06 cvs-3apl Exp $ 9 + */ 10 + 11 + import java.awt.Point; 12 + 13 + import blockworld.lib.Signal; 14 + 15 + // / Agent representation in Env. This doubles as a plugin instance. 16 + public class Agent 17 + { 18 + protected String _name; 19 + 20 + /** 21 + * _position is null means agent did not enter the world. This method is 22 + * only for the blockworld package to prevent setting the position by other 23 + * means that through the north/south/west/east methods of the environment. 24 + * \todo is protection "package" correct? 25 + */ 26 + protected Point _position = null; 27 + 28 + protected TypeObject _bomb = null; 29 + 30 + int _colorID = 0; 31 + 32 + public Agent( String name ) { 33 + _name = name; 34 + } 35 + 36 + public String getName() { 37 + return _name; 38 + } 39 + 40 + /** 41 + * Get current agent position. \returns This will return null if the agent 42 + * is not entered in the world. 43 + */ 44 + public Point getPosition() { 45 + return _position; 46 + } 47 + 48 + // / Sense if agent is carrying a bomb. 49 + // / \return True if agent is carrying a bomb, false otherwise. 50 + public TypeObject senseBomb() { 51 + return _bomb; 52 + } 53 + public TypeObject senseBomb(String type) { 54 + return (_bomb != null && _bomb.getType().equals(type))?_bomb:null; 55 + } 56 + 57 + public boolean atCapacity() { 58 + return _bomb != null; 59 + } 60 + 61 + public void pickBomb(TypeObject bomb) { 62 + _bomb = bomb; 63 + } 64 + 65 + public void dropBomb() { 66 + _bomb = null; 67 + } 68 + 69 + /** 70 + * Check if agent is "entered" in the environment. That is, it has a 71 + * position in the world. \returns true if agent is entered in the 72 + * environment 73 + */ 74 + public boolean isEntered() { 75 + return (_position != null); 76 + } 77 + 78 + /** 79 + * Called by the interpreter when the agent this instance refers to is 80 + * reset. \todo signalMove show become special signal enter/exit 81 + */ 82 + public void reset() { 83 + _position = null; 84 + _bomb = null; // Fixed bug pointed out by Bas of UU - Sohan 85 + signalMove.emit(); 86 + } 87 + 88 + /** 89 + * returns the unique name of the agent this instance refers to. 90 + */ 91 + public String toString() { 92 + return getName(); 93 + } 94 + 95 + // / removes all listeners 96 + public void deleteObservers() { 97 + signalMove.deleteObservers(); 98 + signalPickupBomb.deleteObservers(); 99 + signalDropBomb.deleteObservers(); 100 + 101 + signalMoveSucces.deleteObservers(); 102 + signalPickupBombSucces.deleteObservers(); 103 + signalDropBombSucces.deleteObservers(); 104 + } 105 + 106 + // / emitted if agent attemps movement (succesful or not) 107 + public transient Signal signalMove = new Signal( "agent attempts move" ); 108 + 109 + // / emitted if agent attemps to pickup a bomb (succesful or not) 110 + public transient Signal signalPickupBomb = new Signal( 111 + "agent attempts pickup" ); 112 + 113 + // / emitted if agent attemps to drop a bomb (succesful or not) 114 + public transient Signal signalDropBomb = new Signal( "agent attempts drop" ); 115 + 116 + // / emitted if agent succesfully moves 117 + public transient Signal signalMoveSucces = new Signal( 118 + "agent succesful move" ); 119 + 120 + // / emitted if agent (succesfully) picks up a bomb 121 + public transient Signal signalPickupBombSucces = new Signal( 122 + "agent succesful pickup" ); 123 + 124 + // / emitted if agent (succesfully) drops a bomb 125 + public transient Signal signalDropBombSucces = new Signal( 126 + "agent sucessful drop" ); 127 + }
+1110
code/2apl_platform/environments/blockworld/Env.java
··· 1 + package blockworld; 2 + 3 + /* 4 + 3APL Blockworld program by Jelle Herold, copyright 2003. 5 + Written for the Intelligent Systems group at Utrecht University. 6 + This is LGPL software. 7 + 8 + $Id: Env.java,v 1.7 2004/12/27 02:22:41 cvs-3apl Exp $ 9 + */ 10 + 11 + // 2APL imports 12 + import apapl.Environment; 13 + import apapl.ExternalActionFailedException; 14 + import apapl.data.*; 15 + 16 + // Standard java imports 17 + import java.awt.Point; 18 + import java.util.Collection; 19 + import java.util.Iterator; 20 + import java.util.List; 21 + import java.util.Vector; 22 + import java.util.LinkedList; 23 + import javax.swing.SwingUtilities; 24 + import java.awt.Dimension; 25 + import java.util.ArrayList; 26 + import java.util.HashMap; 27 + 28 + import java.io.IOException; 29 + import java.io.InputStream; 30 + import java.io.ObjectInputStream; 31 + import java.io.ObjectOutputStream; 32 + import java.io.OutputStream; 33 + 34 + import blockworld.lib.ObsVectListener; 35 + import blockworld.lib.Signal; 36 + import blockworld.lib.ObsVect; 37 + 38 + public class Env extends Environment implements ObsVectListener 39 + { 40 + // To hold our reference to the window 41 + final protected Window m_window; 42 + 43 + // max distance of cells visible to each agent 44 + protected int _senserange = 5; 45 + 46 + // size of environment 47 + protected Dimension m_size = new Dimension( 16, 16 ); 48 + 49 + private HashMap<String,Agent> agentmap = new HashMap<String,Agent>(); 50 + 51 + /* ---- ALL the stuff in the environment -----*/ 52 + 53 + // list of agents (Agent) 54 + protected ObsVect _agents = new ObsVect( this ); 55 + // location of bomb traps 56 + protected ObsVect _traps = new ObsVect(); 57 + // list of coordinates (Point) containing stones 58 + protected ObsVect _stones = new ObsVect(); 59 + // list of coordinates (Point) containing bombs 60 + protected ObsVect _bombs = new ObsVect(); 61 + // id for identifiable objects 62 + protected String _objType = "default"; 63 + 64 + /* ------------------------------------------*/ 65 + 66 + 67 + /* ---- SIGNALS -------------------------------*/ 68 + // / emitted on collection of a bomb in the bomb trap 69 + public transient Signal signalBombTrapped = new Signal( "env bomb trapped" ); 70 + 71 + public transient Signal signalSenseRangeChanged = new Signal( 72 + "env sense range changed" ); 73 + 74 + // emitted if environment is resized 75 + public transient Signal signalSizeChanged = new Signal( "env size changed" ); 76 + 77 + // emitted if bomb traps location changed 78 + public transient Signal signalTrapChanged = new Signal( 79 + "env trap position changed" ); 80 + 81 + /* ------------------------------------------*/ 82 + 83 + 84 + // The default constructor 85 + public Env() 86 + { 87 + super(); 88 + // Create the window 89 + m_window = new Window( this ); 90 + } 91 + 92 + /* Called from 2APL */ 93 + 94 + // Enter the agent into the world 95 + // Succesful returns true, else the ExternalActionFailedException exception is thrown 96 + public Term enter( String sAgent, APLNum xt, APLNum yt, APLIdent colort) throws ExternalActionFailedException 97 + { 98 + int x = xt.toInt(); 99 + int y = yt.toInt(); 100 + String color = colort.toString(); 101 + 102 + // Get the agent 103 + Agent agent = getAgent(sAgent); 104 + 105 + // Give a signal that we want to move 106 + agent.signalMove.emit(); 107 + 108 + writeToLog( "Agent entered: " +agent.getName()); 109 + 110 + Point position = new Point(x,y); 111 + String pos = "("+x+","+y+")"; 112 + 113 + // Agent already entered 114 + if( agent.isEntered() ) 115 + { 116 + writeToLog( "agent already entered" ); 117 + throw new ExternalActionFailedException("Agent \""+agent.getName()+"\" has already entered."); 118 + } 119 + 120 + // Is this position within the world? 121 + if (isOutOfBounds(position)) 122 + { 123 + throw new ExternalActionFailedException("Position "+pos+" out of bounds."); 124 + } 125 + // Is this position free? 126 + if( !isFree( position ) ) 127 + { 128 + throw new ExternalActionFailedException("Position "+pos+" is not free."); 129 + } 130 + 131 + // Update the agent his position 132 + agent._position = position; 133 + 134 + // Which color does the agent want to be 135 + int nColorID = getColorID(color); 136 + agent._colorID = nColorID; 137 + 138 + // Redraw so we can see the agent 139 + validatewindow(); 140 + m_window.repaint(); 141 + 142 + // We came so far, this means success! 143 + agent.signalMoveSucces.emit(); 144 + 145 + return wrapBoolean(true); 146 + } 147 + 148 + // Move the agent north 149 + public Term north(String sAgent) throws ExternalActionFailedException 150 + { 151 + // Get the correct agent 152 + Agent agent = getAgent(sAgent); 153 + 154 + // Get the agent his position 155 + Point p = (Point) agent.getPosition().clone(); 156 + p.y = p.y - 1; 157 + 158 + // Set the position for the agent 159 + boolean r = setAgentPosition( agent, p); 160 + 161 + // can't move north 162 + if (!r) throw new ExternalActionFailedException("Moving north failed."); 163 + 164 + // Redraw the window 165 + validatewindow(); 166 + m_window.repaint(); 167 + return wrapBoolean(r); 168 + } 169 + 170 + // Move the agent east 171 + public Term east(String sAgent) throws ExternalActionFailedException 172 + { 173 + // Get the correct agent 174 + Agent agent = getAgent(sAgent); 175 + 176 + // Get the agent his position 177 + Point p = (Point) agent.getPosition().clone(); 178 + p.x = p.x + 1; 179 + 180 + // Set the position for the agent 181 + boolean r = setAgentPosition( agent, p); 182 + 183 + // can't move north 184 + if (!r) throw new ExternalActionFailedException("Moving north failed."); 185 + 186 + // Redraw the window 187 + validatewindow(); 188 + m_window.repaint(); 189 + return wrapBoolean(r); 190 + } 191 + 192 + // Move the agent south 193 + public Term south(String sAgent) throws ExternalActionFailedException 194 + { 195 + // Get the correct agent 196 + Agent agent = getAgent(sAgent); 197 + 198 + // Get the agent his position 199 + Point p = (Point) agent.getPosition().clone(); 200 + p.y = p.y + 1; 201 + 202 + // Set the position for the agent 203 + boolean r = setAgentPosition( agent, p); 204 + 205 + // can't move north 206 + if (!r) throw new ExternalActionFailedException("Moving north failed."); 207 + 208 + // Redraw the window 209 + validatewindow(); 210 + m_window.repaint(); 211 + return wrapBoolean(r); 212 + } 213 + 214 + // Move the agent west 215 + public Term west(String sAgent) throws ExternalActionFailedException 216 + { 217 + // Get the correct agent 218 + Agent agent = getAgent(sAgent); 219 + 220 + // Get the agent his position 221 + Point p = (Point) agent.getPosition().clone(); 222 + p.x = p.x - 1; 223 + 224 + // Set the position for the agent 225 + boolean r = setAgentPosition( agent, p); 226 + 227 + // can't move north 228 + if (!r) throw new ExternalActionFailedException("Moving north failed."); 229 + 230 + // Redraw the window 231 + validatewindow(); 232 + m_window.repaint(); 233 + return wrapBoolean(r); 234 + } 235 + 236 + // Pickup a bomb 237 + public Term pickup( String sAgent ) throws ExternalActionFailedException 238 + { 239 + // Get the agent 240 + Agent agent = getAgent(sAgent); 241 + 242 + // Let everyone know we are going to pick up a bomb 243 + agent.signalPickupBomb.emit(); 244 + 245 + // see if we are not already carrying a bomb 246 + if( agent.atCapacity() ) 247 + { 248 + writeToLog( "Pickup bomb failed" ); 249 + throw new ExternalActionFailedException("Pickup bomb failed"); 250 + } 251 + 252 + // we are not already carying a bomb so get this one 253 + TypeObject bomb = removeBomb( agent.getPosition() ); 254 + if( bomb == null ) 255 + { 256 + writeToLog( "Pickup bomb failed" ); 257 + throw new ExternalActionFailedException("Pickup bomb failed"); 258 + } 259 + 260 + // Yes 261 + agent.signalPickupBombSucces.emit(); 262 + 263 + // there was a bomb at that position, so set token 264 + agent.pickBomb(bomb); 265 + 266 + // show what happened 267 + validatewindow(); 268 + m_window.repaint(); 269 + 270 + return wrapBoolean(true); 271 + } 272 + 273 + // Drop a bomb 274 + public Term drop( String sAgent ) throws ExternalActionFailedException 275 + { 276 + // Get the agent 277 + Agent agent = getAgent(sAgent); 278 + // we are going to drop a bomb 279 + agent.signalDropBomb.emit(); 280 + 281 + TypeObject bomb = agent.senseBomb(); 282 + // see if we are actually carrying a bomb 283 + if( bomb == null) 284 + { 285 + writeToLog( "Drop bomb failed" ); 286 + throw new ExternalActionFailedException("Drop bomb failed"); 287 + } 288 + 289 + Point pos = agent.getPosition(); 290 + // see if we can drop that bomb here 291 + 292 + if( !addBomb(pos) && (isTrap( pos ) == null 293 + || agent.senseBomb( isTrap( pos ).getType() ) == null) ) 294 + { 295 + writeToLog( "Drop bomb failed" ); 296 + throw new ExternalActionFailedException("Drop bomb failed"); 297 + } 298 + 299 + if(isTrap( pos ) != null 300 + && agent.senseBomb( isTrap( pos ).getType() ) != null) 301 + { 302 + signalBombTrapped.emit(); 303 + } 304 + 305 + // unset token 306 + agent.dropBomb(); 307 + 308 + agent.signalDropBombSucces.emit(); 309 + 310 + // Show it 311 + validatewindow(); 312 + m_window.repaint(); 313 + 314 + // return success 315 + return wrapBoolean(true); 316 + } 317 + 318 + // What is the agent his Sense Range 319 + public Term getSenseRange(String agent) 320 + { 321 + // the below function is also used by EnvView 322 + int r = getSenseRange(); 323 + return new APLList(new APLNum(r)); 324 + } 325 + 326 + // Sense all agents. This does not include self. 327 + public synchronized Term senseAllAgent(String sAgent) throws ExternalActionFailedException 328 + { 329 + //Collection c = senseAllAgents(getAgent(agent).getPosition()); 330 + Point position = getAgent(sAgent).getPosition(); 331 + // iterate over all agents 332 + Vector all = new Vector(); 333 + 334 + Iterator j = _agents.iterator(); 335 + while( j.hasNext() ) { 336 + Agent agent = (Agent) j.next(); 337 + Point p = agent.getPosition(); 338 + 339 + // Changed SA: when there are no other agents, this return null, which 340 + // causes Java to throw an exception and never return the empty list. 341 + if (p == null) 342 + continue; 343 + 344 + // skip self 345 + if( p.equals( position ) ) 346 + continue; 347 + 348 + all.add( agent ); 349 + } 350 + 351 + LinkedList<Term> listpar = new LinkedList<Term>(); 352 + for(Object i : all) { 353 + final Agent a = (Agent) i; 354 + APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y)); 355 + listpar.add(tmp); 356 + } 357 + return new APLList(listpar); 358 + } 359 + 360 + // Sense the given agent his position 361 + public synchronized Term sensePosition(String sAgent) throws ExternalActionFailedException 362 + { 363 + Point p = getAgent(sAgent).getPosition(); 364 + return new APLList(new APLNum(p.x),new APLNum(p.y)); 365 + } 366 + 367 + // is there a trap in the senserange of the agent? 368 + public synchronized Term senseTraps(String agent) throws ExternalActionFailedException 369 + { 370 + // Get the agent his position 371 + Point position = getAgent(agent).getPosition(); 372 + 373 + // iterate over all traps and decide according to distance if it is in 374 + // vision range 375 + Vector visible = new Vector(); 376 + 377 + Iterator i = _traps.iterator(); 378 + while( i.hasNext() ) { 379 + TypeObject t = (TypeObject) i.next(); 380 + if( position.distance( t.getPosition() ) <= _senserange ) 381 + visible.add( t ); 382 + } 383 + 384 + return convertCollectionToTerm(visible); 385 + } 386 + 387 + // Get all the traps in the env 388 + public synchronized Term senseAllTraps(String agent) 389 + { 390 + // iterate over all traps 391 + Vector all = new Vector(); 392 + 393 + Iterator i = _traps.iterator(); 394 + while( i.hasNext() ) 395 + { 396 + all.add( (TypeObject) i.next() ); 397 + } 398 + 399 + return convertCollectionToTerm(all); 400 + } 401 + 402 + // Sends a bom in the senserange of the agent 403 + public synchronized Term senseBombs(String agent) throws ExternalActionFailedException 404 + { 405 + // Get the agent his position 406 + Point position = getAgent(agent).getPosition(); 407 + 408 + // iterate over all bombs and decide according to distance if it is in 409 + // vision range 410 + Vector visible = new Vector(); 411 + 412 + Iterator i = _bombs.iterator(); 413 + while( i.hasNext() ) 414 + { 415 + TypeObject b = (TypeObject) i.next(); 416 + if( position.distance( b.getPosition() ) <= _senserange ) 417 + visible.add( b ); 418 + } 419 + 420 + return convertCollectionToTerm(visible); 421 + } 422 + 423 + // Find all the bombs in the environment 424 + public synchronized Term senseAllBombs (String agent) 425 + { 426 + // iterate over all bombs 427 + Vector all = new Vector(); 428 + 429 + Iterator i = _bombs.iterator(); 430 + while( i.hasNext() ) 431 + { 432 + all.add( (TypeObject) i.next() ); 433 + } 434 + 435 + return convertCollectionToTerm(all); 436 + } 437 + 438 + // Sense the stones in the agent senserange 439 + public synchronized Term senseStones(String agent) throws ExternalActionFailedException 440 + { 441 + // Get the agent his position 442 + Point position = getAgent(agent).getPosition(); 443 + 444 + // iterate over all stones and decide according to distance if it is in 445 + // vision range 446 + Vector visible = new Vector(); 447 + 448 + Iterator i = _stones.iterator(); 449 + while( i.hasNext() ) 450 + { 451 + TypeObject t = (TypeObject) i.next(); 452 + if( position.distance( t.getPosition() ) <= _senserange ) 453 + visible.add( t ); 454 + } 455 + 456 + return convertCollectionToTerm(visible); 457 + } 458 + 459 + // Sense all stones 460 + public synchronized Term senseAllStones (String agent) 461 + { 462 + // iterate over all traps 463 + Vector all = new Vector(); 464 + 465 + Iterator i = _stones.iterator(); 466 + while( i.hasNext() ) 467 + { 468 + all.add( (TypeObject) i.next() ); 469 + } 470 + 471 + return convertCollectionToTerm(all); 472 + } 473 + 474 + // Sense visible area for agents. This does not include self. 475 + public synchronized Term senseAgent(String sAgent) throws ExternalActionFailedException 476 + { 477 + //Collection c = senseAgents(getAgent(agent).getPosition()); 478 + Point position = getAgent(sAgent).getPosition(); 479 + 480 + // iterate over all agents and decide according to distance if it is in 481 + // vision range 482 + Vector visible = new Vector(); 483 + 484 + Iterator j = _agents.iterator(); 485 + 486 + while( j.hasNext() ) 487 + { 488 + Agent agent = (Agent) j.next(); 489 + 490 + Point p = agent.getPosition(); 491 + 492 + // Changed SA: when there are no other agents, this return null, which 493 + // causes Java to throw an exception and never return the empty list. 494 + if (p == null) 495 + continue; 496 + 497 + // skip self 498 + if( p.equals( position ) ) 499 + continue; 500 + 501 + // agent within visible range 502 + if( position.distance( p ) <= _senserange ) 503 + visible.add( agent ); 504 + } 505 + 506 + LinkedList<Term> listpar = new LinkedList<Term>(); 507 + for(Object i : visible) { 508 + final Agent a = (Agent) i; 509 + APLListVar tmp = new APLList(new APLIdent(a.getName()),new APLNum(a.getPosition().x),new APLNum(a.getPosition().y)); 510 + listpar.add(tmp); 511 + } 512 + return new APLList(listpar); 513 + } 514 + 515 + 516 + 517 + /* Standard functions --------------------------------------*/ 518 + 519 + private void notifyAgents(APLFunction event, String... receivers) { 520 + throwEvent(event, receivers); 521 + } 522 + 523 + private void notifyEvent(String parm1, Point ptPosition) 524 + { 525 + APLNum nX = new APLNum((double)(ptPosition.getX())); 526 + APLNum nY = new APLNum((double)(ptPosition.getY())); 527 + 528 + // Send an external event to all agents within the senserange. 529 + ArrayList<String> targetAgents = new ArrayList<String>(); 530 + for (Agent a : agentmap.values()) 531 + { 532 + // Changed SA: I got no idea why there is always 1 agent which does not exists, 533 + // but this fixes the exceptions 534 + if ((a.getPosition() != null) && (ptPosition.distance(a.getPosition()) <= getSenseRange())) 535 + targetAgents.add(a.getName()); 536 + } 537 + 538 + writeToLog("EVENT: "+parm1+"("+nX+","+nY+")"+" to "+targetAgents); 539 + 540 + if (!targetAgents.isEmpty()) 541 + { 542 + notifyAgents(new APLFunction(parm1,nX,nY),targetAgents.toArray(new String[0])); 543 + } 544 + } 545 + 546 + // Add an agent to the environment 547 + public synchronized void addAgent(String sAgent) { 548 + String sAgentMain = getMainModule(sAgent); 549 + // Agent not yet in the environment 550 + if (agentmap.keySet().contains(sAgentMain)) { 551 + agentmap.put(sAgent,agentmap.get(sAgentMain)); 552 + writeToLog("linking " + sAgent + ""); 553 + } else{ 554 + final Agent agent = new Agent(sAgentMain); 555 + _agents.add(agent); 556 + agentmap.put(sAgent, agent); 557 + writeToLog("agent " + agent + " added"); 558 + } 559 + } 560 + 561 + // Remove the agent from the environment 562 + public synchronized void removeAgent(String sAgent) 563 + { 564 + try 565 + { 566 + //String sAgentMain = getMainModule(sAgent); 567 + 568 + Agent a = getAgent(sAgent); 569 + agentmap.remove( sAgent ); 570 + 571 + // there can be several agent 572 + if (!agentmap.containsValue(a)) { 573 + _agents.remove(a); 574 + a.reset(); 575 + } 576 + 577 + writeToLog("Agent removed: " + sAgent); 578 + 579 + synchronized( this ) 580 + { 581 + notifyAll(); 582 + } 583 + } 584 + catch (ExternalActionFailedException e) {} 585 + } 586 + 587 + /* END Standard functions --------------------------------------*/ 588 + 589 + 590 + 591 + 592 + /* Helper functions --------------------------------------*/ 593 + 594 + // Get the size of the blockworld 595 + public synchronized Term getWorldSize(String agent) 596 + { 597 + int w = getWidth(); 598 + int h = getHeight(); 599 + return new APLList(new APLNum(w),new APLNum(h)); 600 + } 601 + 602 + // Get the agent from its name 603 + private synchronized Agent getAgent(String name) throws ExternalActionFailedException 604 + { 605 + Agent a = null; 606 + //a = agentmap.get(getMainModule(name)); 607 + a = agentmap.get(name); 608 + if (a==null) throw new ExternalActionFailedException("No such agent: "+name); 609 + else return a; 610 + 611 + } 612 + 613 + private static String getMainModule(String sAgent) 614 + { 615 + int dotPos; 616 + if ((dotPos = sAgent.indexOf('.')) == -1) 617 + return sAgent; 618 + else 619 + return sAgent.substring(0, dotPos); 620 + } 621 + 622 + // Get the environment width 623 + public synchronized int getWidth() { return m_size.width; } 624 + 625 + // Get the environment height 626 + public synchronized int getHeight() { return m_size.height; } 627 + 628 + // Return the agents 629 + public synchronized Collection getBlockWorldAgents() 630 + { 631 + return new Vector(_agents); 632 + } 633 + 634 + // convert a collection to a term 635 + private static Term convertCollectionToTerm(Collection c) 636 + { 637 + LinkedList<Term> listpar = new LinkedList<Term>(); 638 + for(Object i : c) { 639 + final TypeObject o = (TypeObject) i; 640 + APLListVar tmp = new APLList(new APLIdent(o.getType()),new APLNum(o.getPosition().x),new APLNum(o.getPosition().y)); 641 + listpar.add(tmp); 642 + } 643 + return new APLList(listpar); 644 + } 645 + 646 + // Get senserange 647 + public int getSenseRange() 648 + { 649 + return _senserange; 650 + } 651 + 652 + // Redrawing the window is a nightmare, this does some redraw stuff 653 + private void validatewindow() 654 + { 655 + Runnable repaint = new Runnable() 656 + { 657 + public void run() 658 + { 659 + //try {Thread.sleep(500);} catch(Exception e) {} 660 + m_window.doLayout(); 661 + 662 + /*if (!m_window.isVisible()) 663 + { 664 + m_window.setVisible( true ); 665 + }*/ 666 + } 667 + }; 668 + SwingUtilities.invokeLater(repaint); 669 + } 670 + 671 + // Move the agent 672 + private synchronized boolean setAgentPosition( Agent agent, Point position) 673 + { 674 + agent.signalMove.emit(); 675 + 676 + if( isOutOfBounds( position ) ) 677 + return false; 678 + 679 + // suspend thread if some other agent is blocking our entrance 680 + 681 + // Is the position free? 682 + if( !isFree( position ) ) 683 + return false; 684 + 685 + agent.signalMoveSucces.emit(); 686 + 687 + // there may be other threads blocked because this agent was in the way, 688 + // notify 689 + // them of the changed state of environment 690 + synchronized( this ) 691 + { 692 + notifyAll(); 693 + } 694 + 695 + // set the agent position 696 + agent._position = position; 697 + return true; 698 + } 699 + 700 + // check if point is within environment boundaries 701 + // return false is p is within bounds 702 + protected boolean isOutOfBounds( Point p ) 703 + { 704 + if( (p.x >= m_size.getWidth()) || (p.x < 0) || (p.y >= m_size.getHeight()) || (p.y < 0) ) 705 + { 706 + return true; 707 + } 708 + 709 + return false; 710 + } 711 + 712 + // Is the position free? 713 + public synchronized boolean isFree( final Point position ) 714 + { 715 + return (isStone( position )) == null && (isAgent( position ) == null); 716 + } 717 + 718 + // Check for agent at coordinate. \return Null if there is no agent at the 719 + // specified coordinate. Otherwise return a reference to the agent there. 720 + public synchronized Agent isAgent( final Point p ) 721 + { 722 + synchronized (_agents) { 723 + Iterator i = _agents.iterator(); 724 + while( i.hasNext() ) { 725 + final Agent agent = (Agent) i.next(); 726 + if( p.equals( agent.getPosition() ) ) 727 + return agent; 728 + } 729 + return null; 730 + } 731 + } 732 + 733 + // Is there a stone at this point 734 + public synchronized TypeObject isStone( Point p ) 735 + { 736 + synchronized (_agents) { 737 + Iterator i = _stones.iterator(); 738 + while( i.hasNext() ) { 739 + TypeObject stones = (TypeObject) i.next(); 740 + if( p.equals( stones.getPosition() ) ) 741 + return stones; 742 + } 743 + return null; 744 + } 745 + } 746 + 747 + // see if there is a trap at the specified coordinate 748 + public synchronized TypeObject isTrap( Point p ) { 749 + synchronized (_traps) { 750 + Iterator i = _traps.iterator(); 751 + while( i.hasNext() ) { 752 + TypeObject trap = (TypeObject) i.next(); 753 + if( p.equals( trap.getPosition() ) ) 754 + return trap; 755 + } 756 + return null; 757 + } 758 + } 759 + 760 + public synchronized TypeObject isBomb( Point p ) 761 + { 762 + synchronized (_bombs) { 763 + Iterator i = _bombs.iterator(); 764 + while( i.hasNext() ) { 765 + TypeObject bomb = (TypeObject)i.next(); 766 + if(p.equals(bomb.getPosition())) 767 + return bomb; 768 + } 769 + return null; 770 + } 771 + } 772 + 773 + // Remove bomb at position TODO Jaap; why is this different then remove stone??? 774 + public synchronized TypeObject removeBomb( Point position ) 775 + { 776 + // find bomb in bombs list 777 + synchronized(this) { 778 + Iterator i = _bombs.iterator(); 779 + while (i.hasNext()) 780 + { 781 + TypeObject bomb = (TypeObject) i.next(); 782 + if (position.equals(bomb.getPosition())) 783 + { 784 + i.remove(); 785 + return bomb; 786 + } 787 + } 788 + } 789 + 790 + notifyEvent("bombRemovedAt", position); 791 + return null; 792 + 793 + } 794 + 795 + // remove stone at position 796 + public synchronized boolean removeStone( Point position ) 797 + { 798 + synchronized(_stones) { 799 + // find stone in stones list 800 + Iterator i = _stones.iterator(); 801 + while (i.hasNext()) 802 + //if( position.equals( i.next() ) ) { 803 + // Changed SA: 804 + if (position.equals(((TypeObject)i.next()).getPosition())) 805 + { 806 + i.remove(); 807 + 808 + // there may be other threads blocked because this agent was in 809 + // the way, notify 810 + // them of the changed state of environment 811 + synchronized( this ) 812 + { 813 + notifyAll(); 814 + } 815 + 816 + return true; 817 + } 818 + } 819 + notifyEvent("wallRemovedAt", position); 820 + return false; 821 + } 822 + 823 + // remove trap at position 824 + public synchronized boolean removeTrap( Point position ) 825 + { 826 + synchronized(_traps) { 827 + // find trap in traps list 828 + Iterator i = _traps.iterator(); 829 + while (i.hasNext()) { 830 + if (position.equals(((TypeObject)i.next()).getPosition())) 831 + { 832 + i.remove(); 833 + 834 + 835 + // Sohan: I believe this notification is unnecessary, commented it out: 836 + //synchronized( this ) { 837 + // notifyAll(); 838 + //} 839 + 840 + return true; 841 + } 842 + } 843 + } 844 + 845 + notifyEvent("trapRemovedAt", position); 846 + return false; 847 + } 848 + 849 + // Add a stone at the given position 850 + public synchronized boolean addStone( Point position ) throws IndexOutOfBoundsException 851 + { 852 + // valid coordinate 853 + if( isOutOfBounds( position ) ) 854 + throw new IndexOutOfBoundsException( "setStone out of range: " 855 + + position + ", " + m_size ); 856 + 857 + // is position clear of other stuff 858 + // Changed SA: 859 + if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 860 + return false; 861 + 862 + synchronized (_stones) { 863 + _stones.add( new TypeObject(_objType,position) ); 864 + } 865 + notifyEvent("wallAt", position); 866 + 867 + return true; 868 + } 869 + 870 + // Add a bomb to the environment 871 + public synchronized boolean addBomb( Point position ) throws IndexOutOfBoundsException 872 + { 873 + if( isOutOfBounds( position ) ) 874 + throw new IndexOutOfBoundsException( "addBomb outOfBounds: " 875 + + position + ", " + m_size ); 876 + 877 + // is position clear of other stuff 878 + if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 879 + return false; 880 + 881 + // all clear, accept bomb 882 + synchronized (_bombs) { 883 + _bombs.add( new TypeObject(_objType,position) ); 884 + } 885 + notifyEvent("bombAt", position); 886 + 887 + return true; 888 + } 889 + 890 + // Add a trap at the given position 891 + public synchronized boolean addTrap( Point position ) throws IndexOutOfBoundsException { 892 + // valid coordinate 893 + if( isOutOfBounds( position ) ) 894 + throw new IndexOutOfBoundsException( "setTrap out of range: " 895 + + position + ", " + m_size ); 896 + 897 + // is position clear of other stuff 898 + // Changed SA: 899 + if( isBomb( position ) != null || isStone( position ) != null || isTrap( position ) != null ) 900 + return false; 901 + synchronized(_traps) { 902 + _traps.add( new TypeObject(_objType,position) ); 903 + } 904 + notifyEvent("trapAt", position); 905 + 906 + return true; 907 + } 908 + 909 + // Print a message to the console 910 + static public void writeToLog(String message) { 911 + //System.out.println("blockworld: " + message); 912 + } 913 + 914 + // helper function to wrap a boolean value inside a ListPar. 915 + static public APLListVar wrapBoolean( boolean b ) 916 + { 917 + return new APLList(new APLIdent(b ? "true" : "false")); 918 + } 919 + 920 + // Which color does the agent want to be! 921 + private int getColorID(String sColor) 922 + { 923 + if (sColor.equals("army") ) 924 + { 925 + return 0; 926 + } 927 + else if (sColor.equals("blue") ) 928 + { 929 + return 1; 930 + } 931 + else if (sColor.equals("gray") ) 932 + { 933 + return 2; 934 + } 935 + else if (sColor.equals("green") ) 936 + { 937 + return 3; 938 + } 939 + else if (sColor.equals("orange") ) 940 + { 941 + return 4; 942 + } 943 + else if (sColor.equals("pink") ) 944 + { 945 + return 5; 946 + } 947 + else if (sColor.equals("purple") ) 948 + { 949 + return 6; 950 + } 951 + else if (sColor.equals("red") ) 952 + { 953 + return 7; 954 + } 955 + else if (sColor.equals("teal") ) 956 + { 957 + return 8; 958 + } 959 + else if (sColor.equals("yellow") ) 960 + { 961 + return 9; 962 + } 963 + 964 + // Red is the default 965 + return 7; 966 + } 967 + 968 + // Set the senserange 969 + public void setSenseRange( int senserange ) 970 + { 971 + _senserange = senserange; 972 + signalSenseRangeChanged.emit(); 973 + } 974 + 975 + // helper function, calls setSize(Dimension) 976 + public void setSize( int width, int height ) 977 + { 978 + setSize( new Dimension( width, height ) ); 979 + } 980 + 981 + // resize world 982 + public void setSize( Dimension size ) 983 + { 984 + m_size = size; 985 + signalSizeChanged.emit(); 986 + 987 + Iterator i = _bombs.iterator(); 988 + while( i.hasNext() ) { 989 + if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) ) 990 + i.remove(); 991 + } 992 + i = _stones.iterator(); 993 + while( i.hasNext() ) { 994 + if( isOutOfBounds( (Point) i.next() ) ) 995 + i.remove(); 996 + } 997 + i = _traps.iterator(); 998 + while( i.hasNext() ) { 999 + if( isOutOfBounds( ((TypeObject) i.next()).getPosition() ) ) 1000 + i.remove(); 1001 + } 1002 + } 1003 + 1004 + // what kind of object is it, bomb, stone, wall ? 1005 + public String getObjType() 1006 + { 1007 + return _objType; 1008 + } 1009 + 1010 + // what kind of object is it, bomb, stone, wall ? 1011 + public void setObjType(String objType) 1012 + { 1013 + _objType = objType; 1014 + } 1015 + 1016 + // Remove everything 1017 + public void clear() 1018 + { 1019 + _stones.removeAllElements(); 1020 + _bombs.removeAllElements(); 1021 + _traps.removeAllElements(); 1022 + } 1023 + 1024 + // Save the environment 1025 + public void save( OutputStream destination ) throws IOException 1026 + { 1027 + ObjectOutputStream stream = new ObjectOutputStream( destination ); 1028 + stream.writeObject( m_size ); 1029 + 1030 + stream.writeInt( _senserange ); 1031 + 1032 + stream.writeObject( (Vector) _stones ); 1033 + stream.writeObject( (Vector) _bombs ); 1034 + stream.writeObject( (Vector) _traps ); 1035 + stream.flush(); 1036 + } 1037 + 1038 + // Load the environment 1039 + public void load( InputStream source ) throws IOException, ClassNotFoundException 1040 + { 1041 + ObjectInputStream stream = new ObjectInputStream( source ); 1042 + Dimension size = (Dimension) stream.readObject(); 1043 + 1044 + int senserange = stream.readInt(); 1045 + 1046 + Vector stones = (Vector) stream.readObject(); 1047 + Vector bombs = (Vector) stream.readObject(); 1048 + Vector traps = (Vector) stream.readObject(); 1049 + 1050 + // delay assignments until complete load is succesfull 1051 + m_size = size; 1052 + _senserange = senserange; 1053 + 1054 + signalSizeChanged.emit(); 1055 + signalTrapChanged.emit(); 1056 + signalSenseRangeChanged.emit(); 1057 + 1058 + clear(); 1059 + 1060 + _stones.addAll( stones ); 1061 + _bombs.addAll( bombs ); 1062 + _traps.addAll( traps ); 1063 + } 1064 + 1065 + /* END Helper functions --------------------------------------*/ 1066 + 1067 + /* Listeners ------------------------------------------------*/ 1068 + 1069 + // / This listener is notified upon changes regarding the Agent list. 1070 + // / Please note that this only involves registering new agents or 1071 + // / removing existing agents. To track agent position changes, add 1072 + // / a listener to that specific agent. 1073 + // / \sa Agent 1074 + public void addAgentListener( ObsVectListener o ) 1075 + { 1076 + _agents.addListener( o ); 1077 + } 1078 + 1079 + // / This listener is notified upon changes regarding the Stones list. 1080 + public void addStonesListener( ObsVectListener o ) 1081 + { 1082 + _stones.addListener( o ); 1083 + } 1084 + 1085 + // / This listener is notified upon changes regarding the Bombs list. 1086 + public void addBombsListener( ObsVectListener o ) 1087 + { 1088 + _bombs.addListener( o ); 1089 + } 1090 + 1091 + // / This listener is notified upon changes regarding the Traps list. 1092 + public void addTrapsListener( ObsVectListener o ) 1093 + { 1094 + _traps.addListener( o ); 1095 + } 1096 + 1097 + /* END Liseteners ------------------------------------------*/ 1098 + 1099 + 1100 + /* Overrides for ObsVector ---------------------------------*/ 1101 + 1102 + public void onAdd( int index, Object o ) {} 1103 + 1104 + public void onRemove( int index, Object o ) 1105 + { 1106 + ((Agent) o).deleteObservers(); 1107 + } 1108 + 1109 + /* END Overrides for ObsVector ---------------------------------*/ 1110 + }
+571
code/2apl_platform/environments/blockworld/EnvView.java
··· 1 + package blockworld; 2 + 3 + import java.awt.Color; 4 + import java.awt.Dimension; 5 + import java.awt.Graphics; 6 + import java.awt.Point; 7 + import java.awt.Image; 8 + import java.awt.image.ImageProducer; 9 + import java.net.URL; 10 + import java.net.MalformedURLException; 11 + import java.io.IOException; 12 + import java.awt.event.MouseEvent; 13 + import java.awt.event.MouseListener; 14 + import java.awt.event.MouseMotionListener; 15 + import java.util.HashMap; 16 + import java.util.Map; 17 + import java.util.Iterator; 18 + import java.util.Observable; 19 + import java.util.Observer; 20 + 21 + 22 + import javax.swing.JPanel; 23 + 24 + import blockworld.lib.ObsVectListener; 25 + import blockworld.lib.Signal; 26 + 27 + /// Grid view of agent enviroment ('world') 28 + class EnvView extends JPanel implements ObsVectListener, Observer { 29 + 30 + Map imgMap = new HashMap(); 31 + 32 + // Changed SA: 33 + // String urlPath = "file:/"; 34 + 35 + Image imgStone = null; 36 + // Image imgAgent = null; 37 + // Image imgHolding = null; 38 + Image imgBomb = null; 39 + Image imgTrap = null; 40 + 41 + // Changed SA: 42 + Image[] imgAgents = new Image[10]; 43 + Image[] imgAgentsHolding = new Image[10]; 44 + 45 + private static final long serialVersionUID = -6981513623481400558L; 46 + 47 + class MouseTool implements MouseListener, MouseMotionListener { 48 + Env _env; 49 + 50 + final int STATE_SELECT = 0; 51 + 52 + final int STATE_REMOVE = 1; 53 + 54 + final int STATE_ADDBOMB = 2; 55 + 56 + final int STATE_ADDWALL = 3; 57 + 58 + final int STATE_ADDTRAP = 4; 59 + 60 + int _state = STATE_SELECT; 61 + 62 + public MouseTool( Env env ) { 63 + _env = env; 64 + } 65 + 66 + protected Point toEnv( MouseEvent e ) { 67 + final double cw = getWidth() / _env.getWidth(); 68 + final double ch = getHeight() / _env.getHeight(); 69 + final int cx = (int) ((double) e.getX() / cw); 70 + final int cy = (int) ((double) e.getY() / ch); 71 + return new Point( cx, cy ); 72 + } 73 + 74 + public void drag( MouseEvent e ) 75 + { 76 + final Point p = toEnv( e ); 77 + 78 + // Changed SA: Java is the best option to keep things slow, 79 + // to slow things down a bit more we accessively use exceptions. 80 + // But we don't want to see them all the time! 81 + try 82 + { 83 + // don't draw on agents 84 + if( (_env.isAgent( p ) != null)) 85 + return; 86 + 87 + switch( _state ) { 88 + // add bombs, removing if needed 89 + case STATE_ADDBOMB: 90 + // Changed SA: 91 + if( _env.isStone( p ) != null ) 92 + _env.removeStone( p ); 93 + 94 + if( _env.isTrap( p ) != null ) 95 + _env.removeTrap( p ); 96 + 97 + _env.addBomb( p ); 98 + break; 99 + 100 + // add walls, removing if needed 101 + case STATE_ADDWALL: 102 + if( _env.isBomb( p ) != null ) 103 + _env.removeBomb( p ); 104 + 105 + if( _env.isTrap( p ) != null ) 106 + _env.removeTrap( p ); 107 + 108 + _env.addStone( p ); 109 + break; 110 + 111 + // add traps, removing if needed 112 + case STATE_ADDTRAP: 113 + if( _env.isBomb( p ) != null ) 114 + _env.removeBomb( p ); 115 + 116 + // Changed SA: 117 + if( _env.isStone( p ) != null ) 118 + _env.removeStone( p ); 119 + 120 + _env.addTrap( p ); 121 + break; 122 + 123 + // remove all 124 + case STATE_REMOVE: 125 + // Changed SA: 126 + if( _env.isStone( p ) != null) 127 + _env.removeStone( p ); 128 + 129 + if( _env.isBomb( p ) != null ) 130 + _env.removeBomb( p ); 131 + 132 + if( _env.isTrap( p ) != null ) 133 + _env.removeTrap( p ); 134 + break; 135 + } 136 + } 137 + catch (Exception error){ /* Ignore this*/} 138 + } 139 + 140 + // Changed SA: 141 + public void start( MouseEvent e ) 142 + { 143 + final Point p = toEnv( e ); 144 + 145 + try 146 + { 147 + if (_state == STATE_SELECT) 148 + { // Try to select an agent 149 + // if it's an agent, change selected agent 150 + final Agent a = _env.isAgent(p); 151 + if (a != null) 152 + { 153 + _selectedAgent = (a != _selectedAgent) ? a : null; 154 + signalSelectionChanged.emit(); 155 + repaint(); 156 + // _state = STATE_SELECT; 157 + } 158 + } 159 + else 160 + { // Place the selected object (or erase when chosen) 161 + drag(e); 162 + } 163 + 164 + 165 + /* 166 + // if it's a stone, make it a bomb 167 + if (_env.senseStone( p ) != null) 168 + { 169 + _env.removeStone( p ); 170 + _env.addBomb( p ); 171 + _state = STATE_ADDBOMB; 172 + return; 173 + } 174 + 175 + // if it's a bomb, make it a trap 176 + if( _env.senseBomb( p ) != null ) 177 + { 178 + _env.removeBomb( p ); 179 + _env.addTrap( p ); 180 + _state = STATE_ADDTRAP; 181 + return; 182 + } 183 + 184 + // if it's a trap, remove it 185 + if( _env.senseTrap( p ) != null ) 186 + { 187 + _env.removeTrap( p ); 188 + _state = STATE_REMOVE; 189 + return; 190 + } 191 + 192 + // it's a empty spot, so add a stone 193 + _env.addStone( p ); 194 + _state = STATE_ADDWALL; 195 + */ 196 + } 197 + catch( Exception error ) 198 + { 199 + // ignore error 200 + } 201 + } 202 + 203 + public void mouseClicked( MouseEvent arg0 ) { 204 + } 205 + 206 + public void mouseEntered( MouseEvent arg0 ) { 207 + } 208 + 209 + public void mouseExited( MouseEvent arg0 ) { 210 + } 211 + 212 + public void mousePressed( MouseEvent arg0 ) { 213 + start( arg0 ); 214 + } 215 + 216 + public void mouseReleased( MouseEvent arg0 ) { 217 + } 218 + 219 + public void mouseDragged( MouseEvent arg0 ) { 220 + drag( arg0 ); 221 + } 222 + 223 + public void mouseMoved( MouseEvent arg0 ) { 224 + } 225 + }; 226 + 227 + protected Env _env; 228 + 229 + double _cw = 10; 230 + 231 + double _ch = 10; 232 + 233 + public Signal signalSelectionChanged = new Signal( "Selected agent changed" ); 234 + 235 + // Changed SA: We need something to indicate the window (static and env) that the agent has moved. 236 + // We cannot use an agent for this because they are not created in the beginnning 237 + // public Signal signalRefresh = new Signal( "Refresh of screen" ); 238 + 239 + protected Agent _selectedAgent = null; 240 + 241 + public MouseTool tool; 242 + 243 + EnvView( Env env ) { 244 + _env = env; 245 + 246 + // Changed SA: 247 + /* 248 + try { 249 + String rawUserDir = System.getProperty("user.dir"); 250 + String procUserDir = rawUserDir.replaceAll("\\\\","/"); 251 + int pathLen = procUserDir.length(); 252 + urlPath = urlPath + procUserDir + "/plugins/BlockWorld/"; 253 + System.out.println("Pictogramomslag: "+urlPath); 254 + } catch(Exception xEx) { 255 + xEx.printStackTrace(); 256 + } 257 + */ 258 + 259 + 260 + 261 + try 262 + { 263 + // Changed SA: 264 + imgStone = createImage((ImageProducer)(this.getClass().getResource("images/stone.gif")).getContent()); 265 + } catch(Exception xEx) { 266 + xEx.printStackTrace(); 267 + } 268 + 269 + 270 + 271 + try 272 + { 273 + // Changed SA: 274 + imgAgents[0] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_army.gif")).getContent()); 275 + imgAgents[1] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_blue.gif")).getContent()); 276 + imgAgents[2] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_gray.gif")).getContent()); 277 + imgAgents[3] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_green.gif")).getContent()); 278 + imgAgents[4] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_orange.gif")).getContent()); 279 + imgAgents[5] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_pink.gif")).getContent()); 280 + imgAgents[6] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_purple.gif")).getContent()); 281 + imgAgents[7] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_red.gif")).getContent()); 282 + imgAgents[8] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_teal.gif")).getContent()); 283 + imgAgents[9] = createImage((ImageProducer)(this.getClass().getResource("images/agents/agent_yellow.gif")).getContent()); 284 + } 285 + catch(Exception xEx) 286 + { 287 + xEx.printStackTrace(); 288 + } 289 + 290 + 291 + try 292 + { 293 + // Changed SA: 294 + imgAgentsHolding[0] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_army.gif")).getContent()); 295 + imgAgentsHolding[1] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_blue.gif")).getContent()); 296 + imgAgentsHolding[2] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_gray.gif")).getContent()); 297 + imgAgentsHolding[3] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_green.gif")).getContent()); 298 + imgAgentsHolding[4] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_orange.gif")).getContent()); 299 + imgAgentsHolding[5] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_pink.gif")).getContent()); 300 + imgAgentsHolding[6] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_purple.gif")).getContent()); 301 + imgAgentsHolding[7] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_red.gif")).getContent()); 302 + imgAgentsHolding[8] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_teal.gif")).getContent()); 303 + imgAgentsHolding[9] = createImage((ImageProducer)(this.getClass().getResource("images/agents/holding_yellow.gif")).getContent()); 304 + } catch(Exception xEx) { 305 + xEx.printStackTrace(); 306 + } 307 + 308 + try 309 + { 310 + // Changed SA: 311 + imgBomb = createImage((ImageProducer)(this.getClass().getResource("images/bomb.gif")).getContent()); 312 + } catch(Exception xEx) { 313 + xEx.printStackTrace(); 314 + } 315 + 316 + try 317 + { 318 + // Changed SA: 319 + imgTrap = createImage((ImageProducer)(this.getClass().getResource("images/trap.gif")).getContent()); 320 + } catch(Exception xEx) { 321 + xEx.printStackTrace(); 322 + } 323 + 324 + _env.addAgentListener( new ObsVectListener() 325 + { 326 + public void onAdd( int i, Object o ) 327 + { 328 + final Agent a = (Agent) o; 329 + a.signalDropBomb.addObserver( EnvView.this ); 330 + a.signalPickupBomb.addObserver( EnvView.this ); 331 + a.signalMove.addObserver( EnvView.this ); 332 + repaint(); 333 + } 334 + 335 + public void onRemove( int i, Object o ) { 336 + final Agent a = (Agent) o; 337 + a.signalDropBomb.deleteObserver( EnvView.this ); 338 + a.signalPickupBomb.deleteObserver( EnvView.this ); 339 + a.signalMove.deleteObserver( EnvView.this ); 340 + repaint(); 341 + } 342 + } ); 343 + 344 + // repaint on changes in stones, size, 345 + _env.addStonesListener( this ); 346 + _env.addBombsListener( this ); 347 + _env.addTrapsListener( this ); 348 + _env.signalSenseRangeChanged.addObserver( this ); 349 + _env.signalSizeChanged.addObserver( this ); 350 + _env.signalTrapChanged.addObserver( this ); 351 + 352 + // track mouse events 353 + tool = new MouseTool( _env ); 354 + addMouseListener( tool ); 355 + addMouseMotionListener( tool ); 356 + 357 + } 358 + 359 + 360 + public Dimension getPreferredSize() { 361 + return new Dimension( _env.getWidth() * (int) _cw, _env.getHeight() 362 + * (int) _ch ); 363 + } 364 + 365 + // / \todo fix size stuff, it's now done incorrectly (use 100 * 100 grid to 366 + // / see result). this is because the way casting is done (doubles converted 367 + // / to int before use, instead of afterwards) 368 + protected void paintComponent( Graphics g ) { 369 + super.paintComponent( g ); 370 + 371 + final double cw = getWidth() / _env.getWidth(); 372 + final double ch = getHeight() / _env.getHeight(); 373 + 374 + // clear 375 + g.setColor( Color.white ); 376 + g.fillRect( 0, 0, getWidth(), getHeight() ); 377 + 378 + // draw grid contents + lines 379 + for( double x = 0; x < _env.getWidth(); x++ ) 380 + for( double y = 0; y < _env.getHeight(); y++ ) { 381 + final Point p = new Point( (int) x, (int) y ); 382 + 383 + // draw stone (blue square) 384 + // Changed SA 385 + if( _env.isStone( p ) != null ) { 386 + if(imgStone == null) { 387 + g.setColor( Color.blue ); 388 + g.fillRect( (int) (x * cw), (int) (y * ch), (int) cw, 389 + (int) ch ); 390 + } else { 391 + g.drawImage(imgStone, (int) (x * cw + 1), (int) (y * ch + 1), (int) cw - 1, 392 + (int) ch - 1, this); 393 + } 394 + } 395 + 396 + // draw bomb (red oval) 397 + TypeObject bomb = null; 398 + if( (bomb = _env.isBomb( p )) != null ) 399 + { 400 + // Changed SA: there's only one type of bomb! 401 + /* 402 + if(!imgMap.containsKey("bomb_"+bomb.getType())) { 403 + try { 404 + imgMap.put(("bomb_"+bomb.getType()),createImage((ImageProducer)( 405 + new URL(urlPath+"bomb_"+bomb.getType()+".gif")).getContent())); 406 + System.out.println("Type ["+bomb.getType()+"] bomb icon found"); 407 + } catch(Exception xEx) { 408 + xEx.printStackTrace(); 409 + imgMap.put(("bomb_"+bomb.getType()),null); 410 + } 411 + } 412 + if(imgMap.get("bomb_"+bomb.getType()) != null) { 413 + g.drawImage((Image) imgMap.get("bomb_"+bomb.getType()), 414 + (int) (x * cw + 1), (int) (y * ch + 1), (int) cw - 1, 415 + (int) ch - 1, this); 416 + } else*/ 417 + if(imgBomb != null) 418 + { 419 + g.drawImage(imgBomb, (int)(x*cw + 1), (int)(y*ch + 1), (int)(cw-1), (int)(ch-1), this); 420 + } 421 + else 422 + { 423 + g.setColor(Color.red); 424 + g.fillOval((int)(x*cw), (int)(y*ch), (int)cw, (int)ch); 425 + } 426 + } 427 + 428 + // draw trap (orange square) 429 + TypeObject trap = null; 430 + if( (trap = _env.isTrap( p )) != null ) 431 + { 432 + // Changed SA: there's only one type of trap! 433 + /* 434 + if(!imgMap.containsKey("trap_"+trap.getType())) { 435 + try { 436 + imgMap.put(("trap_"+trap.getType()),createImage((ImageProducer)( 437 + new URL(urlPath+"trap_"+trap.getType()+".gif")).getContent())); 438 + System.out.println("Type ["+trap.getType()+"] trap icon found"); 439 + } catch(Exception xEx) { 440 + xEx.printStackTrace(); 441 + imgMap.put(("trap_"+trap.getType()),null); 442 + } 443 + } 444 + if(imgMap.get("trap_"+trap.getType()) != null) { 445 + g.drawImage((Image) imgMap.get("trap_"+trap.getType()), 446 + (int) (x * cw + 1), (int) (y * ch + 1), (int) cw - 1, 447 + (int) ch - 1, this); 448 + } else*/ 449 + if(imgTrap != null) 450 + { 451 + g.drawImage(imgTrap, (int)(x*cw + 1), (int)(y*ch + 1), (int)(cw-1),(int)(ch-1), this); 452 + } 453 + else 454 + { 455 + g.setColor(Color.orange); 456 + g.fillRect((int)(x*cw), (int)(y*ch), (int)cw, (int)ch); 457 + } 458 + } 459 + // draw gridline 460 + g.setColor( Color.gray ); 461 + g.drawRect( (int) (x * cw), (int) (y * ch), (int) cw, (int) ch ); 462 + } 463 + 464 + Iterator a = _env.getBlockWorldAgents().iterator(); 465 + while( a.hasNext() ) { 466 + final Agent agent = (Agent) a.next(); 467 + 468 + // skip agents not entered in the world 469 + if( !agent.isEntered() ) 470 + continue; 471 + 472 + // Changed SA: don't know what this is for, do know it's giving a lot of exceptions 473 + // Seems like you can use a custom picture 474 + /*if(!imgMap.containsKey("agent_"+agent.getName())) { 475 + try { 476 + imgMap.put(("agent_"+agent.getName()),createImage((ImageProducer)( 477 + new URL(urlPath+"agent_"+agent.getName()+".gif")).getContent())); 478 + System.out.println("Unloaded agent ["+agent.getName()+"] icon found"); 479 + } catch(Exception xEx) { 480 + xEx.printStackTrace(); 481 + imgMap.put(("agent_"+agent.getName()),null); 482 + } 483 + } 484 + 485 + if(!imgMap.containsKey("holding_"+agent.getName())) { 486 + try { 487 + imgMap.put(("holding_"+agent.getName()),createImage((ImageProducer)( 488 + new URL(urlPath+"holding_"+agent.getName()+".gif")).getContent())); 489 + System.out.println("Loaded agent ["+agent.getName()+"] icon found"); 490 + } catch(Exception xEx) { 491 + xEx.printStackTrace(); 492 + imgMap.put(("holding_"+agent.getName()),null); 493 + } 494 + }*/ 495 + 496 + // draw agent position 497 + final Point p = agent.getPosition(); 498 + final int x = (int) ((double) p.x * cw); 499 + final int y = (int) ((double) p.y * ch); 500 + if (agent.senseBomb() != null) 501 + { 502 + // Changed SA: there's only one type of agent holding a bomb! 503 + /* 504 + if(imgMap.get("holding_"+agent.getName()) != null) { 505 + g.drawImage((Image) imgMap.get("holding_"+agent.getName()), 506 + x + 1, y + 1, (int) cw - 1,(int) ch - 1, this); 507 + } else*/ 508 + if(imgAgentsHolding[agent._colorID] != null) 509 + { 510 + g.drawImage(imgAgentsHolding[agent._colorID], x+1, y+1, (int)(cw-1), (int)(ch-1), this); 511 + } 512 + else 513 + { 514 + g.setColor((agent != _selectedAgent) ? Color.green : Color.green.darker()); 515 + g.fillRect(x+1, y+1, (int)(cw-1), (int)(ch-1)); 516 + g.setColor(Color.black ); 517 + g.drawRect(x+2, y+2, 3, 3); 518 + } 519 + } 520 + else 521 + { 522 + // Changed SA: there are 10 agents to choose from! 523 + /* 524 + if(imgMap.get("agent_"+agent.getName()) != null) { 525 + g.drawImage((Image) imgMap.get("agent_"+agent.getName()), 526 + x + 1, y + 1, (int) cw - 1,(int) ch - 1, this); 527 + } 528 + else*/ 529 + if(imgAgents[agent._colorID] != null) 530 + { 531 + g.drawImage(imgAgents[agent._colorID], x+1, y+1, (int)(cw-1), (int)(ch-1), this); 532 + } 533 + else 534 + { 535 + g.setColor((agent != _selectedAgent) ? Color.green : Color.green.darker()); 536 + g.fillRect(x+1, y+1, (int)(cw-1), (int)(ch-1)); 537 + } 538 + } 539 + 540 + // draw sensing range 541 + final int rw = (int) ((double) _env.getSenseRange() * cw); 542 + final int rh = (int) ((double) _env.getSenseRange() * ch); 543 + g.setColor( Color.blue ); 544 + g.drawOval( x - (rw), y - (rh), rw * 2, rh * 2 ); 545 + } 546 + } 547 + 548 + public void onRemove( int i, Object o ) { 549 + repaint(); 550 + } 551 + 552 + public void onAdd( int i, Object o ) { 553 + repaint(); 554 + } 555 + 556 + public void update( Observable o, Object arg ) { 557 + repaint(); 558 + // Changed SA: we need some sort of event to indicate to our window we want 559 + // it to update its screen 560 + // signalRefresh.emit(); 561 + } 562 + 563 + public Agent getSelectedAgent() { 564 + return _selectedAgent; 565 + } 566 + 567 + public Env getEnv() { 568 + return _env; 569 + } 570 + 571 + }
+1
code/2apl_platform/environments/blockworld/Manifest.txt
··· 1 + Main-Class: blockworld.Env
+34
code/2apl_platform/environments/blockworld/TypeObject.java
··· 1 + package blockworld; 2 + 3 + import java.awt.Point; 4 + 5 + // APLIdentifiable Object representation in 3APL BlockWorld 6 + public class TypeObject implements java.io.Serializable { 7 + private String _type; 8 + private Point _position; 9 + 10 + public TypeObject( String type, Point position ) { 11 + _type = type; 12 + _position = position; 13 + } 14 + 15 + public String toString() { 16 + return _type+"("+_position.x+","+_position.y+")"; 17 + } 18 + 19 + public Point getPosition() { 20 + return _position; 21 + } 22 + 23 + public void setPosition(Point position) { 24 + _position = position; 25 + } 26 + 27 + public String getType() { 28 + return _type; 29 + } 30 + 31 + public void setType(String type) { 32 + _type = type; 33 + } 34 + }
+452
code/2apl_platform/environments/blockworld/Window.java
··· 1 + package blockworld; 2 + 3 + import java.awt.BorderLayout; 4 + import java.awt.Dimension; 5 + import java.awt.Point; 6 + import java.awt.event.ActionEvent; 7 + import java.awt.event.ActionListener; 8 + import java.io.File; 9 + import java.io.FileInputStream; 10 + import java.io.FileOutputStream; 11 + import java.util.Observable; 12 + import java.util.Observer; 13 + import java.util.StringTokenizer; 14 + import java.util.Vector; 15 + 16 + import javax.swing.JComponent; 17 + import javax.swing.JFileChooser; 18 + import javax.swing.JFrame; 19 + import javax.swing.JMenu; 20 + import javax.swing.JMenuBar; 21 + import javax.swing.JMenuItem; 22 + import javax.swing.JOptionPane; 23 + import javax.swing.JScrollPane; 24 + import javax.swing.JSplitPane; 25 + import javax.swing.JTable; 26 + import javax.swing.table.AbstractTableModel; 27 + 28 + import javax.swing.ImageIcon; 29 + import javax.swing.JButton; 30 + import javax.swing.JToggleButton; 31 + import javax.swing.JToolBar; 32 + import java.util.ArrayList; 33 + 34 + import blockworld.lib.Signal; 35 + 36 + /// statistics view 37 + class Statistics extends AbstractTableModel implements Observer { 38 + 39 + private static final long serialVersionUID = 5372407086612717903L; 40 + 41 + protected EnvView _view = null; 42 + 43 + protected Vector _signals = new Vector(); 44 + 45 + 46 + public int getColumnCount() { 47 + return 2; 48 + } 49 + 50 + public int getRowCount() { 51 + return _signals.size(); 52 + } 53 + 54 + public Object getValueAt( int row, int col ) { 55 + // get requested row 56 + final Signal signal = (Signal) _signals.get( row ); 57 + 58 + // name column requested 59 + if( col == 0 ) 60 + return signal.getName(); 61 + 62 + // count column requested 63 + return new Integer( (int) signal.getEmitCount() ); 64 + } 65 + 66 + public String getColumnName( int i ) { 67 + if( i == 0 ) 68 + return "Signal Description"; 69 + return "Total Emitted Events"; 70 + } 71 + 72 + public Statistics( EnvView view ) { 73 + setEnvView( view ); 74 + } 75 + 76 + // \todo throw illegal argument exception is view == null 77 + public void setEnvView( EnvView view ) { 78 + // remove old listener 79 + if( _view != null ) 80 + _view.signalSelectionChanged.deleteObserver( this ); 81 + 82 + _view = view; 83 + _view.signalSelectionChanged.addObserver( this ); 84 + 85 + 86 + // Changed SA: On a screen refresh of the environment update yourself (call the update( Observable o, Object arg )) 87 + // _view.signalRefresh.addObserver(this); 88 + // agent/env changed 89 + update(); 90 + } 91 + 92 + public void update( Observable o, Object arg ) { 93 + // agent/env changed 94 + update(); 95 + } 96 + 97 + // \todo het is netter als ipv tableStructureChanged, rowsInserted 98 + // en rowsDeleted wordt gesignaled 99 + public void update() { 100 + // empty list 101 + _signals.clear(); 102 + 103 + final Env env = _view.getEnv(); 104 + final Agent agent = _view.getSelectedAgent(); 105 + 106 + // add events 107 + _signals.add( env.signalBombTrapped ); 108 + if( agent != null ) { 109 + _signals.add( agent.signalDropBomb ); 110 + _signals.add( agent.signalDropBombSucces ); 111 + _signals.add( agent.signalMove ); 112 + _signals.add( agent.signalMoveSucces ); 113 + _signals.add( agent.signalPickupBomb ); 114 + _signals.add( agent.signalPickupBombSucces ); 115 + } 116 + 117 + // notify observers 118 + // fireTableRowsUpdated(0, _signals.size() - 1); 119 + 120 + // Changed SA: This is better, this will not change the layout of the complete table! 121 + fireTableDataChanged(); 122 + } 123 + 124 + } 125 + 126 + // / Window with EnvView and buttons for loading/saving 127 + // / \todo implement step blocking function and gray done button 128 + // / when step is not possible 129 + public class Window extends JFrame{ 130 + 131 + private static final long serialVersionUID = -462965463955994316L; 132 + 133 + protected boolean _done = false; 134 + 135 + protected boolean _init = false; 136 + 137 + protected File _lastFile = null; 138 + 139 + private JToolBar m_tbToolbar = null; 140 + private ArrayList<JToggleButton> m_aEditOptions = new ArrayList<JToggleButton>(); 141 + 142 + 143 + public Window( final Env env ){ 144 + super( "Blockworld" ); 145 + 146 + // create clear menuitem + action 147 + JMenuItem clear = new JMenuItem( "Clear environment" ); 148 + clear.addActionListener( new ActionListener() { 149 + public void actionPerformed( ActionEvent e ) { 150 + // popup confirmation dialog 151 + final int rv = JOptionPane.showConfirmDialog( Window.this, 152 + "Are you sure you want to clear the environment", 153 + "Confirm clear", JOptionPane.YES_NO_OPTION ); 154 + 155 + // abort unless YES is pressed 156 + if( rv != JOptionPane.YES_OPTION ) 157 + return; 158 + 159 + env.clear(); 160 + } 161 + } ); 162 + 163 + // create revert menuitem + action 164 + JMenuItem revert = new JMenuItem( "Revert to saved" ); 165 + revert.addActionListener( new ActionListener() { 166 + public void actionPerformed( ActionEvent e ) { 167 + // see if we can revert at all 168 + if( _lastFile == null ) { 169 + JOptionPane 170 + .showMessageDialog( 171 + Window.this, 172 + "You did not load or save this environment yet.\nPlease load or save first", 173 + "Nothing to revert to", 174 + JOptionPane.ERROR_MESSAGE ); 175 + return; 176 + } 177 + 178 + // popup confirmation dialog 179 + final int rv = JOptionPane.showConfirmDialog( Window.this, 180 + "Are you sure you want revert to " 181 + + _lastFile.getPath(), 182 + "Confirm revert to saved", JOptionPane.YES_NO_OPTION ); 183 + 184 + // abort unless YES is pressed 185 + if( rv != JOptionPane.YES_OPTION ) 186 + return; 187 + 188 + try { 189 + env.clear(); 190 + final FileInputStream stream = new FileInputStream( 191 + _lastFile ); 192 + env.load( stream ); 193 + } 194 + catch( Exception ex ) { 195 + System.out.println( "Loading failed! " + ex ); 196 + } 197 + } 198 + } ); 199 + 200 + // create 'load' button + action 201 + JMenuItem load = new JMenuItem( "Load from File" ); 202 + load.addActionListener( new ActionListener() { 203 + public void actionPerformed( ActionEvent e ) { 204 + try { 205 + final File cwd = new File( "." ); 206 + final JFileChooser fc = new JFileChooser( cwd ); 207 + final int rv = fc.showOpenDialog( Window.this ); 208 + 209 + // cancel pressed 210 + if( rv != JFileChooser.APPROVE_OPTION ) 211 + return; 212 + 213 + final File file = fc.getSelectedFile(); 214 + final FileInputStream stream = new FileInputStream( file ); 215 + env.load( stream ); 216 + _lastFile = file; 217 + } 218 + catch( Exception ex ) { 219 + System.out.println( "Loading failed! " + ex ); 220 + } 221 + } 222 + } ); 223 + 224 + JMenuItem save = new JMenuItem( "Save to File" ); 225 + save.addActionListener( new ActionListener() { 226 + public void actionPerformed( ActionEvent e ) { 227 + try { 228 + final File cwd = new File( "." ); 229 + final JFileChooser fc = new JFileChooser( cwd ); 230 + final int rv = fc.showSaveDialog( Window.this ); 231 + 232 + // cancel pressed 233 + if( rv != JFileChooser.APPROVE_OPTION ) 234 + return; 235 + 236 + final File file = fc.getSelectedFile(); 237 + final FileOutputStream stream = new FileOutputStream( file ); 238 + env.save( stream ); 239 + _lastFile = file; 240 + } 241 + catch( Exception ex ) { 242 + System.out.println( "Saving failed! " + ex ); 243 + } 244 + } 245 + } ); 246 + 247 + JMenuItem resize = new JMenuItem( "Environment Size" ); 248 + resize.addActionListener( new ActionListener() { 249 + public void actionPerformed( ActionEvent e ) { 250 + // show dialog 251 + String size = (String) JOptionPane.showInputDialog( 252 + Window.this, 253 + "Resize environment (X * Y) (X, Y) (X x Y) (X Y)", 254 + "Resize environment", JOptionPane.PLAIN_MESSAGE, null, null, env 255 + .getWidth() 256 + + " * " + env.getHeight() ); 257 + 258 + // if a string was returned, parse and set size 259 + if( (size != null) && (size.length() > 0) ) { 260 + StringTokenizer st = new StringTokenizer( size, "*x, " ); 261 + 262 + if( !st.hasMoreTokens() ) 263 + return; 264 + String x = st.nextToken(); 265 + 266 + if( !st.hasMoreTokens() ) 267 + return; 268 + String y = st.nextToken(); 269 + 270 + try { 271 + Dimension d = new Dimension( Integer.parseInt( x ), 272 + Integer.parseInt( y ) ); 273 + 274 + env.setSize( d ); 275 + } 276 + catch( NumberFormatException e1 ) { 277 + JOptionPane 278 + .showMessageDialog( 279 + Window.this, 280 + "Invalid number format, it should be one of (X * Y) (X, Y) (X x Y) (X Y)", 281 + "Error parsing size", 282 + JOptionPane.ERROR_MESSAGE ); 283 + } 284 + } 285 + } 286 + } ); 287 + 288 + JMenuItem setid = new JMenuItem( "Default APLIdentifier" ); 289 + setid.addActionListener( new ActionListener() { 290 + public void actionPerformed( ActionEvent e ) { 291 + // show dialog 292 + String objType = ((String) JOptionPane.showInputDialog( 293 + Window.this, 294 + "Enter default identifier for new bombs/traps (all char, first char in lowercase)", 295 + "Set Default Object APLIdentifier", JOptionPane.PLAIN_MESSAGE, null, null, env.getObjType() )); 296 + // -- validate objType -- 297 + if(objType == null) return; 298 + boolean invalid = false; 299 + for(int i = 0; i < objType.length(); i++) { 300 + char ichar = objType.charAt(i); 301 + if(!Character.isLetter(ichar) || (i ==0 && !Character.isLowerCase(ichar))) { 302 + invalid = true; 303 + break; 304 + } 305 + } 306 + 307 + if(objType.length() == 0 || invalid) { 308 + JOptionPane.showMessageDialog(Window.this, 309 + "The object type identifier must be an all-character string with the first character in lowercase", 310 + "Invalid object type", 311 + JOptionPane.ERROR_MESSAGE ); 312 + return; 313 + } 314 + 315 + env.setObjType(objType); 316 + } 317 + } ); 318 + 319 + JMenuItem senserange = new JMenuItem( "Sensor Range" ); 320 + senserange.addActionListener( new ActionListener() { 321 + public void actionPerformed( ActionEvent e ) { 322 + String range = (String) JOptionPane.showInputDialog( 323 + Window.this, "Set agent sensor range in cells", 324 + "Set Sensor Range in Cells", JOptionPane.PLAIN_MESSAGE, null, null, 325 + Integer.toString( env.getSenseRange() ) ); 326 + 327 + // If a string was returned, parse and set range 328 + if( (range != null) && (range.length() > 0) ) 329 + env.setSenseRange( Integer.parseInt( range ) ); 330 + } 331 + } ); 332 + 333 + JMenuItem about = new JMenuItem( "About BlockWorld" ); 334 + about.addActionListener( new ActionListener() { 335 + public void actionPerformed( ActionEvent e ) { 336 + // show dialog 337 + JOptionPane.showMessageDialog(Window.this, 338 + "BlockWorld for 2APL\n\n"+ 339 + "http://www.cs.uu.nl/2apl/\n\n"+ 340 + "Developed by\n"+ 341 + " The 2APL development group\n"+ 342 + " Utrecht University, the Netherlands", 343 + "About BlockWorld", 344 + JOptionPane.INFORMATION_MESSAGE ); 345 + } 346 + } ); 347 + 348 + JMenu world = new JMenu( "World" ); 349 + world.add( load ); 350 + world.add( save ); 351 + world.add( revert ); 352 + world.add( clear ); 353 + 354 + JMenu properties = new JMenu( "Properties" ); 355 + properties.add( resize ); 356 + properties.add( senserange ); 357 + properties.add( setid ); 358 + 359 + JMenu help = new JMenu( "Help" ); 360 + help.add( about ); 361 + 362 + JMenuBar menubar = new JMenuBar(); 363 + menubar.add( world ); 364 + menubar.add( properties ); 365 + menubar.add( help ); 366 + 367 + // create window 368 + getContentPane().setLayout( new BorderLayout() ); 369 + 370 + final EnvView envView = new EnvView( env ); 371 + final Statistics stats = new Statistics( envView ); 372 + final JComponent statsView = new JScrollPane( new JTable( stats ) ); 373 + 374 + final JSplitPane sp = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, 375 + envView, statsView ); 376 + sp.setOneTouchExpandable( true ); 377 + sp.setDividerLocation( 230 ); 378 + sp.setResizeWeight(1); 379 + getContentPane().add( sp, BorderLayout.CENTER ); 380 + setJMenuBar( menubar ); 381 + 382 + // The toolbar 383 + m_tbToolbar = new JToolBar(); 384 + m_tbToolbar.setFloatable(false); 385 + addButton("info.gif", "Select agent", envView.tool.STATE_SELECT, envView.tool).setSelected(true); 386 + addButton("bomb.gif", "Place bombs", envView.tool.STATE_ADDBOMB, envView.tool); 387 + addButton("stone.gif", "Place walls", envView.tool.STATE_ADDWALL, envView.tool); 388 + addButton("trap.gif", "Place traps", envView.tool.STATE_ADDTRAP, envView.tool); 389 + addButton("eraser.gif", "Erase objects", envView.tool.STATE_REMOVE, envView.tool); 390 + getContentPane().add(m_tbToolbar, BorderLayout.NORTH); 391 + 392 + 393 + // pack(); 394 + setSize( 400, 250 ); 395 + setVisible( true ); 396 + } 397 + 398 + // / the first call to this method will display the 399 + // / edit window and block until user has finished editing 400 + public synchronized void init() { 401 + if( _init ) 402 + return; 403 + 404 + while( !_done ) { 405 + try { 406 + wait(); 407 + } catch( InterruptedException e ) { 408 + } 409 + } 410 + 411 + _init = true; 412 + } 413 + 414 + // / call this method to end the editing session. This function 415 + // / is called from a JButton. 416 + protected synchronized void done() { 417 + _done = true; 418 + notifyAll(); 419 + } 420 + 421 + 422 + public JToggleButton addButton(String sImage, String tooltip, final int nState, final EnvView.MouseTool tool) 423 + { 424 + final JToggleButton button = new JToggleButton(makeIcon(sImage)); 425 + button.addActionListener(new ActionListener() 426 + { 427 + public void actionPerformed(ActionEvent e) 428 + { 429 + tool._state = nState; 430 + 431 + for (JToggleButton cmdButton : m_aEditOptions) 432 + { 433 + if (button != cmdButton) 434 + { 435 + cmdButton.setSelected(false); 436 + } 437 + } 438 + } 439 + }); 440 + button.setToolTipText(tooltip); 441 + m_tbToolbar.add(button); 442 + m_aEditOptions.add(button); 443 + 444 + return button; 445 + } 446 + 447 + private ImageIcon makeIcon(String sImage) 448 + { 449 + sImage = "images/toolbar/"+sImage; 450 + return new ImageIcon(this.getClass().getResource(sImage)); 451 + } 452 + }
code/2apl_platform/environments/blockworld/documents/Blockworld.doc

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/documents/Buglog_blockworld.doc

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_army.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_blue.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_gray.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_green.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_orange.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_pink.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_purple.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_red.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_teal.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/agent_yellow.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_army.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_blue.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_gray.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_green.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_orange.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_pink.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_purple.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_red.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_teal.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/agents/holding_yellow.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/bomb.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/stone.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/toolbar/bomb.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/toolbar/eraser.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/toolbar/info.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/toolbar/stone.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/toolbar/trap.gif

This is a binary file and will not be displayed.

code/2apl_platform/environments/blockworld/images/trap.gif

This is a binary file and will not be displayed.

+74
code/2apl_platform/environments/blockworld/lib/AttrGroup.java
··· 1 + package blockworld.lib; 2 + 3 + import java.awt.GridBagConstraints; 4 + import java.awt.GridBagLayout; 5 + import java.awt.Insets; 6 + 7 + import javax.swing.BorderFactory; 8 + import javax.swing.JComponent; 9 + import javax.swing.JLabel; 10 + import javax.swing.JPanel; 11 + import javax.swing.border.Border; 12 + 13 + /** 14 + * Groups a set of attribute views together into a Panel. The group has a title 15 + * that is shown in a border. 16 + */ 17 + public class AttrGroup extends JPanel { 18 + 19 + private static final long serialVersionUID = -1420253091328660372L; 20 + 21 + protected GridBagLayout gridbag = new GridBagLayout(); 22 + 23 + protected GridBagConstraints constraints = new GridBagConstraints(); 24 + 25 + /** Construct a AttrGroup with the title "Attributes". */ 26 + public AttrGroup() { 27 + this( "Attributes" ); 28 + } 29 + 30 + /** 31 + * Construct a group with the specified title. 32 + * 33 + * @param title 34 + * The title of this group, this shown in a border. 35 + */ 36 + public AttrGroup( String title ) { 37 + Border etche = BorderFactory.createEtchedBorder(); 38 + 39 + setBorder( BorderFactory.createTitledBorder( etche, title ) ); 40 + // setLayout(new GridLayout(0, 2, 5, 0)); 41 + 42 + // / set initial layout settings 43 + setLayout( gridbag ); 44 + constraints.anchor = GridBagConstraints.EAST; 45 + constraints.insets = new Insets( 1, 5, 2, 5 ); 46 + } 47 + 48 + protected void addRow( String descr, JComponent view ) { 49 + JLabel label = new JLabel( descr ); 50 + 51 + label.setLabelFor( view ); 52 + 53 + constraints.gridwidth = GridBagConstraints.RELATIVE; // next-to-last 54 + constraints.fill = GridBagConstraints.NONE; // reset to default 55 + constraints.weightx = 0.0; // reset to default 56 + 57 + gridbag.setConstraints( label, constraints ); 58 + add( label ); 59 + 60 + constraints.gridwidth = GridBagConstraints.REMAINDER; // end row 61 + constraints.fill = GridBagConstraints.HORIZONTAL; 62 + constraints.weightx = 1.0; 63 + gridbag.setConstraints( view, constraints ); 64 + add( view ); 65 + } 66 + 67 + /** 68 + * Add a read only int attribute to this attribute group. This constructs 69 + * the correct view and adds a label showing the attribute name. 70 + */ 71 + public void add( ROIntegerAttr attr ) { 72 + addRow( attr.getName(), new ROIntegerView( attr ) ); 73 + } 74 + }
+18
code/2apl_platform/environments/blockworld/lib/Error.java
··· 1 + package blockworld.lib; 2 + 3 + public class Error extends Exception { 4 + 5 + private static final long serialVersionUID = 1502566209757475201L; 6 + 7 + public Error( String s ) { 8 + super( s ); 9 + } 10 + 11 + public Error( Exception e ) { 12 + super( e.getMessage() ); 13 + } 14 + 15 + public Error( String s, Exception e ) { 16 + super( s + ": " + e.getMessage() ); 17 + } 18 + }
+36
code/2apl_platform/environments/blockworld/lib/IntegerAttr.java
··· 1 + package blockworld.lib; 2 + 3 + public class IntegerAttr extends ROIntegerAttr { 4 + public IntegerAttr( String name ) { 5 + this( name, new Integer( 0 ) ); 6 + } 7 + 8 + public IntegerAttr( String name, Integer value ) { 9 + super( name, value ); 10 + } 11 + 12 + public IntegerAttr( String name, int value ) { 13 + super( name, new Integer( value ) ); 14 + } 15 + 16 + /** 17 + * Set the value, this notifies the IntegerAttrListener of changes. 18 + * 19 + * @param value 20 + * value to be set 21 + */ 22 + public void setValue( Integer value ) { 23 + _value = value; 24 + setChanged(); 25 + notifyObservers( value ); 26 + } 27 + 28 + /** Convenience method. Calls setValue() with integer argument */ 29 + public void setValue( int value ) { 30 + setValue( new Integer( value ) ); 31 + } 32 + 33 + public void setName( String name ) { 34 + _name = name; 35 + } 36 + }
+26
code/2apl_platform/environments/blockworld/lib/IntegerAttrAdapter.java
··· 1 + package blockworld.lib; 2 + 3 + import java.util.Observable; 4 + import java.util.Observer; 5 + 6 + /*! The IntegerAttrAdapter wraps a IntegerAttrListener into a Observer object. 7 + * Calling update on this class will convert the arg parameter into a Integer 8 + * and call FloatAttrListener.onValueChange(). 9 + * \todo this class can go away, implement Observable in IntegerAttr 10 + */ 11 + public class IntegerAttrAdapter implements Observer { 12 + protected IntegerAttrListener _listener; 13 + 14 + public IntegerAttrAdapter( IntegerAttrListener listener ) { 15 + _listener = listener; 16 + } 17 + 18 + public void update( Observable o, Object arg ) { 19 + _listener.onValueChange( (Integer) arg ); 20 + } 21 + 22 + public boolean equals( Object o ) { 23 + return o instanceof IntegerAttrAdapter 24 + && ((IntegerAttrAdapter) o)._listener.equals( _listener ); 25 + } 26 + }
+5
code/2apl_platform/environments/blockworld/lib/IntegerAttrListener.java
··· 1 + package blockworld.lib; 2 + 3 + public interface IntegerAttrListener { 4 + void onValueChange( Integer value ); 5 + }
+259
code/2apl_platform/environments/blockworld/lib/ObsVect.java
··· 1 + package blockworld.lib; 2 + 3 + /* 4 + This file is part of the Bushfire project. 5 + 6 + Copyright 2003, M. Albers, H Boros, R Burema, N. Goh, J. Herold, B. 7 + Maassen, R. Peek, J. Priem, Bas Steunebrink, P. van de Werken. 8 + 9 + The Bushfire project is free software; you can redistribute it 10 + and/or modify it under the terms of the GNU General Public License 11 + as published by the Free Software Foundation; either version 2 of 12 + the License, or (at your option) any later version. 13 + 14 + The Bushfire project is distributed in the hope that it will be 15 + useful, but WITHOUT ANY WARRANTY; without even the implied warranty 16 + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 + General Public License for more details. 18 + 19 + You should have received a copy of the GNU General Public License 20 + along with the Bushfire project; if not, write to the Free Software Foundation, 21 + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 + 23 + $Id: ObsVect.java,v 1.4 2004/09/14 10:58:06 cvs-3apl Exp $ 24 + */ 25 + 26 + import java.util.Collection; 27 + import java.util.Iterator; 28 + import java.util.Observable; 29 + import java.util.Observer; 30 + import java.util.Vector; 31 + 32 + /** 33 + * Any methods that have not been overwritten do not need to be, because those 34 + * methods only call overwritten methods anyway. 35 + */ 36 + public class ObsVect extends Vector { 37 + 38 + private static final long serialVersionUID = 1140345853942753525L; 39 + 40 + protected static final int ADD = 0, REMOVE = 1; 41 + 42 + transient protected MyObservable _observable = new MyObservable(); 43 + 44 + public ObsVect() { 45 + super(); 46 + } 47 + 48 + public ObsVect( Collection c ) { 49 + super( c ); 50 + } 51 + 52 + public ObsVect( int initialCapacity ) { 53 + super( initialCapacity ); 54 + } 55 + 56 + public ObsVect( int initialCapacity, int capacityIncrement ) { 57 + super( initialCapacity, capacityIncrement ); 58 + } 59 + 60 + public ObsVect( ObsVectListener l, int initialCapacity ) { 61 + super( initialCapacity ); 62 + addListener( l ); 63 + } 64 + 65 + public ObsVect( ObsVectListener l, int initialCapacity, 66 + int capacityIncrement ) { 67 + super( initialCapacity, capacityIncrement ); 68 + addListener( l ); 69 + } 70 + 71 + public ObsVect( ObsVectListener l ) { 72 + super(); 73 + addListener( l ); 74 + } 75 + 76 + public ObsVect( ObsVectListener l, Collection c ) { 77 + super( c ); 78 + addListener( l ); 79 + } 80 + 81 + public boolean add( Object obj ) { 82 + addElement( obj ); 83 + return true; 84 + } 85 + 86 + public void addElement( Object obj ) { 87 + synchronized(this) { 88 + super.addElement( obj ); 89 + } 90 + Object[] data = new Object[ 3 ]; 91 + data[ 0 ] = new Integer( ADD ); 92 + data[ 1 ] = new Integer( size() - 1 ); 93 + data[ 2 ] = obj; 94 + _observable.setChanged(); 95 + _observable.notifyObservers( data ); 96 + } 97 + 98 + public boolean addAll( Collection c ) { 99 + return addAll( size(), c ); 100 + } 101 + 102 + public boolean addAll( int index, Collection c ) { 103 + boolean result; 104 + synchronized(this) { 105 + result = super.addAll( index, c ); 106 + } 107 + if( result ) { 108 + Object[] data = new Object[ 3 ]; 109 + data[ 0 ] = new Integer( ADD ); 110 + Iterator iter = c.iterator(); 111 + int i = 0; 112 + while( iter.hasNext() ) { 113 + data[ 1 ] = new Integer( ++i ); 114 + data[ 2 ] = iter.next(); 115 + _observable.setChanged(); 116 + _observable.notifyObservers( data ); 117 + } 118 + return true; 119 + } 120 + return false; 121 + } 122 + 123 + public void insertElementAt( Object obj, int index ) { 124 + synchronized(this) { 125 + super.insertElementAt( obj, index ); 126 + } 127 + Object[] data = new Object[ 3 ]; 128 + data[ 0 ] = new Integer( ADD ); 129 + data[ 1 ] = new Integer( index ); 130 + data[ 2 ] = obj; 131 + _observable.setChanged(); 132 + _observable.notifyObservers( data ); 133 + } 134 + 135 + public synchronized Object remove( int index ) { 136 + Object obj = get( index ); 137 + removeElementAt( index ); 138 + return obj; 139 + } 140 + 141 + public synchronized void removeAllElements() { 142 + Iterator iter = iterator(); 143 + while( iter.hasNext() ) { 144 + iter.next(); 145 + iter.remove(); 146 + } 147 + } 148 + 149 + public void removeElementAt( int index ) { 150 + Object obj; 151 + synchronized (this) { 152 + obj = get( index ); 153 + super.removeElementAt( index ); 154 + } 155 + 156 + Object[] data = new Object[ 3 ]; 157 + data[ 0 ] = new Integer( REMOVE ); 158 + data[ 1 ] = new Integer( index ); 159 + data[ 2 ] = obj; 160 + _observable.setChanged(); 161 + _observable.notifyObservers( data ); 162 + } 163 + 164 + public Object set( int index, Object element ) { 165 + Object removed; 166 + synchronized(this) { 167 + removed = get( index ); 168 + super.set( index, element ); 169 + } 170 + Object[] data = new Object[ 3 ]; 171 + data[ 0 ] = new Integer( REMOVE ); 172 + data[ 1 ] = new Integer( index ); 173 + data[ 2 ] = removed; 174 + _observable.setChanged(); 175 + _observable.notifyObservers( data ); 176 + data[ 0 ] = new Integer( ADD ); 177 + data[ 1 ] = new Integer( index ); 178 + data[ 2 ] = element; 179 + _observable.setChanged(); 180 + _observable.notifyObservers( data ); 181 + return removed; 182 + } 183 + 184 + public void setElementAt( Object obj, int index ) { 185 + Object removed; 186 + synchronized(this) { 187 + removed = get( index ); 188 + super.setElementAt( obj, index ); 189 + } 190 + Object[] data = new Object[ 3 ]; 191 + data[ 0 ] = new Integer( REMOVE ); 192 + data[ 1 ] = new Integer( index ); 193 + data[ 2 ] = removed; 194 + _observable.setChanged(); 195 + _observable.notifyObservers( data ); 196 + data[ 0 ] = new Integer( ADD ); 197 + data[ 1 ] = new Integer( index ); 198 + data[ 2 ] = obj; 199 + _observable.setChanged(); 200 + _observable.notifyObservers( data ); 201 + } 202 + 203 + public synchronized void setSize( int newSize ) { 204 + if( newSize < size() ) { 205 + Iterator iter = listIterator( newSize ); 206 + while( iter.hasNext() ) { 207 + iter.next(); 208 + iter.remove(); 209 + } 210 + } 211 + else 212 + super.setSize( newSize ); 213 + } 214 + 215 + public void addListener( ObsVectListener listener ) { 216 + _observable.addObserver( new ObsVectAdapter( listener ) ); 217 + } 218 + 219 + public void removeListener( ObsVectListener listener ) { 220 + _observable.deleteObserver( new ObsVectAdapter( listener ) ); 221 + } 222 + 223 + private class MyObservable extends Observable { 224 + /** 225 + * we must create a public setChanged because that method is protected 226 + * in Observable 227 + */ 228 + public void setChanged() { 229 + super.setChanged(); 230 + } 231 + } 232 + 233 + protected class ObsVectAdapter implements Observer { 234 + protected ObsVectListener _listener; 235 + 236 + public ObsVectAdapter( ObsVectListener listener ) { 237 + _listener = listener; 238 + } 239 + 240 + public void update( Observable o, Object arg ) { 241 + Object[] _arg = (Object[]) arg; 242 + switch( ((Integer) _arg[ 0 ]).intValue() ) { 243 + case ADD: 244 + _listener.onAdd( ((Integer) _arg[ 1 ]).intValue(), _arg[ 2 ] ); 245 + break; 246 + 247 + case REMOVE: 248 + _listener 249 + .onRemove( ((Integer) _arg[ 1 ]).intValue(), _arg[ 2 ] ); 250 + break; 251 + } 252 + } 253 + 254 + public boolean equals( Object o ) { 255 + return o instanceof ObsVectAdapter 256 + && _listener.equals( ((ObsVectAdapter) o)._listener ); 257 + } 258 + } 259 + }
+31
code/2apl_platform/environments/blockworld/lib/ObsVectListener.java
··· 1 + package blockworld.lib; 2 + 3 + /* 4 + This file is part of the Bushfire project. 5 + 6 + Copyright 2003, M. Albers, H Boros, R Burema, N. Goh, J. Herold, B. 7 + Maassen, R. Peek, J. Priem, Bas Steunebrink, P. van de Werken. 8 + 9 + The Bushfire project is free software; you can redistribute it 10 + and/or modify it under the terms of the GNU General Public License 11 + as published by the Free Software Foundation; either version 2 of 12 + the License, or (at your option) any later version. 13 + 14 + The Bushfire project is distributed in the hope that it will be 15 + useful, but WITHOUT ANY WARRANTY; without even the implied warranty 16 + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 + General Public License for more details. 18 + 19 + You should have received a copy of the GNU General Public License 20 + along with the Bushfire project; if not, write to the Free Software Foundation, 21 + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 + 23 + $Id: ObsVectListener.java,v 1.1 2004/02/17 19:24:46 cvs-3apl Exp $ 24 + */ 25 + 26 + /** the Observable Vector listener interface */ 27 + public interface ObsVectListener { 28 + void onAdd( int index, Object element ); 29 + 30 + void onRemove( int index, Object element ); 31 + }
+76
code/2apl_platform/environments/blockworld/lib/ROIntegerAttr.java
··· 1 + package blockworld.lib; 2 + 3 + /* 4 + 5 + This file is part of the Bushfire project. 6 + 7 + Copyright 2003, M. Albers, H Boros, R Burema, N. Goh, J. Herold, B. 8 + Maassen, R. Peek, J. Priem, Bas Steunebrink, P. van der Werken. 9 + 10 + The Bushfire project is free software; you can redistribute it 11 + and/or modify it under the terms of the GNU General Public License 12 + as published by the Free Software Foundation; either version 2 of 13 + the License, or (at your option) any later version. 14 + 15 + The Bushfire project is distributed in the hope that it will be 16 + useful, but WITHOUT ANY WARRANTY; without even the implied warranty 17 + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 + General Public License for more details. 19 + 20 + You should have received a copy of the GNU General Public License 21 + along with the Bushfire project; if not, write to the Free Software Foundation, 22 + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 + 24 + $Id: ROIntegerAttr.java,v 1.3 2004/09/14 10:58:06 cvs-3apl Exp $ 25 + 26 + */ 27 + 28 + import java.util.Observable; 29 + 30 + public class ROIntegerAttr extends Observable implements Cloneable { 31 + String _name; 32 + 33 + Integer _value; 34 + 35 + public ROIntegerAttr( String name, Integer value ) { 36 + _name = name; 37 + _value = value; 38 + } 39 + 40 + public void addAttributeListener( IntegerAttrListener listener ) { 41 + addObserver( new IntegerAttrAdapter( listener ) ); 42 + } 43 + 44 + public void removeAttributeListener( IntegerAttrListener listener ) { 45 + deleteObserver( new IntegerAttrAdapter( listener ) ); 46 + } 47 + 48 + public int getValue() { 49 + return _value.intValue(); 50 + } 51 + 52 + /** 53 + * Similar to getValue, except that this method returns an Integer instead 54 + * of an int 55 + */ 56 + public Integer getIntegerValue() { 57 + return _value; 58 + } 59 + 60 + public String getName() { 61 + return _name; 62 + } 63 + 64 + /** 65 + * Returns a copy of this object. The clone has no observers. 66 + * 67 + * @return A copy of this object. 68 + */ 69 + public Object clone() throws CloneNotSupportedException { 70 + ROIntegerAttr a = (ROIntegerAttr) super.clone(); 71 + a._name = new String( _name ); 72 + a._value = new Integer( _value.intValue() ); 73 + a.deleteObservers(); 74 + return a; 75 + } 76 + }
+22
code/2apl_platform/environments/blockworld/lib/ROIntegerView.java
··· 1 + package blockworld.lib; 2 + 3 + import javax.swing.JLabel; 4 + 5 + public class ROIntegerView extends JLabel implements IntegerAttrListener { 6 + 7 + private static final long serialVersionUID = -4767694535801030958L; 8 + 9 + protected ROIntegerAttr _attr; 10 + 11 + public ROIntegerView( ROIntegerAttr attr ) { 12 + _attr = attr; 13 + _attr.addAttributeListener( this ); 14 + 15 + // update text 16 + onValueChange( new Integer( attr.getValue() ) ); 17 + } 18 + 19 + public void onValueChange( Integer value ) { 20 + setText( value.toString() ); 21 + } 22 + }
+45
code/2apl_platform/environments/blockworld/lib/Signal.java
··· 1 + package blockworld.lib; 2 + 3 + /// works like an observable, allows emitting signals 4 + import java.util.Observable; 5 + 6 + public class Signal extends Observable { 7 + protected String _name; 8 + 9 + protected long _counter = 0; 10 + 11 + public Signal( String name ) { 12 + _name = name; 13 + } 14 + 15 + public void emit() { 16 + emit( null ); 17 + } 18 + 19 + public void emit( Object o ) { 20 + setChanged(); 21 + notifyObservers( o ); 22 + _counter++; 23 + } 24 + 25 + public void setChanged() { 26 + super.setChanged(); 27 + } 28 + 29 + public void clearChanged() { 30 + super.clearChanged(); 31 + } 32 + 33 + public String toString() { 34 + return _name; 35 + } 36 + 37 + public String getName() { 38 + return _name; 39 + } 40 + 41 + public long getEmitCount() { 42 + return _counter; 43 + } 44 + 45 + }