Domains

Domain interface

Domains in Crema are located in the ch.idsia.crema.model package. They are all instances of the Domain interface. This simple interface declares basic methods to query the domain about variables and their cardinality.

Domain domain = ...;
domain.getSizes();
domain.getVariables();

Note

Returned arrays should never be modified!

SimpleDomain

The simplest implementation of the Domain interface is the SimpleDomain. This class encapsulates two integer arrays. One with the variable labels and one with their cardinality.

Domain domain = new SimpleDomain(
		new int[]{1, 4, 6}, // variables 1, 4, 6
		new int[]{3, 2, 3}  // the corresponding cardinalities
);

assertTrue(domain.contains(6));

Warning

When creating a SimpleDomain the list of variables must be sorted! Crema will not automatically sort them, but for some operations will assume they are.

DomainBuilder

While creating a SimpleDomain by passing the arrays of variables and their sizes is possible and valid, a slightly more friendly method is available using the DomainBuilder. Laveraging the ellipses of Java the DomainBuilder class avoids the explicit creation of the arrays as shown in the following example.

Domain dom = DomainBuilder.var(1, 4, 6).size(3, 2, 3);

Strides

A more sophisticated and more frequently used implementation of the Domain interface is the Strides class. In addition to the arrays of variables and their cardinality, this class caches the cumulative sizes of the variables in the provided order. The access to this additional array is seldomly required by the end-user. They are mostly required to index parts of a probability table.

The Strides class offers a much richer set of functionalities both related to the domain itself and the aforementioned indexing of probability tables.

Creating Strides

We we first look at how Strides instances can be created conveniently.

Note

The variable’s cardinalities are accumlated starting from the variable at index 0.

Strides domain = new Strides(
		new int[]{1, 4, 6}, // variables 1, 4, 6
		new int[]{3, 2, 3}  // the corresponding cardinalities
);

Again, just as with the SimpleDomain, creating the object specifying the arrays is valid, but not the most readable solution. The following example shows an alternative way of creation where variables are added along with their cardinality.

Strides other = Strides.as(1, 3).and(4, 2).and(6, 3);

Alternative ways to create strides are based on operations on them. Generally Domains are considered unmutable objects and any alteration will result in a new instance.

// remove variable 4 and 6
Strides smaller = domain.remove(4, 6);

A number of common set operations are available:

  • union

  • intersect

  • remove

Working with Strides