> For the complete documentation index, see [llms.txt](https://docs.rumi.systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rumi.systems/rumi-core/guides/developing-applications/modeling-messages-and-state/the-modeling-language.md).

# The Modeling Language

Rumi aims to make working with messages and state as simple as working with Plain Old Java Objects (POJOs). The **Application Data Modeler** (ADM) defines an XML language to model messages and state entities and tools to generate message and state classes. The generated code handles encoding, serialization and transactional functionality that underpin the platform. This section describes how to model messages and state and to use the code generation tools.

## Overview

The following depicts the various sections of an ADM model.

```xml
<model xmlns="http://www.neeveresearch.com/schema/x-adml"
       xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
       namespace="com.example.trading"
       name="MyModel"
       doc="My model"
       defaultFactoryId="100" >
     
  <import model="../other/model.xml"/>
  <import model="com/mynamespace/messages.xml"/>
  <factories>
    <factory name="MyObjectFactory" id="1" />
  </factories>
 
  <enumerations>
    <!-- Enumerations -->
  </enumerations>
 
  <types>
    <!-- Semantic types -->
  </types>
 
  <fields>
    <!-- reusable field definitions -->
  </fields>
 
  <messages>
    <!-- Messages -->
  </messages>
 
  <entities>
    <!-- Entity Definitions -->
  </entities>
 
  <collections>
    <!-- Collection Definitions -->
  </collections>
</model>
```

### Messages

The `messages` section contains message definitions.

* Each message is assigned a numeric id.
  * Message ids must be unique across all messages, entities and collections in a model and should only be reused over the lifetime of the model per the ADM model versioning rules.
* Each message is comprised of a set of fields.
* Each field is of a type supported by the [ADM Type System](#the-adm-type-system).
* Each field is assigned a numeric id.
  * Field ids must be unique within a message definition and should only be reused over the lifetime of a message definition per the ADM model versioning rules.

The following defines an `AddCustomerMessage` message. It contains the following fields

* A field named `firstName` of type `String` with an id of 1.
* A field named `lastName` of type `String` with an id of 2
* A field name `age` of type `Integer` with an id of 3
* A field named `address` of type `Address` with an id of 4.
  * `Address` is an Embedded Entity type

{% hint style="info" %}
The `state` and `city` fields in the `Address` entity are enumerations whose definitions are not depicted in the example below.
{% endhint %}

```xml
    <messages>
        <message name="AddCustomerMessage" id="1">
            <field name="firstName" type="String" id="1"/>
            <field name="lastName" type="String" id="2"/>
            <field name="age" type="String" id="3"/>
            <field name="address" type="Address" id="4"/>
            .
            .
            .
        </message>
    </messages>
   
    <entities>
        <entity name="Address" id="100" asEmbedded="true">
            <field name="streetNumber" type="Integer" id="1"/>
            <field name="streetName" type="String" id="2"/>
            <field name="city" type="City" id="3"/>
            <field name="state" type="State" id="4"/>
            <field name="zipCode" type="Integer" id="5"/>
            .
            .
            .
        </entity>
   </entities>
```

{% hint style="info" %}
See [Fields](#fields-1) for more information on the various attributes in the `field` element
{% endhint %}

Each message definition generates a Java class. The class's namespace is the model's namespace, and the class name is the name of the message in the model. The following is a code snippet depicting how the generated message is instantiated and populated.

```java
// Code that creates and populates the message
AddCustomerMessage message = AddCustomerMessage.create();
message.setFirstName("John");
message.setLastName("Doe");
message.setAge(42);
Address address = Address.create();
address.setStreetNumber(42);
address.setStreetName("Doe Lane");
address.setCity(City.SomeCity);
address.setState(State.XY);
address.setZipCode(12456);
message.setAddress(address);
```

See [Messages](#messages-1) for more information

### Entities

The `entities` section contains entity definitions. There are two types of entities:

* State Entities
* Embedded Entities

Each entity definition supports an `asEmbedded` attribute. An entity with `asEmbedded=false` is a state entity while an entity with `asEmbedded=true` is an embedded entity.

#### Embedded Entities

An embedded entity is a field container just like a message in that it can contain fields of types from the [ADM Type System](#the-adm-type-system). Embedded entities are technically part of the [ADM Type System](#the-adm-type-system) and, as such, can contain fields that reference other embedded entities as the field type. Embedded entities cannot contain fields that reference other state entities and collections as the field type.

#### State Entities

State entities are used to model a microservice's state tree. A state entity is a field container just like a message or embedded entity except that state entities can contain fields that reference other State entities and [collections](#collections) as the field type. These references to other state entities and [collections](#collections) form the parent child relationships in the modeled state tree.

The following is an example of a state tree modeled using ADM

```xml
    <entities>
        <entity name="Store" id="1">
            <field name="customers" type="Customers" id="1"/>
            .
            .
            .
        </entity>
        
        <entity name="Customer" id="2">
            <field name="firstName" type="String" id="1"/>
            <field name="lastName" type="String" id="2"/>
            <field name="age" type="String" id="3"/>
            <field name="address" type="Address" id="4"/>
            .
            .
            .
        </entity>
        
        <entity name="Order" id="3">
            <field name="orderId" type="String" id="1"/>
            <field name="terms" type="PaymentTerms" id="2"/>
            .
            .
            .
        </entity>
                
        <entity name="Address" id="100" asEmbedded="true">
            <field name="streetNumber" type="Integer" id="1"/>
            <field name="streetName" type="String" id="2"/>
            <field name="city" type="City" id="3"/>
            <field name="state" type="State" id="4"/>
            <field name="zipCode" type="Integer" id="5"/>
            .
            .
            .
        </entity>
    </entities>
```

In the above, the `Customer` entity contains an embedded `Address` entity and references a child `Order` entity. The `Customer`-`Order` relationship is a parent-child relationship in the state model.

Setting and getting referenced state entities is the same as with embedded entities. Here is an example that illustrates the creation and population of a `Customer` entity object

```
Customer customer = Customer.create();
customer.setFirstName("John");
customer.setLastName("Doe");
customer.setAge(42);
Address address = Address.create();
address.setStreetNumber(42);
address.setStreetName("Doe Lane");
address.setCity(City.SomeCity);
address.setState(State.XY);
address.setZipCode(12456);
customer.setAddress(address);
Order order = Order.create();
order.setOrderId(5);
order.setTerms(PaymentTerms.NET30);
customer.setOrder(order);
store.getCustomers().put(100, customer); <-- the code that adds the customer to the collection
```

#### Difference Between a State and Embedded Entity

The difference between a state entity and embedded entity is related to object serialization. When a message, state entity or embedded entity containing another embedded entity is serialized, then the contents of container message, state entity or embedded entity and the contained embedded entity are all serialized together into the same unit of transportation and/or persistence. However, when a state entity A contains a field that references another state entity B as uts type, then when A is serialized, it only serializes the contents of A and not B. In other words, a state entity containing a field that refers to another state entity is a referential relationship while a field of an embedded entity type is a containment relationship.

In the above example, if the `Customer` object was serialized, the serialized contents would contain the `Customer` fields and the `Address` fields. If the serialized contents were used to materialize a new `Customer` object, then the `Address` field would also be present in the materialized `Customer`. However, when the `Customer` object is serialized, the serialized contents do not contain the serialized form of the `Order` object i.e. the `order` field would be null in the `Customer` object materialized from the serialized contents. In other words, when the state tree is persisted, each node in the tree is persisted independently and the relationships reconstituted when deserialized from the persisted form. The storing and reconstituting of the relationships between the objects is done by the Rumi runtime during state persistence and replication so that the application always works with the fully constituted object tree.

See [Entities](#entities-1) for more information

### Collections

The `Collections` section contain collection definitions. A `Collection` is a collection of State Entities of one of the following types

* Queue
* LongMap
* StringMap

`Collections` are part of a state tree model. It serves as a node in the state tree that is a child node of the State Entity that references the Collection and is the parent node of each State Entity that it contains. Here is an example to illustrate this

```xml
    <entities>
        <entity name="Customer" id="1">
            <field name="id" type="Long" id="1" isKey="true"/>
            <field name="firstName" type="String" id="2"/>
            <field name="lastName" type="String" id="3"/>
            <field name="age" type="String" id="4"/>
            <field name="address" type="Address" id="5"/>
            .
            .
            .
        </entity>
        
        <entity name="Order" id="2">
            <field name="orderId" type="String" id="1"/>
            <field name="terms" type="PaymentTerms" id="2"/>
            .
            .
            .
        </entity>
                
        <entity name="Address" id="100" asEmbedded="true">
            <field name="streetNumber" type="Integer" id="1"/>
            <field name="streetName" type="String" id="2"/>
            <field name="city" type="City" id="3"/>
            <field name="state" type="State" id="4"/>
            <field name="zipCode" type="Integer" id="5"/>
            .
            .
            .
        </entity>
    </entities>
    
    <collections>
        <collection name="Customers" is="LongMap" contains="Customer" id="1000"/>
    </collections>
```

The above model is the same as the prior model but extended to hold a collection of `Customer` state entities in a map keyed by the customer id of type Long. The following code illustrates how to work with code generated from the above.

See [Collections](#collections-1) for more information

### Factories

Message and Object factories are used by the Rumi runtime to convert message and state POJOs to and from its serialized form that is transported on the wire and/or persisted to disk. Rumi provides programmatic and configuration mechanisms by which message and object factories are registered with Rumi SMA and ODS runtimes for this purpose.

The `factories` section defines the various factories generated by the model. Each factory is assigned an id that needs to be unique in the entire system. In addition, each message, state entity and embedded entity is assigned a factory id that must be one of the ids of the factories defined in the `factories` section. The model also contains a `defaultFactoryId` attribute that set the default factory id for those messages and entities that don't have a factory id explicitly assigned to them. The `defaultFactoryId` also needs to be one of the ids of the factories defined in the `factories` section of the model.

### Enumerations

Enumerations are part of the [ADM Type System](#the-adm-type-system). The `enumerations` section contains definitions of the various enumerations in the model. The following example illustrates how an enumeration - the `PaymentTerms` enumeration used by the `terms` field in the `Order` entity - is defined.

```xml
    <enumerations>
        <enum name="PaymentTerms">
            <const name="NET30" value ="0"/>
        </enum>
    </enumerations>

    <entities>
        <entity name="Customer" id="1">
            <field name="id" type="Long" id="1" isKey="true"/>
            <field name="firstName" type="String" id="2"/>
            <field name="lastName" type="String" id="3"/>
            <field name="age" type="String" id="4"/>
            <field name="address" type="Address" id="5"/>
            .
            .
            .
        </entity>
        
        <entity name="Order" id="2">
            <field name="orderId" type="String" id="1"/>
            <field name="terms" type="PaymentTerms" id="2"/>
            .
            .
            .
        </entity>
                
        <entity name="Address" id="100" asEmbedded="true">
            <field name="streetNumber" type="Integer" id="1"/>
            <field name="streetName" type="String" id="2"/>
            <field name="city" type="City" id="3"/>
            <field name="state" type="State" id="4"/>
            <field name="zipCode" type="Integer" id="5"/>
            .
            .
            .
        </entity>
    </entities>
    
    <collections>
        <collection name="Customers" is="LongMap" contains="Customer" id="1000"/>
    </collections>
```

The ADM code generator generates a Java enum class for each enumeration defined in a model.

### Namespace

Each model contains a `namespace` attribute. This value of this attribute is sets the name of the package of the generated classes. For example, in the below model, all the generated classes - `AddCustomerMessage`, `Customer`, `Address`, `PaymentTerms`, `Order` and `Customers` - are in the same `com.mycompany` Java package.

```xml
<model xmlns="http://www.neeveresearch.com/schema/x-adml"
       xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
       namespace="com.mycompany"
       name="MyModel"
       doc="My model"
       defaultFactoryId="100" >
     
  <import model="../other/model.xml"/>
  <import model="com/mynamespace/messages.xml"/>
  <factories>
    <factory name="MyObjectFactory" id="1" />
  </factories>
  
  <messages>
    <message name="AddCustomerMessage" id="1">
        <field name="firstName" type="String" id="1"/>
        <field name="lastName" type="String" id="2"/>
        <field name="age" type="String" id="3"/>
        <field name="address" type="Address" id="4"/>
            .
            .
            .
        </message>
    </messages>
   
    <entities>
        <entity name="Customer" id="1">
            <field name="id" type="Long" id="1" isKey="true"/>
            <field name="firstName" type="String" id="2"/>
            <field name="lastName" type="String" id="3"/>
            <field name="age" type="String" id="4"/>
            <field name="address" type="Address" id="5"/>
            .
            .
            .
        </entity>
        
        <entity name="Order" id="2">
            <field name="orderId" type="String" id="1"/>
            <field name="terms" type="PaymentTerms" id="2"/>
            .
            .
            .
        </entity>
                
        <entity name="Address" id="100" asEmbedded="true">
            <field name="streetNumber" type="Integer" id="1"/>
            <field name="streetName" type="String" id="2"/>
            <field name="city" type="City" id="3"/>
            <field name="state" type="State" id="4"/>
            <field name="zipCode" type="Integer" id="5"/>
            .
            .
            .
        </entity>
    </entities>
    
    <collections>
        <collection name="Customers" is="LongMap" contains="Customer" id="1000"/>
    </collections>
</model>
```

### Imports

Since ADM models are essentially class definitions, Rumi aims to enable developers to work with these models as development artifacts equivalent to Java classes. The first part of this integrated support with build tools to inject the code generation seamlessly into the build cycle. The other is the ability to import models as one would do with Java classes.

The `imports` section defines models that contain entity definitions that are referenced in other models. Once a model A is imported into another model B, then entities in B can reference entities in A.

See [Imports](#imports-1) for more information.

### Fields

The ADM model allows fields either to be defined in place (directly on the message or entity that is using the field) or by reference to a field declared in the model's `fields` section. It is a matter of preference which approach an application developer uses: if many messages contain the same field, then it may be more convenient to model the fields in a reusable fashion in the element, but in other cases, it may be more convenient to define the fields in place.

The following is an example that illustrates the use of field definitions>

```xml
<fields>
    <field name="ssn" type="String" id="50"/>
</fields>
 
<entities>
    <entity name="Customer" id="1">
            <field name="id" type="Long" id="1" isKey="true"/>
            <fieldRef ref="ssn"/>
            <field name="firstName" type="String" id="2"/>
            <field name="lastName" type="String" id="3"/>
            <field name="age" type="String" id="4"/>
            <field name="address" type="Address" id="5"/>
            .
            .
            .
        </entity>
</entities>
```

See [Fields](#fields-1) for more information.

### Types

The `types` section is used to define types based on the platform's primitive and built-in types. Such semantic types can be used in place of their corresponding primitive type in the model and will inherit their documentation. The type used for the field in generated messages and entities will be the base type specified by the named field.

The following is an example that illustrates the use of semantic types

```xml
<types>
    <type name="SocialSecurityNumber" base="String" length="12"/>
</types>
 
<fields>
    <field name="ssn" type="SocialSecurityNumber" id="50"/>
</fields>
 
<entities>
    <entity name="Customer" id="1">
            <field name="id" type="Long" id="1" isKey="true"/>
            <fieldRef ref="ssn"/>
            <field name="firstName" type="String" id="2"/>
            <field name="lastName" type="String" id="3"/>
            <field name="age" type="String" id="4"/>
            <field name="address" type="Address" id="5"/>
            .
            .
            .
        </entity>
</entities>
```

See [Semantic Types](#semantic-types) for more information.

## The ADM Type System

ADM supports the following types.

* Primitive Types
* Primitive Type Arrays
* Enumerations
* Enumeration Arrays
* Embedded Entities (Field Groups)
* Embedded Entity Arrays

These types can be used as field types for fields in messages and entity definitions in an ADM model.

### Primitive Types

<table><thead><tr><th width="226">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>Boolean</td><td>boolean</td></tr><tr><td>Byte</td><td>byte</td></tr><tr><td>Char</td><td>char</td></tr><tr><td>Short</td><td>short</td></tr><tr><td>Integer</td><td>int</td></tr><tr><td>Long</td><td>long</td></tr><tr><td>Float</td><td>float</td></tr><tr><td>Double</td><td>double</td></tr><tr><td>String</td><td>java.lang.String</td></tr><tr><td></td><td>com.neeve.lang.XString</td></tr><tr><td>Date</td><td>java.util.Date</td></tr></tbody></table>

### Primitive Type Array

<table><thead><tr><th width="227">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>Boolean[]</td><td>boolean[]<br>com.neeve.lang.XBooleanSequence</td></tr><tr><td>Byte[]</td><td>byte[]<br>com.neeve.lang.XByteSequence</td></tr><tr><td>Char[]</td><td>char[]<br>com.neeve.lang.XCharSequence</td></tr><tr><td>Short[]</td><td>short[]<br>com.neeve.lang.XShortSequence</td></tr><tr><td>Integer[]</td><td>int[]<br>com.neeve.lang.XIntSequence</td></tr><tr><td>Long[]</td><td>long[]<br>com.neeve.lang.XLongSequence</td></tr><tr><td>Float[]</td><td>float[]<br>com.neeve.lang.XFloatSequence</td></tr><tr><td>Double[]</td><td>double]<br>com.neeve.lang.XDoubleSequence</td></tr><tr><td>String[]</td><td>java.lang.String[]<br>com.neeve.lang.XStringSequence</td></tr><tr><td>Date[]</td><td>long[]<br>com.neeve.lang.XDateSequence</td></tr></tbody></table>

### Enumerations

<table><thead><tr><th width="229">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>{enumeration name}</td><td>{model namespace}.{enumeration name}</td></tr></tbody></table>

Enumerations are defined in the `enumerations` section of the ADM model. The name of the enumeration can be used as the type of message and entity fields. See [Enumerations](#enumerations-2) for sample and more details.

### Enumeration Array

<table><thead><tr><th width="228">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>{enumeration name}[]</td><td>{model namespace}.{enumeration name}[]<br>com.neeve.lang.X{Enum Type}Sequence</td></tr></tbody></table>

### Embedded Entity

<table><thead><tr><th width="228">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>{entity name}</td><td>{model namespace}.{entity name}</td></tr></tbody></table>

Embedded entities serve as field groups and can be referenced as a type by fields in entities and messages. Embedded entities are defined in the `entities` section of the ADM model.

### Embedded Entity Array

<table><thead><tr><th width="234">ADM Type</th><th>Java Type</th></tr></thead><tbody><tr><td>{entity name}[]</td><td>{model namespace}.{entity name}[]<br>com.neeve.lang.XLinkedList&#x3C;{model namespace}.{entity name}></td></tr></tbody></table>

## The ADM Model

ADM models are XML models specified by the `x-adml.xsd` schema included at the root of the `nvx-rumi-adm-<version>jar`. Be sure to update your editor's schema validator to reference it.

{% hint style="info" %}
If you are working in an IDE such as Eclipse, try importing the ADM XSD schema into your [eclipse XML catalog](https://wiki.eclipse.org/Using_the_XML_Catalog) so that you can get usage tips on the ADM model by pressing control-space.

{% endhint %}

### Root Element

The root element of a message model is the `model` element, which is used to define a namespace qualified set of modeling elements. To conform to `x-adml.xsd schema` and pass validation, a model must define target XML namespace xmlns="<http://www.neeveresearch.com/schema/x-adml>". This is not to be confused with the `namespace` attribute described below.

The following table describes the various attributes of the root element

<table><thead><tr><th width="178">Attribute</th><th width="484">Description</th><th>Required</th></tr></thead><tbody><tr><td>name</td><td>The Model name can now be specified in the model element itself instead of being supplied externally. If the name contains spaces, then in cases where it used as type name it will be converted to camel case with no spaces (for example "Trading model" would become "TradingModel")</td><td>No</td></tr><tr><td>namespace</td><td>The model's namespace. Model elements use the model namespace as the package name when generating other models, and model importing another model refer to imported model elements using the imported model's namespace.</td><td>Yes</td></tr><tr><td>defaultFactoryId</td><td>The default factory id to be used on model elements that require a factoryId but don't specify one. If the model doesn't define a factory element with a matching id, one is created implicitly by camel casing the model name and appending 'Factory', e.g. TradingFactory.</td><td>No</td></tr><tr><td>doc</td><td>A brief one-line description of the model.</td><td>No</td></tr></tbody></table>

#### Model Name

The ADM model has a name property which is used in certain ways during code generation.

For example, the name of the model is used with the Protobuf code generator as follows:

* As the name of .proto file when IDL is generated.
* As the name of outer java class that wraps model types

The model name can be derived from the model filename, or by defining it explicitly in XML. Name explicitly defined in XML takes precedence over the filename.

{% code title="sampleModel.xml" %}

```xml
<model name="model name goes here">
<!-- Model definitions.....-->
</model>
```

{% endcode %}

For the example above, name property will have a value of *ModelNameGoesHere* (it will be converted to Pascal Notation). If we do not define *name* attribute, the model name would be derived from filename: sampleModel.xml -> *SampleModel*.

{% hint style="info" %}
**Naming the model**

The conversion of a user-defined name to an internally used ADM name can only handle white space as a word separator. Since name will be used to declare a Java class, the developer must take care when specifying a model name to only use white space and characters that are allowed in a class name. \\

If we named the model *sample-model*, the resulting ADM name would be *Sample-model,* which cannot be used as a Java class name because it contains a dash character. Generating code with Protobuf encoding would then cause Java compilation errors. This applies both to name defined through XML attribute and filename-derived ones.
{% endhint %}

### Imports

The import statement allows you to import messages entities and fields from a model in a different namespace.

```xml
<!--Import from the file system-->
<import model="../other/model.xml"/>
 
<!--Or import from the classpath-->
<import model="com/other/model/namespace/model.xml"/>
 
<!-- Types from the imported model can then be referenced -->
<messages>
  <message name="AddCustomerMessage" factoryid="1" id="1">
    <!-- Using a type imported from another model -->
    <field name="address" type="com.other.model.namespace.AddressEntity id="1"/>
    <!-- Using a field definition imported from another model-->
    <fieldRef ref="com.other.model.namespace.firstName"/>
  </message>
</messages>
```

{% hint style="success" %}
**Unqualified Imports Names**

It is not strictly necessary to qualify imported types or fields providing the name is unambiguous across all of a model's imports. However, it is best practice to use the qualified name as it insulates the model from changes to imported models that may cause an unqualified reference to become ambiguous in the future.
{% endhint %}

{% hint style="info" %}
**Mixing encodings via import**

Types from a model imported must be generated with the same encoding type as the model importing them. It is not possible to mix and match different encoding types within a message or entity. So if MessageA is generated with Xbuf and embedded EntityB is generated with Protobuf, Message cannot use EntityB as a field.

When generating code and copying model to output, encoding information is written to target model XML as a directive. If the model for which code is generated and any of its imports have an encoding mismatch, the code generator will raise a model validation error. For imported models that do not have encoding info (resolved directly to OS path or packaged to jar with earlier versions of ADM), this validation is not enforced.
{% endhint %}

See also Imported Model Resolution (TODO)

### Factories

The factories section defines object factories that are used to instantiate the generated object. Each factory in an application must have a unique factory id. The ID is serialized along with the object or transported in MessageMetadata and is used to reconstitute its objects during deserialization. A single model can define multiple factories, allowing Messages and Entities to be grouped together as the application sees fit. A factory can contain a maximum of 32767 types, so in practice, it is rarely a *requirement* to use multiple factories within a single model. Each type in the model defines the factory to which it belongs via its factoryid attribute. The user application may define factories with ids greater than or equal to 1, ids <= 0 are reserved for platform internal use.

```xml
<factories>
  <factory name="CommonFactory" id="1"/>
  <factory name="PizzaServiceFactory" id="2"/>
  <factory name="BookStoreServiceFactory" id="3"/>
</factories>
```

<table><thead><tr><th width="150">Attribute</th><th width="489">Description</th><th>Required</th></tr></thead><tbody><tr><td>name</td><td>The factory name to be used for code generation.</td><td>No</td></tr><tr><td>id</td><td><p>The factory's id which is used to register the factory with the runtime and identify the factory to use when decoding the factory's serialized objects.</p><p>The factory id must be between 1 and 65536 inclusive. Values of 0 or less are reserved for use by the platform.</p></td><td>Yes</td></tr><tr><td>doc</td><td>A brief one-line description for the factory. If more detailed documentation is needed, a child &#x3C;documentation> element can be used.</td><td>No</td></tr><tr><td>deprecated</td><td>When a factory is marked as deprecated it will be marked as deprecated by the code generator.</td><td>No</td></tr></tbody></table>

### Enumerations

Enumeration types can be modeled as follows and can be declared as fields of entity or message types. Unlike other model elements, enumerations aren't tied to a particular factory despite being scoped to the model's namespace. Generated java enumerations don't have dependencies on the rest of the platform and can be used standalone.

#### ADM

```xml
<enumerations>
  <enum name="IntEnumeration" type="int">
    <const name="Value1" value="1"/>
    <const name="Value2" value="2"/>
  </enum>
  <enum name="CharEnumeration" type="char">
    <const name="Value1" value="A"/>
    <const name="Value2" value="B"/>
  </enum>
  <enum name="StringEnumeration" type="string">
    <const name="Value1" value="zero"/>
    <const name="Value2" value="one"/>
  </enum>
</enumerations>
```

Generated Enumerations can specify a type which is accessible in the generated code. The type can be int, char or String. A const should not be removed from an enumeration, if the newly generated code is expected to deserialize enums that were persisted with an earlier version, in addition, the order of constants is important and should not be changed.

#### Generated Source

```java
@Generated(value="com.neeve.adm.AdmEnumeration", date="Fri Jan 23 02:03:22 PST 2015")
public enum IntEnumeration {
  Value1 (1),
  Value2 (2);
  
  /**
   * A zero garbage alternative to {@link Enumeration#values()}.
   */
  final public static List<Enumeration> VALUES = Collections.unmodifiableList(Arrays.asList(Enumeration.values()));
 
  //...
}
```

### Fields

The ADM model allows fields either to be defined in place (directly on the message or entity that is using the field) or by reference to a field declared in the model's section. It is a matter of preference which approach an application developer uses: if many messages contain the same field, then it may be more convenient to model the fields in a reusable fashion in the element, but in other cases, it may be more convenient to define the fields in place.

```xml
<!-- Defines fields which may be referenced by other model elements-->
<fields>
    <field name="myStringField" type="String" id="10000" length="16" doc="My String field" />
</fields>
 
<messages>
    <message name="Message" factoryid="1" id="1">
        <fieldRef  ref="myStringField" name="myField"required="true" />
        <field name="myOtherField" type="String" length="16" id=10001" doc="My Other String field" />
    </message>
</messages>
```

#### Field Element Attributes

<table><thead><tr><th width="135">Attribute</th><th width="520">Description</th><th>Required</th></tr></thead><tbody><tr><td>name</td><td>The name of the field</td><td>yes</td></tr><tr><td>type</td><td>The field type. For message and embedded entities, the supported field types are the types in the <a href="#the-adm-type-system">ADM Type System.</a> For state entities, the field type could also be a name of another state entity or collection.</td><td>yes</td></tr><tr><td>id</td><td><p>The field id</p><p><em><strong>Note:</strong> Field ids must be between 0 and 32767 inclusive</em></p></td><td>no</td></tr><tr><td>required</td><td>Whether or not the field is a required field for the type. Fields declared as required will cause a check to be added for the value being set in the generated types isValid() method.</td><td>no</td></tr><tr><td>isKey</td><td>True if this field should serve as the key when inserting the entity into a Map collection. In this case, insertion of the message in the collection with a given key will update this field with the key value.<br><br><em>Note: Only applies to state entity elements</em></td><td>no</td></tr><tr><td>doc</td><td>The doc to use for the field</td><td>no</td></tr><tr><td>deprecated</td><td>When true all methods generated for the field will be marked as deprecated.</td><td>no</td></tr></tbody></table>

#### Field Reference Element Attributes

<table><thead><tr><th width="134">Attribute</th><th width="521">Description</th><th>Required</th></tr></thead><tbody><tr><td>ref</td><td><p>The name of the referenced field. Name may be with or without the namespace of owner model (i.e. full-qualified name). In both cases, the field will be looked up in both the current model and its imports. If the non-fully-qualified name is declared in the current model and in one of the imports, the field from the current model will take precedence. If multiple imports define the same name, an error will be reported.</p><p>The best practice when importing a field from another model is to use the fully qualified name of the imported field (e.g. com.example.importedmodel.someField), as this insulates your model from future conflicts in the event that imported models are changed.</p></td><td>Yes</td></tr><tr><td>name</td><td>Optionally, a name for the field that overrides the referenced field's name. Otherwise, the field name defaults to that of the referenced field.</td><td>No</td></tr><tr><td>jsonName</td><td>Optionally overrides the referenced field's jsonName.</td><td>No</td></tr><tr><td>id</td><td><p>Optionally overrides the id specified by the field being referenced.</p><p><em>Field ids must be between 0 and 32767 inclusive</em></p></td><td>No</td></tr><tr><td>required</td><td>Whether or not the field is a required field for the type. Fields declared as required will cause a check to be added for the value being set in the generated types isValid() method.</td><td>No</td></tr><tr><td>isKey</td><td>True if this field should serve as the key when inserting the entity into a LongMap or StringMap collection. In this case, insertion of the message in the collection with a given key will update this field with the key value.<br><br><em>Note: Only applies to state entity elements</em></td><td>No</td></tr><tr><td>doc</td><td>The doc to use for the field which overrides that specified by the referenced field.</td><td>No</td></tr><tr><td>deprecated</td><td>When true all methods generated for the field will be marked as deprecated.</td><td>No</td></tr></tbody></table>

{% hint style="warning" %}
**Qualifying conflicting type names**

It is not possible to define two types with the same name in a given model. One exception to this rule is that it is possible (though strongly discouraged) to define a type with the same name as a built-in type. In this case, using an unqualified type reference will favor the built-in type. In this case, it is possible to reference the local type by using the *this* keyword, or the fully qualified name:

```
<model xmlns="http://www.neeveresearch.com/schema/x-adml"
       xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
       name="Trading" 
       namespace="com.mycompany.trading" 
       defaultFactoryId="100">
       doc="Contains Messages used by the Trading Application">
 
  <import model="../orderprocessing.xml"/>
 
  <!--An unfortunately named entity:-->
  <entity name="Currency" id="1">
    <field name="currencyCode" type="String"/>
  </entity>
 
  <entity name="MyEntity" id="2">
    <!--
      Without qualifying the Currency type, the built
      in Currency type is used, and the generated entity
      will return a java.util.Currency.
    -->
    <field name="javaCurrency" type="Currency" />
    <!-- 
      The 'this' keyword indicates that this is the Currency
      entity defined in this model:
    -->
    <field name="currencyEntity" type="this.Currency"/>
    <!--
      Namespace qualifying the typeindicates that this is the Currency
      entity defined in this model:
    -->
    <field name="currencyEntity2" type="com.mycompany.trading.Currency"/>
    <!--
      Namespace qualifying the type indicates that this is the Currency
      entity defined in some imported model:
    -->
    <field name="currencyEntity3" type="com.mycompany.orderprocessing.Currency"/>
  </entity>
</model>
```

{% endhint %}

### Semantic Types

The ADM model provides a section that can be used to define types based on the platform's primitive and built-in types. Such semantic types can be used in place of their corresponding primitive type in the model and will inherit their documentation. The type used for the field in generated messages and entities will be the base type specified by the named field.

#### ADM Model

```xml
<!-- Defines semantic types that can be referenced elsewhere in the model in place -->
<types>
    <type name="SocialSecurityNumber" base="String" length="12" poolable="true">
            doc="Represents a social security number in the format xxx-xx-xxxx">
            <documentation>
          <![CDATA[
            Detailed information about social security numbers can be found at the <a href="http://www.ssa.gov/">Social Security Website</a>.
          ]]>
        </documentation>
    </type>
    <type name="Salary" base="float" doc="Represents a salary in US dollars."/>
</types>
 
<fields>
    <field name="ssn" type="SocialSecurityNumber" id="1"/>
    <field name="salary" type="Salary" id="2"/>
</fields>
 
<messages>
    <message name="EmployeeSalarayUpdateMessage" factoryid="1" id="1">
        <fieldRef ref="ssn" required="true" />
        <fieldRef ref="salary" required="true" />
    </message>
</messages>
```

#### Generated Code

Note that "Price" doesn't result in a new java type being created. The generated source uses its base type (float) directly and inherits the documentation of the semantic type. This is true of all semantic types, with the exception of types with a base of String that are declared as poolable (See poolable string types below).

```java
public interface IEmployeeSalarayUpdateMessage extends IRogNode, IRogMessage {
    /**
     * Sets the value of this field to the provided SocialSecurityNumber value.
     */
    public void setSsn(final String val);
 
    /**
     * Gets the value of 'ssn'
     *
     * @return The value of 'ssn'
     */
    public String getSsn(); 
     
    /**
     * Sets the value of 'salary'
     * <p>     
     * <h2>Field Semantics</h2>     
     * <p>
     * Represents a salary in US dollars.
     *
     * @param val The value to set.
     */
    public void setSalary(final float val);
    /**
     * Gets the value of 'salary'
     * <p>     
     * <h2>Field Semantics</h2>
     * <p>
     * Represents a salary in US dollars.
     *
     * @return The value of 'salary'
     */
    public float getSalary();
}
 
/**
 * <p>
 * Represents a social security number in the format xxx-xx-xxxx.
 * <p>
 * Detailed information about social security numbers can be found at the <a href="http://www.ssa.gov/">Social Security Website</a>.
 */
public class SocialSecurityNumber extends XString {...}
```

### Entities

Entities are defined in the entities element of the model and must have a unique id with respect to other types within the scope of their factory.

#### ADM Model

```xml
<entities>
  <entity name="EntityA" factoryid="1" id="1">
    <field name="enumField" type="Enumeration" doc="tests an enum field" id="1"/>
    <field name="enumArrayField" type="Enumeration[]" doc="tests an enum array field." id="2"/>
    <field name="intEnumField" type="IntEnumeration" id="3"/>
    <field name="intEnumArrayField" type="IntEnumeration[]" id="4"/>
    <field name="charEnumField" type="CharEnumeration" id="5"/>
    <field name="charEnumArrayField" type="CharEnumeration[]" id="6"/>
    <field name="stringEnumField" type="StringEnumeration" id="7"/>
    <field name="stringEnumArrayField" type="StringEnumeration[]" id="8"/>
    <field name="booleanField" type="Boolean" id="9"/>
    <field name="booleanArrayField" type="Boolean[]" id="10"/>
    <field name="byteField" type="Byte" id="11"/>
    <field name="byteArrayField" type="Byte[]" id="12"/>
    <field name="shortField" type="Short" id="13"/>
    <field name="shortArrayField" type="Short[]" id="14"/>
    <field name="intField" type="Integer" id="15"/>
    <field name="intArrayField" type="Integer[]" id="16"/>
    <field name="longField" type="Long" id="17"/>
    <field name="longArrayField" type="Long[]" id="18"/>
    <field name="floatField" type="Float" id="19"/>
    <field name="floatArrayField" type="Float[]" id="20"/>
    <field name="doubleField" type="Double" id="21"/>
    <field name="doubleArrayField" type="Double[]" id="22"/>
    <field name="stringField" type="String" id="23"/>
    <field name="stringArrayField" type="String[]" id="24"/>
    <field name="dateField" type="Date" id="25"/>
    <field name="dateArrayField" type="Date[]" id="26"/>
    <field name="charField" type="Char" id="27"/>
    <field name="charArrayField" type="Char[]" id="28"/>
    <field name="currencyField" type="Currency" id="29"/>
    <field name="currencyArrayField" type="Currency[]" id="30"/>
    <field name="embeddedEntityField" type="EmbeddedEntity" id="32"/>
    <field name="embeddedEntityArrayField" type="EmbeddedEntity[]" id="33"/>
    <field name="entityField" type="EntityB" id="34"/>
    <field name="entityBMapField" type="EntityBLongMap" id="35"/>
  </entity>
  
  </entity name="EntityB" factoryid="1" id="2">
    <field name="intField", type="Integer" id="1"/>
  <entity>
  
  <entity name="EmbeddedEntity" factoryid="1" id="3" asEmbedded="true">
    <field name="longField", type="Long" id="1"/>
  </entity>
    
</entities>
```

**Entity Element Attributes**

<table><thead><tr><th width="152">Attribute</th><th width="496">Description</th><th>Required</th></tr></thead><tbody><tr><td>name</td><td>The name of the field, must be unique within the model.</td><td>Yes</td></tr><tr><td>id</td><td><p>The id of the entity, which must be unique within the scope of all types in the entity's factory.</p><p><em>Entity / Message ids must be between 0 and 32767 inclusive</em></p></td><td>Yes</td></tr><tr><td>factoryId</td><td>The id of the factory, which must be unique within the scope of all factories used within an application. When a message is received, the factoryId and entityId uniquely identify the type to be deserialized. factory IDs &#x3C;= 0 are reserved for platform use.</td><td>Yes</td></tr><tr><td>asEmbedded</td><td>Defaults to false, Indicates whether or not this entity is generated to be used as an embedded or child field of another entity. Embedded entities are always serialized transported with its parent entity. Entities used as fields in messages must be declared as embedded.</td><td>No</td></tr><tr><td>transactional</td><td>Whether or not this entity supports transaction commit and rollback via the applications ODS store.</td><td>No</td></tr></tbody></table>

#### Entity Field Element Attributes

<table data-header-hidden><thead><tr><th width="151">Attribute</th><th width="499">Description</th><th>Required</th></tr></thead><tbody><tr><td>type</td><td><p>The type of the element.</p><p>If the type is defined in this namespace, is defined in only one of the imported models, or is a primitive or collection type, only the simple name of the type need be used. A non-fully-qualified name will be looked up in both the current model and its imports. If name is declared in the current model and in one of the imports, the type from the current model will take precedence. If multiple imports define the same name, an error will be reported. The best correction in such case is to use the fully-qualified name.</p><p>If the field is an array type, it should be suffixed with array indices such as MyEntity[] to denote it as an array.</p></td><td>Yes</td></tr><tr><td>name</td><td>The name of the field, must be unique within the model.</td><td>Yes</td></tr><tr><td>jsonName</td><td>Contains the name of the JSON property that will be used for the field when the message is serialized to JSON. Defaults to using the value defined in name.</td><td>No</td></tr><tr><td>id</td><td><p>The id of the field. The id must be unique with the scope of this entity. For Xbuf/Protobuf encoding, this tag is used as the tag value for the field on the wire. If not set, a unique id will be generated by the source code generator. For better control over compatibility, it is recommended that application set this value manually.</p><p><em>Field ids must be between 0 and 32767 inclusive</em></p></td><td>No</td></tr><tr><td>isKey</td><td>True if this field should serve as the key when inserting the entity into a LongMap or StringMap collection. In this case, insertion of the message in the collection with a given key will update this field with the key value.</td><td>No</td></tr></tbody></table>

#### Generated Source

For an entity named "MyEntity" an interface and an implementation will be generated using the model's namespace. Entities will extend IRogNode or IRogContainerNode marking that they can be used as nodes with ODS.

```java
@Generated(value="com.neeve.adm.AdmGenerator", date="Fri Jan 23 02:03:22 PST 2015")
public interface IEntityA extends IRogContainerNode {...}
```

```java
@Generated(value="com.neeve.adm.AdmXbufGenerator", date="Fri Jan 23 02:03:22 PST 2015")
final public class EntityA extends RogContainerNode implements IEntityA, IXbufDesyncer, IRogJsonizable {...}
```

### Messages

Messages are defined in the messages element and must have a unique id with respect to other types within the scope of their factory. Messages can use all modeling capabilities of entities, but cannot use non-embedded entities or non-array collections as fields.

#### ADM Model

```xml
<messages>
  <message name="SampleMessage" factoryid="1" id="1">
    <field name="sno" type="Long" id="10000"/>
    <field name="symbol" type="String" id="10001" />
    <field name="price" type="Float" id="10002"/>
    <field name="embeddedEntity" type="EmbeddedEntity" id="10003"/>
    <field name="embeddedEntityArray" type="EmbeddedEntity[]" id="10003"/>
  </message>
</messages>
```

#### **Message Attributes**

Messages support the same attributes as entities (listed above) with the exception that transactional defaults to false for messages.

#### **Message Field Attributes**

Message fields support the same attributes as entities (listed above).

#### **Notes**

For Xbuf and Protobuf encoding, the id is used to generate the protobuf field tag, so changing field ids will break wire compatibility. If fields are not explicitly assigned IDs in the model, then the ADM generate will assign them automatically. in this case, fields should not be removed or changed in order to maintain wire compatibility with earlier versions of the generated code.

Messages can only declare primitive types, built-in types, embedded types as fields (or arrays of those types). Collections, Messages, and non-embedded Entities can't be used.

#### Generated Source

For an entity named "MyMessage" an interface and an implementation will be generated using the model's namespace. Entities will extend IRogNode or IRogMessage, marking that they can be used as nodes with the platform's ODS and SMA modules.

```java
@Generated(value="com.neeve.adm.AdmGenerator", date="Fri Jan 23 02:03:22 PST 2015")
public interface IMyMessage extends extends IRogNode, IRogMessage {...}
```

```java
@Generated(value="com.neeve.adm.AdmXbufGenerator", date="Fri Jan 23 02:03:22 PST 2015")
final public class MyMessage extends RogNode implements IMessage, ILnkMessage, MessageReflector, IXbufDesyncer, IRogJsonizable  {...}
```

### Collections

Collections are defined in the element and must also have a unique id with respect to other types within the scope of their factory. Collections may not be declared as embedded at this time.

#### ADM Model

```xml
<collections>
  <collection name="MyQueue" is="Queue" contains="MyEntity"
    factoryid="1" id="5" />
  <collection name="MyLongMap" contains="MyEntity" is="LongMap"
    factoryid="1" id="6" />
</collections> 
<entity name="MyEntity" factoryid="1" id="1">
 <field name="longKey" type="Long" isKey="true" id="1"/>
 <field name="aQueue" type="MyQueue" id="2"/>
 <field name="aMap" type="MyStringMap id="3"/>
</entity>
```

#### Generated Source

```java
@Generated(value="com.neeve.adm.AdmGenerator", date="Fri Jan 23 02:03:22 PST 2015")
public interface IChildLongMap extends IRogLongMap<Child1>, Map<Long, Child1> {}
 
@Generated(value="com.neeve.adm.AdmXbufGenerator", date="Fri Jan 23 02:03:22 PST 2015")
final public class ChildLongMap extends RogLongMap<Child1> implements IChildLongMap, IXbufDesyncer, IRogJsonizable {...}
```
