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

jadex.micro.testcases.DependendServicesAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.testcases;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.bridge.service.types.monitoring.IMonitoringEvent;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishEventLevel;
import jadex.commons.SUtil;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.future.IntermediateDefaultResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.AgentCreated;
import jadex.micro.annotation.Component;
import jadex.micro.annotation.ComponentType;
import jadex.micro.annotation.ComponentTypes;
import jadex.micro.annotation.Configuration;
import jadex.micro.annotation.Configurations;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;

/**
 *  Starts two agents a and b
 *  a has service sa
 *  b has service sb
 *  init of service sb searches for sa and uses the service
 *
 *  (problem is that component a has finished its init but must execute the service call for sb)
 */
@Description("Test if services of (earlier) sibling components can be found and used.")
@Results(@Result(name="testresults", clazz=Testcase.class))
@ComponentTypes({
    @ComponentType(name="a", clazz=AAgent.class),
    @ComponentType(name="b", clazz=BAgent.class)
})
@Configurations(@Configuration(name="def", components={
    @Component(type="a"),
    @Component(type="b")
}))
@Agent
public class DependendServicesAgent
{
	@Agent
	protected IInternalAccess agent;
	
    /**
     *  Init code.
     */
	@AgentCreated
    public IFuture agentCreated()
    {
        final Future ret = new Future();
        getChildrenAccesses().addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener, Void>(ret)
        {
            public void customResultAvailable(Collection result)
            {
            	IExternalAccess[] childs = (IExternalAccess[])result.toArray(new IExternalAccess[0]);
            	System.out.println("childs: "+SUtil.arrayToString(childs));
                final CollectionResultListener> lis = new CollectionResultListener>(childs.length, true, 
                	agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IResultListener>>()
                {
                    public void resultAvailable(Collection> result)
                    {
						System.out.println("fini: "+result);
						List tests = new ArrayList();
//						Collection col = (Collection)result;
						for(Iterator> it=result.iterator(); it.hasNext(); )
						{
							Collection tmp = (Collection)it.next();
							tests.addAll(tmp);
						}
						agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(tests.size(), (TestReport[])tests.toArray(new TestReport[tests.size()])));
						
						agent.killComponent();
                    }
                    
                    public void exceptionOccurred(Exception exception)
                    {
						agent.killComponent(exception);
                    }
                }));

                for(int i=0; i()
					{
						public void intermediateResultAvailable(IMonitoringEvent result)
						{
							child.getResults().addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IResultListener>()
                            {
                                public void resultAvailable(Map res)
                                {
                                  System.out.println("del: "+child.getComponentIdentifier()+" "+res);
//                                    Map res = (Map)result;
                                    List tests = (List)res.get("testcases");
                                    lis.resultAvailable(tests);
                                }
                                public void exceptionOccurred(Exception exception)
                                {
                                	lis.exceptionOccurred(exception);
                                }
                            }));
						}
						
						public void exceptionOccurred(Exception exception)
						{
                        	lis.exceptionOccurred(exception);
						}
					});
                }
//                ret.setException(null);	// ???
                ret.setResult(null);
            }
        }));
        return ret;
    }

    /**
     *  The agent body.
     */
	@AgentBody
    public IFuture executeBody()
    {
        getChildrenAccesses().addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DefaultResultListener>()
        {
            public void resultAvailable(Collection result)
            {
                IExternalAccess[] childs = (IExternalAccess[])result.toArray(new IExternalAccess[0]);
                for(int i=0; i(); // never kill?
    }

	/**
	 *  Get the children (if any).
	 *  @return The children.
	 */
	public IFuture> getChildrenAccesses()
	{
		final Future> ret = new Future>();
		
		SServiceProvider.getService(agent, IComponentManagementService.class, RequiredServiceInfo.SCOPE_PLATFORM)
			.addResultListener(new ExceptionDelegationResultListener>(ret)
		{
			public void customResultAvailable(IComponentManagementService result)
			{
				final IComponentManagementService cms = (IComponentManagementService)result;
				
				cms.getChildren(agent.getComponentIdentifier()).addResultListener(new ExceptionDelegationResultListener>(ret)
				{
					public void customResultAvailable(IComponentIdentifier[] children)
					{
						IResultListener	crl	= new CollectionResultListener(children.length, true,
							new DelegationResultListener>(ret));
						for(int i=0; !ret.isDone() && i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy