jadex.bdi.examples.garbagecollector_classic.Environment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-bdi Show documentation
Show all versions of jadex-applications-bdi Show documentation
The Jadex BDI applications package contain
several example applications, benchmarks and
testcases using BDI agents.
package jadex.bdi.examples.garbagecollector_classic;
import jadex.commons.SimplePropertyChangeSupport;
import jadex.commons.collection.MultiCollection;
import jadex.commons.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* The environment.
*/
public class Environment
{
//-------- constants --------
/** The directions. */
public static final String UP = "up";
public static final String DOWN = "down";
public static final String LEFT = "left";
public static final String RIGHT = "right";
/** The world object/agent types. */
public static final String BURNER = "GarbageBurner";
public static final String COLLECTOR = "GarbageCollector";
public static final String GARBAGE = "Garbage";
//-------- attributes --------
/** The size. */
protected int size;
/** The agents (name -> agent info). */
protected Map name_objects;
/** The garbages. */
protected MultiCollection pos_objects;
/** The random number generator. */
protected Random randgen;
/** The helper object for bean events. */
//public ConcurrentPropertyChangeSupport pcs;
public SimplePropertyChangeSupport pcs;
//-------- constructors --------
/**
* Create an environment.
*/
public Environment(int size)
{
this.size = size;
this.name_objects = Collections.synchronizedMap(new HashMap());
this.pos_objects = new MultiCollection();
this.randgen = new Random();
//this.pcs = new ConcurrentPropertyChangeSupport(this);
this.pcs = new SimplePropertyChangeSupport(this);
}
protected static Environment instance;
/**
* Get a singleton instance.
*/
public static Environment getInstance(String type, String name)
{
// System.out.println("Called getInstance with: "+type+" "+name);
if(instance==null)
{
instance = new Environment(10);
//instance.addWorldObject(GARBAGE, "garb#0", new Position(0,0));
//instance.addWorldObject(GARBAGE, "garb#1", new Position(0,0));
// System.out.println("Created new environment object: "+instance);
}
if(type!=null && name!=null)
instance.addWorldObject(type, name, null);
return instance;
}
/**
* Clear the singleton instance.
*/
public static void clearInstance()
{
instance = null;
}
//-------- methods --------
/*java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock();
/**
* Invoke some code with agent behaviour synchronized on the agent.
* @param code The code to execute.
* Note: 1.5 compliant code.
* /
public void getLock()
{
boolean haslock = false;
try{haslock = lock.tryLock(5000, java.util.concurrent.TimeUnit.MILLISECONDS);}
catch(InterruptedException e){e.printStackTrace();}
if(!haslock)
throw new RuntimeException("Could not get lock: "
+Thread.currentThread()+" "+lock);
lock.unlock();
}*/
/**
* Add an object to the environment.
*/
public void addWorldObject(String type, String name, Position pos)
{
WorldObject wo;
// Do not synchronize property change as it may cause microplansteps.
synchronized(this)
{
//System.out.println("[add");
Position newpos = pos!=null? pos: getFreePosition();
if(newpos==null)
newpos = getRandomPosition();
wo = new WorldObject(name, type, newpos);
name_objects.put(name, wo);
pos_objects.put(wo.getPosition(), wo);
}
pcs.firePropertyChange("worldObjects", null, wo);
//System.out.println("add]");
}
/**
* Go in a specific direction.
*/
public void go(String name, String dir)
{
WorldObject wo;
Position pos;
Position newpos;
// Do not synchronize property change as it may cause microplansteps.
synchronized(this)
{
//System.out.println("[go");
assert dir.equals(UP) || dir.equals(DOWN) || dir.equals(LEFT) || dir.equals(RIGHT);
pos = getPosition(name);
newpos = null;
int px = pos.getX();
int py = pos.getY();
if(UP.equals(dir))
newpos = new Position(px, (py-1+size)%size);
else if(DOWN.equals(dir))
newpos = new Position(px, (py+1)%size);
else if(LEFT.equals(dir))
newpos = new Position((px-1+size)%size, py);
else if(RIGHT.equals(dir))
newpos = new Position((px+1)%size, py);
assert newpos!=null;
assert newpos.getX()>=0 && newpos.getX()=0 && newpos.getY()0.5)
{
Position pos = getPosition(name);
WorldObject[] wos = getWorldObjects(pos);
WorldObject garb = null;
for(int i=0; i