-- File: VLTest2Impl.mesa
-- Contents: Simple test program (2nd of 2) for DB.mesa
-- Created by: Rick Cattell, 18-Dec-81 13:59:57
-- Last Edited by: Cattell, January 16, 1983 2:37 pm

DIRECTORY
  DB, IO, Rope;

VLTest2Impl: PROGRAM
  IMPORTS DB, IO, Rope =

BEGIN OPEN IO, DB;

INT: TYPE = LONG INTEGER;

tty: IO.Handle← CreateViewerStreams["VLTest2Impl.log"].out;

mySeg: DB.Segment = $Test;
Person, Frog, Thing: Domain;
Friend: Relation;
  friendOf, friendIs: Attribute;
Phone: Relation;
  phoneOf, phoneIs: Attribute;
ageProp, heightProp: Attribute;
wsh, rick: --Person-- Entity;
george: --Frog-- Entity;
rock, pebble: --Thing-- Entity;

Initialize: PROC =
  BEGIN
  tty.Put[rope["Defining data dictionary..."], char[CR]];
  -- Declare domains and make Persons and Frogs be subtypes of Things:
  Person← DeclareDomain["Person", mySeg];
  Thing← DeclareDomain["Thing", mySeg];
  Frog← DeclareDomain["Frog", mySeg];
  --DeclareSubType[of: Thing, is: Person];
  --DeclareSubType[of: Thing, is: Frog];
  -- Declare phone age and height property of Things
  ageProp← DeclareProperty["Age", Thing, IntType, mySeg];
  heightProp← DeclareProperty["Height", Thing, StringType, mySeg];
  -- Declare Friend relation between Persons
  Friend← DeclareRelation["Friend", mySeg];
   friendOf← DeclareAttribute[Friend, "of", Person];
   friendIs← DeclareAttribute[Friend, "is", Person,,, FALSE];
  -- Declare Phone relation between Persons and integers
  Phone← DeclareRelation["Phone", mySeg];
   phoneOf← DeclareAttribute[Phone, "of", Person];
   phoneIs← DeclareAttribute[Phone, "is", IntType];
  []← DeclareIndex[Phone, LIST[phoneIs], NewOrOld];
  END;

InsertMoreData: PROCEDURE =
  BEGIN t: Relship; flag: BOOL← FALSE;
  tty.Put[rope["Creating data..."], char[CR]];
  wsh← CreateEntity[Person];
  rick← FetchEntity[Person, "Rick Cattell"];
  rock← CreateEntity[Thing, "Harry the rock"];
  pebble← CreateEntity[Thing, "Fred the pebble"];
  george← CreateEntity[Frog, "George the frog"];
  []← CreateEntity[Frog, "Harry the frog"];
  SetName[wsh, "Willie-Sue H"];
  SetPW[wsh, heightProp, S2V["medium"]];
  SetPW[rick, heightProp, S2V["medium"]];
  SetPW[rock, heightProp, S2V["big"]];
  SetPW[pebble, heightProp, S2V["little"]];
  SetPW[george, heightProp, S2V["little"]];
  SetPW[george, heightProp, I2V[10000]
    ! Error => IF code=MismatchedAttributeValueType THEN {flag← TRUE; CONTINUE}];
  IF NOT flag THEN ERROR;
  t← CreateRelship[Friend]; SetF[t, friendOf, rick]; SetF[t, friendIs, wsh];
  flag← FALSE;
  SetF[t, friendOf, george
    ! Error => IF code=MismatchedAttributeValueType THEN {flag← TRUE; CONTINUE}];
  IF NOT flag THEN ERROR;
  END;

DestroySomeData: PROCEDURE =
  BEGIN flag: BOOL← FALSE;
  tty.Put[char[CR], rope["Deleting some data: Willie-Sue and the frogs..."], char[CR]];
  DestroyEntity[FetchEntity[Person, "Willie-Sue"]];
  DestroyDomain[Frog];
  END;

PrintEveryone: PROCEDURE =
  BEGIN
  thing: -- Thing -- Entity;
  es: EntitySet;
  r: ROPE;
  tty.PutF["\nThings:\n\n"];
  tty.PutF["Type	Name		Height\n"];
  es← DomainSubset[Thing];
  WHILE (thing← NextEntity[es])#NIL DO
    tty.Put[rope[GetName[DomainOf[thing]]], char[TAB]];
    tty.Put[rope[GetName[thing]], char[TAB]];
    r← V2S[GetP[thing, heightProp]];
    tty.Put[rope[IF r.Length[]=0 THEN "[unknown]" ELSE r], char[CR]];
    ENDLOOP;
  ReleaseEntitySet[es];
  tty.PutF["\nPeople Names:\n\n"];
  es← DomainSubset[Person];
  WHILE (thing← NextEntity[es])#NIL DO
    tty.Put[rope[GetName[thing]], char[CR]];
    ENDLOOP;
  ReleaseEntitySet[es];
  END;

PrintFriends: PROCEDURE =
  BEGIN
  friendRS: --Friend-- Relship;
  friendName: --Person-- ROPE; 
  rs: RelshipSet;
  harry: Entity← FetchEntity[Person, "Harry Q. Bovik"];
  tty.Put[char[CR], rope["Harry's friends:"], char[CR]];
  rs← RelationSubset[Friend, LIST[[friendIs, harry]]];
  WHILE (friendRS← NextRelship[rs])#NIL DO
    friendName← GetFS[friendRS, friendOf];
    tty.Put[rope[friendName], rope["	"], char[CR]];
    ENDLOOP;
  ReleaseRelshipSet[rs];
  END;



tty.Put[rope["Creating database..."], char[CR]];
DB.Initialize[];
DB.DeclareSegment["[Local]Test.segment", mySeg];
DB.OpenTransaction[mySeg];
Initialize[];
PrintFriends[];
InsertMoreData[];
DestroySomeData[];
CloseTransaction[TransactionOf[mySeg]];
tty.Close[];


END.