Storing Tabular Data (with dbd) -
12-12-2006
, 05:40 PM
Following is a preliminary dbd example that creates two "tables", one
for persons john and mary; and another for animals fido, miffy and
mickey using the setInst+ function which is similar to SQL's INSERT. In
addition, the db stores which animals are pets of which persons and
their feelings for various things. The last query gets the things that
mary's relationship to, is similar to john's feeling for mary.
Comments, questions and comparisons with alternative solutions
appreciated.
(new 'person)
(new 'animal)
(new 'gender)
(new 'age)
(new 'ss#)
(; Set default attributes for persons)
(set person attribute name)
(set person attribute gender)
(set person attribute age)
(set person attribute ss#)
(; Add john and mary)
(setInst+ person 'john 'male '25 '123-45-6789)
(setInst+ person 'mary 'female '5 '333-44-5555)
(; Set default attributes for animals)
(set animal attribute name)
(set animal attribute gender)
(set animal attribute age)
(; Add fido, miffy and micky)
(setInst+ animal 'fido 'male '3)
(setInst+ animal 'miffy 'female '7)
(setInst+ animal 'mickey 'male '1)
(; Assign pets)
(new 'pet)
(set john pet fido)
(set mary pet miffy)
(set mary pet mickey)
(; Create feelings)
(new 'like 'feeling)
(new 'love 'feeling)
(new 'similarTo)
(set like similarTo love)
(set love similarTo like)
(; Create child/like/love relationships)
(set john child mary)
(set john love mary)
(set mary like fido)
(set mary like miffy)
(; Get things that are male)
(; Gets john, fido and mickey)
(get * gender male)
(; Get things that are female)
(; Gets mary and miffy)
(get * gender female)
(; Get animals that are male)
(; Gets fido and mickey)
(and (get animal instance *)
(get * gender male))
(; Get age of things named miffy)
(; Gets 7)
(get (get * name 'miffy) age *)
(; Get animals over the age of 5)
(; Get miffy)
(and (get animal instance *)
(get * age (> (get age instance *) 5)))
(; Get things between the age of 2 and 6)
(; Get mary and fido)
(get * age (> (< (get age instance *) 6) 2))
(; Get john's pet that mary likes)
(; Gets fido)
(and (get john pet *)
(get mary like *))
(; Get the things that mary's relationship to
is similar to john's feeling for mary)
(; Gets fido and miffy)
(get mary
(get (and (get feeling instance *)
(get john * mary))
similarTo
*)
*) |