-- File: PhoneList.mesa
-- Contents: Program to print phone list from personal database in Squirrel segment.
-- Directions: Run it, on database containing phone relation, Persons, etc. (see Rick for schema)
-- Output: PhoneList.tioga file with tabs between person, work phone, home phone
-- For printing nicely: Set format of file to Table3, set character looks of file to s (small).
-- Set the "all" BOOL below to TRUE to print all people, FALSE to do only those that
-- are a member of the Organization "Phone List".
-- Last edited by
  -- Cattell: July 19, 1983 9:16 am

DIRECTORY DB, FS, IO, Rope;


PhoneList: PROGRAM IMPORTS DB, FS, IO, Rope =

BEGIN OPEN DB;

all: BOOL← FALSE; -- whether to print all or only those in Phone List
truncate: BOOL← TRUE; -- whether to truncate names to last name and first initial

member: Relation← DeclareRelation["member", $Squirrel];
memberOf: Attribute← DeclareAttribute[member, "of"];
memberIs: Attribute← DeclareAttribute[member, "is"];
phone: Relation← DeclareRelation["phone", $Squirrel];
phoneOf: Attribute← DeclareAttribute[phone, "of"];
phoneIs: Attribute← DeclareAttribute[phone, "is"];
phoneAt: Attribute← DeclareAttribute[phone, "at"];
Person: Domain← DeclareDomain["Person", $Squirrel];
Organization: Domain← DeclareDomain["Organization", $Squirrel];
myList: Entity← DeclareEntity[Organization, "phone list"];
memberProp: Attribute= memberIs;
him: --Person-- Entity;
phone1, phone2: ROPE;

TruncateName: PROC[old: ROPE] RETURNS[new: ROPE] =
  -- Truncate all but first letter of first name in old
  BEGIN endOfLast: INT← old.Find[","];
  IF endOfLast=-1 THEN RETURN[old]
  ELSE RETURN[old.Substr[0, endOfLast+3]];
  END;

Print: PROC =
  BEGIN
  people: EntitySet← DomainSubset[Person, "", "\177"];
  outFile: IO.STREAM← FS.StreamOpen["PhoneList.tioga", $create];
  outFile.PutF["Person	Work#	Home#\n"];
  UNTIL Null[him← NextEntity[people]] DO
    phones: LIST OF Relship; name: ROPE;
    interest: RelshipSet← RelationSubset[member, LIST[ [memberIs, him], [memberOf, myList] ]];
    temp: Relship← NextRelship[interest];
    ReleaseRelshipSet[interest];
    IF NOT all AND temp=NIL THEN LOOP;
    phones← RelshipSetToList[RelationSubset[phone, LIST[[phoneOf, him]]]];
    phone1← phone2← NIL;
    FOR phonesT: LIST OF Relship← phones, phonesT.rest UNTIL phonesT=NIL DO
      place: ROPE← GetFS[phonesT.first, phoneAt];
      IF Rope.Equal[place, "work"] THEN
        phone1← GetFS[phonesT.first, phoneIs]
      ELSE IF place.Length[]=0 OR Rope.Equal[place, "home"] THEN
        phone2← GetFS[phonesT.first, phoneIs];
      ENDLOOP;
    IF phone1=NIL AND phone2=NIL THEN LOOP;
    name← GetName[him]; 
    IF truncate THEN name← TruncateName[name];
    outFile.PutF["%g	%g	%g\n", IO.rope[name], IO.rope[phone1], IO.rope[phone2]];
    ENDLOOP;
  outFile.Close[];
  END;

Print[];

END.