uid |
---|
Constraints |
Constraints as well as Capabilities are used to filter the possible matches of Resources and Activities. In Contrast to capabilities which are sets of provided and required abilities, constraints are boolean conditions which arise from the system's context. To check if the context's constraints are met the Check() method of the constraint is called. The IConstraintContext contains all the information which is needed to perform this check.
The ExpressionConstraint provides an example for the usage of constraints.
It can compare a given value with the one returned by a typed lambda expression and supports standard Equals
comparison as well was LessOrEqual
and GreaterOrEqual
.
The DummyContext
provides the value to check against.
public class DummyContext : IConstraintContext
{
public int Foo { get; set; }
}
public void ExpressionExamples()
{
// Define constraints
var equals = ExpressionConstraint.Equals<DummyContext>(dummy => dummy.Foo, 42);
var lessOrEqual = ExpressionConstraint.LessOrEqual<DummyContext>(dummy => dummy.Foo, 42);
var greaterOrEqual = ExpressionConstraint.GreaterOrEqual<DummyContext>(dummy => dummy.Foo, 42);
// Check constraints
IConstraintContext context = new DummyContext { Foo = 42 };
var r1 = equals.Check(context); // True
var r1 = lessOrEqual.Check(context); // True
var r1 = greaterOrEqual.Check(context); // True
context = new DummyContext { Foo = 100 };
var r2 = equals.Check(context); // False
var r2 = lessOrEqual.Check(context); // False
var r2 = greaterOrEqual.Check(context); // True
context = new DummyContext { Foo = 0 };
var r3 = equals.Check(context); // False
var r3 = lessOrEqual.Check(context); // True
var r3 = greaterOrEqual.Check(context); // False
}
To define a context for the constraint it is enough to implement the IConstraintContext interface. Since Processes implement the IConstraintContext interface, every process can be used for a check like in the following example:
var identity = new ProductIdentity("123456", 0);
var constraint = ExpressionConstraint.Equals<IProcess>(p => ((IProductRecipe) p.Recipe).Product.Identity, identity);