A data type table defines an OpenL Tablets data carrier. Using data types inside the OpenL Tablets rules is recommended, since using data types created in OpenL Tablets table from Java code is limited in the current implementation. For Java code, the preferable method is to use Java or Vocabulary type. For information on how this is done, see Data Table.
The following is an example of a data type table defining a data type called Person:
The first row is the header containing the keyword 'Datatype' followed by the name of the data type. Every row, beginning with the second one, represents one property of the data type. The first column contains property types; the second column contains corresponding property names.
There is an optional 3rd column, that defines default values for fields. The following example extends the type Person with default values for some fields:
Fields gender and male will have the given values for all newly created instances, if other values won`t be provided.
Datatypes tables are being processed second after Properties Table it is done to build a domain model that is used in rules.
Datatype header format:
[Datatype <typename>] or [Datatype <typename> extends <parentTypeName>] or [Datatype <typename> <aliastype>]
1.Define datatype table in Excel
2.At runtime for each datatype, java class is being generated (see Byte code generation at runtime)
3.If it was used the Generating a Static Wrapper and the goal generate datatypes was called, the appropriate java files will be added to classpath (see Java files generation)
There is possibility to inherit one datatype from another in OpenL Tablets. New datatype that inherits another will have access to all fields defined in parent datatype. If child datatype contains fields that defined in parent you will get warnings or errors (if field declared with different types in child and parent).
Also constructor with all fields of child datatype will contains all fields from parent fields and toString, equals and hashCode methods will use all fields form parent datatype.
At runtime, when Openl engine instance is being built, for each datatype
component java byte code is being generated in case there are no
previously generated java files on classpath (see
Java files generation) it represents simple java bean for this datatype. This byte code is
being loaded to classloader so the object of type
Class<?>
can be accessible. Using this object through reflections new instances
are being created and fields of datatypes are being initialized (see
DatatypeOpenClass
and DatatypeOpenField
classes). Remember! If you have previously generated java files for your datatypes on
classpath, they will be used at runtime. And it doesn`t matter that you
have made any changes in Excel. To apply these changes, remove java
files and run
Generating a Static Wrapper.
As generation of datatypes is performed on runtime and users can`t
access this classes in their code, so to the
JavaWrapperAntTask
was added a goal "generate datatypes". It adds the
possibility of generation java files and putting it to the file system.
So users can use this types in their
code.
After parsing, each datatype is put to compilation context, so it will
be accessible for rules during binding. Also all datatypes are placed to
IOpenClass
of whole module and will be accessible from CompiledOpenClass#getTypes
when Openl wrapper will be generated.
Each TableSyntaxNode
that is of type xls.datatype contains an object of datatype as its member.
Working with Datatype arrays from rules
There are 2 possibilities to work with Datatype arrays from rules:
by numeric index, starting from 0
by user defined index
In the first case by call drivers[5] you will get the 6 element of the Datatype array.
Second case is a little more complicated. First field of datatype is considered to be the user defined index. For example if we have a Datatype Driver with first String field name, we can create aData Table, initializing two instances of Driver with names: John and David. Than in rules we can call the
instance we need by drivers[David]. You can use all Java types (including primitives) and Datatypes for your indexes. When the first field of Datatype is of int type called id, to call the instance from array, wrap it with quotes: e.g. drivers[7], in this case you won`t get the 8 element in the array, but the Driver with id equals to 7.