![]() |
|
Ex110 - Nearest Common Ancestor
Background: Related: Examples, Ex100, Ex105, Ex110b - Source Code Description: This example stores a command hierarchy and generates a report indicating the nearest common commander between various things. The root of the command hierarchy is God. God commands Army (a force) and Trinity (a church), Army commands John and Mary, etc. Things in the hierarchy have different classifications and attributes. NLI Script
(; Create a db item named god)
(new 'god)
(; Create a force named army)
(new 'army 'force)
(; Create a church named trinity)
(new 'trinity 'church)
(; Create db item named age, weight & color)
(new 'age)
(new 'weight)
(new 'color)
(; Create a person named john whose age is 35)
(new 'john 'person)
(set+ (it) age '35)
(; Create a person named mary whose weight is 130)
(new 'mary 'person)
(set+ (it) weight '130)
(; Create a person named luke whose color is red)
(new 'luke 'person)
(set+ (it) color 'red)
(; Create a dog named fido)
(new 'fido 'dog)
(; Create a computer named laptop1)
(new 'laptop1 'computer)
(; Create a verb named command which leads down in hierarchy)
(new 'command 'verb)
(set (it) leads down)
(; Create a verb named follow which leads up in hierarchy)
(new 'follow 'verb)
(set (it) leads up)
(; Create reciprocal relationship between command and follow)
(set command reciprocal follow)
(set follow reciprocal command)
(; Create command hierarchy)
(setRR god command army)
(setRR god command trinity)
(setRR army command john)
(setRR army command mary)
(setRR trinity command mary)
(setRR trinity command luke)
(setRR john command laptop1)
(setRR mary command laptop1)
(setRR john command fido)
(setRR mary command fido)
(setRR luke command fido)
(; Get persons that army commands)
(; Gets john and mary)
(and (get person instance *)
(get army command *))
(; Get things that weigh 130,
are commanded by something commanded by god
and commands something named fido)
(; Gets mary)
(and (get * weight 130)
(get (get god command *) command *)
(get * command (get * name 'fido)))
To create the Nearest Common Ancestor Report, select 'god' and right-click. Select 'Generate Anc Report'. The report is written to a tab-delimited text file named "god_ID.txt" in the same directory as the database file. Data in text file can be imported into Excel or Access. The report is shown below and is included in the zip:
Notes: Technically, the report shows any one of the closest ancestor between 2 things. Download Database: Ex110.zip (23 KB) |