Meta-model entity generator
Generator that takes an entity-relationship-like model as input and generates various blocks of source files that either describe the model in the target language or perform some functionality with the structures of the model.
The tool's goals are:
- Reduce boilerplate while maintaining transparency
- Reduce points where error might be manually introduced
- Respect multiple languages and preserve type consistency
Example uses: class definition, initialization methods, comparators, database storage/retrieval operations, protocol specifications, documentation, etc.
This tool is to be used when language magic is not desired and when transparent code and ability to diagnose is prefered. Examples of language magic: Python metaclasses, synthesized properties and methods.
The entigen
tool requires Python >= 3.6
usage: entigen [-h] [-b BLOCK_TYPE] [-f READER] [-t WRITER] [-V VARIABLES]
model [entities [entities ...]]
Process some integers.
positional arguments:
model Model source
entities Entities to be included
optional arguments:
-h, --help show this help message and exit
-b BLOCK_TYPE, --block BLOCK_TYPE
Block type the writer writes
-f READER, --from READER
Metamodel input format
-t WRITER, --to WRITER
Text output format
-V VARIABLES, --variable VARIABLES
Text output format
Example: go to the examples
directory and run:
entigen thing.model > thing.py
Then see the generated thing.py
file.
The following writers are available:
python
– Python source file or snippet writerinfo
– Text output writer
The Python writer writes type-annotated Python 3.6 source code. Blocks:
class
– class with instance variable annotations and the__init__
and__eq__
methodclass_file
– file with classes of specified entitiesenums_file
– file with definitions of all enums
__init__
– method takes one argument per entity property and then assigns
it to the corresponding instance variable. If a variable is composite, such as
list, and has a default value, then the default value is assigned within the
method, not in the argument list.
__eq__
– method takes other object, then compares whether the other object is
of the same subclass as the entity. All properties of the entity are compared
with the properties of the other entity.
Variables:
entities_module
– module name from which entities are importedentity_per_module
- if set to true, then every entity imported has it's own sub-module within theentities_module
. For example, if entity dImportantThing
is required, and the entities module isentities
then the import will befrom entities.important_thing import ImportantThing
enums_module
– module from which enums are imported
The info
writer can be used by shell scripts to learn more about the moden
and the have better control over overal output generation using other writers.
To list names of all entities in a model, one per line, use:
entigen -w info model
Variables:
decamelize
– write entity names as lower-case identifiers
The default reader is a csv
reader. The meta-model is a directory with CSV
files describing meta-model entities. Files:
properties.csv
– list of entity properties. Fields: category, entity, name, type, optional, tag, label, descriptionentities.csv
– list of entities.
The main reason for the CSV input format is that it is structured and can be edited as text or as a spreadsheet. Spreadsheet applications are wide-spread enough and they have quite comfortable user interface for editing structured data.
The generator comes with it's own very simple data types. The variety of types is intentionally limited so we are able to cover wide variety of outputs with easy type and type handling translation.
The base types are:
string
int
identifier
- internally same as string, but writers can implement checks for content beign valid identifierobjref
- object reference – interpretation left to application or writer. UsuallyAny
orobject
type, in database it might be justinteger
oruuid
The complex types are:
list<BASETYPE1,BASETYPE2,...>
- list of base-type objects
The following special values should be compared as whole strings for their corresponding types.
[]
is a special default value for a string meaning an empty list
Some things to keep in mind while working on this tool:
- The generated source should be as human-readable as possible, it should be nicely formatted and not far from how a person with a good style would write it.
- When adding a core data type its availability or convertibility to other languages (programming or modelling) should be strongly considered.
Author: Stefan Urbanek [email protected]
License: MIT