forked from rekino/opencog-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpln_examples.py
141 lines (120 loc) · 3.72 KB
/
pln_examples.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'''Runs PLN examples, similar to PLNUTest. Currently only works from the cogserver due
to linking issues.'''
import ConfigParser
import util
from pyopencog.atomspace import AtomSpace, Atom, Handle
import scheme_wrapper
files_list = ('''
future_fc/7_test.conf
future_fc/InheritanceOsamaAbu_test.conf
future_fc/6_test.conf
future_fc/InheritanceMuhammadTerrorist_test.conf
#psi/water_test.conf
psi/psi_planning_one_step_test.conf
psi/psi_planning_two_step_test.conf
# The obsolete agisim tests are known not to work.
#agisim_planning/19_test.conf
#agisim_planning/23_test.conf
#agisim_planning/FetchDemo5_test.conf
#agisim_planning/FetchDemo5_subgoal_test.conf
#agisim_planning/11_test.conf
bc/new/NotEvaluatorRule_test.conf
bc/new/and_test.conf
bc/new/or_test.conf
bc/new/LookupRule_test.conf
bc/targetslist/4_test.conf
bc/targetslist/9_test.conf
bc/targetslist/0_test.conf
#bc/targetslist/30_test.conf
bc/multiple_roots_spawning_test.conf
bc/PsiActionSelection_test.conf
both/new/ModusPonensRule_test.conf
both/new/ModusPonensRule_3steps_test.conf
both/new/ModusPonensRule_2steps_test.conf
both/new/BasicForAllDemo_test.conf
both/new/BasicForAllDemo2_test.conf
both/new/DeductionRule_test.conf
both/new/InversionRule_test.conf
#both/inverse_binding_test.conf
both/targetslist/21_test.conf
#both/woa_demo_test.conf
bc/AnotBdemo_full_test.conf
both/AnotBdemo_partial_test.conf
both/28_test.conf
bc/plus_test.conf
#bc/new/pathfinding_test.conf
bc/new/before_test.conf
''')
#files_list = '''bc/new/before_test.conf'''
#files_list = '''bc/plus_test.conf'''
#files_list ='''
#both/new/BasicForAllDemo_test.conf
#both/new/BasicForAllDemo2_test.conf
#'''
files = files_list.split('\n')
def test_all(a):
for f in files:
if f != '' and not f.startswith('#'):
run_pln_example(a, f)
print 'Passed %s out of %s tests' % (len(passed), len(passed+failed))
if len(failed):
print 'Failed tests:'
for f in failed:
print f
passed = []
failed = []
def run_pln_example(a, f):
a.clear()
testdir = '../tests/reasoning/pln/targets/'
datadirs = ['../tests/reasoning/pln/scm/',
'../opencog/']
fname = testdir+f
config = ConfigParser.ConfigParser()
read_files = config.read(fname)
if not read_files:
raise Exception("no such file:",fname)
def get(field):
return config.get('PLN_TEST',field)
print f
def load_axioms(fname):
for d in datadirs:
kf = d+fname+'.scm'
try:
tmp = open(kf,'r')
scheme_wrapper.load_scm(a, kf)
print kf
return
except IOError:
continue
raise IOError("missing data file: "+kf)
data_files = get('load').replace(' ','').split(',')
for fname in data_files:
load_axioms(fname)
scm_target = '(cog-handle %s)' % (get('target'),)
print scm_target
handle_str = scheme_wrapper.scheme_eval(scm_target)
try:
h = int(handle_str)
except ValueError:
print handle_str
raise Exception("Scheme error in target")
nsteps = int(get('max_steps'))
target = Atom(Handle(h), a)
print target
import logic
import tree
# hack - won't work if the Scheme target is some variable that doesn't contain "Demand"
if "Demand" in scm_target:
# superhack - doesn't care which target you say
target_tr = tree.tree_from_atom(target)
res = logic.do_planning(a, target_tr)
else:
c = logic.Chainer(a)
target_tr = tree.tree_from_atom(target)
res = c.bc(target_tr, nsteps)
if len(res):
print 'PASSED'
passed.append(f)
else:
print 'FAILED'
failed.append(f)