# Policy: Can RETRIEVE all but one of SSN and age.
# Input current query to be checked for compliance and query history.
#
# Workflow:
# 0. User calls "check-compliance POLICY NEW_QUERY QUERY_DESCRIPTOR" wrapper script.
#    POLICY is an AIR policy; NEW_QUERY is a query in n3, perhaps the output
#    of the sparql2n3 script, and QUERY_DESCRIPTOR is a file like query-req.n3
#    that defines the location of the query and history files.
# 1. Wrapper script appends NEW_QUERY to the history file in QUERY_DESCRIPTOR,
#    say, history.n3. This accomplishes three things:
#    a. We only need to look at QUERY_HISTORY when reasoning, making it much
#       easier to automatically generate policies that only grow linearly with
#       the number of terms.
#    b. AIR will explicitly know which query is the current (new) query.
#    c. This guarantees that the QUERY_HISTORY is never empty, eliminating a
#       tricky edge case.
# 2. Wrapper script writes NEW_QUERY to a file, say, query.n3.
# 3. Wrapper script calls policyrunner.py, and returns the output of
#    policyrunner.py for viewing.
# 4. (Optional) If policy is non compliant, we can remove it from the history
#    file.
#
# In doing so, we would modify query-req.n3 to look like:
## Test using file name for query as well as log/history
# [] a s:ComplianceQuery;
#   s:query <URI/TO/query.n3>;
#   s:history <URI/TO/history.n3>.
#
# We would also modify policyrunner.py to include a scenario, like this:
#'pirLocal' : (['URI/TO/query-req.n3'], ['URI/TO/policy.n3']),
#
# Alternately, we could just call policyrunner.py from the command line, with
# two arguments: URI/TO/query-req.n3 and URI/TO/policy.n3.


@prefix air: <http://dig.csail.mit.edu/TAMI/2007/amord/air#> .
@prefix owl: <http://www.w3.org/2002/07/owl#>.
@prefix math: <http://www.w3.org/2000/10/swap/math#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix s: <http://dig.csail.mit.edu/2009/IARPA-PIR/sparql#> .
@prefix : <http://dig.csail.mit.edu/2009/IARPA-PIR/ssn-policy#> .

@forAll :Q, :P, :W, :V, :T, :F, :S, :CURRENT, :HISTORY.
@forAll :X, :Y, :Z, :A, :B, :O.

:SSNORAGEPolicy a air:Policy;
    air:label "SSN sample policy for IARPA PIR project";
    air:rule :SSN_WRULE1.

:SSN_WRULE1 a air:BeliefRule;
       air:label "SSN policy wrule1";
       air:pattern {
        :CURRENT a s:ComplianceQuery;
             s:Query :Q;
             s:History :HISTORY.
       };
        air:description (:Q " is a SPARQL query and is the current query being checked. The history is in " :HISTORY " and contains " :Q " as well as prior queries.");
        air:rule :SSN_WRULE2.

# Variables used once - :HISTORY, :H.
# Variables repeated - :Qi, :Pi, :Wi, :Vi, :Ti, :Xi, :Yi.
@forAll :HISTORY, :H.
@forAll :Q1, :P1, :W1, :V1, :T1, :X1, :Y1.
@forAll :Q2, :P2, :W2, :V2, :T2, :X2, :Y2.
:SSN_WRULE2 a air:BeliefRule;
        air:label "SSN policy wrule2";
        air:pattern {
            # Parse the history file.
            :HISTORY log:semantics :H.
            #  Cannot RETRIEVE any SSN. This is variable i=1.
            :H log:includes {
                :Q1 a s:Query;
                    s:VarList :P1;
                 	s:Clause :W1.
                :P1 s:variable :V1. 
                :W1 s:TriplePattern :T1 }.
            :T1 log:includes { [] <http://xmlns.com/foaf/0.1/ssn> [] }.
            # Cannot RETRIEVE any age. This is variable i=2.
            :H log:includes {
                :Q2 a s:Query;
                    s:VarList :P2;
                 	s:Clause :W2.
                :P2 s:variable :V2. 
                :W2 s:TriplePattern :T2 }.
            :T2 log:includes { [] <http://xmlns.com/foaf/0.1/age> [] }.
        };
        air:description ("We cannot execute the query " :Q " because query " :Q1 " references SSN and query " :Q2 " references age."); 
        air:assert { :Q air:non-compliant-with :SSNPolicy_WhereClause };
        air:alt [ air:assert { :Q air:compliant-with :SSNPolicy_WhereClause } ] .
