OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bindlink.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 # bindlink.py
4 #
5 """
6 Example of how to use the pattern matcher.
7 Based on the following example in the wiki:
8 http://wiki.opencog.org/w/Pattern_matching#The_Simplified_API
9 """
10 
11 __author__ = 'Cosmo Harrigan'
12 
13 from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name
14 from opencog.scheme_wrapper import load_scm, scheme_eval, scheme_eval_h, __init__
15 
16 atomspace = AtomSpace()
17 __init__(atomspace)
18 
19 data = ["opencog/atomspace/core_types.scm",
20  "opencog/scm/utilities.scm"]
21 
22 for item in data:
23  load_scm(atomspace, item)
24 
25 # Define several animals and something of a different type as well
26 scheme_animals = \
27  '''
28  (InheritanceLink (ConceptNode "Frog") (ConceptNode "animal"))
29  (InheritanceLink (ConceptNode "Zebra") (ConceptNode "animal"))
30  (InheritanceLink (ConceptNode "Deer") (ConceptNode "animal"))
31  (InheritanceLink (ConceptNode "Spaceship") (ConceptNode "machine"))
32  '''
33 scheme_eval_h(atomspace, scheme_animals)
34 
35 # Define a graph search query
36 scheme_query = \
37  '''
38  (define find-animals
39  (BindLink
40  ;; The variable to be bound
41  (VariableNode "$var")
42 
43  ;; The pattern to be searched for
44  (InheritanceLink
45  (VariableNode "$var")
46  (ConceptNode "animal")
47  )
48 
49  ;; The value to be returned.
50  (VariableNode "$var")
51  )
52  )
53  '''
54 scheme_eval_h(atomspace, scheme_query)
55 
56 # Run the above pattern and print the result
57 result = scheme_eval_h(atomspace, '(cog-bind find-animals)')
58 print "The result of pattern matching is:\n\n" + str(atomspace[result])