This article is not up to date and it has been substitute with new up to date content.

Please look the new up to date content.

The derivation in the data model

by Michela Frigerio
9,003 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

The data model can be enriched with many informtion that can be derived from other elements in the model. This kind of information is called "derived data". In WebRatio the derivation is expressed through IFML OQL derivation queries. In the database all derived information are installed as views. In order to specify a derived information you have to use the Derivation Wizard. The wizard is available clicking on the button next to the "Derivation" property in the General tab of the Properties View after you added the element you want to derive in the data model. In a data model you can derive:

  • Entities. A derived entity is an abstract concepts that represent a subclass of objects having a specific property. Usually a derived entoty filters the records of another entity (called master entity) using some criteria. In the following figure there is an example. The derived entity "Italian Artist" groups only the records of the entity "Artist" which as "Italian" as nationality. The derived entity has all the attributes of the master entity and also shares all the relationships outgoing from the master entity. You can also add attributes and relationships to the derived entity, but all these information belong only to the derived entity and not to the master entity.
  • Attributes. A derived attributes is an abstract concepts that represent a specific entity property. In this case the value of the property can be inferred by other attributes (either of the same entity or other entities) or by relationships. There are different ways to derive an attribute:
    • Constant attribute. It defines an entity property that as a constant value.
    • Imported attribute. It defines an entity property copying the value from another attribute of a different entity. You can copy values only from attributes belonging to entities connected to the interested one with a 1-N relationship. In particular the 1 cardinality has to belong to the entity on which you are specifying the derived attribute. The derivation wizard shows you two different options for imported attributes:
      • Simple Imported Attribute. It is an attribute which is merely copied from one entity connected to the one containing the attribute. For example, the Category attribute of the entity Product shown in the first figure of this article. The word "simple" means that the Database Synchronizer will not present this attribute to export on the database, because there is no more the need to have a view containing this information. The attribute is retrieved automatically through Hibernate every time a unit request it.
      • Complex Imported Attribute. It is the standard imported attribute which corresponds to a database view. All imported attributes that have condition associated (name of the product having the max price) are included in this category.
    • Calculated attribute. It defines an entity property through a calculation using other attributes or through an aggregation of other properties. The available aggregation operators are: sum, avg, min, max.
    • Manually mapped attribute. This type of attribute is used when the other types does not fit you purposes. It happens that the Derivation Wizard is not able to construct the SQL view exactly as you want. In these cases you can manually write the SQL code and create the view on the database and then map that view on an attribute. This is the type of attribute you have to choose in order to perform this operation.

Here is an example of data model using derived attributes.

  • Relationships. A derived relationship is an abstract concept representing an aggregation between entities which can be definied starting from an existing relationship or specifying conditions on the envolved entities. So there are three ways to derive relationships:
    • By restriction. In this case the derived relationship is definied specifying some conditions of an existing relationship. The connected records are a subset of the master relationship.
    • By concatenation. The derived relationship is a composition of existing relationships.
    • New relationship. It is defined specifying conditions on the entities the new relationship is going to connect.

All derived elements can be used in the Web model as any other element of the data model.

The Simple Imported Attribute

WebRatio improved the management of imported attributes. Now the derivation wizard shows you two different options for imported attributes:

  • Simple Imported Attribute
  • Complex Imported Attribute

The Simple Imported Attribute is an attribute which is merely copied from one entity connected to the one containing the attribute. For example, the Category attribute of the entity Product shown in the first figure of this article. The word "simple" means that the Database Synchronizer will not present this attribute to export on the database, because there is no more the need to have a view containing this information. The attribute is retrieved automatically through Hibernate every time a unit request it.

The Complex Imported Attribute is the standard imported attribute which corresponds to a database view. All imported attributes that has condition associated (name of the product having the max price) are included in this category.

Counting the records of an Entity

WebRatio allows you to create derivated attributes that count the number of records stored in an Entity.

Suppose that you want to count the number of Users. The first thing that you will try to do is to add the attribute on the entity User. This is not correct. In fact, the number of Users it is not a property of each user, so the attribute must not be placed here. WebRatio prevent you to create such attribute avoiding to show you the commands needed in order to construct the derivation expression.

The "number of Users" attribute must be placed on another entity which is connected to the "User" entity through a relationship. For example adding the attribute to the "Group" entity it's possible to count all the users belonging to a specific group. In the figure you can see the derivation expression of this attribute, which is a Calculated attribute

When you synchronize the database, you fill find a DDL code like the following


create view "public"."group_users_number_view" as

select AL1."oid" as "oid", count(distinct AL2."oid") as "der_attr"

from  "public"."group" AL1 

               left outer join "public"."user" AL2 

on AL1."oid"=AL2."group_oid"

group by AL1."oid";



It's not possible to create the derivated attribute corresponding to the count(*) from User query since this query does not involve any relationship. In order to have such attribute in the Data Model you have to work bottom-up. This means that you have to manually add the view making this count. Then, you have to add the attribute to the entity of your interest (e.g. a "Statistics" entity and manually map the attribute using the Mapping tab in its Properties View.

 
 

This article is not up to date and it has been substitute with new up to date content.

Please look the new up to date content.