I generate a name in a weave, How can I reference it in a reduction?
- Open the attached project in MPS 2.0 build 20.7566
- Right click "sandbox" and select "Preview Generated Text"
- Open "main" from in the Generator
- Open the inspector for the property macro around "Name" in the reduction rule for "Thing2"
- Swap comments to comment out the second line and uncomment the first
- Make the language
- Right click "sandbox" and select "Preview Generated Text"
I would like to be able to reference the things I generated in the weave from reductions. This is a simplified version of what I'm trying to do in my full project. How should I accomplish this?
Thanks!
Jake
LabelTest.zip (62KB)
Please sign in to leave a comment.
Each minor step transforms input model into output. It consists of:
1. Building output model node structure.
a. applying 'create root' rules
b. applying 'root mappings'
c. copying the rest into the output model
2. Applying weaving rules.
3. Applying post-processing stuff (see $MAP_SRC$ -> post processing).
4. Restoring references (invokes reference macro code).
PS: In MPS 2.0 you can use $WEAVE$ macro instead of weaving rules. It annotates the target node and you have to provide input nodes query.
I tried using the $WEAVE$ macro instead of a weave rule and get the same problem.
I'm not sure you answered my question. What is the proper way to accomplish this? Should I be generating the unique name where I reference it and assigning that to a label, then looking it up when I generate the thing to be named so it happens in the right order?
This seems like a fairly common thing to need to do. The real case is I'm generating C++ (GText) and I have a construct that needs to generate some code where it is and also generate a class off to the side somewhere to be referenced from the code generated in place.
Thanks!
Here's the project using $WEAVE$ instead: LabelTest2.zip (62KB)
Here's the project with them reversed: LabelTest3.zip (62KB)
"areMappingsAvailable" is at TemplateGenerator:668 and is true if !myStrictMode or "myAreMappingsReady".
myStrictMode seems to be false by default, but is apparently true when I run the generator, so then examining areMappingsAvailable I find that during "apply" (TemplateGenerator.java:89) it gets set to false while applying reductions. Does this mean you can't look up labels when applying reductions? Is there a reason for that, or am I misreading the code?
In non-strict mode (compatibility mode for old projects) we cannot generate incrementally or in parallel.
Here's what I think I've learned so far:
What I'm trying to do isn't very compatible with these restrictions. To sum it up again:
I think what I need is an additional concept that my AnonymousFunction reduces to which contains the information from the AnonymousFunction plus a reference to the node I can get the name from. I can then use a reference macro to read the label and create another reduction for that concept that finally brings it down to GText. This seems to me like a workaround. Any better ideas? I'll try to modify my LabelTest example to work this way and see what happens.
1. weaving rule extracts method, gives it a name, and stores it under a label
2. ExtractStaticMethod_CallExpression is reduced into static method call, reference to extracted method is get from the mapping label in reference macro
There are several ways to annotate node.
1. (model way) create a concept extending NodeAttribute, set concept property: role = nameAnnotation, set concept link: attributed = AnonymousFunction
create editor: [- nameAttribute(name={name}) [>attributed node<] -]
2. node/.putUserObject("nameAttribute", "...")
3. (generator only way) genContext.transient object [ "name for: " + node/.getId() ] = ...;
Btw I tried adding the extra concept and reducing to it and then reducing that to what I wanted so I could use a reference macro, but I'm generating to GText, and I can't reduce to something that isn't in GText. I guess I'd have to extend GText and make the thing extend something in GText, but wow that's a lot of working around, I'll stick w/ method #2.
Thanks for the help!
node<> node = ...; // node<> is smodel type
SNode nodeAsSNode = node/; // SNode is lower level java type