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

jadex.bdiv3.runtime.impl.DefaultAnnotationMapper Maven / Gradle / Ivy

Go to download

BDIV3 Kernel that supports annotated POJOs and uses byte code manipulation for performance improvements.

There is a newer version: 4.0.267
Show newest version
package jadex.bdiv3.runtime.impl;

import java.lang.reflect.Method;
import java.util.List;

import jadex.bdiv3.features.impl.IInternalBDIAgentFeature;
import jadex.bdiv3.model.IBDIModel;
import jadex.bdiv3.model.MGoal;
import jadex.bdiv3.model.MParameter;
import jadex.bdiv3.model.MPlan;
import jadex.bridge.IInternalAccess;
import jadex.commons.MethodInfo;
import jadex.commons.SReflect;

/**
 *  Service mapper that uses the annotations in a goal.
 *  If not present it will try the following:
 *  - use declared goal parameters as service parameters
 *  - use declared goal result
 */
public class DefaultAnnotationMapper implements IServiceParameterMapper
{
	/** The agent. */
	protected IInternalAccess agent;
	
	/** The service name. */
	protected String sername;
	
	/**
	 * 
	 */
	public DefaultAnnotationMapper(String sername, IInternalAccess agent)
	{
		this.sername = sername;
		this.agent = agent;
	}
	
	/**
	 *  Create service parameters.
	 *  @param goal The pojo goal.
	 *  @param m The service method called.
	 *  @return The parameter array for the service call.
	 */
	public Object[] createServiceParameters(T goal, Method m, RPlan plan)
	{
		Object[] ret = null;
		
		IBDIModel model = (IBDIModel)agent.getComponentFeature(IInternalBDIAgentFeature.class).getBDIModel();
		
		boolean done = false;
		
		MGoal mgoal;
		// Use plan parameters in xml case because they should exactly match the call in order and type
		if(goal instanceof RGoal)
		{
			mgoal = (MGoal)((RGoal)goal).getModelElement();
			MPlan mplan = (MPlan)plan.getModelElement();
			List params = mplan.getParameters();
			if(params==null)
			{
				ret = new Object[0];
				done = true;
			}
			else
			{
				ret = new Object[m.getParameterTypes().length];
				int cnt = 0;
				for(MParameter mparam: params)
				{
					if(MParameter.Direction.IN.equals(mparam.getDirection())
						|| MParameter.Direction.INOUT.equals(mparam.getDirection()))
					{
						ret[cnt++] = plan.getParameter(mparam.getName()).getValue();
					}
				}
				done = true;
			}
		}
		else
		{
			mgoal = model.getCapability().getGoal(goal.getClass().getName());
			MethodInfo mi = mgoal.getServiceParameterMapping(sername==null? "": sername);
		
			if(mi!=null)
			{
				try
				{
					Method me = mi.getMethod(agent.getClassLoader());
					ret = (Object[])me.invoke(goal, new Object[]{m});
					done = true;
				}
				catch(RuntimeException e)
				{
					throw e;
				}
				catch(Exception e)
				{
					throw new RuntimeException(e);
				}
			}
		}
		
		// Try using goal parameters if nothing is specified
		if(!done)
		{
			List params = mgoal.getParameters();
			if(params==null)
			{
				ret = new Object[0];
			}
			else
			{
				ret = new Object[params.size()];
				for(int i=0; i params = mplan.getParameters();
			if(params!=null)
			{
				for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy