Element Factories: Creating a new node with a default child
Hello, I've seen how to create an element factory that returns a new node using:
new node<MyConcept>()
The concept I want to create has 1 abstract child, and I'd like to default it to something. I'd like to do something like this:
new node<MyConcept>(child = new node<MyOtherConcept>)
But the editor language doesn't seem to allow me to do that.
Is there any documentation on this? How would I go about setting one of the children?
Thanks
new node<MyConcept>()
The concept I want to create has 1 abstract child, and I'd like to default it to something. I'd like to do something like this:
new node<MyConcept>(child = new node<MyOtherConcept>)
But the editor language doesn't seem to allow me to do that.
Is there any documentation on this? How would I go about setting one of the children?
Thanks
Please sign in to leave a comment.
node<MyConcept> myNode = new node<MyConcept>
myNode.child.set new(MyOtherConcept)
myNode
Also, MyConcept has a reference to a root concept (sort of like a namespace concept). Ideally this would be set upon construction. Would I create a behavior, and inside the constructor do something like:
this.namespace.set(this.ancestor<concept = NamespaceConcept>) ? I tried this but the editor complains that the reference on the node isn't set.
Without knowing the rationale behind your need for an explicit reference to the root concept, I'd suggest, instead of managing an explicit pointer, to create a behavior method on MyConcept that would retrieve this.ancestor<...> each time when invoked.
If you need a real reference to the ancestor, the constructor will not help you since at node construction time the node is not yet part of the AST and so querying ancestors will return null.
For constructing relatively complex node structures there's j.m.quotation language in MPS. After importing this language, try typing "<quotation>" anywhere where you can use expression. Quotation is a construction, which will produce a node (written inside of it) at runtime. In your case, just type "MyConcept" inside of the quotation and you'll see the editor for MyConcept, where you can edit its children.
BTW, there's an "add antiquotation" intention available inside of a quotation to create parts of nodes that will be computed at runtime. The paradigm of the language is very simple - you write known-at-runtime-parts of created nodes just inside a quotation, and use antiquotations for properties/references/children which you will know only at runtime
I've found that the language is not documented yet (filed http://youtrack.jetbrains.com/issue/MPS-17784), but you can ask here if you have questions.
Way 2 (simplier, but for now it adds a bit trash to the concept). You can have a reference in your node to the namespace node as you do now, but you need to create "node factories" in editor aspect of your language. A node factory is a "node setup" code executed by editor, where you can add children/set ref targets/set properties for your node. You can get the namespace node there and set a reference to it in your node. Note that this code is executed only in editor, opposite to constructor, which is executed for each node creation (that is the reason why it's illegal to gen ancestors in constructor)