5.6.2. foreach=”reference”
Context |
code instructions/foreach loop |
Signature |
foreach=”reference” |
Description |
foreach=”reference” instructs CodeComposer to generate code for each model reference of the current model object regardless of its multiplicity. Warning foreach=”reference” only works when a current model object is present in the context: either specify foreach=”object” or refer directly to an object with object=”<object name>” in your code instruction. Note CodeComposer will place the current model reference in the context with name modelReference for use in build-in functions, custom functions and snippets during generation. |
Exceptions |
This foreach raises an InvalidPatternException when the context does not contain a model object: Error InvalidPatternException: invalid pattern: trying to create a <patternType> foreach reference but currentModelObject is null. Either specify a foreach=”object” in pattern or refer directly to an object of the model with the package=”<package name>” and object=”<object name>” statement. Failed to produce code due to invalid pattern. |
Example |
CodeComposer input 1<?xml version="1.0" encoding="UTF-8"?>
2<java_package xmlns="https://metafactory.io/xsd/v1/java-codeinstruction"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="https://metafactory.io/xsd/v1/java-codeinstruction https://metafactory.io/xsd/v1/java-codeinstruction.xsd"
5 name="${pattern.property.java.package.base}.entities.simple"
6 path="${pattern.property.java.main.directory}"
7 package="domain_model">
8 <class name="${createSimpleEntityClassName(${object.name})}"
9 foreach="object">
10 <field name="${reference.name}"
11 access="rw"
12 foreach="reference"
13 >
14 <datatype>
15 ${createSimpleEntityClassName(${reference.type})}
16 </datatype>
17 <apicommentline>${fmsnippet.examples.exampleCommentForReference}</apicommentline>
18 </field>
19 </class>
20</java_package>
1<#if !(modelReference)??> <#stop "modelReference not found in context" ></#if>
2
3This is an example comment for a reference with any multiplicity: ${modelReference.type} ${modelReference.name}.
1<model>
2 <package>
3 <object name="ExampleEmployee">
4 <metadata></metadata>
5 <attribute name="name" type="String" length="255" notnull="false">
6 <metadata></metadata>
7 </attribute>
8 <reference name="manager" type="ExampleEmployee" multiplicity="0..1" notnull="false">
9 <metadata></metadata>
10 </reference>
11 <reference name="colleagues" type="ExampleEmployee" multiplicity="0..n" notnull="false">
12 <metadata></metadata>
13 </reference>
14 </object>
15 </package>
16</model>
CodeComposer output 1public class ExampleEmployee {
2 private ExampleEmployee manager; // This is an example comment for a reference with any multiplicity: ExampleEmployee manager
3 private Set<ExampleEmployee> colleagues; // This is an example comment for a reference with any multiplicity: ExampleEmployee colleagues
4}
|