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

jadex.bpmn.BpmnFactory 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;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import jadex.bpmn.features.impl.BpmnComponentFeature;
import jadex.bpmn.features.impl.BpmnExecutionFeature;
import jadex.bpmn.features.impl.BpmnMessageComponentFeature;
import jadex.bpmn.features.impl.BpmnMonitoringComponentFeature;
import jadex.bpmn.features.impl.BpmnProvidedServicesFeature;
import jadex.bpmn.model.MBpmnModel;
import jadex.bridge.BasicComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IResourceIdentifier;
import jadex.bridge.component.IComponentFeatureFactory;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.component.IMonitoringComponentFeature;
import jadex.bridge.component.impl.ComponentFeatureFactory;
import jadex.bridge.modelinfo.IModelInfo;
import jadex.bridge.service.BasicService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IProvidedServicesFeature;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.factory.IComponentFactory;
import jadex.bridge.service.types.factory.SComponentFactory;
import jadex.bridge.service.types.library.ILibraryService;
import jadex.bridge.service.types.library.ILibraryServiceListener;
import jadex.commons.LazyResource;
import jadex.commons.SReflect;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.kernelbase.IBootstrapFactory;


/**
 *  Factory for loading bpmn processes.
 */
public class BpmnFactory extends BasicService implements IComponentFactory, IBootstrapFactory
{
	//-------- constants --------
	
	/** The supported component types (file extensions).
	 *  Convention used by platform config panel. */
	public static final String[]	FILETYPES	= new String[]{".bpmn", ".bpmn2"};
	
	/** The bpmn process file type. */
	public static final String	FILETYPE_BPMNPROCESS = "BPMN Process";
	
	/** The bpmn legacy process file type. */
	public static final String	FILETYPE_BPMNLEGACYPROCESS = "BPMN Legacy Process";
	
	/** The image icon. */
	protected static final LazyResource ICON = new LazyResource(BpmnFactory.class, "/jadex/bpmn/images/bpmn_process.png");
	
	public static final Collection BPMN_FEATURES = Collections.unmodifiableCollection(
		Arrays.asList(
			new ComponentFeatureFactory(IProvidedServicesFeature.class, BpmnProvidedServicesFeature.class),
			BpmnComponentFeature.FACTORY,
			new ComponentFeatureFactory(IExecutionFeature.class, BpmnExecutionFeature.class),
			BpmnMessageComponentFeature.FACTORY,
			new ComponentFeatureFactory(IMonitoringComponentFeature.class, BpmnMonitoringComponentFeature.class)
		));
	
	//-------- attributes --------
	
	/** The provider. */
	protected IInternalAccess provider;
	
	/** The model loader */
	protected BpmnModelLoader loader;
	
	/** The library service. */
	protected ILibraryService libservice;
	
	/** The library service listener */
	protected ILibraryServiceListener libservicelistener;
	
	/** The properties. */
	protected Map fproperties;
	
	/** The standard + micro component features. */
	protected Collection	features;
	
	//-------- constructors --------
	
	/**
	 *  Create a new factory for startup.
	 *  @param platform	The platform.
	 */
	// This constructor is used by the Starter class and the ADFChecker plugin. 
	public BpmnFactory(String providerid)
	{
		super(new BasicComponentIdentifier(providerid), IComponentFactory.class, null);
		this.loader = new BpmnModelLoader();
		this.features = SComponentFactory.orderComponentFeatures(SReflect.getUnqualifiedClassName(getClass()), Arrays.asList(SComponentFactory.DEFAULT_FEATURES, BPMN_FEATURES));
	}
	
	/**
	 *  Create a new BpmnProcessService.
	 */
	public BpmnFactory(IInternalAccess provider, Map properties)
	{
		super(provider.getComponentIdentifier(), IComponentFactory.class, null);

		this.provider = provider;
		this.loader = new BpmnModelLoader();
		this.fproperties	= properties;
		this.features = SComponentFactory.orderComponentFeatures(SReflect.getUnqualifiedClassName(getClass()), Arrays.asList(SComponentFactory.DEFAULT_FEATURES, BPMN_FEATURES));

		this.libservicelistener = new ILibraryServiceListener()
		{
			public IFuture resourceIdentifierRemoved(IResourceIdentifier parid, IResourceIdentifier rid)
			{
				loader.clearModelCache();
				return IFuture.DONE;
			}
			
			public IFuture resourceIdentifierAdded(IResourceIdentifier parid, IResourceIdentifier rid, boolean rem)
			{
				loader.clearModelCache();
				return IFuture.DONE;
			}
		};
	}
	
	/**
	 *  Start the service.
	 */
	public IFuture startService(IInternalAccess component, IResourceIdentifier rid)
	{
		this.provider = component;
		this.providerid = provider.getComponentIdentifier();
		createServiceIdentifier("BootstrapFactory", IComponentFactory.class, rid, IComponentFactory.class, null);
		return startService();
	}
	
