ParameterCatalogs2Creation.adoc 40.8 KB
Newer Older
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
1
2
== How to Implement Parameter Catalogs with Eclipse
:imagesdir: ParameterCatalogs2Images
3

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
4
To build a new parameter catalog from scratch, we first have to understand some basics about Eclipse, and then install the correct Eclipse package.
5
Thereafter, we can model our data with Ecore considering some best practices, followed by the generation of Java classes and user interface (UI).
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
6
7
We, then, will add some plug-ins to "pimp" our Eclipse installation, (a) to enable deployment of parameter catalog applications, and (b) to add units and quantities to the mix.
Some hints on special modeling problems and versioning parameter catalogs conclude this how-to guide.
8
9
10
11
12
13
14
15
16
17
18

=== Eclipse Basics

https://en.wikipedia.org/wiki/Eclipse_(software)[Eclipse] was originally developed by IBM and became Open Source in 2001.
It is best known for its Integrated Development Environments (_Eclipse IDEs_), not only for Java, but also for C++, Python and many other programming languages.
These IDEs are created on top of the Eclipse Rich Client Platform (Eclipse RCP), an application framework and plug-in system based on Java and OSGi.
Eclipse RCP is foundation of a plethora of general-purpose applications, too.

First time users of Eclipse better understand the following concepts.

.Eclipse Packages
19

20
21
22
An Eclipse package is an Eclipse distribution dedicated to a specific type of task.footnote:[The notion of an Eclipse package has nothing to do with Java packages.]
A list of packages is available at https://www.eclipse.org/downloads/packages/[eclipse.org].
Beside others it contains _Eclipse IDE for Java Developers_, _Eclipse IDE for Scientific Computing_, and the package we will use: _Eclipse Modeling Tools_.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
23
Note that third parties offer many other packages, e.g. _GAMA_ for multi-agent-simulation or _Obeo Designer Community_ for creating Sirius diagram editors.
24
25
26

[NOTE]
====
27
Several Eclipse packages can be installed side by side, even different releases of the same package. Multiple Eclipse installations can run at the same time, each on its own _workspace_ (see below).
28
29
====

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
30

31
.Plug-ins / Features
32

33
An installed Eclipse package consists of a runtime core and a bunch of additional plug-ins.
34
Technically, a plug-in is just a special kind of Java archive (JAR file) that uses and can be used by other plug-ins with regard to OSGi specifications.
35
36
Groups of plug-ins that belong together are called a _feature_.

37
Often, a user will add plug-ins or features to an Eclipse installation to add new capabilities.
38
E.g. writing this documentation within my Eclipse IDE is facilitated by the plug-in https://marketplace.eclipse.org/content/asciidoctor-editor[Asciidoctor Editor].
39
40
Plug-ins can easily be installed via main menu command `Help → Eclipse Marketplace...` or `Help → Install New Software...`.
Some plug-ins may be self-made like our plug-in `de.hftstuttgart.units` that enables Ecore to deal with quantities.
41
These may be provided via _Git_ or as download and have to be added to an Eclipse installation manually.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
42
43

  
44
.Git
45

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
46
47
48
49
https://git-scm.com[Git] is the industry standard for collaborative work on, and versioning of, source code and other textual data.
Collaborative development of parameter catalogs benefits massively from using Git.
Git support is built into _Eclipse Modeling Tools_, the Eclipse package we will use.
However, if Eclipse needs to connect to a Git server that uses SSH protocol (not HTTPS with credentials), access configuration is more involved and may be dependent on your operating system.
50

51
Some users, anyway, prefer to use Git from the command line or with one of the client application listed https://git-scm.com/downloads/guis[here], e.g. https://tortoisegit.org[TortoiseGit] for Windows.
52
53
54
55
56
57
58

While it is required to get Git working at some point, we won't refer to it in this document and, for now, do not cover the installation of Git on your machine or configuration of Git in Eclipse.

.Workspaces

When you start a new Eclipse installation for the first time, you are asked to designate a new directory in your file system to store an _Eclipse workspace_.
Eclipse is always running with exact one workspace open.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
59
As the name implies, a workspace stores everything needed in a given context of work, namely a set of related projects the user is working on as well as meta-data like preference settings, the current status of projects, to do lists, and more.
60
In case a user wants to work in different contexts, e.g. on different tasks, command `File -> Switch Workspace` allows to create additional workspaces and to switch between them.
61
62
63

[NOTE]
====
64
65
Any plug-in from the original Eclipse package or installed by the user later will be copied into the Eclipse installation directory, *not* in any workspace.
Configuration and current state of plug-ins, on the other hand, are stored in workspaces.
66
67
====

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
68

69
.Projects
70

71
72
73
74
75
An Eclipse project is a technical term for a directory that often contains:

