Meta-object protocols — earlier version

An earlier look at modelling protocols.

metaDepth has been extended with Meta-Object protocols. Simply put, this permits intercepting modelling events and execute some code (described with any of the Epsilon model management languages) over annotated elements. To get a flavour of how this work, start metaDepth by typing:

metaDepth / code
java -jar metaDepth.jar

then, the system should greet you with something like:

Console output
  MetaDepth v0.2
  Top level command shell
  Mon Apr 25 17:05:08 CEST 2016
  >

The ">" symbol is the prompt, where you can type commands. You can for example try loading the following model.

metaDepth / code
load "GraphicalAnnotations.annotations"

@Diagram
Model AnExampleModel {
  enum Gender { Male, Female}
   
  @Ellipse
  Node Person {
    @Label
    name : String;
    gender : Gender = Male;
    @Connector(color=red, style=dotted, label=teaches)
    teaches : Subject[*];
    @Connector(color=black, label=writes_with)
    coauthor : Person[*];
  }
  
  @Mdiamond(bgColor=green, lineColor=blue )
  Node Subject {
    @Label
    courseName : String;
    @Connector
    degree : Degree;
  }
  
  @Msquare(bgColor=lightgrey)
  Node University{
    @Label
    name : String;
    @Connector
    employees : Person[*];
    @Connector
    degrees : Degree[*];
  }
  
  @Circle(radius = 1)
  Node Degree {
    @Label
    name : String;
  }
}

AnExampleModel MyModel {
  Person p1 { name = "Juan"; teaches = [PADS]; coauthor = [p2, p3]; }
  Person p2 { name = "Esther"; gender=Female; teaches = [PADS,DASOF]; coauthor = [p1, p3];}
  Person p3 { name = "Jesus"; teaches = [Prog]; coauthor = [p1, p2];}
  Subject PADS { courseName = "PADSOF"; degree = CompSci; }
  Subject DASOF { courseName = "DASOF"; degree = CompSci; }
  Subject Prog { courseName = "Programming"; degree = Teleco; }
  Degree CompSci { name = "Computer Science"; }
  Degree Teleco { name = "Telecom"; }
  University UAM { name = "UAM"; employees = [p1, p2, p3]; degrees = [CompSci, Teleco]; }
}

The previous listing contains both a meta-model (named AnExampleModel) and an instance of it (named MyModel). Elements in the meta-model are tagged with a set of annotations (@Ellipse, @Label, @Connector), which dictate how the instance models (MyModel in this case) are to be visualized. The annotations generate of a .dot file, which is then fed into graphviz to produce a PDF file. The produced PDF can be found here. The main point is that this process is automatically triggered on model load.

MetaDepth permits tagging any (meta-)model element with annotations. The syntax of annotations is defined through a meta-model. As an example the following listing describes the annotations for graphical representation.

metaDepth / code
load "Annotation"

AnnotationSyntax GraphRepresentation {
   enum Color {
     red, green, blue, black, white, lightgrey
   }
   
   enum EdgeStyle {
     bold, dotted
   }
   
   ModelAnnotation Diagram {
     semanticHooks = [GeneratePlantUML];
     atLeastPotModel: $ self.target.potency = 1 $
   }
   
   abstract ClabjectAnnotation Graphical {
     reqs = [Diagram];		// requires Diagram in the container object because Diagram is a ModelAnnotation
     forbids = [Graphical];
     bgColor   : Color[0..1] = white;
     lineColor : Color[0..1] = black;
     sizex : double = 1;
     sizey : double = 1;
   }
   
   ClabjectAnnotation Rectangle : Graphical {
   }
   
   ClabjectAnnotation Circle : Graphical {
     radius : double = 1;
   }
   
   ClabjectAnnotation Ellipse : Graphical {}
   
   ClabjectAnnotation Mdiamond : Graphical {}

   ClabjectAnnotation Msquare : Graphical {}

   
   PrimFieldAnnotation Label {
     reqs = [Graphical];	// requires Graphical in the container object, because Graphical is a ClabjectAnnotation
     mandatory: $ self.target.minCardinality > 0 $
   }     
   
   ReferenceAnnotation Connector {
     reqs = [Graphical];
     color : Color[0..1] = black;
     label : String = "";
     style : EdgeStyle = bold;
   }
   
   CodeGenerationAction GeneratePlantUML {
      execPoint = post;
      trigger = createModel;
      level = 1;
      actionCodeFile = "GenDot.egl";
      genFileExtension = "dot";
      postSystemCommands = ["dot %genfile -Tpdf -O", "Acrobat %genfile.pdf"]; 
      annotations = [Diagram];
   }
}

The listing creates a ModelAnnotation named Diagram, aimed to annotate models (to indicate that their instances should be graphically represented), then several ClabjectAnnotations are defined targeted to annotate Clabjects (to indicate that their instances should be represented using some form, like rectangles or ellipses). Annotations may define required ones in field "reqs" (e.g., any kind of Graphical annotation required a Diagram annotation in the container model), and forbidden ones in the "forbids" field (e.g., any kind of Graphical annotation cannot be placed on an element if such element is already annotated with another Graphical annotation). A PrimFieldAnnotation named Label is defined to annotated primitive fields (to indicate that a certain field is to be the label of a graphical node). Finally, a ReferenceAnnotation named Connector is declared to indicate that instances of the given reference should be included in the graphic, with a certain style. Please note that both Label and Connector require a Graphical annotation in their container clabjects. Any kind of annotation can declare their own parameters using linguistic extensions, natively supported by MetaDepth. For example, the Connector annotation defined a color, label and style.

The last part of the listing creates a CodeGenerationAction (named GeneratePlantUML) triggered on the "createModel" event. The action executes the "GenDot.egl" generator, defined using the Epsilon Generated Language. The generator transform the model instance into the dot format. This action is associated with the Diagram annotation. The action also defines some system commands, which compile the produced dot file and start the PDF viewer.