Commit cb1e06b8 authored by Vít Novotný's avatar Vít Novotný
Browse files

added example 5.2_11

parent fe8559da
% nacteni:
/* ['5.2_11.pl']. */
?- op(600, xfx, --->).
?- op(500, xfx, :).
a ---> or:[b,c].
b ---> and:[d,e].
c ---> and:[f,g].
e ---> or:[h].
f ---> and:[h,i].
goal(d).
goal(g).
goal(h).
solve(Node,Node) :- goal(Node).
solve(Node,Node ---> Tree) :-
Node ---> or:Nodes, member(Node1,Nodes), solve(Node1,Tree).
solve(Node,Node ---> and:Trees) :-
Node ---> and:Nodes, solveall(Nodes,Trees).
solveall([],[]).
solveall([Node|Nodes],[Tree|Trees]) :- solve(Node,Tree), solveall(Nodes,Trees).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
start/0.
start:-
write('Prohledavani AND/OR grafu'),nl,nl,
write(' Graf:'),nl,
write(' a ---> or:[b,c].'),nl,
write(' b ---> and:[d,e].'),nl,
write(' c ---> and:[f,g].'),nl,
write(' e ---> or:[h].'),nl,
write(' f ---> and:[h,i].'),nl,
write(' goal(d).'),nl,
write(' goal(g).'),nl,
write(' goal(h).'),nl,nl,
write('Vysledek dotazu "solve(a,Tree)":'),nl,
solve(a,Tree),
write('Tree = '),write(Tree),nl.
?-start.
:- retractall(start/0).
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
graph = dict(
a=("or", LinkedList(["b", "c"])),
b=("and", LinkedList(["d", "e"])),
c=("and", LinkedList(["f", "g"])),
e=("or", LinkedList(["h"])),
f=("and", LinkedList(["h", "i"])))
goals = dict(d=True, g=True, h=True)
def is_goal(node):
return node in goals
def solve(node):
if is_goal(node):
yield node
if node in graph:
nodes = graph[node][1]
if graph[node][0] == "or":
for node1 in member_anyX(nodes):
for tree in solve(node1):
yield (node, "--->", tree)
elif graph[node][0] == "and":
for trees in solveall(nodes):
yield (node, "--->", ("and", trees))
def solveall(nodes):
if nodes == Nil:
yield Nil
else:
for tree in solve(nodes.head):
for trees in solveall(nodes.tail):
yield Cons(tree, trees)
def member_anyX(xs):
if xs == Nil:
return
yield xs.head
for x in member_anyX(xs.tail):
yield x
# demonstracni vypis
if __name__ == "__main__":
print('Prohledavani AND/OR grafu')
print('\n Graf:')
print(' a ---> or:[b,c].')
print(' b ---> and:[d,e].')
print(' c ---> and:[f,g].')
print(' e ---> or:[h].')
print(' f ---> and:[h,i].')
print(' goal(d).')
print(' goal(g).')
print(' goal(h).')
print('\nVysledky dotazu solve("a"):')
for solution in solve("a"):
print(solution)
Prohledavani AND/OR grafu
Graf:
a ---> or:[b,c].
b ---> and:[d,e].
c ---> and:[f,g].
e ---> or:[h].
f ---> and:[h,i].
goal(d).
goal(g).
goal(h).
Vysledky dotazu solve("a"):
('a', '--->', ('b', '--->', ('and', ['d', ('e', '--->', 'h')])))
...@@ -22,6 +22,9 @@ output: ...@@ -22,6 +22,9 @@ output:
python "$$FILE" > $$TEMP; \ python "$$FILE" > $$TEMP; \
diff "$$FILE.out" $$TEMP; \ diff "$$FILE.out" $$TEMP; \
printf '# python output test "%s" ok\n' "$$FILE"; \ printf '# python output test "%s" ok\n' "$$FILE"; \
else \
printf 'There exists no output for script %s!' "$$FILE"; \
exit 1; \
fi; \ fi; \
done done
# python output test ok # python output test ok
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment