All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.bdi.planlib.DefaultBDIVisionProcessor Maven / Gradle / Ivy

Go to download

The Jadex applib BDI package contain ready to use functionalities for BDI agents mostly in form of modules called capabilities.

There is a newer version: 2.4
Show newest version
package jadex.bdi.planlib;

import jadex.bdi.runtime.IBDIExternalAccess;
import jadex.bdi.runtime.IBDIInternalAccess;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.cms.IComponentDescription;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.IValueFetcher;
import jadex.commons.SUtil;
import jadex.commons.SimplePropertyObject;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.transformation.annotations.Classname;
import jadex.extension.envsupport.environment.IEnvironmentSpace;
import jadex.extension.envsupport.environment.IPerceptProcessor;
import jadex.extension.envsupport.environment.ISpaceObject;
import jadex.extension.envsupport.environment.space2d.Space2D;
import jadex.extension.envsupport.math.IVector1;
import jadex.extension.envsupport.math.IVector2;
import jadex.extension.envsupport.math.Vector1Double;
import jadex.javaparser.IParsedExpression;
import jadex.javaparser.SimpleValueFetcher;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *  Default bdi agent vision processor.
 *  Updates the agent's beliefsets according to the percepts of new/disappeared waste.
 */
public class DefaultBDIVisionProcessor extends SimplePropertyObject implements IPerceptProcessor
{
	//-------- constants --------

	/** The percept types property. */
	public static String PROPERTY_PERCEPTTYPES = "percepttypes";
	
	
	/** The add action. */
	public static String ADD = "add";
	
	/** The remove action. */
	public static String REMOVE = "remove";
	
	/** The remove_outdated action (checks all entries in the belief set, if they should be seen, but are no longer there). */
	public static String REMOVE_OUTDATED = "remove_outdated";
	
	/** The set action. */
	public static String SET = "set";
	
	/** The unset action (sets a belief fact to null). */
	public static String UNSET = "unset";
	
	/** The maxrange property. */
	public static String PROPERTY_MAXRANGE = "range";

	/** The maxrange property. */
	public static String PROPERTY_RANGE = "range_property";

	//-------- attributes --------
	
	/** The percepttypes infos. */
	protected Map percepttypes;
	
	//-------- methods --------
	
	/**
	 *  Process a new percept.
	 *  @param space The space.
	 *  @param type The type.
	 *  @param percept The percept.
	 *  @param agent The agent identifier.
	 *  @param agent The avatar of the agent (if any).
	 */
	public void processPercept(final IEnvironmentSpace space, final String type, final Object percept, final IComponentDescription agent, final ISpaceObject avatar)
	{
		boolean	invoke	= false;
		final String[][] metainfos = getMetaInfos(type);
		for(int i=0; !invoke && metainfos!=null && i()
									{
										@Classname("add")
										public IFuture execute(IInternalAccess ia)
										{
											IBDIInternalAccess	scope	= (IBDIInternalAccess)ia;
											Object[]	facts	= scope.getBeliefbase().getBeliefSet(name).getFacts();
											if(cond!=null)
												fetcher.setValue("$facts", facts);
											
											if(!SUtil.arrayContains(facts, percept) && (cond==null || evaluate(cond, fetcher)))
											{
												scope.getBeliefbase().getBeliefSet(name).addFact(percept);
//												System.out.println("added: "+percept+" to: "+belset);
											}
											return IFuture.DONE;
										}
									});
								}
								else if(REMOVE.equals(metainfos[i][0]))
								{
									exta.scheduleStep(new IComponentStep()
									{
										@Classname("remove")
										public IFuture execute(IInternalAccess ia)
										{
											IBDIInternalAccess	scope	= (IBDIInternalAccess)ia;
											Object[]	facts	= scope.getBeliefbase().getBeliefSet(name).getFacts();
											if(cond!=null)
												fetcher.setValue("$facts", facts);
											
											if(SUtil.arrayContains(facts, percept) && (cond==null || evaluate(cond, fetcher)))
											{
												scope.getBeliefbase().getBeliefSet(name).removeFact(percept);
//												System.out.println("removed: "+percept+" from: "+belset);
											}
											return IFuture.DONE;
										}
									});
								}
								else if(SET.equals(metainfos[i][0]))
								{
									exta.scheduleStep(new IComponentStep()
									{
										@Classname("set")
										public IFuture execute(IInternalAccess ia)
										{
											IBDIInternalAccess	scope	= (IBDIInternalAccess)ia;
											Object	fact	= scope.getBeliefbase().getBelief(name).getFact();
											if(cond!=null)
												fetcher.setValue("$fact", fact);
											
											if(cond==null || evaluate(cond, fetcher))
											{
												scope.getBeliefbase().getBelief(name).setFact(percept);
//												System.out.println("set: "+percept+" on: "+belset);
											}
											return IFuture.DONE;
										}
									});
								}
								else if(UNSET.equals(metainfos[i][0]))
								{
									exta.scheduleStep(new IComponentStep()
									{
										@Classname("unset")
										public IFuture execute(IInternalAccess ia)
										{
											IBDIInternalAccess	scope	= (IBDIInternalAccess)ia;
											Object	fact	= scope.getBeliefbase().getBelief(name).getFact();
											if(cond!=null)
												fetcher.setValue("$fact", fact);
											
											if(cond==null || evaluate(cond, fetcher))
											{
												scope.getBeliefbase().getBelief(name).setFact(null);
//												System.out.println("unset: "+percept+" on: "+belset);
											}
											return IFuture.DONE;
										}
									});
								}
								else if(REMOVE_OUTDATED.equals(metainfos[i][0]) && percept.equals(avatar))
								{
									exta.scheduleStep(new IComponentStep()
									{
										@Classname("removeoutdated")
										public IFuture execute(IInternalAccess ia)
										{
											IBDIInternalAccess	scope	= (IBDIInternalAccess)ia;
											Object[]	facts	= scope.getBeliefbase().getBeliefSet(name).getFacts();
											if(cond!=null)
												fetcher.setValue("$facts", facts);
											
											if(cond==null || evaluate(cond, fetcher))
											{
												IVector1 vision	= getRange(avatar);
												Space2D	space2d	= (Space2D)space;
												IVector2	mypos	= (IVector2)avatar.getProperty(Space2D.PROPERTY_POSITION);
												ISpaceObject[]	known	= (ISpaceObject[])facts;
												Set	seen = space2d.getNearObjects(mypos, vision);
												for(int j=0; j




© 2015 - 2025 Weber Informatics LLC | Privacy Policy