As we saw in lesson 1a, a fact's fields identify it. But sometimes fields are not enough. In such cases we use the "unique" keyword.

Add the following fact to the model from lesson 1a.
fact Machine {
key:
    unique;
}

Click "Transform All Templates" and compile. A machine is unique even though it has no identifying fields. Let's test this.

Add a unit test that verifies that new machines are unique.
[TestMethod]
public void MachinesAreUnique()
{
    Machine firstMachine = _community.AddFact(new Machine());
    Machine secondMachine = _community.AddFact(new Machine());
    Assert.AreNotSame(firstMachine, secondMachine);
}

A fact is identified by its key fields. If Machine had no key fields, there could be only one Machine in the whole system. To make the fact truly unique, use the "unique" keyword.

Be careful not to overuse "unique". Carefully consider whether the fact already has enough key fields to distinguish it from other facts of the same type. If you use "unique" in the wrong place, you could end up with duplicate facts that should not truly be distinct.

Next: Lesson 1c: Predecessors

Last edited May 15, 2011 at 4:21 AM by MichaelLPerry1971, version 8

Comments

No comments yet.