Dynamically generating methods within class

Hi,

This is a continuation of my post from here: https://mps-support.jetbrains.com/hc/en-us/community/posts/115000710264-Unresolved-references-in-Generated-text

It will help to get some context.

My project structure looks like :

 

 

And my language looks as follows:

 

Using the declaration of Persons (pre-defined) in the 2nd line of my language code, I am able to generate the required fields using a $LOOP$ macro over node.persons where node refers to rrot concept Workflow and persons refers to PersonReferences as shown below:

My Workflowstep concept looks as follows:

The only remaining problem is that I need to generate 3 classes and fill them up with methods based on the action names, i.e. 

> there should be 1 class Waiter with method writes_down_order()

>  there should be 1 class Chef with method cooks_meal() 

> there should be 1 class Customer with 2 methods places_order() and pays_bill()

 

How may I achieve the 3rd point listed above? If I iterate over the worksflowsteps, I get 2 customer classes, each one containing 1 method only! My generator looks as below



Please provide your suggestions. Thank you very much!

 

3 comments
Comment actions Permalink

In my understanding, I need to some how connect my actions to individual Persons so I added a Person reference to my Action concept like so:

 

 

I am assuming that this would allow me to query all actions linked to a Person when I want to dynamically generate the methods as in the previous post.

But adding the PersonReference as shown above leads to some errors in my language editor as shown below:

The error I get is : No reference in obligatory role person

Not sure why this is happening...

0
Comment actions Permalink

To dynamically generate methods I would LOOP through unique Actions of the current Person, e.g. 

node.model.nodes(WorkflowStep).where{it -> it.person.target :eq: node}.select{it -> it.action}.where{it -> !it.previous-siblings.any{other -> it.name :eq: other.name}}

 

The error you are seeing after the last change to your language is caused by the fact that "person" in "Action" is of cardinality 1, but your model does not contain any person in the actions. The "reflective editor" will show you that.

0
Comment actions Permalink

Hello Sir,

Sorry it took a while to get back. 

I was able to solve the problem. The LOOP macro query which gave the desired results was this:

The Java code generated now as I had planned.

Thank you!

0

Please sign in to leave a comment.