[565184]: examples / family_relations / example_fc.py  Maximize  Restore  History

Download this file

190 lines (174 with data), 7.3 kB

  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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# example.py
from __future__ import with_statement, absolute_import, division
from pyke import contexts, pattern, fc_rule, rule_base
from pyke import lookup, assert_
This_rule_base = rule_base.get_create('example')
def son_of(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
for dummy in (None,) if index == 0 else \
lookup('family', 'son_of', context, rule.foreach_patterns(0)):
assert_('family', 'child_parent',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),
rule.pattern(2).as_data(context),
rule.pattern(3).as_data(context),))
assert_('family', 'child_parent',
(rule.pattern(0).as_data(context),
rule.pattern(4).as_data(context),
rule.pattern(5).as_data(context),
rule.pattern(3).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('son_of', This_rule_base, son_of,
(('family', 'son_of',
(contexts.variable('child'),
contexts.variable('father'),
contexts.variable('mother'),)),),
(contexts.variable('child'),
contexts.variable('father'),
pattern.pattern_literal('father'),
pattern.pattern_literal('son'),
contexts.variable('mother'),
pattern.pattern_literal('mother'),))
def daughter_of(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
for dummy in (None,) if index == 0 else \
lookup('family', 'daughter_of', context, rule.foreach_patterns(0)):
assert_('family', 'child_parent',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),
rule.pattern(2).as_data(context),
rule.pattern(3).as_data(context),))
assert_('family', 'child_parent',
(rule.pattern(0).as_data(context),
rule.pattern(4).as_data(context),
rule.pattern(5).as_data(context),
rule.pattern(3).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('daughter_of', This_rule_base, daughter_of,
(('family', 'daughter_of',
(contexts.variable('child'),
contexts.variable('father'),
contexts.variable('mother'),)),),
(contexts.variable('child'),
contexts.variable('father'),
pattern.pattern_literal('father'),
pattern.pattern_literal('daughter'),
contexts.variable('mother'),
pattern.pattern_literal('mother'),))
def brothers(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
for dummy in (None,) if index == 0 else \
lookup('family', 'son_of', context, rule.foreach_patterns(0)):
for dummy in (None,) if index == 1 else \
lookup('family', 'son_of', context, rule.foreach_patterns(1)):
if context.lookup_data('brother1') != context.lookup_data('brother2'):
assert_('family', 'siblings',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),
rule.pattern(2).as_data(context),
rule.pattern(2).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('brothers', This_rule_base, brothers,
(('family', 'son_of',
(contexts.variable('brother1'),
contexts.variable('father'),
contexts.variable('mother'),)),
('family', 'son_of',
(contexts.variable('brother2'),
contexts.variable('father'),
contexts.variable('mother'),)),),
(contexts.variable('brother1'),
contexts.variable('brother2'),
pattern.pattern_literal('brother'),))
def sisters(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
for dummy in (None,) if index == 0 else \
lookup('family', 'daughter_of', context, rule.foreach_patterns(0)):
for dummy in (None,) if index == 1 else \
lookup('family', 'daughter_of', context, rule.foreach_patterns(1)):
if context.lookup_data('sister1') != context.lookup_data('sister2'):
assert_('family', 'siblings',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),
rule.pattern(2).as_data(context),
rule.pattern(2).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('sisters', This_rule_base, sisters,
(('family', 'daughter_of',
(contexts.variable('sister1'),
contexts.variable('father'),
contexts.variable('mother'),)),
('family', 'daughter_of',
(contexts.variable('sister2'),
contexts.variable('father'),
contexts.variable('mother'),)),),
(contexts.variable('sister1'),
contexts.variable('sister2'),
pattern.pattern_literal('sister'),))
def brother_and_sister(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
for dummy in (None,) if index == 0 else \
lookup('family', 'son_of', context, rule.foreach_patterns(0)):
for dummy in (None,) if index == 1 else \
lookup('family', 'daughter_of', context, rule.foreach_patterns(1)):
assert_('family', 'siblings',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),
rule.pattern(2).as_data(context),
rule.pattern(3).as_data(context),))
assert_('family', 'siblings',
(rule.pattern(1).as_data(context),
rule.pattern(0).as_data(context),
rule.pattern(3).as_data(context),
rule.pattern(2).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('brother_and_sister', This_rule_base, brother_and_sister,
(('family', 'son_of',
(contexts.variable('brother'),
contexts.variable('father'),
contexts.variable('mother'),)),
('family', 'daughter_of',
(contexts.variable('sister'),
contexts.variable('father'),
contexts.variable('mother'),)),),
(contexts.variable('brother'),
contexts.variable('sister'),
pattern.pattern_literal('sister'),
pattern.pattern_literal('brother'),))
def facts_for_bc_rules(rule, context = None, index = None):
if context is None: context = contexts.simple_context()
try:
assert_('family', 'as_au',
(rule.pattern(0).as_data(context),
rule.pattern(1).as_data(context),))
assert_('family', 'as_au',
(rule.pattern(2).as_data(context),
rule.pattern(3).as_data(context),))
assert_('family', 'as_nn',
(rule.pattern(4).as_data(context),
rule.pattern(5).as_data(context),))
assert_('family', 'as_nn',
(rule.pattern(6).as_data(context),
rule.pattern(7).as_data(context),))
finally:
context.done()
fc_rule.fc_rule('facts_for_bc_rules', This_rule_base, facts_for_bc_rules,
(),
(pattern.pattern_literal('brother'),
pattern.pattern_literal('uncle'),
pattern.pattern_literal('sister'),
pattern.pattern_literal('aunt'),
pattern.pattern_literal('son'),
pattern.pattern_literal('nephew'),
pattern.pattern_literal('daughter'),
pattern.pattern_literal('niece'),))

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks