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

Ok, i found it. I was leaving out the attribute and the name of the RHS package node. This mixed things up.

Thanks,
Johannes

2017-06-06 11:37 GMT-07:00 Johannes Groß <johannes.t.gross@xxxxxxxxx>:
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.

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?
Am i specifying the parameter and attribute correctly?
Any leads on why the name is set to something weird?

Thanks!
Johannes

2017-06-06 10:18 GMT-07:00 Daniel Strüber <strueber@xxxxxxxxxxxxxx-marburg.de>:

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



Back to the top