Using RPE 1.1.2 Type Cast


When using Type Cast-ing in Rational Publishing Engine a few rules must be followed:

  • use Type Cast when and where supported by the data source
  • use a correct query structure

Use Type Cast when and where supported

RPE will allow you to CAST any type to any other type without checking if there is a relationship between those types to make the CAST valid. Defining invalid casts won’t break the template or cause run time errors but those queries will not return any results.

Using Rhapsody as an example I will give two examples: one schema element (type_ where CAST is valid and one where CAST isn’t.

Valid (Meaningful) Cast – ModelElement

Using the following URL in a browser on a machine where the Rhapsody Web-server is running will give the the list of model elements from each top level package: http://localhost:27463/Rational/Rhapsody/?fields=Projects/Project/Packages/Package/(name|NestedElements/(ModelElement/(name)))

The elements are of type ModelElement, which is a generic type, but the actual collection of results contains instances of derived types such as class, use case etc. You can observe the concrete type ( the xsi:type property) highlighted  in the screenshot bellow.

CAST-ing the ModelElement to Class or Event is valid and will result the subset of NestedElements that are classes/events.

CAST-ing the ModelElement to UseCase is also valid but in my example  ( using the CofeeMachine project) nothing will be returned since the top level packages do not contain any UseCases

Invalid CAST – Package

Using the following URL in a browser on a machine where the Rhapsody Web-server is running will give the the list of model elements from each top level package: http://localhost:27463/Rational/Rhapsody/?fields=Projects/Project/Packages/Package

Examining the results one can observe that the result collection is homogenous, it contains only Packages ( and there is no xsi:type property) so CAST doesn’t make sense here. RPE will allow you to cast the Package to pretty much anything else but that CAST will return no results.

Use a correct query structure

When defining a CAST you cannot define a query on the base type and as a child a query on the CAST type. While RPE will allow you to do that the result will be an invalid template that will fail to load.

To demonstrate this I will use Rhapsody schema and define a CAST to Class on Projects/Project/Packages/Package/NestedElements/ModelElement

Invalid query structure

Defining the following query structure will result in an invalid template:

  •  query 1: Projects/Project/Packages/Package/NestedElements/ModelElement
    • query 2 (child of query 1): Projects/Project/Packages/Package/NestedElements/ModelElement(Class)

The error is marked in red above and it basically means that a query is defined as a child of itself since ModelElement and ModelElement(Class) work on the same data .

The results of invalid query structures can range from being unable to load the template (see this DevWorks thread) to erratic behavior or no results at runtime.

Valid query structure

The proper way to define cast is to handle the base type and the cast as a single query and define it in the context of the base type schema parent. In the above example this means that query 1 must stop at “NestedElements”:

  • query 1: Projects/Project/Packages/Package/NestedElements
    • query 2 (child of query 1): Projects/Project/Packages/Package/NestedElements/ModelElement(Class)

Advertisements

Author: Dragos Cojocari

Architect for Rational Publishing Engine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s