JetBrains MPS - Constrain ClassifierType reference to subclasses of a given type

114 Views Asked by At

I'm writing a DSL in MPS to define event producer/consumers, like this:

on FooEventProducer
    then FooEventConsumer

where:

class FooEventProducer implements Producer<FooEvent> {}
class FooEventConsumer implements Consumer<FooEvent> {}

I have an On concept containing a ClassifierType reference.

How do I constrain the ClassifierType reference to subclasses of Producer?

1

There are 1 best solutions below

0
On

I recommend that you create and use your own sub-concept of ClassifierType, which specifies constraints on the "classifier" reference, something like:

concepts constraints MyClassifierType { 
  can be child <none> 

  can be parent <none> 

  can be ancestor <none> 


  <<property constraints>> 

  link {classifier} 
    referent set handler:<none> 
    scope: 
      (exists, referenceNode, contextNode, containingLink, linkTarget, operationContext, enclosingNode, model, position, contextRole)->Scope { 

        sequence<node<Classifier>> candidates = model.nodesIncludingImported(Classifier).where({~it => it.getExtendedClassifierTypes().any({~clazz => clazz.classifier :eq: node/Runnable/; }); }); 
        new ListScope(candidates) { 
          public string getName(node<> child) { 
            child : INamedConcept.name; 
          } 
        }; 
      } 
    presentation : 
      <no presentation> 
  ; 

  default scope 
    <no default scope> 

}