OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / greater_than.scm

139 lines · scm ref:
;
; greater_than.scm
;
; Test data for the GroundedPredicateNode, which is a virtual node.
; This declares the net worth of four individuals.  It also declares
; four QueryLinks, which find everyone who is richer.  The greater-than
; comparison of richness is performed via the virtual link evaluation.
;
(EvaluationLink
	(PredicateNode "net-worth")
	(ListLink
		(ConceptNode "Bill Gates")
		(NumberNode "500000")
	)
)

(EvaluationLink
	(PredicateNode "net-worth")
	(ListLink
		(ConceptNode "Obama")
		(NumberNode "1000")
	)
)

(EvaluationLink
	(PredicateNode "net-worth")
	(ListLink
		(ConceptNode "Susan M. from Peoria")
		(NumberNode "200")
	)
)

(EvaluationLink
	(PredicateNode "net-worth")
	(ListLink
		(ConceptNode "George P. from Waxahachie")
		(NumberNode "310")
	)
)

(define (richer-than-person-x-cmp person-x comp-link)
	(CollectionOf
	(QueryLink
		(VariableList
			(VariableNode "$who")
			(TypedVariableLink
				(VariableNode "$less-wealth")
				(TypeNode "NumberNode")
			)
			(TypedVariableLink
				(VariableNode "$more-wealth")
				(TypeNode "NumberNode")
			)
		)
		(AndLink
			(EvaluationLink
				(PredicateNode "net-worth")
				(ListLink
					person-x
					(VariableNode "$less-wealth")
				)
			)

			(EvaluationLink
				(PredicateNode "net-worth")
				(ListLink
					(VariableNode "$who")
					(VariableNode "$more-wealth")
				)
			)

			comp-link
		)

		;; output result: just the concept node of who it is.
		(VariableNode "$who")
	)
	)
)

;; -----------------------------------------------------
;; This variant uses the built-in GreaterThanLink
(define builtin-cmp
	(GreaterThanLink
		(VariableNode "$more-wealth")
		(VariableNode "$less-wealth")
	)
)

(define (builtin-than-person-x person-x)
	(richer-than-person-x-cmp person-x builtin-cmp))

(define (builtin-than-gates)
	(builtin-than-person-x (ConceptNode "Bill Gates")))

(define (builtin-than-obama)
	(builtin-than-person-x (ConceptNode "Obama")))

(define (builtin-than-george)
	(builtin-than-person-x (ConceptNode "George P. from Waxahachie")))

(define (builtin-than-susan)
	(builtin-than-person-x (ConceptNode "Susan M. from Peoria")))


;; -----------------------------------------------------
;; This variant uses a hand-rolled scm compare function
(define (cog-number x) (cog-value-ref x 0))
(define (richer a b)
	(> (cog-number a) (cog-number b))
)

; the compare eval link
(define scm-cmp
	(EvaluationLink
		(GroundedPredicateNode "scm:richer")
		(ListLink
			(VariableNode "$more-wealth")
			(VariableNode "$less-wealth")
		)
	)
)

;; Use the "richer" function defined immediately above.
(define (scm-than-person-x person-x)
	(richer-than-person-x-cmp person-x scm-cmp))

(define (scm-than-gates)
	(scm-than-person-x (ConceptNode "Bill Gates")))

(define (scm-than-obama)
	(scm-than-person-x (ConceptNode "Obama")))

(define (scm-than-george)
	(scm-than-person-x (ConceptNode "George P. from Waxahachie")))

(define (scm-than-susan)
	(scm-than-person-x (ConceptNode "Susan M. from Peoria")))