|
| 1 | +Puppet is a model driven system - a desired system state is modeled by describing the |
| 2 | +desired state of a set of resources, these are compiled into a catalog, and the catalog is |
| 3 | +applied to a system to make it have the desired modeled state. |
| 4 | + |
| 5 | +What is new in the "future parser/evaluator" features in Puppet is that we started using modeling technology to implement how puppet itself works. |
| 6 | + |
| 7 | +In this post, I am going to talk a bit about modeling in general, the Ecore modeling technology |
| 8 | +and the RGen Ruby implementation of Ecore. |
| 9 | + |
| 10 | +### What is a "model" really? |
| 11 | + |
| 12 | +If at first when you hear the word *model*, you think about Tyra Banks or Marcus Schenkenberg and then laugh a little because that is obviously not the kind of models we are talking about here, you are actually both right and wrong at the same time. |
| 13 | + |
| 14 | +A model is an abstraction. We use abstractions all the time; when we talk about something like a "Car" we are talking about an unspecific instance of something like "a powered means of transportation, probably a 4 door sedan". Fashion models such as Tyra or Marcus are also abstractions of "people wearing clothes" albeit very good looking ones - in a way like if we design a Car icon to depict a Lamborghini, but now I am beginning to get off topic and into a completely different debate. |
| 15 | + |
| 16 | +We can express a model concretely: |
| 17 | + |
| 18 | + A Car has an engine, 2 to 5 doors, a steering wheel, breaks, and 4 wheels. |
| 19 | + . . . |
| 20 | + |
| 21 | +We can also express such a model in a programming language: |
| 22 | + |
| 23 | + class Car |
| 24 | + attr_accessor :engine |
| 25 | + attr_accessor :doors |
| 26 | + attr_accessor :steering_wheel |
| 27 | + attr_accessor :breaks |
| 28 | + attr_accessor :wheels |
| 29 | + |
| 30 | + . . . |
| 31 | + end |
| 32 | + |
| 33 | +As you can see in the above attempt to model a Car we lack the semantics in Ruby to declare |
| 34 | +more details about the Car's attributes - there is no way to declaratively state |
| 35 | +how many doors there could be, the number of wheels etc. There is also no way to declare |
| 36 | +that the engine attribute must be of engine type, etc. All such details must be implemented |
| 37 | +as logic in the setter and getter methods that manipulate a Car instance. While this is fine |
| 38 | +from a runtime perspective (it protects us from mistakes when using the class), we can not |
| 39 | +(at least not easily) introspect the class and deduce the number of allowed doors, or the allowed type of the various attributes. |
| 40 | + |
| 41 | +While Ruby is a very fluid implementation language, in itself it is not very good at |
| 42 | +expressing a model. |
| 43 | + |
| 44 | +### Modeling Language |
| 45 | + |
| 46 | +A modeling language (in contrast to an implementation language such as Ruby) lets us |
| 47 | +describe far more details about the abstraction without having to express them in imperative |
| 48 | +code. One such family of "languages" are those that are used to describe data formats - they |
| 49 | +are referred to a schemas - and you are probably familiar with XmlSchema, JsonSchema, Yamlschema etc |
| 50 | +that allows a declaration to be made about what is allowed in data that conforms to the schema. |
| 51 | + |
| 52 | +A schema is a form of modeling language. What is interesting about them is that it now possible |
| 53 | +to transform between them! Given an XmlSchema, it is possible to transform it into a corresponding |
| 54 | +Yaml or JsonSchema, and likewise transform data conformant with one such schema into data comformant |
| 55 | +with the transformed schema. (The problems doing this in practice has to do with the difference in semantic power between the different technologies - we may be able to express rules/constraints |
| 56 | +in one such schema technology that does not exist the others). |
| 57 | + |
| 58 | +### Schemas and Meta Models |
| 59 | + |
| 60 | +When we look at a schema - we are actually looking at a meta model; a model that describes a model. |
| 61 | +That is if we describe a car in Json we have a car model: |
| 62 | + |
| 63 | + { "engine": "combustion", |
| 64 | + "steering-wheel": "sport-leather", |
| 65 | + "wheels": ... |
| 66 | + } |
| 67 | + |
| 68 | +And if we describe a schema for it: |
| 69 | + |
| 70 | + { "title": "Car Schema", |
| 71 | + "type"; "object", |
| 72 | + "properties": { |
| 73 | + "engine": { "type": "string"}, |
| 74 | + "steering-wheel": { "type": "string" }, |
| 75 | + . . . |
| 76 | + } |
| 77 | + "required": ["engine", "steering-wheel", ...] |
| 78 | + } |
| 79 | + |
| 80 | +We have a Car meta-model. |
| 81 | + |
| 82 | +In everyday speak, we typically refer to the schema as "schema" or "model" and simply ignore |
| 83 | +its "meta status". But since we are on the topic of meta models - what we can do now is to |
| 84 | +also express the meta model as a model - i.e. what is the schema for a jsonschema? |
| 85 | +Here is an excerpt from the [Json "Core/Validation Meta-Schema"][1] |
| 86 | + |
| 87 | + { |
| 88 | + "id": "http://json-schema.org/draft-04/schema#", |
| 89 | + "$schema": "http://json-schema.org/draft-04/schema#", |
| 90 | + . . . |
| 91 | + "title": { |
| 92 | + "type": "string" |
| 93 | + }, |
| 94 | + . . . |
| 95 | + "required": { "$ref": "#/definitions/stringArray" }, |
| 96 | + . . . |
| 97 | + } |
| 98 | + |
| 99 | +If you are interested in what it looks like, do download it. Be warned that you will quickly become |
| 100 | +somewhat disoriented since it is a schema describing a schema that describes what Json data |
| 101 | +should look like... |
| 102 | + |
| 103 | +[1]:http://json-schema.org/documentation.html |
| 104 | + |
| 105 | +A meta schema such as that for jsonschema is very useful as it can be used to validate schemas |
| 106 | +that describe data. |
| 107 | + |
| 108 | +Schemas such as these are good for describing data, but they becomes somewhat difficult to use |
| 109 | +in practice for the construction of software. There are other modeling languages that are more specifically targeting software system constructs. There are both graphical and textual languages |
| 110 | +as well as those that have both types of representations. |
| 111 | + |
| 112 | +What we are specifically interested in is an [Object modeling language][2] |
| 113 | + |
| 114 | +[2]:http://en.wikipedia.org/wiki/Object_modeling_language |
| 115 | + |
| 116 | +### Levels of reality / meta-ness |
| 117 | + |
| 118 | +| level | Description | |
| 119 | +| --- | --- | |
| 120 | +| M0 | Real Object - e.g. the movie Casablance on a DVD | |
| 121 | +| M1 | User Model / Instance Level - e.g. a computer abstraction of the DVD - `aVideo = Video.new('Casablanca')` | |
| 122 | +| M2 | Meta Model - e.g. defines what `Video` is, its attributes and operations | |
| 123 | +| M3 | Meta meta model - e.g. defines how the definition of what a `Video` is, is expressed | |
| 124 | + |
| 125 | + |
| 126 | +### Object Modeling Language |
| 127 | + |
| 128 | +An Object Modeling Language is a language that directly supports the kinds of elements we are interested in when constructing software. E.g. classes, methods, the properties of objects, etc. You probably heard of one such technology called [Unified Modeling Language][3] - this is a broad modeling technology and is associated with object oriented software development methodologies such as Booch, OMT, Objectory, IBM's RUP, and Dynamic Systems Development Method. This was the big thing in the 90's, but UML has since then more or less slid into darkness as "the way to write better |
| 129 | +software". An interesting debate from 2009, can be found [here][4]. |
| 130 | + |
| 131 | +[3]:http://en.wikipedia.org/wiki/Unified_Modeling_Language |
| 132 | +[4]:http://codebetter.com/jeremymiller/2009/09/12/how-relevant-is-uml-modeling-today/ |
| 133 | + |
| 134 | +There is however a very useful part of the UML technology that is often overlooked - the so |
| 135 | +called Meta Object Facility (MOF) that sits at the very core of UML and it contains the meta model |
| 136 | +that UML itself is defined in. MOF plays the same role for models as what Extended Backus Naur Form (EBNF) plays for programming languages - it defines the grammar. Thus MOF can be said to be a DSL used to define meta models. The technology used in MOF is called Ecore - it is the reference implementation of MOF. |
| 137 | + |
| 138 | +### Ecore |
| 139 | + |
| 140 | +Ecore is part of [Eclipse EMF][5], and is heavily used within Eclipse for a wide variety of |
| 141 | +IDE applications and application development domains. In the Puppet Domain Ecore technology is |
| 142 | +used in the [Puppetlabs Geppetto IDE][7] tool in combination with additional frameworks for language development [Xtext][6] |
| 143 | + |
| 144 | +[5]:http://en.wikipedia.org/wiki/Eclipse_Modeling_Framework |
| 145 | +[6]:http://en.wikipedia.org/wiki/Xtext |
| 146 | +[7]:http://puppetlabs.github.io/geppetto/ |
| 147 | +[8]:https://github.com/mthiede/rgen |
| 148 | +[9]:https://code.google.com/p/emf4cpp/ |
| 149 | + |
| 150 | +Eclipse EMF is a Java centric implementation of Ecore. There are also implementations for |
| 151 | +Ruby ([RGen][8], and C++ [EMF4CPP][9]). |
| 152 | + |
| 153 | +Thus, there are many different ways to express an Ecore model. The UML MOF has defined one |
| 154 | +serialization format known as XMI based on XML, but there are many other concrete formats such |
| 155 | +as Xcore (a DSL built with Xtext, annotated Java, Json, binary serialization formats, the Rgen score |
| 156 | +DSL in Ruby, etc.) |
| 157 | + |
0 commit comments