-- File: NutOpsImpl.mesa
-- Last edited by
-- Maxwell on: September 7, 1982 2:52 pm
-- Willie-Sue on: February 22, 1983 4:24 pm
-- Cattell on: June 6, 1983 4:17 pm
-- Donahue, June 1, 1983 3:40 pm


DIRECTORY
Atom USING [MakeAtom], CommandTool, DB, DBFileAlpine,
MessageWindow, NutOps, NutViewer, Rope, Runtime, ViewerClasses;

NutOpsImpl: CEDAR MONITOR
IMPORTS CommandTool, DB, DBFileAlpine, MessageWindow, NutViewer, Rope, Runtime, Atom
EXPORTS NutOps

= BEGIN OPEN DB;

Viewer: TYPE = ViewerClasses.Viewer;
ROPE: TYPE = Rope.ROPE;

-- generally useful DB routines

IsSystemDomain: PUBLIC PROC[ d: Domain ] RETURNS[ BOOLEAN ] = {
RETURN[ SELECT TRUE FROM
DB.Eq[d, DomainDomain] => TRUE,
DB.Eq[d, RelationDomain] => TRUE,
DB.Eq[d, DataTypeDomain] => TRUE,
DB.Eq[d, AttributeDomain] => TRUE,
DB.Eq[d, IndexDomain] => TRUE,
DB.Eq[d, IndexFactorDomain] => TRUE,
ENDCASE => FALSE ] };

IsSystemRelation: PUBLIC PROC[r: Relation] RETURNS[BOOLEAN] = {
RETURN[ SELECT TRUE FROM
DB.Eq[r, dSubType] => TRUE,
DB.Eq[r, aRelation] => TRUE,
DB.Eq[r, aType] => TRUE,
DB.Eq[r, aUniqueness] => TRUE,
DB.Eq[r, aLength] => TRUE,
DB.Eq[r, aLink] => TRUE,
DB.Eq[r, ifIndex] => TRUE,
DB.Eq[r, ifAttribute] => TRUE,
ENDCASE => FALSE ] };

AttributesOf: PUBLIC PROC[r: Relation] RETURNS[AttributeList] =
{IF Eq[r, dSubType] THEN RETURN[LIST[dSubTypeIs, dSubTypeOf]];
IF Eq[r, aRelation] THEN RETURN[LIST[aRelationOf, aRelationIs]];
IF Eq[r, aType] THEN RETURN[LIST[aTypeOf, aTypeIs]];
IF Eq[r, aUniqueness] THEN RETURN[LIST[aUniquenessOf, aUniquenessIs]];
IF Eq[r, aLength] THEN RETURN[LIST[aLengthOf, aLengthIs]];
IF Eq[r, aLink] THEN RETURN[LIST[aLinkOf, aLinkIs]];
IF Eq[r, ifIndex] THEN RETURN[LIST[ifIndexOf, ifIndexIs]];
IF Eq[r, ifAttribute] THEN RETURN[LIST[ifAttributeOf, ifAttributeIs]];
RETURN[VL2EL[GetPList[r, aRelationOf]]]};

FirstAttributeOf: PUBLIC PROC[r: Relation] RETURNS[Attribute] =
BEGIN es: AttributeList← VL2EL[GetPList[r, aRelationOf]];
IF es=NIL THEN RETURN[NIL] ELSE RETURN[es.first]
END;

EntityValued: PUBLIC PROC [a: Attribute] RETURNS[BOOL] =
BEGIN type: Entity;
IF a=NIL OR Null[a] THEN RETURN[FALSE];
type← V2E[GetP[a, aTypeIs]];
SELECT type FROM
IntType, StringType, BoolType, RecordType, TimeType => RETURN[FALSE];
ENDCASE => RETURN[TRUE];
END;

