Search code examples
object-role-modeling

ORM: Specifying a value equality constraint against two referenced entities


I am trying to model a concept using object-role-modelling, and I can't find the necessary constraint type. I'm wondering if it exists.

Here are three facts:

  • Commodity must be of one CommodityCategory
  • EntityDescriptor must be of of CommodityCategory
  • EntityDescriptor may be for one Commodity

This is straightforward to model:

alt text

But here's the constraint:

  • If an EntityDescriptor is for a Commodity, the CommodityCategory referenced by the Commodity must equal the CommodityCategory referenced by the EntityDescriptor

For example, suppose we had these commodities.

*--------------------*------------*
| CommodityCategory  | Commodity  |
*--------------------*------------*
| Fuel               | Gas        |
| Fuel               | Petrol     |
| Food               | Sugar      |
*--------------------*------------*

These are legal

*------------------*-------------------*-----------*
| EntityDescriptor | CommodityCategory | Commodity |
*------------------*-------------------*-----------*
| 1                | Fuel              |           |
| 2                | Fuel              | Gas       |
| 3                | Food              |           |
| 4                | Food              | Sugar     |
*------------------*-------------------*-----------*

But this is illegal

*------------------*-------------------*-----------*
| EntityDescriptor | CommodityCategory | Commodity |
*------------------*-------------------*-----------*
| 5                | Food              | Petrol    |
*------------------*-------------------*-----------*

I looked at the Equality constraint, but that is about the existence of the relationship, not the actual values in the relationship.

Is there something I can use to model this constraint?


Solution

  • Written in CQL see the ActiveFacts home page, you need a subset constraint like this:

    some EntityDescriptor references some Commodity
        only if that EntityDescriptor is for some CommodityCategory and that Commodity is of that CommodityCategory;
    

    Note that this becomes more fluent if you include a reading in each direction.

    In NORMA, you need a subset constraint that has two role pairs:

    The subset pair is the two roles of "EntityDescriptor references Commodity". The superset pair is the role of EntityDescriptor in "EntityDescriptor is for CommodityCategory, and the role of Commodity in "Commodity is of CommodityCategory".

    Note that the first role of each pair is played by the same type (EntityDescriptor), and likewise with the second role of each pair (Commodity). It's also possible to use compatible subtype/supertypes, but the types must be compatible in this way.

    An equality constraint is like two subset constraints, one running in each direction. It always requires that at least one reference exists whenever an EntityDescriptor is for some CommodityCategory and that Commodity is of that CommodityCategory, as well as vice versa.