	/**
	 *  Start the service.
	 */
	public IFuture startService()
	{
		final Future ret = new Future();
		SServiceProvider.getService(provider, ILibraryService.class, RequiredServiceInfo.SCOPE_PLATFORM)
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(ILibraryService result)
			{
				libservice = result;
				libservice.addLibraryServiceListener(libservicelistener);
				BpmnFactory.super.startService().addResultListener(new DelegationResultListener(ret));
			}
		});
		return ret;
	}
	
	//-------- methods --------
	
	/**
	 *  Start the service.
	 * /
	public synchronized IFuture	startService()
	{
		return super.startService();
	}*/
	
	/**
	 *  Shutdown the service.
	 *  @param listener The listener.
	 */
	public synchronized IFuture	shutdownService()
	{
		final Future	ret	= new Future();
		libservice.removeLibraryServiceListener(libservicelistener)
			.addResultListener(new DelegationResultListener(ret)
		{
			public void customResultAvailable(Void result)
			{
				BpmnFactory.super.shutdownService().addResultListener(new DelegationResultListener(ret));
			}
		});
		return ret;
	}
	
	/**
	 *  Load a  model.
	 *  @param model The model (e.g. file name).
	 *  @param The imports (if any).
	 *  @return The loaded model.
	 */
	public IFuture loadModel(final String model, final String[] imports, final IResourceIdentifier rid)
	{
		final Future ret = new Future();
//		System.out.println("filename: "+filename);
		
		if(libservice!=null)
		{
			libservice.getClassLoader(rid).addResultListener(
				new ExceptionDelegationResultListener(ret)
			{
				public void customResultAvailable(ClassLoader cl)
				{
					try
					{
						MBpmnModel amodel = loader.loadBpmnModel(model, imports, cl, new Object[]{rid, getProviderId().getRoot()});
						ret.setResult(amodel.getModelInfo());
					}
					catch(Exception e)
					{
						ret.setException(e);
					}
				}
			});
		}
		else
		{
			try
			{
				ClassLoader cl = getClass().getClassLoader();
				MBpmnModel amodel = loader.loadBpmnModel(model, imports, cl, new Object[]{rid, getProviderId().getRoot()});
				ret.setResult(amodel.getModelInfo());
			}
			catch(Exception e)
			{
				ret.setException(e);
			}			
		}
		
		return ret;
	}
	
	/**
	 *  Test if a model can be loaded by the factory.
	 *  @param model The model (e.g. file name).
	 *  @param The imports (if any).
	 *  @return True, if model can be loaded.
	 */
	public IFuture isLoadable(String model, String[] imports, IResourceIdentifier rid)
	{
		return new Future(model.endsWith(".bpmn") || model.endsWith(".bpmn2")? Boolean.TRUE: Boolean.FALSE);
	}

	/**
	 *  Test if a model is startable (e.g. an component).
	 *  @param model The model (e.g. file name).
	 *  @param The imports (if any).
	 *  @return True, if startable (and loadable).
	 */
	public IFuture isStartable(String model, String[] imports, IResourceIdentifier rid)
	{
		return new Future(model.endsWith(".bpmn") || model.endsWith(".bpmn2")? Boolean.TRUE: Boolean.FALSE);
	}
	
	/**
	 *  Get the names of ADF file types supported by this factory.
	 */
	public String[] getComponentTypes()
	{
		return new String[]{FILETYPE_BPMNPROCESS, FILETYPE_BPMNLEGACYPROCESS};
	}

	/**
	 *  Get a default icon for a file type.
	 */
	public IFuture getComponentTypeIcon(String type)
	{
		Future	ret	= new Future();
		if(type.equals(FILETYPE_BPMNPROCESS) || type.equals(FILETYPE_BPMNLEGACYPROCESS))
		{
			try
			{
				ret.setResult(ICON.getData());
			}
			catch(IOException e)
			{
				ret.setException(e);
			}
		}
		else
		{
			ret.setResult(null);
		}
		return ret;
	}	

	/**
	 *  Get the component type of a model.
	 *  @param model The model (e.g. file name).
	 *  @param The imports (if any).
	 */
	public IFuture getComponentType(String model, String[] imports, IResourceIdentifier rid)
	{
		return new Future(model.endsWith(".bpmn") ? FILETYPE_BPMNLEGACYPROCESS : model.endsWith(".bpmn2") ? FILETYPE_BPMNPROCESS : null);
	}
		
	/**
	 *  Get the properties.
	 *  Arbitrary properties that can e.g. be used to
	 *  define kernel-specific settings to configure tools.
	 *  @param type	The component type. 
	 *  @return The properties or null, if the component type is not supported by this factory.
	 */
	public Map	getProperties(String type)
	{
		return FILETYPE_BPMNPROCESS.equals(type) || FILETYPE_BPMNLEGACYPROCESS.equals(type)
		? fproperties : null;
	}
	
	/**
	 *  Get the component features for a model.
	 *  @param model The component model.
	 *  @return The component features.
	 */
	public IFuture> getComponentFeatures(IModelInfo model)
	{
		return new Future>(features);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy