Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [henshin-dev] Rule and Attribute creation via API

Hi Johannes,

Am 06.06.2017 um 20:37 schrieb Johannes Groß:
Hi Daniel,

thanks for the explanation, escaping the name value gets me the desired result.
I do need to create the rules via api since i want to generate them from the UML level model, so i cannot use the henshin UI.

Ah, I see - you're creating Henshin rules from UML models, which is sort of an advanced use-case. We did something similar in a research work [1] where we "turned UML into a transformation language" by extending it with transformation stereotypes, and translating the stereotyped UML models to Henshin rules [1]. I think I could obtain the source code for you, if you're interested.

 [1] Vlad Acretoaie, Harald Störrle, Daniel Strüber: Transparent Model Transformation: Turning Your Favourite Model Editor into a Transformation Tool. ICMT 2015: 121-130 https://www.uni-marburg.de/fb12/arbeitsgruppen/swt/forschung/publikationen/2015/ASS15.pdf



I seem to have another issue now, i am trying to set the name of a newly created Class and the it ends up with the name qualified name of the java object.

Here is how the code proceeds:
// Manually create two nodes on the RHS.
Node rhsClassNode = factory.createNode(rhsGraph, rhsClass.eClass(), null);
Node rhsPackageNode = factory.createNode(rhsGraph, rhsPackage.eClass(), null);
Edge rhsPackEdge = factory.createEdge(rhsPackageNode, rhsClassNode, UMLFactory.eINSTANCE.getUMLPackage().getPackage_PackagedElement());

String rhsname = (String) rhsPackage.eGet(UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name());
rhsPackageNode.setName(rhsname);
rhsClassNode.setName("name");
Attribute nameatt = HenshinFactoryImpl.eINSTANCE.createAttribute();
nameatt.setType(UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name());
nameatt.setValue("name");
rhsClassNode.getAttributes().add(nameatt);

Parameter nameParam = HenshinFactoryImpl.eINSTANCE.createParameter();
nameParam.setKind(ParameterKind.IN);
nameParam.setType(EcoreFactoryImpl.eINSTANCE.getEcorePackage().getEString());
nameParam.setName("name");
nameParam.setUnit(rule);
rule.getParameters().add(nameParam);

Mapping m1 = factory.createMapping(lhsPackageNode, rhsPackageNode);
rule.getMappings().add(m1);

Engine engine = new EngineImpl();
UnitApplication createAccountApp = new UnitApplicationImpl(engine);
createAccountApp.setEGraph(graph);
createAccountApp.setUnit(rule);
createAccountApp.setParameterValue("name", "myGreatName");
if (!createAccountApp.execute(null)) {
    throw new RuntimeException("Error creating class.");
}
Do i need to set the name of the nodes?
In general no. The intended use of node names it is that it allows you to parametrize a node: You can set the node name to a parameter. For LHS nodes, you can then use RuleApplication.setParameterValue() to bind the parameter to a particular object before the rule execution. This is an alternative to searching an object by its name. For RHS nodes, the parameter value is set automatically during the rule application, and can be used to retrieve an object from the changed model after the rule was executed.
Am i specifying the parameter and attribute correctly? Any leads on why the name is set to something weird?
In your case, the unintended behavior seems to arise because you use the same parameter twice, first for the node (via its name), and then also for its "name" attribute. Apparently, during the rule execution, the following things happen: First, the "name" parameter is re-assigned by setting it to the newly created class (thus overwriting your specified value), and then this new value is used to set the "name" attribute (turning it into a String by using its FQN). Therefore, I was able to reproduce your problem, and I also could avoid it by removing the line " rhsClassNode.setName("name"); " Best regards, Daniel
Thanks!
Johannes
2017-06-06 10:18 GMT-07:00 Daniel Strüber <strueber@xxxxxxxxxxxxxxxxxxxxxxxxx>:

Hi Johannes,

there are two different ways of searching an element by its name:

  • If the name is known at design time, you can specify it in a hard-coded manner in the rule, using a string value.
  • If the name is unknown at design time, you can use a parameter as a "placeholder" for the name, and insert the name before you execute the rule (using RuleApplication.setParameter())

If you want to use a hard-coded string in an attribute value, you have to escape it in quotation marks. If you want to use a parameter, you can just specify the parameter name without quotation marks.

So what happens in your example code, is that you don't escape the name value. Consequently, the Henshin interpreter assumes that there should be a parameter called myPackage, even though you didn't declare one - so you will get an error. To fix this, just escape the value by adding quotation marks at the beginning and end of the name.

More generally, it's usually a good idea to define rules by using the graphical editor, because it shows you error/warning messages in cases such as this one.

Best regards, Daniel

Am 06.06.2017 um 18:47 schrieb Johannes Groß:
Hi,
i am trying to integrate Henshin in a EMF-based UML tool. To test this, I am creating a rule and all of its components programatically. I am trying to have a UML Package on the LHS that is searched by its name. So i am trying to create an attribute to filter by the name. I also tried naming the node but that does not seem to have any effect. Here is my code:
lhsPackage is my Package named "myPackage".
Module module = factory.createModule();
module.getImports().add(UMLFactory.eINSTANCE.getUMLPackage());

Rule rule = factory.createRule();
module.getUnits().add(rule);

rule.setActivated(true);
rule.setName("createClassInPackage_Generated");
Graph lhsGraph = rule.getLhs(); // left-hand side of the rule
Node lhsPackageNode = factory.createNode(lhsGraph, lhsPackage.eClass(), null);
String name = (String) lhsPackage.eGet(UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name());
lhsPackageNode.setName(name);
Attribute att = HenshinFactoryImpl.eINSTANCE.createAttribute();
att.setType(UMLFactory.eINSTANCE.getUMLPackage().getNamedElement_Name());
att.setValue(name);
lhsPackageNode.getAttributes().add(att);
 However when executing the rule, I get an exception: java.lang.RuntimeException: ReferenceError: "myPackage" is not defined in <eval> at line number 1     at org.eclipse.emf.henshin.interpreter.impl.EngineImpl.evalAttributeExpression(EngineImpl.java:963)     at org.eclipse.emf.henshin.interpreter.info.VariableInfo.createConstraints(VariableInfo.java:189)     at org.eclipse.emf.henshin.interpreter.info.VariableInfo.createVariables(VariableInfo.java:111)     at org.eclipse.emf.henshin.interpreter.info.VariableInfo.<init>(VariableInfo.java:77)     at org.eclipse.emf.henshin.interpreter.info.RuleInfo.<init>(RuleInfo.java:26)     at org.eclipse.emf.henshin.interpreter.impl.EngineImpl.getRuleInfo(EngineImpl.java:748)     at org.eclipse.emf.henshin.interpreter.impl.EngineImpl$MatchFinder.<init>(EngineImpl.java:323)     at org.eclipse.emf.henshin.interpreter.impl.EngineImpl$MatchGenerator.iterator(EngineImpl.java:268)     at org.eclipse.emf.henshin.interpreter.impl.RuleApplicationImpl.execute(RuleApplicationImpl.java:92)     at org.eclipse.emf.henshin.interpreter.impl.UnitApplicationImpl.executeRule(UnitApplicationImpl.java:176)     at org.eclipse.emf.henshin.interpreter.impl.UnitApplicationImpl.doExecute(UnitApplicationImpl.java:100)     at org.eclipse.emf.henshin.interpreter.impl.UnitApplicationImpl.execute(UnitApplicationImpl.java:90)
Any leads what i am missing?
Thanks,
Johannes
_______________________________________________
henshin-dev mailing list
henshin-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/henshin-dev
-- 
Dr. rer. nat. Daniel Strüber, Dipl.-Inf.
Software Engineering Research Group
Philipps-Universität Marburg, Germany
_______________________________________________ henshin-dev mailing list henshin-dev@xxxxxxxxxxx To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://dev.eclipse.org/mailman/listinfo/henshin-dev
_______________________________________________
henshin-dev mailing list
henshin-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/henshin-dev
-- 
Dr. rer. nat. Daniel Strüber, Dipl.-Inf.
Software Engineering Research Group
Philipps-Universität Marburg, Germany

Back to the top