GetUniquenessString: PUBLIC PROC[a: Attribute] RETURNS[ROPE] =
BEGIN u: Uniqueness;
IF Eq[a, dSubTypeIs] THEN RETURN["NonKey"];
IF Eq[a, dSubTypeOf] THEN RETURN["NonKey"];
IF Eq[a, aRelationOf] THEN RETURN["Key"];
IF Eq[a, aRelationIs] THEN RETURN["NonKey"];
IF Eq[a, aTypeOf] THEN RETURN["Key"];
IF Eq[a, aTypeIs] THEN RETURN["NonKey"];
IF Eq[a, aUniquenessOf] THEN RETURN["Key"];
IF Eq[a, aUniquenessIs] THEN RETURN["NonKey"];
IF Eq[a, aLengthOf] THEN RETURN["Key"];
IF Eq[a, aLengthIs] THEN RETURN["NonKey"];
IF Eq[a, aLinkOf] THEN RETURN["Key"];
IF Eq[a, aLinkIs] THEN RETURN["NonKey"];
IF Eq[a, ifIndexOf] THEN RETURN["Key"];
IF Eq[a, ifIndexIs] THEN RETURN["NonKey"];
IF Eq[a, ifAttributeOf] THEN RETURN["Key"];
IF Eq[a, ifAttributeIs] THEN RETURN["NonKey"];
u← V2U[GetP[a, aUniquenessIs]];
SELECT u FROM
Key => RETURN["Key"];
KeyPart => RETURN["KeyPart"];
None => RETURN["NonKey"];
OptionalKey => RETURN["OptionalKey"];
ENDCASE => RETURN["???"];
END;

GetTuples: PUBLIC PROC[e: Entity, a: Attribute] RETURNS [tl: LIST OF Relship] =
-- returns all the tuples that reference e in attribute a
{RETURN[RelshipSetToList[RelationSubset[GetRelation[a], LIST[[a, e]] ]]]};

GetRelation: PUBLIC PROC[a: Attribute] RETURNS[r: Relation] =
-- finds a's relation
{RETURN[V2E[GetP[a, aRelationIs]]]};

GetRefAttributes: PUBLIC PROC[d: Domain] RETURNS[al: LIST OF Attribute] =
-- returns all the attributes that can reference an entity from domain d
BEGIN
starRelation: Relation← DeclareRelation["*", SegmentOf[d]];
starOf: Attribute;
starIs: Attribute;
starList: LIST OF Attribute;
starOf ← DeclareAttribute[r: starRelation, name: "of", type: AnyDomainType, version: OldOnly
        ! DB.Error => CHECKED { starOf ← NIL; CONTINUE } ];
starIs ← DeclareAttribute[r: starRelation, name: "is", type: AnyDomainType, version: OldOnly
        ! DB.Error => CHECKED { starIs ← NIL; CONTINUE } ];
starList ← SELECT TRUE FROM
   starOf = NIL AND starIs = NIL => NIL,
   starIs = NIL => LIST[starOf],
   starOf = NIL => LIST[starIs],
   ENDCASE => LIST[starOf, starIs];
al← GetDomainRefAttributes[d];
al← AppendAttributes[al, starList];
END;

RemoveAttribute: PUBLIC PROC[a: Attribute, al: AttributeList] RETURNS[AttributeList] =
-- destructively removes a from al
BEGIN alLast: AttributeList← NIL;
FOR alT: AttributeList← al, alT.rest UNTIL alT=NIL DO
IF Eq[alT.first, a] THEN
IF alLast=NIL THEN RETURN[alT.rest] -- was first element
ELSE {alLast.rest← alT.rest; RETURN[al]}; -- was another element
alLast← alT;
ENDLOOP;
RETURN[al] -- not found
END;

AppendAttributes: PUBLIC PROC [al1, al2: AttributeList] RETURNS [al: AttributeList] =
BEGIN IF al1=NIL THEN RETURN [al2];
al← al1;
UNTIL al1.rest=NIL DO
al1← al1.rest ENDLOOP;
al1.rest← al2;
RETURN[al]
END;

RSetSize: PUBLIC PROC[rs: RelshipSet] RETURNS[INT] =
BEGIN
FOR size: INT IN [0..200) DO
IF Null[NextRelship[rs]] THEN
{ReleaseRelshipSet[rs]; RETURN[size]};
ENDLOOP;
ReleaseRelshipSet[rs];
RETURN[9999]; -- just tell 'im there are lots for now
END;

