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

jadex.bdi.planlib.cms.CMSLocalUpdateComponentsPlan 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.cms;

import jadex.bdi.runtime.IBDIInternalAccess;
import jadex.bdi.runtime.Plan;
import jadex.bridge.ComponentTerminatedException;
import jadex.bridge.ICMSComponentListener;
import jadex.bridge.IComponentDescription;
import jadex.bridge.IComponentManagementService;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.TerminationAdapter;
import jadex.commons.future.IFuture;
import jadex.xml.annotation.XMLClassname;

/**
 *  Update the belief set containing the local components.
 */
public class CMSLocalUpdateComponentsPlan extends Plan
{ 
	//-------- attributes --------
	
	/** The listener. */
	protected ICMSComponentListener	listener;
	
	//-------- methods --------
	
	/**
	 *  The body method.
	 */
	public void body()
	{
		final IComponentManagementService	ces	= (IComponentManagementService)getServiceContainer().getRequiredService("cms").get(this);
		this.listener	= new ICMSComponentListener()
		{
			public IFuture componentAdded(final IComponentDescription desc)
			{
				try
				{
					getExternalAccess().scheduleStep(new IComponentStep()
					{
						@XMLClassname("addFact")
						public Object execute(IInternalAccess ia)
						{
							((IBDIInternalAccess)ia).getBeliefbase().getBeliefSet("components").addFact(desc);
							return null;
						}
					});
				}
				catch(ComponentTerminatedException ate)
				{
					ces.removeComponentListener(null, this);
				}
				return IFuture.DONE;
			}
					
			public IFuture componentRemoved(final IComponentDescription desc, java.util.Map results)
			{
				try
				{
					getExternalAccess().scheduleStep(new IComponentStep()
					{
						@XMLClassname("removeFact")
						public Object execute(IInternalAccess ia)
						{
							((IBDIInternalAccess)ia).getBeliefbase().getBeliefSet("components").removeFact(desc);
							return null;
						}
					});
				}
				catch(ComponentTerminatedException ate)
				{
					ces.removeComponentListener(null, this);
				}
				return IFuture.DONE;
			}

			public IFuture componentChanged(IComponentDescription desc)
			{
				return IFuture.DONE;
			}
		};
		
		ces.addComponentListener(null, listener);
		
		IFuture fut = ces.getComponentDescriptions();
		IComponentDescription[] descs = (IComponentDescription[])fut.get(this);
		getBeliefbase().getBeliefSet("components").addFacts(descs);
		
		getScope().addComponentListener(new TerminationAdapter()
		{	
			public void componentTerminated()
			{
				ces.removeComponentListener(null, listener);
			}
		});
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy