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

jadex.base.test.impl.ComponentStartTest Maven / Gradle / Ivy

Go to download

Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.

There is a newer version: 4.0.267
Show newest version
package jadex.base.test.impl;


import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import jadex.base.Starter;
import jadex.base.test.IAbortableTestSuite;
import jadex.base.test.util.STest;
import jadex.bridge.ComponentTerminatedException;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IResourceIdentifier;
import jadex.bridge.LocalResourceIdentifier;
import jadex.bridge.ResourceIdentifier;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.modelinfo.IModelInfo;
import jadex.bridge.service.types.factory.SComponentFactory;
import jadex.commons.future.CounterResultListener;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import junit.framework.TestResult;


/**
 * Test if a component can be started.
 */
public class ComponentStartTest extends ComponentTest
{
	// -------- attributes --------

	/** The delay after which the started component is stopped again. */
	// Extra to super.timeout, because timeout is used by super class also to
	// stop init...
	public long delay;

	// -------- constructors --------

	public ComponentStartTest()
	{
		Logger.getLogger("ComponentStartTest").log(Level.SEVERE, "ComponentSTartTest empty constructor called");
	}

	/**
	 * Create a component test.
	 */
	public ComponentStartTest(IExternalAccess platform, IModelInfo comp, IAbortableTestSuite suite)
	{
		super(platform, comp, suite);
		// Hack???
		delay = 500; // Do not use scaled default timeout, because delay of
						// Timeout.NONE makes no sense.
	}

	// -------- methods --------

	/**
	 * Called when a component has been started.
	 * 
	 * @param cid The cid, set as soon as known.
	 */
	protected void componentStarted(IFuture fut)
	{
		// For start-test kill component after some delay. Let base test collect the result (start exception vs. finished vs. timeout)
//		System.out.println("component start test 0: "+filename);
		fut.then(exta ->
		{
//			IComponentIdentifier	cid = exta.getId();
//			System.out.println("component start test 1: "+filename+", "+cid);

			// Wait some time (simulation and real time) and kill the component
			// afterwards.
			final IResultListener lis = new CounterResultListener(1, new DefaultResultListener()
			{
				public synchronized void resultAvailable(Void result)
				{
					// if(cid.getName().indexOf("ParentProcess")!=-1)
					// System.out.println("destroying "+cid);
					if(platform != null)
					{
						// if(cid.getName().indexOf("ParentProcess")!=-1)
						// System.out.println("destroying1 "+cid);
						try
						{
//							System.out.println("component start test 2: "+exta.getId());
							exta.killComponent().get();
//							System.out.println("component start test 3: "+exta.getId());
						}
						catch(ComponentTerminatedException e)
						{
//							System.out.println("component start test 4: "+exta.getId());
							// ignore, if agent killed itself already
						}
						// if(cid.getName().indexOf("ParentProcess")!=-1)
						// System.out.println("destroying2 "+cid);
					}
				}

				@Override
				public void exceptionOccurred(Exception exception)
				{
					System.err.println("COULD NOT STOP COMPONENT!! Exception:");
					super.exceptionOccurred(exception);
				}
			});
			// {
			// @Override
			// public void resultAvailable(Void result)
			// {
			//// if(cid.getName().indexOf("ParentProcess")!=-1)
			// System.out.println("waiting returned for "+cid);
			// super.resultAvailable(result);
			// }
			// };

			platform.scheduleStep(new IComponentStep()
			{
				public IFuture execute(IInternalAccess ia)
				{
					// if(cid.getName().indexOf("ParentProcess")!=-1)
					// System.out.println("waiting false for "+cid);
					return ia.getFeature(IExecutionFeature.class).waitForDelay(delay, false);
				}
			}).addResultListener(lis);
			
			platform.scheduleStep(new IComponentStep()
			{
				public IFuture execute(IInternalAccess ia)
				{
					// if(cid.getName().indexOf("ParentProcess")!=-1)
					// System.out.println("waiting true for "+cid);
					return ia.getFeature(IExecutionFeature.class).waitForDelay(delay, true);
				}
			}).addResultListener(lis);
		});
	}

	/**
	 * Optional checking after component has finished.
	 * 
	 * @param res The results.
	 */
	protected void checkTestResults(Map res)
	{
		// Nop.
	}

	/**
	 * Get a string representation of this test.
	 */
	public String toString()
	{
		return "start: " + super.toString();
	}

	/**
	 * Command line test: start the given agent in the given rid
	 * 
	 * @param args Two arguments representing the component file name and the
	 *        rid file name.
	 */
	public static void main(String[] args) throws IOException
	{
		IExternalAccess platform = Starter.createPlatform(STest.getLocalTestConfig(ComponentStartTest.class)).get();
//		IComponentManagementService cms = platform.searchService(new ServiceQuery<>(IComponentManagementService.class)).get();

		String filename = null;
		String ridname = null;
		if(args.length == 2)
		{
			filename = args[0];
			ridname = args[1];
		}
		else if(args.length == 0)
		{
			filename = "jadex/bdi/tutorial/TranslationA1.agent.xml";
			ridname = "../jadex-applications-bdi/target/classes";
		}
		else
		{
			System.out.println("Usage: ComponentStartTest  ");
			System.exit(0);
		}

		IResourceIdentifier rid = new ResourceIdentifier(new LocalResourceIdentifier(platform.getId(), new File(ridname).getCanonicalFile().toURI()), null);
//		IModelInfo model = cms.loadComponentModel(filename, rid).get();
		IModelInfo model = SComponentFactory.loadModel(platform, filename, rid).get();

		ComponentStartTest test = new ComponentStartTest(platform, model, null);

		TestResult result = test.run();

		System.out.println("Result (run/error/failure): " + result.runCount() + "/" + result.errorCount() + "/" + result.failureCount());

		platform.killComponent().get();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy