The KnownSpace Datamanager
"Halfway To Anywhere"

A Constraint Example

Constraints specify subsets of entities. You can build complex constraints from simpler ones using the equivalent of the three first-order logic connectives: AndConstraint, OrConstraint, and NotConstraint. The "literal", Constraint.NONE, is of type Constraint and it means "no constraint at all", so all entities satisfy it. Other constraints specify predicates on an entity's name, value, or its links to other entities.

For example, to build a constraint specifying the set of all entities that are
either (1) attributes of an entity whose value is "Hello",
or (2) that have a value of Integer(5) and at least one attribute with the name "Contents", but no attributes with the string "URL" in their name, you might write the following:

Constraint searchConstraint =
   new OrConstraint(
      //clause (1): we want entities that are attributes of an entity...
      new BaseExistsConstraint(
         //whose value is "Hello"
         new ValueEqualsConstraint(new StringEntityValue("Hello"))),

      //clause (2)
      new AndConstraint(
         new ValueEqualsConstraint(new IntegerEntityValue(5)),
         new AndConstraint(
            //has at least one attribute with the name "Contents"
            new AttributeExistsConstraint(
               new NameEqualsConstraint("Contents")),
            //has no attributes with the string "URL" in their name
            new NotConstraint(
               new AttributeExistsConstraint(
                 new NameContainsConstraint("URL"))))));

However, it's probably better coding style to create each subconstraint separately then combine them into one:

Constraint hasHelloBaseConstraint =
      new BaseExistsConstraint(
         new ValueEqualsConstraint(new StringEntityValue("Hello")));

Constraint hasIntegerValueConstraint =
      new ValueEqualsConstraint(new IntegerEntityValue(5));

Constraint hasContentsAttributeConstraint =
      new AttributeExistsConstraint(new NameEqualsConstraint("Contents"));

Constraint hasURLAttributeConstraint =
      new AttributeExistsConstraint(new NameContainsConstraint("URL"));

Constraint searchConstraint =
   new OrConstraint(
      new AndConstraint(
         new AndConstraint(
            new NotConstraint(hasURLAttributeConstraint))));