Ex110 - Nearest Common Ancestor

Related: Examples, Ex100, Ex105, Ex110b - Source Code

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:

Common 'command' Report for 'god'
ThingX	ThingY	CmnAnc	Dist
army	john	army	1
army	laptop1	army	2
army	fido	army	2
army	mary	army	1
army	trinity	god	2
army	luke	god	3
john	laptop1	john	1
john	fido	john	1
john	mary	army	2
john	trinity	god	3
john	luke	god	4
laptop1	fido	john	2
laptop1	mary	mary	1
laptop1	trinity	trinity	2
laptop1	luke	trinity	3
fido	mary	mary	1
fido	trinity	trinity	2
fido	luke	luke	1
mary	trinity	trinity	1
mary	luke	trinity	2
trinity	luke	trinity	1
Time elapsed: 15 msec (on 500 Mhz PC)

Technically, the report shows any one of the closest ancestor between 2 things.

Download Database:
Ex110.zip (23 KB)
The creation of rolex replica the watch is inspired by the retro and elegant money of rolex replica watches the brand's round watch. The chronograph is omega replica simple and stylish, with the line features of rolex replica uk the watch series.

CM ©2000-2007