5.6.1. Functions

Functions can be used in expressions (patterns) or from a (Freemarker or Velocity) snippet by using the generator context variable.

Based at the functionality we distinguish the following function categories:

  • String functions

    Globale beschrijving

  • Metadata functions

    Globale beschrijving

  • Miscellaneous

    Other

5.6.1.1. String functions

Name

Description

Details

allLower

Converts all characters to lower case.

allUpper

Converts all characters to upper case.

camelToAllUpperUnderscore

firstLower

Converts the first character to lower case.

Example:

<pattern>
        ...
        <class name="${firstLower(${object.name})}">
                ...
        </class>
        ...
</pattern>

firstUpper

Converts the first character to upper case.

5.6.1.2. Metadata functions

Name

Description

Details

Naam

Beschrijving

5.6.1.3. Miscellaneous

Name

Description

Details

extendsFromObject

Returns true if specified model object extends from some other object (using the extends attribute).

getActionType

getHibernateType

getJavaType(String sxmiType)

Returns the java type that belongs to the given sxmiType

getJavaWrapperClass(String sxmiType)

Returns the wrapper class (Boolean, Short, Integer etc.) which is part of the java type that belongs to the given sxmiType

isParentOfObject

Returns true if there are model objects in the current model package which extends from the specified model object.

isPrimitiveJavaType(String sxmiType)

Returns true if the java type that belongs to the sxmiType is a primitive type(boolean, short, int etc.), otherwise false

length

packageToPath

propertyExists

Returns true if the property is present (referenced in the expression), otherwise – false.

Example:

<attribute name="${attribute.name}" foreach="attribute" access="rw">
        <library>javax.persistence.Column</library>
        <datatype>${attribute.type}</datatype>
        <annotation.getter>${snippet.java.annotation.jpa.column}</annotation.getter>
        <annotation.getter>${snippet.java.annotation.jpa.required}</annotation.getter>
        <annotation.getter evaluate="true">
                ${snippet.java.annotation.hibernate.validator.length}</annotation.getter>
        <body condition="${propertyExists(${model.property.attribute.entity.default.value})}">
                ${model.property.attribute.entity.default.value}</body>
        <setterBody>${snippet.java.model.operation.setter}</setterBody>
</attribute>

firstToken(String expression, String token)

Evaluates the expression, divides the outcome by means of the given token (see java.lang.String.split(token)). Returns the 1st String from the array which that split produces.

getAttributeType(String objectname, String associationPath)

Finds the type of the attribute which is specified in the last part of associationPath.

  • @param objectname : Name of object element to start associationPath

  • @param associationPath

  • @return Type (sxmi type) of attribute

  • @throws InvalidPatternException

    Thrown if objectElement is not a object or last part of associationPath is not a attributeAn associationPath is a path through the model: e.g. project.responsiblePerson.homeAddress

getLastElementFromAssociationPath(String associationPath, Element parentElement)

Finds the last element by starting with parentElement and following all associations in associationPath.

  • @param associationPath

  • @param parentElement

  • @return Element corresponding with last part of association path. It can be a attribute element or a reference element

  • @throws InvalidPatternException

    Thrown if association path does not contain a dot, or elements are not found when following associationPathAn associationPath is a path through the model: e.g. companyAddress.address.street

getObjectName(String objectname, String associationPath)

Finds the name of the object, the last part of the associationPath it belongs to. When associationPath is contactPerson.workAddress.streetName, and the objectName is Relatation, and the Relation object has a reference named ‘contactPerson’ (the starting point of the associationPath) it returns the name of the object where streetName (model attribute) belongs to.

In this case it returns Address and the Address model object has a attribute named streetName.

getReferenceType(String objectname, String associationPath)

Finds the type of the reference which is specified in the last part of associationPath.

  • @param objectname : Name of object element to start associationPath

  • @param associationPath

    An associationPath is a path through the model: e.g. project.responsiblePerson.homeAddress

  • @return Type : (object name) of reference

  • @throws InvalidPatternException

    Thrown if objectElement is not a object or last part of associationPath is not a valid reference.

getSetterName(String referenceType, String modelObjectName)

Finds the name of the setter from referenceType of the property modelObjectName.

It looks in all modelObjects for an object with name “referenceType”. From this object it looks for a reference with type “modelObjectName”. The setterName is constructed and returned from the name of this reference.example:

Example:

<model>
        <object name="Object1">
                <reference name="myObject2" type="Object2" multiplicity="1..1" />
        </object>

        <object name="Object2">
                <reference name=myObject1 type="Object1" multiplicity="0..n" />
        </object>
</model>

getSetterName(“Object1”, “Object2”) = setMyObject2

The reference to modelObjectName must have a multiplicity of 0..n or 1..n otherwise a InvalidPatternException is thrown.

  • @param referenceType : object name (not reference!!) to look for

  • @param modelObjectName : reference with type modelObjectName to look for

  • @return : name of the setter for the reference

  • @throws InvalidPatternException if model or reference not found

getValue(String expression, String defaultValue)

Evaluates the expression and returns defaultValue if the outcome of the expression is empty (nil or an empty string)

isAttribute(String objectname, String associationPath)

Checks the last element of the associationPath and returns true if it’s a model attribute

isReference(String objectname, String associationPath)

Checks the last element of the associationPath and returns true if it’s a model reference

lastToken()

Evaluates the expression, divides the outcome by means of the given token (see java.lang.String.split(token)). Returns the last String from the array which that split produces

map(String mapName, String key)

getTokenByIndex(String expression, String token, String tokenNumberString)

Finds a token in expression, by splitting expression using token as seperator. The tokenNumber is the number of token to find

  • @param expression : String to find token in

  • @param token : Token to use when splitting expression

  • @param tokenNumberString : Index of token array to return

  • @return Found token

  • @throws InvalidPatternException

    Thrown if token not found in expression or tokenNumber is larger than size of array of tokenized expression, or when tokenNumberString can not be converted to a integer