OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
blocksworld.py
Go to the documentation of this file.
1 try:
2  from opencog.atomspace import AtomSpace, types, Atom, TruthValue, get_type_name
3  import opencog.cogserver
4 except ImportError:
5  from atomspace_remote import AtomSpace, types, Atom, TruthValue, get_type_name
6 
7 from rules import evaluation_link_template, execution_link_template, actionDone_template
8 
9 from tree import *
10 
11 t = types
12 
13 def action_template(action,arguments):
14  return execution_link_template(
15  action,
16  arguments)
17 
18 def not_template(link):
19  return T('NotLink', link)
20 
21 def parse_params(vars, relation_l):
22  return [vars[v_str] for v_str in relation_l[1:]]
23 
24 def parse_conditions_list(vars, conditions_str):
25  conditions = []
26  for c_str in conditions_str.split('|'):
27  conditions_l = c_str.split()
28 
29  pred_str = conditions_l[0]
30 
31  pred_true = True
32  if pred_str[0] == '~':
33  pred_str = pred_str[1:]
34  pred_true = False
35 
36  pred = predicates[pred_str]
37  params = parse_params(vars, conditions_l)
38 
39  link = evaluation_link_template(pred,params)
40  if not pred_true:
41  link = not_template(link)
42  conditions.append(
43  link
44  )
45  return conditions
46 
47 def new_rule(atomspace, action_str, pre_str, post_str):
48  action_l = action_str.split()
49  action = actions[action_l[0]]
50 
51  vars = {}
52  for v_str in action_l[1:]:
53  vars[v_str] = atomspace.add(t.VariableNode, v_str)
54 
55  preconditions = parse_conditions_list(vars, pre_str)
56  preconditions.append(actionDone_template(atomspace, action_template(action, parse_params(vars, action_l))))
57 
58  postconditions = parse_conditions_list(vars, post_str)
59 
60  pil = T('PredictiveImplicationLink',
61  T('SimultaneousAndLink',
62  preconditions
63  ),
64  T('SimultaneousAndLink',
65  postconditions
66  )
67  )
68  atom_from_tree(pil, atomspace).tv = TruthValue(1, TruthValue().confidence_to_count(1))
69 
70  print pil
71  return pil
72 
73 
74 blocks = {}
75 actions = {}
76 predicates = {}
77 
78 def blocksworld_rules(atomspace):
79  for letter in 'ABC':
80  blocks[letter] = atomspace.add(t.ObjectNode, 'movable_block_'+letter)
81 
82  for action_name in 'unstack stack pickup putdown start end'.split(' '):
83  actions[action_name] = atomspace.add(t.SchemaNode, action_name)
84 
85  for predicate_name in 'clear on ontable handempty holding'.split(' '):
86  predicates[predicate_name] = atomspace.add(t.PredicateNode,predicate_name)
87 
88 # top = new_var()
89 # bottom = new_var()
90 #
91 # preconditions = [
92 # evaluation_link_template(predicates['clear'],[top]),
93 # evaluation_link_template(predicates['on'],[top, bottom]),
94 # evaluation_link_template(predicates['handempty'],[])
95 # ]
96 # postconditions = [
97 # evaluation_link_template(predicates['holding'],[top]),
98 # evaluation_link_template(predicates['clear'],[bottom]),
99 # # what?
100 # not_template(evaluation_link_template(predicates['clear'],[top])),
101 # not_template(evaluation_link_template(predicates['on'],[top, bottom])),
102 # not_template(evaluation_link_template(predicates['handempty'],[]))
103 # ]
104 #
105 # pil = T('PredictiveImplicationLink',
106 # T('SimultaneousAndLink',
107 # preconditions,
108 # actionDone_template(atomspace, action_template(actions['unstack'], [top, bottom]))
109 # ),
110 # T('SimultaneousAndLink',
111 # postconditions
112 # )
113 # )
114 # atom_from_tree(pil, atomspace)
115 
116 # new_rule([('clear',top), ('on',top, bottom), ('handempty')],
117 # [('holding', top), ('clear', bottom), ('~clear', top), ('~on', top, bottom), ('~handempty')],
118 # ('unstack', top, bottom)
119 # )
120 #
121 
122 # new_rule([('holding',top), ('clear', bottom)],
123 # [('on',top,bottom),('clear',top),('handempty'),('~holding',top),('~clear, bottom')],
124 # ('stack',top, bottom)
125 # )
126  new_rule(atomspace,
127  'unstack top bottom',
128  'clear top | on top bottom | handempty',
129  'holding top | clear bottom | ~clear top | ~on top bottom | ~handempty'
130  )
131 
132  new_rule(atomspace,
133  'stack top bottom',
134  'holding top | clear bottom',
135  'on top bottom | clear top | handempty | ~holding top | ~clear bottom'
136  )
137 
138  new_rule(atomspace,
139  'pickup block',
140  'ontable block | clear block | handempty',
141  'holding block | ~ontable block | ~clear block | ~handempty'
142  )
143 
144  new_rule(atomspace,
145  'putdown block',
146  'holding block',
147  'ontable block | clear block | handempty | ~holding block'
148  )
149 
150  initial_conditions_and = T('SimultaneousAndLink',
151  #parse_conditions_list(blocks, 'on C A | handempty | ontable A | ontable B | clear B | clear C')
152  parse_conditions_list(blocks, 'ontable C | handempty | ontable A | ontable B | clear B | clear C')
153  )
154  atom = atom_from_tree(initial_conditions_and, atomspace)
155  atom.tv = TruthValue(1, TruthValue().confidence_to_count(1))
156 
157 def blocksworld_test(atomspace):
158  atomspace.clear()
159  blocksworld.blocksworld_rules(atomspace)
160 
161  import logic
162 
163  target_tr = T('SimultaneousAndLink',
164  #parse_conditions_list(blocks, 'on A B | on B C')
165  parse_conditions_list(blocks, 'on A B')
166  )
167 
168  c = logic.Chainer(atomspace)
169 
170  res = logic.do_planning(atomspace, target_tr,c)
171 
172  f = '<blocksworld_test>'
173  if len(res):
174  print 'PASSED'
175  else:
176  print 'FAILED'