-- ***********************************************
AtomFromSegment: PUBLIC PROC[ segR: ROPE ] RETURNS[ ATOM ] = {
end: INT;
 begin: INT← Rope.Find[segR, ">"];
IF begin < 0 THEN begin← Rope.Find[segR, "]" ];
 end← Rope.Find[segR, ".", MAX[begin, 0] ];
IF end < 0 THEN end← segR.Length[];
 begin ← MAX[ begin+1, 0 ];
RETURN[Atom.MakeAtom[Rope.Substr[ segR, begin, (end-1)-begin+1]]] };

IsLocalName: PUBLIC PROC[ segR: ROPE ] RETURNS[ BOOL ] = {
IF Rope.Equal[Rope.Substr[base: segR, start: 0, len: 7], "[Local]"] THEN RETURN[TRUE];
RETURN[FALSE] };

SetUpSegment: PUBLIC PROC[
segmentFile: ROPE, seg: DB.Segment, readOnly: BOOLTRUE, number: NAT← 0] = TRUSTED
-- A "foolproof" way to open a segment & transaction, regardless of whether
-- the segment is already declared, doesn't exist on the disk, or already has a transaction open.
-- Insures that AlpineUserImpls is loaded if segment is remote.
{ --ENABLE InterimAlpineDirectory.Error =>
--{NutViewer.Error[NIL, "Alpine call failed!"]; CONTINUE};
success: BOOLTRUE;
alpineNeeded: BOOL← Rope.Find[segmentFile, "<"]#-1;
IF alpineNeeded AND ~Runtime.IsBound[DBFileAlpine.CreateTransaction] THEN {
err: ROPE;
NutViewer.Message[NIL, "Loading and starting AlpineUserImpls... "];
err← CommandTool.DoCommand["Run", "AlpineUserImpls"].err;
IF err.Length[]#0 THEN NutViewer.Message[NIL, err]};
DB.DeclareSegment[segmentFile, seg, number, readOnly ! DB.Error => TRUSTED
{ IF code=TransactionAlreadyOpen THEN GOTO AlreadyDone }];
DB.OpenTransaction[seg ! DB.Error => TRUSTED
{IF code=FileNotFound THEN success← FALSE; CONTINUE}];
IF NOT success THEN {
MessageWindow.Append[Rope.Cat[segmentFile, " does not exist, so creating it..."]];
DB.CloseTransaction[DB.TransactionOf[seg]]; -- DB has left the transaction open
DB.DeclareSegment[segmentFile, seg, number, readOnly, NewOnly];
DB.OpenTransaction[seg];
DB.MarkTransaction[DB.TransactionOf[seg]] }; -- make sure segment initializated if abort
MessageWindow.Append["", TRUE]
EXITS
AlreadyDone => NULL };

TryRestart: PUBLIC PROC[trans: DB.Transaction] = {
-- Attempts to re-open an aborted transaction if user confirms.
segment: DB.Segment;
MessageWindow.Blink[];
MessageWindow.Clear[];
MessageWindow.Append["Transaction aborted! Please confirm, to try re-opening"];
IF NOT MessageWindow.Confirm[] THEN
{MessageWindow.Append["... not re-opened."]; RETURN};
MessageWindow.Append["... "];
segment← GetSegment[trans];
DB.AbortTransaction[trans];
DB.OpenTransaction[segment];
MessageWindow.Append["done."];
};

GetSegment
: PROC [t: DB.Transaction] RETURNS [seg: DB.Segment] = {
-- Finds the first database segment associated with segment t.
-- Note we assume all applications have only one segment per transaction right now.
segs: LIST OF DB.Segment← DB.GetSegments[];
FOR segs← segs, segs.rest UNTIL segs=NIL DO
IF DB.TransactionOf[segs.first]=t THEN RETURN[segs.first] ENDLOOP;
RETURN
[NIL]
};

END.

Changes since October 82:

Cattell October 13, 1982 9:28 am: Save and Reset buttons should Fork.

Cattell December 1, 1982 4:29 pm: Should call GetRope instead of GetToken for 2nd DBShow argument.