I am trying to create a rule out of a model file programmatically and for this, I encounter 2 problems:
1. My model file represents a finite state machine, so it contains some "States". For each State, I create a Node in the Henshin rule, using the method
rule.createNode(..), which adds a node of type State to both "left hand side" and "right hand side". I also want to add an
attribute to the node which is created, but when I add the attribute using the method createAttribute(..), the Attribute is created only in the "left hand side", and therefore, the Action of it, is set to "delete". I wanted to know how I can add an attribute to a node (programmatically) in a way that I can "preserve" it. For this, I do the following steps:
// create module
module = HenshinFactoryImpl.eINSTANCE.createModule();
// create the rule
rule = HenshinFactoryImpl.eINSTANCE.createRule();
// add the rule to the module
module.getUnits().add(rule);
// create a node for each state
for (State state : myModel.getStates()) {
Node stateNode = rule.createNode(state.eClass());
// create isInitial attribute
EAttribute isInitialType = (EAttribute)
getTypeByName(state.eClass(), "isInitial");
String isInitialValue = state.isInitial() ? "true" : "false";
HenshinFactoryImpl.eINSTANCE.
createAttribute(stateNode, isInitialType, isInitialValue);
}
protected EStructuralFeature getTypeByName(EClass eclass, String typeName) {
for (EStructuralFeature sf : eclass.getEAllStructuralFeatures()) {
if (sf.getName() == typeName) {
return sf;
}
}
return null;
}