-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathdefine.scm
97 lines (82 loc) · 2.22 KB
/
define.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
;
; define.scm -- Using DefineLink to give names to things.
;
(use-modules (opencog) (opencog exec))
;; Some data to populate the AtomSpace.
(Inheritance
(Concept "battery")
(Concept "electrical device"))
(Inheritance
(Concept "transistor")
(Concept "electrical device"))
(Evaluation
(Predicate "PartOf")
(List
(Concept "battery")
(Variable "car")))
(Evaluation
(Predicate "PartOf")
(List
(Concept "transistor")
(Variable "phone")))
(Evaluation
(Predicate "PartOf")
(List
(Concept "windshield")
(Variable "car")))
;; Define the concept of electrical parts of things.
;; Both clauses must be present, for this to evaluate to true!
(DefineLink
(DefinedPredicate "Electrical Part Of")
(Present
(Inheritance
(Variable "$x")
(Concept "electrical device"))
(Evaluation
(Predicate "PartOf")
(List
(Variable "$x")
(Variable "$y")))))
;; Define a pattern to find the electrical parts of things.
;; Variables are automatically extracted from the definition.
(define get-elect
(Get (DefinedPredicate "Electrical Part Of")))
;; Search the AtomSpace for electrical things.
(cog-execute! get-elect)
;; ==================================================================
;;
;; Patterns can also be assembled out of multiple Defines,
;; and mixed with other kinds of clauses.
(DefineLink
(DefinedPredicate "Electrical Thing")
(Inheritance
(Variable "$x")
(Concept "electrical device")))
(DefineLink
(DefinedPredicate "Part-whole Relation")
(Evaluation
(Predicate "PartOf")
(List
(Variable "$x")
(Variable "$y"))))
;; Define a predicate to "do things" (in this case, to print)
;; Be sure to return a truth value!
(define cnt 0)
(define (do-stuff atom)
(set! cnt (+ cnt 1))
(format #t "At count ~a found this part: ~a \n" cnt atom)
(stv 1 1))
(DefineLink
(DefinedPredicate "Counter Printer")
(Evaluation (GroundedPredicate "scm: do-stuff")
(List (Variable "$x"))))
;; Assemble a pattern out of the parts above. Notice that the variables
;; in each of the different defines are joined together.
(define get-electrical-parts
(Get
(And
(DefinedPredicate "Electrical Thing")
(DefinedPredicate "Part-whole Relation")
(DefinedPredicate "Counter Printer")
)))
(cog-execute! get-electrical-parts)