Product SiteDocumentation Site

5.3.13.28. Examples

Example 5.172. Relation class - examples

/* Use a relation to express parent-child relationships */
family = .relation~new
family["Henry"] = "Peter"    /* Peter is Henry's child   */
family["Peter"] = "Bridget"  /* Bridget is Peter's child */
family["Henry"] = "Jane"     /* Jane is Henry's child    */

/* Show all children of Henry recorded in the family relation */
henrys_kids = family~allAt("Henry")
Say "Here are all the listed children of Henry:"
Do kid Over henrys_kids
  Say " "kid
End

/* Show all parents of Bridget recorded in the family relation */
bridgets_parents = family~allIndex("Bridget")
Say "Here are all the listed parents of Bridget:"
Do parent Over bridgets_parents
  Say " "parent
End

/* Display all the grandparent relationships we know about. */
checked_for_grandkids = .set~new         /* Records those we have checked      */
Do grandparent Over family               /* Iterate for each index in family   */
  If checked_for_grandkids~hasIndex(grandparent)
    Then Iterate                         /* Already checked this one           */
  kids = family~allat(grandparent)       /* Current grandparent's children     */
  Do kid Over kids                       /* Iterate for each item in kids      */
    grandkids = family~allAt(kid)        /* Current kid's children             */
    Do grandkid Over grandkids           /* Iterate for each item in grandkids */
      Say grandparent "has a grandchild named" grandkid"."
    End
  End
  checked_for_grandkids~put(grandparent) /* Add to already-checked set         */
End