Key Concepts
- Model: A model is a file containing a tree of elements. Each element have a list of attributes. Each model has an associated generator.
- Generator: A generator is a special model that describes the structure and the meaning of other models. It specifies:
- the types of elements and their attributes
- the hierarchical constraints of the elements
- some custom model constraints
- some transformation rules
- There is a meta-generator (xy.generator.2.0) that describes the structure of all the generators including itself.
- Transformation: It is the process of applying a generator transformation rules on a model in order to get some output models.
- Code generation: It is a special kind of transformation that creates code models (models associated to the ultimate xy.any genrator) that can be easily converted to files, folders and textual elements.
- Design: It is a special kind of transformation that creates graphical models (models associated to the ultimate xy.design.source genrator) displayed in the design view.
Creating a Generator
To create a generator, click on the menu “File/New/New Generator…”. You then obtain a model composed of several nodes:
- The “Input” node below which the generator input data is described.
- The “Output” node below which the specification of the generator output data is described.
Once finished, the generator must be installed through the menu “Engine/Install current generator”.. Note that this action copies the generator model to the engine repository (click on the menu “Tools/Options” to locate the repository directory). For example, the generator which name is “ab.cd” will be installed in the “ENGINE_REPOSITORY/ab/cd” directory. Each generator “ComplexType” item can be associated with an icon. Example: The ComplexType named “ef” of the “ab.cd” generator will be associated with the icon “ENGINE_REPOSITORY/ab/cd/ef.gif”.
Creating a Graphical Editor
To create a graphical editor (designer), click on the menu “Tools/New Designer…”. You then obtain a default designer model. Note that a designer is a special type of generator. Once finished, it must be installed through the menu “Engine/Install current generator”. The target models will then be viewable and editable through the design view.
Creating a Reverse Engine
To create a reverse engine, click on the menu “Tools/New Reverse Engine…”. Note that a reverse engine is a special type of generator. Once finished, it must be installed through the menu “Engine/Install current generator”. It will then be possible to reverse-engineer the target nodes.
Creating a Model Migrator
To create a model migrator, click on the menu “Tools/New Migrator…”. Note that a migrator is a special type of generator. Once finished, it must be installed through the menu “Engine/Install current generator”. It will then be possible to migrate the target models.
Export/Import XML
The menus “File/Export/Export Model To XML…” and “File/Import/Import Model From XML…” allow to export and import models for interoperability with other tools.
The built-in generators
The API
In order to automate the build system, you can opt to use Java API. The following jar files must be included in the classpath:
- <INSTALL_DIRECTORY>/lib/generatexy.jar
- <INSTALL_DIRECTORY>/lib/generatexy_lib/*.jar
Java >= 1.6 is required.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import java.io.File; import java.util.List; import xy.Engine; import xy.Serializer; import xy.TransformationResult; import xy.exception.ConstraintException; import xy.exception.DefaultXYException; import xy.exception.GeneratorException; import xy.exception.ModelException; import xy.facade.any.IResourceFacade; import xy.feedback.IProgressListener; import xy.model.Model; import xy.util.ConcreteFacadeUtil; public class APIExample { public static void main(String[] args) throws DefaultXYException, ModelException, GeneratorException, ConstraintException { /* * You must first create the engine that will be necessary to execute * the different tasks and initialize it by specifying the repository * containing the generator specification files that you want to use: */ Engine engine = new Engine(); engine.setRepositoryPath("./repository"); engine.reloadRepository(null, IProgressListener.NULL_PROGRESS_LISTENER); /* * You must then load a model file in memory by using the Serializer: */ Model model = Serializer.loadModel(new File("./examples/example.csv.xymodel")); /* * In order to generate code from a model you just need to invoke the * 'generateCode' method of the engine. This method applies recursive * transformations to the given model and then returns models of the * 'xy.any' generator. These models are wrapped with helper classes from * the 'xy.facade.any' package. Note that you must specify the name of * the source generator that could be different from the edition * generator: */ List<IResourceFacade> resources = engine.generateCode(model, model.getGeneratorName(), true, IProgressListener.NULL_PROGRESS_LISTENER); /* * The next step is to write these 'xy.any' models that represent files * and directories to the disk: */ ConcreteFacadeUtil.writeToDisk(resources, "./generated", IProgressListener.NULL_PROGRESS_LISTENER); /* * In order to just transform a model you need to invoke the 'transform' * method of the engine: */ TransformationResult result2 = engine.transform(model, model.getGeneratorName(), true, true, IProgressListener.NULL_PROGRESS_LISTENER); List<IResourceFacade> resources2 = ConcreteFacadeUtil.serializeToResources( result2.get(), IProgressListener.NULL_PROGRESS_LISTENER); ConcreteFacadeUtil.writeToDisk(resources2, "./generated", IProgressListener.NULL_PROGRESS_LISTENER); /* * It is possible to just validate a model this way: */ engine.validate(model, model.getGeneratorName(), IProgressListener.NULL_PROGRESS_LISTENER); /* * A model can be exported/imported to/from its XML representation this * way: */ Serializer.saveXMLRepresentation(model, new File("./generated/file.xml")); model = Serializer.loadXMLRepresentation(new File("./generated/file.xml")); } } |
Screencast(s)
-
- Video 1: This video shows how to use OxyGenerator to generate code from an XML file
-
- Video 2: This video gives an example of model-to-text transformation through the creation of a C++ code generator
-
- Video 3: This video shows how to create a custom graphical editor using OxyGenerator