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

jadex.bpmn.runtime.handler.EventIntermediateMessageActivityHandler Maven / Gradle / Ivy

Go to download

The Jadex BPMN kernel provides a workflow kernel for the standardized business process modeling notation. The kernel relies on annotated BPMN diagrams, which include detailed execution information.

There is a newer version: 4.0.267
Show newest version
package jadex.bpmn.runtime.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import jadex.bpmn.model.MActivity;
import jadex.bpmn.runtime.ProcessThread;
import jadex.bridge.BasicComponentIdentifier;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IMessageAdapter;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.message.IMessageService;
import jadex.bridge.service.types.message.MessageType;
import jadex.commons.IFilter;
import jadex.commons.SReflect;
import jadex.commons.SUtil;
import jadex.commons.future.IResultListener;

/**
 *  Handler for message events.
 */
public class EventIntermediateMessageActivityHandler extends DefaultActivityHandler
{
	//-------- constants --------
	
	/** The isThrowing property name (distinguishes send/receive events). */
	//public static final String	PROPERTY_THROWING = "isThrowing";	
	
	/** The type property message type identifies the meta type (e.g. fipa). */
	public static final String	PROPERTY_MESSAGETYPE = "messagetype";
	
	/** The filter property describes the filter for receiving a message. */
	public static final String	PROPERTY_FILTER	= "filter";
	
	/** The property message is the message to be sent. */
	public static final String	PROPERTY_MESSAGE = "message";
	
	/** The property message is the message to be sent. */
	public static final String	PROPERTY_CODECIDS = "codecids";
	
	//-------- methods --------
	
	/**
	 *  Execute an activity.
	 *  @param activity	The activity to execute.
	 *  @param instance	The process instance.
	 *  @param thread	The process thread.
	 */
	public void execute(final MActivity activity, final IInternalAccess instance, final ProcessThread thread)
	{
		//boolean	send = thread.hasPropertyValue(PROPERTY_THROWING)? ((Boolean)thread.getPropertyValue(PROPERTY_THROWING)).booleanValue() : false;
		
//		System.out.println("send message acticity2: "+activity);
		
		if(activity.isThrowing())
		{
			sendMessage(activity, instance, thread);
		}
		else
		{
			receiveMessage(activity, instance, thread);
		}
	}
	
	/**
	 *  Send a message.
	 *  @param activity	The activity to execute.
	 *  @param instance	The process instance.
	 *  @param thread	The process thread.
	 */
	protected void sendMessage(final MActivity activity, final IInternalAccess instance, final ProcessThread thread)
	{
		IMessageService ms = SServiceProvider.getLocalService(instance, IMessageService.class, RequiredServiceInfo.SCOPE_PLATFORM);
//		IComponentManagementService cms = SServiceProvider.getLocalService(instance, IComponentManagementService.class, RequiredServiceInfo.SCOPE_PLATFORM);
		String mtname = (String)thread.getPropertyValue(PROPERTY_MESSAGETYPE, activity);
		MessageType mt = mtname!=null? ms.getMessageType(mtname): ms.getMessageType("fipa");
		
		Map msg;
				
		if(thread.hasPropertyValue(PROPERTY_MESSAGE))
		{
			msg = (Map)thread.getPropertyValue(PROPERTY_MESSAGE);
		}
		else
		{
			msg = new HashMap();
		}
			
		// Convenience conversion of strings to component identifiers for receivers.
		String ri = mt.getReceiverIdentifier();
		if(thread.hasPropertyValue(ri))
		{
			Object recs = thread.getPropertyValue(ri);
			List newrecs = new ArrayList();
			if(SReflect.isIterable(recs))
			{
				for(Iterator it=SReflect.getIterator(recs); it.hasNext(); )
				{
					Object rec = it.next();
					if(rec instanceof String)
					{
						newrecs.add(new BasicComponentIdentifier((String)rec, instance.getComponentIdentifier().getParent()));
					}
					else if(rec instanceof IComponentIdentifier)
					{
						newrecs.add((IComponentIdentifier)rec);
					}
				}
			}
			else
			{
				if(recs instanceof String)
				{
					newrecs.add(new BasicComponentIdentifier((String)recs, instance.getComponentIdentifier().getParent()));
				}
				else if(recs instanceof IComponentIdentifier)
				{
					newrecs.add((IComponentIdentifier)recs);
				}
				else
				{
					throw new RuntimeException("Receiver nulls.");
				}
			}
			msg.put(ri, newrecs);
		}
		
		String[] params	= mt.getParameterNames();
		for(int i=0; params!=null && i()
		{
			public void resultAvailable(Void result)
			{
				getBpmnFeature(instance).notify(activity, thread, null);
			}
			
			public void exceptionOccurred(Exception exception)
			{
				thread.setException(exception);
				getBpmnFeature(instance).notify(activity, thread, null);
			}
		});
	}
	
	/**
	 *  Receive a message.
	 *  @param activity	The activity to execute.
	 *  @param instance	The process instance.
	 *  @param thread	The process thread.
	 */
	protected void receiveMessage(final MActivity activity, final IInternalAccess instance, final ProcessThread thread)
	{
		thread.setWaiting(true);
//		thread.setWaitInfo(type);
		IFilter filter = (IFilter)thread.getPropertyValue(PROPERTY_FILTER, activity);
		if(filter==null)
		{
			filter	= new IFilter()
			{
				public boolean filter(Object obj)
				{
					boolean	ret	= obj instanceof IMessageAdapter;
					if(ret)
					{
						try
						{
							IMessageAdapter	msg	= (IMessageAdapter)obj;
							String[]	params	= activity.getPropertyNames();
							for(int i=0; ret && params!=null && i