Access to parent in behaviour-constructor

Hi,

I would like to generate some unique-id for a node. To do that, I want to access the parent concept in the constructor(behaviour) of a concept.

But it is null!

What to do?
6 comments
Comment actions Permalink
You can try making unique-id property "derived" - create a getter for it and return proper value from there.
0
Comment actions Permalink
Hi Alex,

already had that idea on my own.
But the problem is, that I want to save the unique id in the model, so it doesn't change.

I tried this in the getter:
if (node.identifier == -1) { 
  node.identifier = node.generateIdentifier(node.parent as TextSymbolSet); 
} 
return node.identifier;
prettyPrint();

But it throws an exception. Probably I'm not allowed to change the identifier here.
(Why is there nothing red in the editor then)...

Exception:

jetbrains.mps.smodel.IllegalModelChangeError : registered node can only be modified inside undoable command or in 'loading' model [textSymbols] TextSymbol <no name>[8642124865149417439] in [...].sandbox.sandbox
0
Comment actions Permalink
Ah, I just used the "commmand" block. And now it works as expected.
Had to import plugin-language to constraints-model.

EDIT:
Another alternative is to use a node-factory.
0
Comment actions Permalink
You right. Node Factories looks even more suitable here.
0
Comment actions Permalink
While we can use derived properties it'd much easier to have an access to the parent (or may be "probable" parent) in the behavior's constructor.

So the question is still actual, why can't we get a reference to the parent in the constructor?
0
Comment actions Permalink
What if you moved the node between parents? During refactoring for example or you just selected it and pressed Alt-Down. Node factories allows to adapt a node to the environment, but only once! Besides, it works only in an editor. When you write code to manipulate the AST you prefer to be able to create a node first, and then attach it to the parent.

Technically, we can add a "probable" parent to the constructor. But we cannot guarantee when it will be available so it doesn't make sense.
0

Please sign in to leave a comment.