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