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?

Would this work for you?

node<MyConcept> myNode = new node<MyConcept>
myNode.child.set new(MyOtherConcept)
I believe so!  Thanks Vaclav.  Does SModel return the result from the last statement?  I was wondering how one-line element factories work by just passing new node<MyConcept>.  

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.
Yes, in such cases return is implicit.

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.
Thanks Vaclav, I'll try that.
So I made a method in my behaviors for that concept which returns this.ancestor<Concept = NamespaceConcept>, but how do I call this method from within an editor definition?  I think that's why I chose a reference, because it seemed easily usable within the editor definition.
I was thinking of something line the "read only access model access reference" cell type to show the requested properties of the ancestor, but maybe this is not covering the use-case you're looking for.

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 1 (a bit more complex, universal). Use "model access"/"read only model access" editor cell to edit whatever information you want, that is related to your node
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)

Please sign in to leave a comment.