Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
jadex.gpmn.GpmnBDIConverter Maven / Gradle / Ivy
package jadex.gpmn;
import jadex.bdi.OAVBDIModelLoader;
import jadex.bdi.OAVBDIXMLReader;
import jadex.bdi.model.OAVAgentModel;
import jadex.bdi.model.OAVBDIMetaModel;
import jadex.bdi.runtime.interpreter.OAVBDIRuntimeModel;
import jadex.bridge.modelinfo.IModelInfo;
import jadex.commons.collection.MultiCollection;
import jadex.component.ComponentXMLReader;
import jadex.gpmn.model.MActivationEdge;
import jadex.gpmn.model.MActivationPlan;
import jadex.gpmn.model.MBpmnPlan;
import jadex.gpmn.model.MContext;
import jadex.gpmn.model.MContextElement;
import jadex.gpmn.model.MGoal;
import jadex.gpmn.model.MGpmnModel;
import jadex.gpmn.model.MPlanEdge;
import jadex.gpmn.model.MSubprocess;
import jadex.rules.state.IOAVState;
import jadex.rules.state.IOAVStateListener;
import jadex.rules.state.OAVAttributeType;
import jadex.rules.state.OAVObjectType;
import jadex.rules.state.OAVTypeModel;
import jadex.rules.state.io.xml.OAVObjectReaderHandler;
import jadex.rules.state.javaimpl.OAVStateFactory;
import jadex.xml.IContext;
import jadex.xml.IPostProcessor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Class for converting a gpmn model description to an agent description.
*/
public class GpmnBDIConverter
{
//-------- attributes --------
/** The bdi agent loader. */
protected OAVBDIModelLoader loader;
//-------- constructors --------
/**
* Create a new converter.
*/
public GpmnBDIConverter()
{
this.loader = new OAVBDIModelLoader();
}
//-------- methods --------
/**
* Convert a gpmn model to a bdi agent.
*/
public OAVAgentModel convertGpmnModelToBDIAgents(MGpmnModel model, ClassLoader classloader)
{
OAVAgentModel agentmodel = null;
OAVTypeModel typemodel = new OAVTypeModel(model.getModelInfo().getName()+"_typemodel", classloader);
// Requires runtime meta model, because e.g. user conditions can refer to runtime elements (belief, goal, etc.)
typemodel.addTypeModel(OAVBDIRuntimeModel.bdi_rt_model);
IOAVState state = OAVStateFactory.createOAVState(typemodel);
final Set types = new HashSet();
IOAVStateListener listener = new IOAVStateListener()
{
public void objectAdded(Object id, OAVObjectType type, boolean root)
{
// Add the type and its supertypes (if not already contained).
while(type!=null && types.add(type))
type = type.getSupertype();
}
public void objectModified(Object id, OAVObjectType type, OAVAttributeType attr, Object oldvalue, Object newvalue)
{
}
public void objectRemoved(Object id, OAVObjectType type)
{
}
};
// Report report = new Report();
state.addStateListener(listener, false);
Object handle = state.createRootObject(OAVBDIMetaModel.agent_type);
state.setAttributeValue(handle, OAVBDIMetaModel.modelelement_has_name, model.getModelInfo().getName());
//TODO: Add process description
//state.setAttributeValue(handle, OAVBDIMetaModel.modelelement_has_description, model.getDescription());
// state.setAttributeValue(handle, OAVBDIMetaModel.capability_has_package, model.getModelInfo().getPackage());
// String[] imports = (String[]) model.getImports().toArray(new String[0]);
// if(imports!=null)
// {
// for(int i=0; iactivation_plan map
Map activationplanmap = new HashMap();
for (Iterator it = model.getPlanEdges().iterator(); it.hasNext(); )
{
MPlanEdge edge = (MPlanEdge) it.next();
if (model.getActivationPlans().containsKey(edge.getTargetId()))
{
List sourceedges = (List) activationplanmap.get(edge.getSourceId());
if (sourceedges == null)
{
sourceedges = new ArrayList();
activationplanmap.put(edge.getSourceId(), sourceedges);
}
sourceedges.add(model.getActivationPlans().get(edge.getTargetId()));
}
}
// Prepare user_plan->goal map
Map userplanmap = new HashMap();
for (Iterator it = model.getPlanEdges().iterator(); it.hasNext(); )
{
MPlanEdge edge = (MPlanEdge) it.next();
if (model.getBpmnPlans().containsKey(edge.getTargetId()))
{
List targetedges = (List) activationplanmap.get(edge.getTargetId());
if (targetedges == null)
{
targetedges = new ArrayList();
userplanmap.put(edge.getTargetId(), targetedges);
}
targetedges.add(model.getGoals().get(edge.getSourceId()));
}
}
// --- Activation Edge representation ---
// Prepare plan_id->activation_edge map
Map planactivationedges = new HashMap();
for (Iterator it = model.getActivationEdges().iterator(); it.hasNext(); )
{
MActivationEdge edge = (MActivationEdge) it.next();
List outgoingactivationedges = (List) planactivationedges.get(edge.getSourceId());
if (outgoingactivationedges == null)
{
outgoingactivationedges = new ArrayList();
planactivationedges.put(edge.getSourceId(), outgoingactivationedges);
}
outgoingactivationedges.add(edge);
}
// Handle goals
for(Iterator it = model.getGoals().values().iterator(); it.hasNext(); )
{
MGoal goal = (MGoal)it.next();
String name = subprocess? modelname + "." + goal.getName(): goal.getName();
OAVObjectType goaltype = MGoal.Types.ACHIEVE_GOAL.equals(goal.getGoalType())? OAVBDIMetaModel.achievegoal_type:
MGoal.Types.MAINTAIN_GOAL.equals(goal.getGoalType())? OAVBDIMetaModel.maintaingoal_type:
MGoal.Types.PERFORM_GOAL.equals(goal.getGoalType())? OAVBDIMetaModel.performgoal_type: null;
Object goalhandle = createGoal(state, scopehandle, name, goaltype, goal.getRetry(),
goal.getRetryDelay(), goal.getRecur(), goal.getRecurDelay(), goal.getExcludeMode(),
goal.getRetry(), goal.getUnique(), goal.getCreationCondition(), goal.getContextCondition(),
goal.getDropCondition());
if(MGoal.Types.ACHIEVE_GOAL.equals(goal.getGoalType()) && (goal.getTargetCondition()!=null))
{
Object condhandle = state.createObject(OAVBDIMetaModel.condition_type);
state.setAttributeValue(goalhandle, OAVBDIMetaModel.achievegoal_has_targetcondition, condhandle);
state.setAttributeValue(condhandle, OAVBDIMetaModel.expression_has_text, goal.getTargetCondition());
state.setAttributeValue(condhandle, OAVBDIMetaModel.expression_has_language, "jcl");
}
else if(MGoal.Types.MAINTAIN_GOAL.equals(goal.getGoalType()) && goal.getMaintainCondition()!=null)
{
Object condhandle = state.createObject(OAVBDIMetaModel.condition_type);
state.setAttributeValue(goalhandle, OAVBDIMetaModel.maintaingoal_has_maintaincondition, condhandle);
state.setAttributeValue(condhandle, OAVBDIMetaModel.expression_has_text, goal.getMaintainCondition());
state.setAttributeValue(condhandle, OAVBDIMetaModel.expression_has_language, "jcl");
}
// Handle activation plans
List activationplans = (List) activationplanmap.get(goal.getId());
if (activationplans != null)
{
for(Iterator it2 = activationplans.iterator(); it2.hasNext(); )
{
MActivationPlan plan = (MActivationPlan) it2.next();
String actName = "ActivationPlan_Goal:"+name+"_"+String.valueOf(plan.getName());
boolean seq = MActivationPlan.Modes.SEQUENTIAL.equals(plan.getMode());
List activationedges = (List) planactivationedges.get(plan.getId());
if (seq)
{
activationedges = new ArrayList(activationedges);
Collections.sort(activationedges, new Comparator()
{
public int compare(Object o1, Object o2)
{
MActivationEdge ae1 = (MActivationEdge) o1;
MActivationEdge ae2 = (MActivationEdge) o2;
return ae1.getOrder() - ae2.getOrder();
}
});
}
// Create plan
// TODO: Handle orphaned activation plans
Object planhandle = seq
? createPlan(scopehandle, state, actName, "jadex.gpmn.runtime.plan.SequentialGoalExecutionPlan", null, null, "bpmn")
: createPlan(scopehandle, state, actName, "jadex.gpmn.runtime.plan.ParallelGoalExecutionPlan", null, null, "bpmn");
// Create trigger
createPlanTrigger(planhandle, state, new String[]{name}, null, null);
// Create subgoals parameter set
// TODO: Support Subprocesses
List activationtargets = new ArrayList();
for(int j=0; j