Stáhnout: 5.2_11.pl  SWISH   Zobrazit: jednoduše   5.2_11.py

% 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, member_anyX

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):
    # zavisi na resenem problemu
    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)

# 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)

 Stáhnout: 5.2_11.pl  SWISH   Zobrazit: jednoduše   5.2_11.py