* files of specific types for source code, scripts, XML files or other data
* build settings, configurations
* dependency definitions (remember the dependencies between plug-ins above?)
76
* other Eclipse projects.
77

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
78
`File -> New -> Project...` offers many different types of projects that the user can choose from, e.g. Java projects to create Java programs, Ecore modeling  projects, or general projects, that simple hold some arbitrary files.footnote:[Projects possess one or more _natures_ used to define a project's principal type.]
79
80
81
82
83
84
85
86
87
88

[WARNING]
====
Files that do not belong to a project are invisible for Eclipse!
====

The projects belonging to a workspace can either be directly stored within the workspace as sub-directories (the default offered to the user when creating a new project), or linked from it, that is the workspace just holds a link to the project directory that lives somewhere in the file system outside of the workspace.
Linking allows to work with the same projects in different workspaces.

While it sometimes makes sense to share or exchange workspaces between users,footnote:[Or even work on the same workspace provided in the cloud, see https://www.eclipse.org/che/technology/[Eclipse Che].], I do not recommend this for now.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
89
Projects, in contrast, are shared between users most of the time, usually via Git.
90
91
92
93
94
95
96
97
98
In general, I would suggest to store Eclipse projects outside workspaces at dedicated locations in the user's file system.
That way, we can follow the convention that local Git repositories should all be located under 
`<userhome>/git`.


=== Setup Eclipse Modeling Tools

.Install Java

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
99
100
Eclipse runs on 64-bit versions of Windows, Linux, and macOS and requires an according Java Development Kit (JDK), version 11 or higher, to be installed on your machine.
Even if such JDK already exists, please download OpenJDK, version *15* or newer for your operating system from https://adoptopenjdk.net[AdoptOpenJDK].
101
footnote:[AdoptOpenJDK recently joined the Eclipse foundation and soon will change its name to _Adoptium_ for legal reasons.]
102
Choose `HotSpot` as Java Virtual Machine.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
103
Installation process is straight forward, but you can also find links to exhaustive instructions for your operating system.
104

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
105
106
New Java versions appear every six months, so one could tend to stick with older version 11 that comes with long time support (LTE) until version 17 arrives in autumn 2021.
However, actual version 15 conforms to the latest security measures built into macOS Catalina, so it is a must if software we build here shall be deployed to these systems, too.
107

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
108
Note that different versions of Java coexist peacefully.
109
110


Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
111
.Install Eclipse Modeling Tools
112

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
113
114
115
116
117
Now its time to download and install the correct Eclipse package _Eclipse Modeling Tools_, version 2020-09 or newer.footnote:[Please stay away from version 2020-03 and 2020-06 of Eclipse Modeling Tools, since these came with a bug preventing the user from editing data in table cells within the generated UI.]
Please go to https://www.eclipse.org/downloads/packages[Eclipse download page for packages].
On this page you may see _"Try the Eclipse Installer"_ or similar.
Do *not* follow this advice, since we want more control over what versions of Java and Eclipse shall be installed.
Instead, look for package _Eclipse Modeling Tools_ and follow the link for your operating system on the right:
118
119

.Download links for Eclipse Modeling Tools package
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
120
image::EclipseDownload.gif[EclipseDownload, role="thumb"]
121
122
123
124
125
126
127
128
129

Finally, you can click on `Download` and wait for the 400 something MB package to arrive.

[NOTE]
====
Depending on the operating system, several security dialogs have to be acknowledged during installation and first launch of Eclipse.
====

The downloaded installation file contains the application simply named `Eclipse` ready to be copied into `Applications` on macOS or be installed in `Programs` on Windows.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
130
Since later you may add other Eclipse packages -- or different versions of the same package -- I suggest to rename the application more significantly to `EclipseModeling2009` or similar.
131

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
132
After installation has finished launch Eclipse for the first time and you will see a dialog for choosing a new empty directory as its workspace.  
133
134
135
136

.Initial Dialog to Choose a Workspace Directory
image::SelectWorkspaceDirectory.gif[SelectWorkspaceDirectory, 500, role="thumb"]

137
Again, more workspaces might come into existence later, so replace the proposed generic directory path and name with a more specific one, e.g.`EclipseModelingWS`.
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
The Eclipse main window appears with a Welcome Screen open.
It contains links to exhaustive documentation on concept, features and usage of Eclipse that might be of interest later, especially:

* Overview
** Workbench basics
*** Concepts: features, resources, perspectives, views, editors
*** Opening perspectives and views
*** Installing new software manually
** Team support with Git
* Learn how to use the Ecore diagram editor
* Launch the Eclipse Marketplace

For now, you can dismiss the welcome screen. It can be opened anytime by executing `Help -> Welcome`


Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
153
=== Modeling Parameter Catalogs for Simulation with Ecore
154
155

Now you should see the initial layout of Eclipse with _Model Explorer_ and _Outline_ on the left and a big empty editing area with _Properties_ view below to the right.
156

157
Since we will use Ecore diagrams for data modeling, create your first Ecore modeling project now:
158

159
. Execute `File -> New -> Ecore Modeling Project` from main menu -- not `Modeling Project`!
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
160
161
162
. Name it `demo.catalog` and click `Next >`
. Uncheck `Use Default Location` so that the new project is *not* stored in workspace, but a different directory you create/choose, then click `Next >`
. Provide `democatalog` as main Java package name and click `Finish`.
163

164
Eclipse should look like below with an new empty graphical Ecore diagram editor opened.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
165
The diagram is automatically named `democatalog` after the package name for the Java classes that will be generated from it (provided above).
166
The _Model Explorer_ shows the contents of the new Ecore modeling project.
167
168


169
.New Ecore Modeling Project
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
170
image::DemoCatalogEmpty.png[DemoCatalogEmpty, role="thumb"]
171

172
173
174
To get your feet wet, do this:

. Drag a _Class_ from the palette on the right onto the editor's canvas: it will materialize as a rectangle labeled `NewEClass1`.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
175
. The class symbol should be selected initially, so you can see its attributes in the _Properties_ view.
176
177
. In there replace `NewEClass1` by `EnergyComponentsCatalog` to rename the class.
. Click anywhere on the canvas and notice that the class symbol is deselected and the toolbar at the top adapts accordingly.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
178
179
180
181
. In the toolbar change `100%` to `75%` to scale diagram.
. Execute `File -> Save` to save model and diagram on disk.
. Close diagram editor `democatalog` by closing its tab.
. Reopen saved diagram by double click on entry `democatalog` in _Model Explorer_. 
182
183

Technically, everything is in place now to begin modeling the data that the projected catalog shall contain.
184
185
186
187
188
189
Except ... understanding the basics of object-oriented modeling would be helpful.
This is why developers should support domain experts at this stage.

.Model Data with Class Diagrams

Ecore diagrams are simplified UML class diagrams.
190
Here some resources on what this is all about:
191
192
193
194
195
196
197

* http://www.cs.toronto.edu/~sme/CSC340F/slides/11-objects.pdf[Toronto Lecture on Object Oriented Modeling]
* http://agilemodeling.com/artifacts/classDiagram.htm[UML 2 Class Diagrams: An Agile Introduction]
* https://www.amazon.de/UML-Classroom-Einführung-objektorientierte-Modellierung-ebook/dp/B00AIBE1QA/ref=sr_1_2?__mk_de_DE=ÅMÅŽÕÑ&dchild=1&keywords=UML&qid=1585854599&sr=8-2[UML @ Classroom: Eine Einführung in die objektorientierte Modellierung (German Book)]

[TIP]
====
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
198
199
200
Beginners are strongly encouraged to read the first two resources.
The first one contains a gentle introduction, especially suited for domain experts.
The second one can also serve as reference.
201
202
====

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
203
We will touch central object-oriented concepts _Class_, _Object_, _Attribute_, _Association_, _Composition_, and _Multiplicity_ in an example below, but work through above sources to get a deeper understanding and to enhance your modeling skills.
204

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
205
206
Note that above sources differentiate between _conceptual_ and _detailed_ models.
All in all we go for detailed models, since only these contain enough information to generate code.
207
208
209
Having said this, it is usually a good idea to have two or three conceptual iterations at a white board to agree on the broad approach before going too much into detail.
But even if one starts with Ecore models right away, these also can be adapted any time to follow a new train of thought.

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
210
211
See here the essential and typical structure of a parameter catalog in a class diagram.
Instead of artificial example classes like _Foo_ and _Bar_ it shows classes from an existing catalog, albeit in very condensed form.
212

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
213
.Principle Structure of a Parameter Catalog
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
[plantuml, role="thumb"]
----
together {
  class SolarPanel
  class Inverter
}

class EnergyComponentsCatalog {
    author: String
}

abstract class EnergyComponent {
    modelName: String
    revisionYear: int
}

abstract class ChemicalEnergyDevice {
    installedThermalPower: double
}

class Boiler {
    type : BoilerType
}

class CombinedHeatPower {
    thermalEfficiency : double
    electricalEfficiency : double

}

class Manufacturer {
    name : String
}

enum BoilerType {
  LowTemperature
  Condensing
}

class SolarPanel {
    nominalPower : double
    mppVoltage : double
    mppCurrent : double
}

class Inverter {
    nominalPower : double
    maxDCVoltage : double
    maxDCCurrent : double
}

BoilerType -[hidden]- Boiler

SolarPanel --|> EnergyComponent
Inverter --|> EnergyComponent
ChemicalEnergyDevice --|> EnergyComponent
Boiler --|> ChemicalEnergyDevice
CombinedHeatPower --|> ChemicalEnergyDevice

EnergyComponentsCatalog *-- "0..*" Inverter: inverters
EnergyComponentsCatalog *-- "0..*" SolarPanel: solarPanels
EnergyComponentsCatalog *-- "0..*" Boiler: boilers
EnergyComponentsCatalog *-- "0..*" CombinedHeatPower: chps
EnergyComponentsCatalog *-- "0..*" Manufacturer: manufacturers
EnergyComponent -up-> "1..1" Manufacturer: producedBy
----

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
281
The diagram models four types of technical components whose data shall be stored in the catalog, e.g. for parameterization of simulation models later: _Boiler_, _CombinedHeatPower_, _SolarPanel_, and _Inverter_.
282
283
284
285
286
287
288
289
290
291
292
293

The catalog itself is represented by class _EnergyComponentsCatalog_.
Unlike dozens, hundreds, or even thousands of objects to be cataloged -- Boilers, Inverters etc. -- there will be just exactly *one* catalog object in the data representing the catalog itself.
Its "singularity" is not visible in the class diagram, but an _Ecore_ convention requires that all objects must form a composition hierarchy with only one root object.

.Composition
If, in the domain, one object is composed of others, this is expressed by a special kind of association called _composition_.
Compositions are depicted as a link with a diamond shape attached to the containing object. In the _Boiler_ case said link translates to: The _EnergyComponentsCatalog_ contains -- or is composed of -- zero or more (`0..*`) boiler objects stored in a list named `boilers`.

[IMPORTANT]
====
Note that class names -- despite the fact that they model a set of similar objects -- are always written in _singular_! They are written in https://en.wikipedia.org/wiki/Camel_case[Camel case notation] starting with an upper case letter. Associations and attributes are written the same way, but starting with a lower case letter. Names for list-like associations and attributes usually are written in plural form.
294
295
====

296
.Inheritance
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
297
Besides composition of *objects*, the model above shows another, completely different, kind of hierarchy: the inheritance hierarchy between *classes*.
298
Whenever classes of objects share the same attributes or associations, we don't like to repeat ourselves by adding that attribute or relation to all classes again and again.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
299
Instead, we add a _super class_ to define common attributes and associations and connect it to _sub classes_ that will automatically _inherit_ all the features of their super class.
300

301
302
In our example above, common to all four energy components are attributes `modelName` and `revisionYear`, thus these are modeled by class `EnergyComponent` that is directly or indirectly a super class of _Boiler_, _CombinedHeatPower_, _SolarPanel_, and _Inverter_.
Similar, _Boiler_ and _CombinedHeatPower_ share attribute `installedThermalPower` factored out by class _ChemicalEnergyDevice_.
303

304
305
306
.Associations
You probably noticed a fifth type of objects contained in the catalog, namely `Manufacturer` objects stored in list `manufactureres`.
How come? Ok, here is the story:
307

308
309
310
.Domain Expert Meets Developer
****
_Exp_: "`I'd like to store a component's manufacturer. Shall I add a String attribute `manufacturerName` to all classes like _Boiler_, _Inverter_ and so on to store the manufacturer's name?`"
311

312
_Dev_ shudders: "`Well, what do you mean by "... and so on"?`"
313

314
_Exp_: "`Basically, I mean all energy components.`"
315

316
317
_Dev_: "`Fine. We already have a class representing all those energy components, brilliantly named _EnergyComponent_. Thus, we can define `manfacturerName` there, following one of Developer's holy principles: "_DRY_ -- Don't repeat yourself!"
By the way: Is the name all you want to know about manufacturers?`"
318

319
_Exp_: "`Mhm, maybe we need to know if they are still in business ...`"
320

321
_Dev_: "`... or even since when they were out of business, if at all ...`"
322

323
_Exp_: "`... and the country or region they are active.`"
324

325
_Dev_: "`Ok, so it's not just the name -- we need a class `Manufacturer` to model all these information.`"
326

327
_Exp_ sighs.
328

329
_Dev_: "`Come on, its not that hard to add a class to our data model, isn't it?`"
330

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
_Exp_: "`Ok, but how can we express what components a manufacturer produces?`"

_Dev_: "`Wasn't it the other way around? I thought, you just wanted to know the manufacturer of a component?`"

_Exp_: "`What is the difference?`"

_Dev_: "`In data modeling, it is the difference between a uni-directional and a bi-directional association.`"

_Exp_: "`...?`"

_Dev_: "`Let's put it that way: The difference between a link with an arrow on one side or on both sides.`"

_Exp_: "`Ok. We don't need a list of components per manufacturer, but simply a reference from the component to its manufacturer.`"

_Dev_: "`Fine, then in Ecore please create a simple reference from class `EnergyComponent` to class `Manufacturer`, maybe named `producedBy`.`"

_Exp_: "`I will try this and get back to you.`"

_Dev_: "`Fine ... good meeting.`"
****

Observe in our data model, reference `producedBy` points _from_ `EnergyComponent` _to_ `Manufacturer` making it uni-directional reference.
One can simply query the manufacturer of a product, but not so the other way around.
With a bi-directional reference both queries would be available.

Observe also the annotations `0..*` and `1..1` near class `Manufacturer`.
These are _multiplicities_ of associations: An `EnergyComponentsCatalog` contains zero, one, or many objects of class `Manufacturer` and an `EnergyComponent` must reference exactly one manufacturer -- not less, not more.

[.float-group]
--
.Ecore Relations
image::EcoreRelations.gif[EcoreRelations, 200, float="right", role="thumb"]

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
364
To recapitulate: Our example parameter catalog already exhibits all four types of relations provided by Ecore.
365
366
367
368
369
370
371
372
373
374
375
You find these in the Ecore editor's palette shown here.
To create a relation between a sub class and a super class use tool `SuperType`.
Use the other tools to create an association between classes, may it be a simple (uni-directional) reference, a bi-directional reference, or a composition.
--

.Attributes and Enumerations

Obviously, attributes are central in data modeling.
Create one by dragging it from the palette onto our one and only class so far: `EnergyComponentsCatalog`.
The class symbol will turn red to indicate an error.
Hover with the mouse pointer over the new attribute and a tooltip with a more or less helpful error message will appear.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
376
377
Current error is caused by that no data type was set for the new attribute.
Data types for attributes can be integer or floating point numbers, strings, dates, booleans, and more.
378
379
380
381
382
383
384
385
386
387
388
389
390
To get rid of the error:

. If not already selected, select new attribute by clicking at it in the editor.
. In view _Properties_ find `EType` and click button `...` to see a quite long list of available data types.
. Choose `EString [java.lang:String]` from the list and the error is gone.

[.float-group]
--
.Class with Attribute
image::EcoreClassWithAttribute.png[EcoreClassWithAttribute, 200, float="right", role="thumb"]

Change the attribute's name to `author` and the class should look like shown here.

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
391
392
Most data types to choose from begin with letter *E* like in **E**core.
These are just Ecore enabled variants of the respective Java types, thus, choose EInt for an int, EFloat for a 32 bit floating point number, EDouble for a 64 bit one, and so on.
393

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
394
395
Ecore allows to introduce new data types.
We employ this feature later to enable data models with physical units and quantities.  
396
397
--

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
398
There exists one other means to define the values an attribute can take, namely enumerations of distinct literals. Take _Monday_, _Tuesday_, _Wednesday_, ... as a typical example for representing weekdays.
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
In our example data model you'll find one _Enumeration_ named `BoilerType` with values `LowTemperature` and `Condensing`.

.Homework

The next section deals with generation of Java code from data models. To have more to play with, please implement our example model in Ecore now.

[.float-group]
--
.Abstract Class
image::EcoreClassifier.png[EcoreClassifier, 200, float="right", role="thumb"]

To do this, there is one more thing to know about classes: the difference between ordinary classes and abstract classes.
'Ordinary class' doesn't sound nice, therefore, classes that are not abstract are called _concrete_ classes.
Our example diagram depicts abstract classes with letter *A* while concrete classes are labeled with *C*. You add abstract classes to a model with a special palette tool shown here.

The thing is: Objects can be created for concrete classes only!

In our example, it makes no sense to create an object from class _EnergyComponent_, because there is not such a thing like an energy component _per se_.
Therefore, this class is _abstract_.
It is true that an inverter _is_ an energy component, thus inheriting all its features, but it was _created_ as _Inverter_, not as _EnergyComponent_.

Super classes will be abstract most of the time.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
421
So my advice is: Model a super class as abstract class unless you convince yourself that there exist real objects in the domain that belong to the super class but, at the same time, do not belong to any of its sub classes.
422
423
424
In the Ecore editor properties view, you can specify if a class is abstract or not, simply by toggling check box `Abstract`.
--

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
425
426
Two more tips and you are ready to rock and roll! -- At least with your homework.

427
428
429
430
431
432
433
434
[TIP]
====
An exhaustive user manual for Ecore diagram editor is available. Execute `Help -> Welcome` and follow link `Learn how to use the diagram editor`.
====

[TIP]
====
If Ecore models get bigger, you may find it more convenient to work with a form based UI instead of, or in addition to, the diagram editor.
Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
435
Open this kind of editor via command `Open With -> Ecore Editor` from the context menu over entry `democatalog.ecore` in the _Model Explorer_ view.
436
437
Note that Eclipse synchronizes different editors of the same content automatically.
====
438
439


440
441
=== Generation of Java Code from Data Model 

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
442
443
444
445
446
By now, your Ecore model should look like this:

[[fig-example-model]]
.Example Model (Homework)
image::Homework.gif[Homework, role="thumb"]
447

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
448
449
Let us bring the model to life, that is, generate code from it that creates, reads, updates, and deletes concrete data objects of modeled classes in computers.
I would like to tell you that this is done with just one click but, actually, you need two or three:
450

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
451
452
453
. Make sure all files are saved (`File -> Save All`)
. Execute `Generate -> Model Code` from context menu over `democatalog.ecore`
. Execute `Generate -> Edit Code` from context menu over `democatalog.ecore`
454

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
455
456
457
[WARNING]
====
Please do *not* execute `Generate -> All` or `Generate -> Editor Code`.
458

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
459
image::GenerateMenu.png[GeneratedClasses, 260, role="thumb"]
460

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
461
462
This would create code for a simple user interface, but we use more advanced EMF Forms for that later.
If, by mistake, project `demo.catalog.editor` was created, just delete it from _Model Explorer_ and do not forget to check `Delete project contents on disk` in confirmation dialog.
463

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
464
====
465

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
466
467
468
469
[.float-group]
--
.Generated Classes
image::GeneratedClasses.png[GeneratedClasses, 260, float="right", role="thumb"]
470

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
471
`Generate -> Model Code` creates classes that represent the modeled data in code. These classes are located in three packages under directory `src-gen` in `demo.catalog`.
472

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
473
`Generate -> Edit Code` creates a whole new Eclipse project named `demo.catalog.edit`, again with generated classes under directory `src-gen`.
474

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
475
You may have a look at some Java classes for curiosity by double clicking at them in _Model Explorer_. There is no point in trying to understand the code in detail, but observe token `@generated` present in the comments of all classes, fields and methods. Classes, fields and methods marked with this token are (re)generated whenever above commands are executed.
476

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
477
Sometimes it maybe required to manually adapt generated code -- after all our concern is "low code", not "no code" development. In that case, we will replace `@generated` by `@generated NOT` to prevent code regeneration.
478

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
479
After code generation, you may have noticed some warnings showed up in view _Problems_.
480

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
481
482
.Warnings
image::Warnings.gif[Warnings, 500, role="thumb"]
483

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
484
485
In general, it is highly recommended to resolve warnings, and errors of course, but we will make an exception from the rule, since the warnings are uncritical and would reappear each time code is regenerated.
--
486
487


Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
488
=== Generation and Tweaking of User Interface
489

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
490
491
In this section you will learn how to generate and tweak a CRUD user interface based on Ecore data model and Java classes created for our demo parameters catalog above. Topics described here are discussed in more detail in tutorial https://eclipsesource.com/blogs/tutorials/getting-started-with-EMF-Forms/[Getting started with EMF Forms].
To find out what user interface controls and layouts are provided by this framework have a look at https://eclipsesource.com/blogs/tutorials/emf-forms-view-model-elements/[EMF Forms – View Model Elements]. _EMF Forms_ is already part of package _Eclipse Modeling Tools_, so we can create a third Eclipse project/plugin that implements a user interface for editing catalog data without further ado:
492

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
493
494
495
496
. From context menu over `democatalog.ecore` execute `EMF Forms -> Create View Model Project`
. Leave project name `demo.catalog.viewmodel` as is but uncheck `Use default location` -- as we always do -- and browse to the directory containing `demo.catalog`
. Click `Next >` and select `EnergyComponentsCatalog` as data element we want to create a user interface for
. Leave `Fill view model with default layout` checked and click `Finish`.
497

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
498
499
According to these inputs a new project is created with file `EnergyComponentsCatalog.view` under directory `view models`.
This file opens automatically in a special _View Editor_.
500

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
501
502
.New View Model
image::ViewModel.png[ViewModel1, role="thumb"]
503

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
504
Like the *data* of our catalog are modeled as Ecore file using a dedicated graphical editor, so will our catalog’s *user interface* be modeled in `.view` files, again using a special editor.
505

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
506
Since we opted for `Fill view model with default layout` the catalog's UI is filled initially with default controls for all data items assigned to Ecore type `EnergyComponentsCatalog` like a string control for `author` or list controls for `boilers`, `chps`, and so on.
507

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
508
509
See red arrow in the above screen-shot?
It points to a button that opens a functional preview of the modeled user interface.
510

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
511
512
.User Interface Preview
image::ViewEditorPreview.png[ViewModel1, 500, role="thumb"]
513

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
514
515
516
[TIP]
====
Double click on tab _EMF Forms Preview_ to enlarge view for better handling -- double click again to get back.
517

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
518
Enable auto refresh mode image:ViewModelAutomaticRefresh.gif[ViewModelAutomaticRefresh, 40] to let each change in the view model instantly be reflected in the preview.
519

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
520
521
522
Given your screen is big enough, you may want to dock-out the preview by dragging tab _EMF Forms Preview_ out of Eclipse's main window.
Seeing editor and preview side by side is a great way to explore the possibilities of view models. 
====
523

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
524
525
526
527
528
Red input field and exclamation mark in the preview signal missing or inconsistent data.
Ecore data model specifies attribute `author` with a lower bound of one, meaning it is a mandatory attribute.
As soon as an author's name is provided, the error indication disappears.
This is what _functional_ preview means.
You can even create new boilers or other objects in lists provided, with all forms created "automagically" with respect to our underlying Ecore data model.
529

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
530
531
532
Of course, such automatic approach has its limits.
In our case, to have a long list of lists is not very user-friendly, because one has to scroll up and down to find a specific list.
Also, no specific object data are shown in the list and data can only be edited in a pop-up form (no inline editing).
533

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
534
How should a better UI look and feel like?
535

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
If there are many lists (types) of entities -- the normal case for parameter catalogs -- users should select what list they want to work with by selecting it from a list or tree view that is always visible, the _master view_.
Once a type is selected in the master view, a table with all objects of this list/type shall appear sidelong in a _detail view_, ready for editing.

In _EMF Forms_ master-detail-views can be modeled either with _Categorization_ or _Tree Master Detail_ UI components.
The latter not only allows to edit information displayed in the detail view, but also the tree of elements in the master view.
Opposed to that, a _Categorization_ presents a fixed hierarchy of elements to choose from.
This is exactly what we need as there are only a fixed number of types of objects to be edited in a parameters catalog.


==== Adding Tables to the UI
 
[.float-group]
--
.Delete default list controls
image::ViewModelDeleteControls.gif[ViewModelDeleteControls, 300, float="right", role="thumb"]

But first, we replace the default controls for lists of boilers, chps, and so on by tables. As shown here, select all list controls in the view model and execute `Delete` from context menu. Refresh _View Editor Preview_ to verify that only field `Author*` is left.
--

[.float-group]
--
.Create Table Control
image::ViewModelCreateTable.gif[ViewModelCreateTable, 300, float="right", role="thumb"]

Next, create a table that shall display all boilers in a catalog:
Select `EnergyComponentsCatalog`, activate context menu and choose `TableControl` from the list of available UI elements.
(EnergyComponentsCatalog represents the root view of the UI and, as such, accepts quite a lot of different UI components as child components.)

Entry `TableControl` was inserted into the list of interface elements below `Control author`.
Checking updated preview reveals no table but a message basically saying that a reference to the domain model is missing, in other words: _EMF Forms_ does not know yet what data to present in table.
Click on entry `TableControl` to see its details.
A red exclamation mark indicates the missing `Domain Model Reference*`.
Click on image:ButtonLinkPlus.gif[ButtonLinkPlus, 40] and be ready to chase a sequence of dialogs:
--

. Click on another image:ButtonLinkPlus.gif[ButtonLinkPlus, 40] in dialog `Configure TableDomainModelReference`

. In wizard `New Reference Element` select `model -> FeaturePathDomainModelReference` and click `Next >`

. Click `Link Domain Model EFeature` and in appearing pop-up list choose reference to list of objects you want to edit in the table, e.g. `boilers`; confirm with `OK` safely ignoring warning about missing `PropertyDescriptor`.

. `Finish` wizard `New Reference Element`

. `Finish` dialog `Configure TableDomainModelReference`.

This was some work, but as reward we get a fully specified table control in _View Editor_ that "translates" into a preview where we can create, read, update, and delete boilers.

.Table for Boilers
image::ViewModelWithTable.gif[ViewModelWithTable.gif, role="thumb"]

Moreover, clicking at a table header sorts all objects in it (rows) according to the values in this column. 
Column widths can adapted, too.

[.float-group]
--
.Modify Table Control
image::ViewModelTweakTable.gif[ViewModelTweakTable, 300, float="right", role="thumb"]

Table UIs can be tweaked in many ways, e.g. selection and sequence of columns can be declared via list `Column Domain Model References`. To fill this list with defaults, execute `Generate Columns` from table control's context menu. Reorder them as you like or delete columns that are not important to the user.

Notice here an important overall feature of _EMF Forms_: If something is left unspecified, be it the view model for an Ecore object type or the specification of table columns, _EMF Forms_ will always find a default solution! Applied to columns specification this means we get default columns automatically back in the moment the last column is removed from list `Column Domain Model References`.

If explicit column specifications are present further configurations can be added to a table control from its context menu, e.g. initial column widths or read-only status of columns. See https://eclipsesource.com/de/blogs/2018/01/31/emf-forms-1-15-0-feature-enhanced-table-renderer/[here] for details.
--

By default only attributes are displayed and directly editable in tables while references to other objects -- in our case the reference to a manufacturer -- are not.

[.float-group]
--
.Default  Panel for Boilers
image::ViewModelWithPanel.gif[ViewModelWithPanel, 300, float="right", role="thumb"]

To get the default (_sic!_) editing panel for an selected table row, in _View Editor_ just set `Detail Editing*` from `None` to `WithPanel`, *press _Tab_*, and save. For boilers, _EMF Forms_ will create the editing panel shown here. Regardless wether users edit data in the panel or directly in the table -- both will stay in sync any time.
--

[WARNING]
====
_View editor_ exhibits an irritating behavior: With preview auto-refresh turned on, any change in the details view is reflected instantly in the preview, even without saving the form or leaving the edited field.

On the other hand, *saving* an updated view editor only takes into account edited fields after they have lost focus, e.g. by pressing _Tab_ key or clicking with the mouse into another field.
So, saving a form before the focus has shifted from the last edited field won't honor this edit, that is you won't necessarily get what you see.
====

One last thing: Enter `boilers` as name for the table control so we can distinguish it from the other four table controls to come.

Yes! ... Please repeat above procedure to create table controls for chps, solar panels, inverters and manufacturers, too. I did this in about 3 minutes. ;-)


==== Master-Detail View with Categories

In last section we improved our catalog's UI by replacing simple object lists by tables that can be sorted, customized and edited inline as well as in an associated panel.
Alas, instead a list of lists we have got an even bigger list of tables.

High time to introduce a master-detail view that presents categories of object types in a master view and, after one is selected, the according object table as detail.

[.float-group]
--
.Category Tree
image::ViewModelCatgorization1.gif[ViewModelCatgorization1, 180, float="right", role="thumb"]

Add a _Categorization_ view to the list of UI elements in _View Editor_ by selecting `EnergyComponentsCatalog` and choose `Categorization` from its contect menu.

Now add two `Composite Category` elements and one `Leaf Category` to `Categorization` from according context menu. This gives us three top level entries in the hierarchy.
639

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
640
641
642
643
644
645
646
In the same way add two `Leaf Category` elements to each `Composite Category` resulting in the hierarchy depicted here.
--

[.float-group]
--
.Completed View Model
image::ViewModelCatgorization2.gif[ViewModelCatgorization2, 260, float="right", role="thumb"]
647

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
This screen shot shows the view model of our UI when finished. To get there:

. Select UI element `Categorization` and rename it to `Categories`

. Rename composite and leaf categories as depicted here

. Drag all table controls one by one into the suited leaf category

. Confirm master-detail view works as expected in the preview.

--

[IMPORTANT]
====
The UI hierarchy to access tables for entity types is independent, and usually will differ, from aggregation and inheritance hierarchies present in Ecore data model (compare fig. <<fig-example-model, Example Model>>).
====

Note that _EMF Forms Preview_ provides these buttons image:ViewModelPersistence.gif[PreviewPersistanceButtons, 68] to clear, load and store edited data.
In fact, this feature gives us a fully functional prototype.
At least during refinement of model and UI, data sets can be created, edited, and tested for usability without the need to built a full blown, deployable application -- see parts _Accessing and Using Parameter Catalogs_ and _Build (Parameter Catalog) Applications with Eclipse Tycho_ below.

Be aware that in some cases the view model must adapt to changes in the data model, e.g. a new leaf category and table component has to be created for a new catalog object type.
Other changes are automatically reflected in the generated UI, at least for default forms and other UI elements.
To our convenience, view model specifications incompatible with data model are indicated by error badges in the _View Editor_.

Changes in data model also can make existing XML data incompatible. There are tools for data migration, but for now, recreation of test data or manual editing of XML file is the way to go.


==== Summary

What have we achieved so far?

We created a graphical Ecore data model with a catalog class and five classes/types of objects therein.
Classes have been defined by name, attributes, and relations between, often with cardinalities.
Whenever classes shared some attributes or references we factored these out into super classes.
An enumeration introduced a new attribute type from a set of named values.

From this data model, we issued commands to create matching Java code for representing the data in memory as well as to store and retrieve them on and from disk. Methods to create, read, update and delete data objects (CRUD) were generated, too.

Lastly, we thought about a good user interface for this data and used _EMF Forms_ to model and prototype it.


=== Bonus: Solutions for Specific Modeling Problems


==== Add Units to the Mix
694
695
696

*TBD*

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
697
As mentioned earlier, parameter catalogs for simulations should be able to represent quantities, not just bare integer and real numbers.
698
699
700
701
702
703
704
705
706
707
708
709
710

using Indrya, the reference implementation for Units of Measurement in Java (JSR 385)

To this end, the author has created two Eclipse plug-in projects providing this feature to be used by Ecore and EMF Forms.

Third-party libraries like Indrya, usually, are not distributed as plug-ins, but _Tycho_ can wrap them automatically as OSGi plug-ins that can added directly to our application.

Another plug-in, created by the author connects the Ecore and Indrya. We will compile it from source code, simply by importing the projects.

. Copy to file system ...
. Import project but *not* copying it in the workspace (just linking)


Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
711
==== Represent Functions in a Parameter Catalog
712
713
714

*TBD*

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
715
for creating custom UI labels:
716

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
717
718
719
* `ExponentialFunctionItemProvider.java`
* `LinearFunctionItemProvider.java`
* `TableFunctionItemProvider.java`
720

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
721
Custom code marked with `@generated NOT` in `de.hftstuttgart.energycomponents.provider` in project `de.hftstuttgart.energycomponents.edit`
722

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
723
==== How to Model Derived References and Attributes
724
725
726

*TBD*

Kai-Holger Brassel's avatar
Kai-Holger Brassel committed
727
We haven't used derived references or attributes by now. But if one has to implement some by providing a getter, it is necessary to return an unmodifiable list like BasicEList.UnmodifiableEList or EcoreUtil.unmodifiableList(...) instead of EList as described here: https://www.ntnu.no/wiki/plugins/servlet/mobile?contentId=112269388#content/view/112269388 .
728