# ----------------------------
# Policy model finding example
# ----------------------------

@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix : <http://example.org/#>.

# Pat is an individual
:Pat a vcard:Individual.

# if X is an individual then X can do A or B
{
    ?X a vcard:Individual.
} => ($ {
    ?X :canDo :A.
} {
    ?X :canDo :B.
} $).

# if X is an individual who can do A then X can do E or D
{
    ?X a vcard:Individual.
    ?X :canDo :A.
} => ($ {
    ?X :canDo :E.
} {
    ?X :canDo :D.
} $).

# if X is an individual who can do D then X can do E or F
{
    ?X a vcard:Individual.
    ?X :canDo :D.
} => ($ {
    ?X :canDo :E.
} {
    ?X :canDo :F.
} $).

# find model reaching goal
{
    ?Goal :findModel (?Steps ?Model ?Model).
} <= {
    ?Goal log:call true.
    true log:callWithCut true.
}.

{
    ?Goal :findModel (?Steps1 ?Model1 ?Model).
} <= {
    ?Prem => ?Cases.
    ?Cases list:member ?Case.
    ?Prem log:call true.
    ?Steps1 list:notMember {?Prem => ?Cases}.
    (?Steps1 ({?Prem => ?Cases})) list:append ?Steps2.
    (?Model1 (?Case)) list:append ?Model2.
    true log:becomes ?Case.
    {
        ?Goal :findModel (?Steps2 ?Model2 ?Model).
    } log:callWithCleanup {
        ?Case log:becomes true.
    }.
}.

# query: find model where X is an individual who can do E
{
    {
        ?X a vcard:Individual.
        ?X :canDo :E.
    } :findModel (() () ?Model).
} =^ {
    :model :is ?Model.
}.
