Building Queries

Using Facts in Queries

Facts directly asserted in the KB can be used to build a query. An example of this is the ‘Who are spouses?’ query in family.flr. It is defined as:

@!{'Who are spouses?'(?Spouse1, ?Spouse2)} !-
  ?Spouse1 [spouse -> ?Spouse2].

The property spouse asserted for each individual is used in obtaining answers for the query. For example, 3 properties including spouse are asserted for individual Darryn as follow:

Darryn : Man [
  child -> {Greg, Herb},
  spouse -> Dana,
  birthDate -> "1969-11-27"^^\date].

Therefore the query results for ‘Who are spouses?’ will include the answer pair Darryn and Dana.

Using Rules In Queries

Besides facts, rules which define higher order facts are also often used in queries. An example of this is the ‘Who is a great-grandparent?’ query. In family.flr, this query is defined as:

@!{'Who is a great-grandparent?'(?GreatGrandparent)} !-
  ?_GreatGrandchild [greatGrandparent -> ?GreatGrandparent].

It makes use of the GreatGrandparentRule, which is defined as:

@!{GreatGrandparentRule}
?Person [greatGrandparent -> ?GreatGrandparent] :-
  ?Person [grandparent -> ?Parent],
  ?Parent [parent -> ?GreatGrandparent].

The ‘Who is a great-grandparent?’ query will return a list of individuals who satisfy the great grand parent relationship according to the ‘GreatGrandParentRule’.