jadex.bdi.examples.cleanerworld.cleaner.CleanerPanel 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.cleanerworld.cleaner;
import jadex.bdi.runtime.IBDIInternalAccess;
import jadex.bdi.runtime.IExpression;
import jadex.bdi.runtime.IGoal;
import jadex.bridge.ComponentTerminatedException;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.types.cms.IComponentDescription;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.gui.future.SwingDefaultResultListener;
import jadex.commons.transformation.annotations.Classname;
import jadex.extension.envsupport.environment.ISpaceObject;
import jadex.extension.envsupport.environment.space2d.Space2D;
import jadex.extension.envsupport.math.IVector2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JPanel;
/**
* Panel for showing the cleaner world view.
*/
class CleanerPanel extends JPanel
{
//-------- attributes --------
/** The cleaner agent. */
protected IExternalAccess agent;
/** The latest world view information. */
protected DrawData drawdata;
/** Flag to indicate that the draw data is currently updated to avoid multiple updates in parallel. */
protected boolean updating;
//-------- constructors --------
/**
* Create a cleaner panel.
*/
public CleanerPanel(IExternalAccess agent)
{
this.agent = agent;
}
//-------- JPanel methods --------
/**
* Paint the world view.
*/
protected void paintComponent(Graphics g)
{
if(!updating)
{
updating = true;
try
{
IFuture fut = agent.scheduleStep(new IComponentStep()
{
@Classname("copy")
public IFuture execute(IInternalAccess ia)
{
IBDIInternalAccess bia = (IBDIInternalAccess)ia;
DrawData drawdata = new DrawData();
drawdata.daytime = ((Boolean)bia.getBeliefbase().getBelief("daytime").getFact()).booleanValue();
drawdata.visited_positions = (MapPoint[])bia.getBeliefbase().getBeliefSet("visited_positions").getFacts();
drawdata.max_quantity = ((MapPoint)((IExpression)bia.getExpressionbase().getExpression("query_max_quantity")).execute()).getQuantity();
drawdata.xcnt = ((Integer[])bia.getBeliefbase().getBeliefSet("raster").getFacts())[0].intValue();
drawdata.ycnt = ((Integer[])bia.getBeliefbase().getBeliefSet("raster").getFacts())[1].intValue();
drawdata.cleaners = (ISpaceObject[])bia.getBeliefbase().getBeliefSet("cleaners").getFacts();
drawdata.chargingstations = (ISpaceObject[])bia.getBeliefbase().getBeliefSet("chargingstations").getFacts();
drawdata.wastebins = (ISpaceObject[])bia.getBeliefbase().getBeliefSet("wastebins").getFacts();
drawdata.wastes = (ISpaceObject[])bia.getBeliefbase().getBeliefSet("wastes").getFacts();
drawdata.my_vision = ((Double)bia.getBeliefbase().getBelief("my_vision").getFact()).doubleValue();
drawdata.my_chargestate = ((Double)bia.getBeliefbase().getBelief("my_chargestate").getFact()).doubleValue();
drawdata.myself = (ISpaceObject)bia.getBeliefbase().getBelief("myself").getFact();
drawdata.my_location = (IVector2)drawdata.myself.getProperty("position");
drawdata.my_waste = drawdata.myself.getProperty("waste")!=null;
IGoal[] goals = (IGoal[])bia.getGoalbase().getGoals("achievemoveto");
drawdata.dests = new IVector2[goals.length];
for(int i=0; i(drawdata);
}
});
fut.addResultListener(new SwingDefaultResultListener()
{
public void customResultAvailable(DrawData result)
{
CleanerPanel.this.drawdata = result;
updating = false;
}
public void customExceptionOccurred(Exception exception)
{
// exception.printStackTrace();
updating = false; // Keep to true to disable any more updates
}
});
}
catch(ComponentTerminatedException e)
{
// Keep updating to false to disable any more updates
}
}
if(drawdata!=null)
{
// Paint background (dependent on daytime).
Rectangle bounds = getBounds();
g.setColor(drawdata.daytime ? Color.lightGray : Color.darkGray);
g.fillRect(0, 0, bounds.width, bounds.height);
// Paint map points
double cellh = 1/(double)drawdata.ycnt;
double cellw = 1/(double)drawdata.xcnt;
for(int i=0; i0)
h = (int)(((double)drawdata.visited_positions[i].getQuantity())*cellh/drawdata.max_quantity*bounds.height);
int y = (int)(p.y+cellh/2*bounds.height-h);
g.setColor(new Color(54, 10, 114));
//System.out.println("h: "+h);
g.fillRect(p.x+(int)(cellw*0.3*bounds.width), y,
Math.max(1, (int)(cellw/10*bounds.width)), h);
}
for(int i=0; i