AIR Policy Tutorial

For version: http://dig.csail.mit.edu/2009/AIR/

This page contains updated version of the example policies (based on the current version of AIR) used in the tutorial found at http://tw.rpi.edu/proj/tami/AIR_Policy_Tutorial. For detailed tutorial and complete description of the policies used, please refer to that page. A more detailed information about AIR can also be found at http://tw.rpi.edu/proj/tami/AIR_Language_Formalization

Contents

Policy with Single Rule

Policy with Multiple Rules

Variable Quantification and Scoping

Multiple Policies

Justification

Rule with Math

AIR Justifies

Rule with Sparql Query

Log Includes


Demo Data

File: Demo Data (in RDF)

  • Alice lives in Troy
  • Bob lives in Troy
  • George lives in Boston
  • Alice has NY state ID 307578001
  • David has NY state ID 307578002
  • Troy is in state of NY
  • Boston is in state of MA
  • NY is neighbor-state of MA
  • neighbor-state is an owl:SymmetricProperty

Policy with Single Rule

Tutorial Policy 1

Global variables :PERSON and :CITY are declared first (beginning of the document). :ny_state_residency is a policy that contains the rule :state-residency-rule. :state-residency-rule states that for all person and city when a person lives in that city and the city is in NY state, person complies-with the :ny_state_residency_policy.

@forAll :PERSON, :CITY.

:ny_state_residency_policy a air:RuleSet;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.
    };
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}].

Tutorial Policy 2

The same policy can be written without explicitly naming the rule.

@forAll :PERSON, :CITY.

 :ny_state_residency_policy a air:Policy;
       rdfs:label "NY State residency policy";
       air:rule [
         rdfs:label "state residency rule";
         air:if {
            :PERSON tamip:Lives_in_city :CITY.
            :CITY tamip:Has_state :NY.
         };
         air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}]
       ].

Rule with empty pattern

Tutorial Policy 21

A rule with an empty pattern can also be asserted as shown in the example below. :troy-rule adds a fact to the data-set that rpi is located in troy. Similarly :hartford-rule adds the fact rpi is located in hartford.

@forAll :PERSON, :CITY.

:rpi_location_based_policy a air:Policy;
      air:rule :troy-rule;
      air:rule :hartford-rule.

:troy-rule a air:Belief-rule;
    rdfs:label "rpi in troy rule";
    air:if { };
    air:then[air:assert {:rpi tamip:Located_In :troy.}].

:hartford-rule a air:Belief-rule;
    rdfs:label "rpi in hartford rule";
    air:if { };
    air:then[air:assert {:rpi tamip:Located_In :hartford.}].

Policy with Multiple Rules

Nested rules

Tutorial Policy 3

Positively Nested, nested rule becomes active when the pattern get matched (air:rule).

Policy :ny_state_residency_and_id_policy contains two rules- :state-residency-rule and :state-id-check- where rule :state-id-check is contained in :state-residency-rule. Of-these state-id-check is not active initially. state-id-check rule becomes active only when the pattern in :state-residency-rule is matched, and the variable bindings that occur in state-residency-rule are retained when state-id-check is applied.

According to the rules: for all person, city and ny-state-id, such that the person lives in the city and the city is in NY and the person has a NY state ID as well, the person is compliant with :ny_state_residency_and_id_policy.

@forAll :PERSON, :CITY, :NY_STATE_ID.

:ny_state_residency_and_id_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.
    };
    air:then[air:rule :state-id-check].

:state-id-check a air:Belief-rule;
    rdfs:label "state id check rule";
    air:if { :PERSON tamip:Has_ny_state_id :NY_STATE_ID. };
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_and_id_policy.}].

Tutorial Policy 4

Negatively Nested, nested rule becomes active when the pattern doesn't match (air:else).

:ny_neighbor_state_residency_policy contains three rules: :non-ny-residency-rule, :neighbor-state-rule and an anonymous rule nested in :non-ny-residency-rule. The anonymous node is positively nested, i.e. this rule is triggered when there is a match for the pattern in :non-ny-residency-rule. In contrast, the :neighbor-state-rule is negatively nested in the anonymous rule. That is, the :neighbor-state-rule would be triggered when the pattern :CITY tamip:Has_state :NY has no match.

According to the :non-ny-residency-rule, if a person lives in a city and if 'the city in NY state' does not hold, apply the :neighbor-state-rule rule. When the :neighbour-state-rule is applied, if the city is in a state, and the state is a neighboring state of NY, the person is asserted to comply with :ny_neighbor_state_residency_policy.

@forAll :PERSON, :CITY, :STATE.

:ny_neighbor_state_residency_policy a air:Policy;
    air:rule :non-ny-residency-rule.

:non-ny-residency-rule a air:Belief-rule;
    rdfs:label "Non NY residency rule";
    air:if {:PERSON tamip:Lives_in_city :CITY.};
    air:then[air:rule [
        air:if {:CITY tamip:Has_state :NY.};
        air:else [air:rule :neighbor-state-rule]
    ]].

:neighbor-state-rule a air:Belief-rule;
    rdfs:label "neighbor state rule";
    air:if { :CITY tamip:Has_state :STATE.
                  :NY tamip:Neighbor_state :STATE.};
    air:then[air:assert { :PERSON air:compliant-with :ny_neighbor_state_residency_policy. }].

Non-nested rules

Tutorial Policy 5

The :ny_state_residency_or_id_policy policy contains two rules: :state-residency-rule and :state-id-check, that are not nested and sort of independent of each other.

In this case, a person complies with the policy if he has a NY state id or if he stays in a city and that city is in NY. :state-id-check rule checks for ny state id, and :state-residency-rule independently checks if a person lives in a city in NY state.

@forAll :PERSON, :CITY, :NY_STATE_ID.

:ny_state_residency_or_id_policy a air:Policy;
      air:rule :state-residency-rule;
      air:rule :state-id-check.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.
    };
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_or_id_policy.}].

:state-id-check a air:Belief-rule;
    rdfs:label "state id check rule";
    air:if { :PERSON tamip:Has_ny_state_id :NY_STATE_ID. };
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_or_id_policy.}].

Alternative Action (air:else)

Tutorial 16

According the the :state-residency-rule: for all persons, person lives in a city and city in state of NY, the person is compliant with :ny_state_residency_policy. And for all persons, person lives in a city and 'city in state of NY' does not hold, the person is not compliant with :ny_state_residency_policy.

@forAll :PERSON, :CITY.

:ny_state_residency_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
    };
    air:then[air:rule [
       air:if {
          :CITY tamip:Has_state :NY.
       };
       air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}];
       air:else [air:assert {:PERSON air:non-compliant-with :ny_state_residency_policy.}]
    ]].

Variable Quantification and Scoping

Existential Quantification in the pattern

Tutorial Policy 6

This policy is same as the Tutorial Policy 1, except that :CITY is locally declared within the pattern. Note that :CITY is existentially quantified and is not used in the right hand side (assertion). According to the state-residency-rule, for all persons, person lives in some city that is in NY state, the person is compliant with the :ny-state-residency-policy.

Note that if :CITY was universally qualified, the :state-residency-rule would become: for all persons, if person lives in :CITY and for all values that :CITY can take :CITY is in state NY, the person is compliant with :ny_state_residency_policy. Since all URIs in the data are possible values for :CITY, empty set is returned as conclusion.


@forAll :PERSON.

:ny_state_residency_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       @forSome :CITY.
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.
    };    
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}].

Tutorial Policy 8

Note that the existentially quantified variable :CITY can be replaced with a blank node.

@forAll :PERSON.

:ny_state_residency_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city [tamip:Has_state :NY].
    };    
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}].

Multiple Policies

Multiple Policies in 1 Document

Tutorial Policy 19

Here we see that we can define two different policies :ny_state_residency_policy and :ny_state_id_policy in the same documents.

 @forAll :PERSON, :CITY, :NY_STATE_ID.

 :ny_state_residency_policy a air:Policy;
     air:rule :state-residency-rule.

 :state-residency-rule a air:Belief-rule;
   rdfs:label "state residency rule";
   air:if {
      :PERSON tamip:Lives_in_city :CITY.
      :CITY tamip:Has_state :NY.         
   };
   air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}].

 :ny_state_id_policy a air:Policy;
     air:rule :state-id-check.

 :state-id-check a air:Belief-rule;
   rdfs:label "state id check rule";
   air:if { :PERSON tamip:Has_ny_state_id :NY_STATE_ID. };
   air:then[air:assert {:PERSON air:compliant-with :ny_state_id_policy.}].

Justification

Natural language explanation of the rule (air:description)

Tutorial Policy 11

The policy is same as that in Tutorial Policy 1. Here, we also provide a natural language description for the :state-residency-rule, using air:description. Description is given as a sequence of variables and strings.

@forAll :PERSON, :CITY.

:ny_state_residency_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.         
    };
    air:then[air:description(:PERSON "lives in the NY state city -" :CITY);    
    air:assert {:PERSON air:compliant-with :ny_state_residency_policy.}].

Hiding Justification

Tutorial Policy 12

The policy is same as Tutorial Policy 3. But we don't want the state id to show up in the justification. To hide it we declare :state-id-check to be a air:Hidden-rule.

@forAll :PERSON, :CITY, :NY_STATE_ID.

:ny_state_residency_and_id_policy a air:Policy;
      air:rule :state-residency-rule.

:state-residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {
       :PERSON tamip:Lives_in_city :CITY.
       :CITY tamip:Has_state :NY.         
    };
    air:then[air:rule :state-id-check].

:state-id-check a air:Hidden-rule;
    rdfs:label "state id check rule";
    air:if { :PERSON tamip:Has_ny_state_id :NY_STATE_ID. };
    air:then[air:assert {:PERSON air:compliant-with :ny_state_residency_and_id_policy.}].

AIR Built-ins

Math Policy

Say for example that we have some policy called :math_policy with a rule called math-rule. Math rule specifies that :MathAddition is compliant-with :math_policy if only if 1 + 2 = 3. Uses math:sum built-in to check if 1 + 2 == 3, then :MathAddition is compliant with :math_policy. Well, this one is trivial. Here, we've shown how to incorporate some mathematical operation and logic with AIR. We can also say things like

 :AREA math:greaterThan "50" 
for a rule that takes some :AREA and determines whether it is greater than 50 and asserts a conclusion/s from that. For more information about math, please visit http://www.w3.org/2000/10/swap/math.
:math_policy a air:Policy;
      air:rule :math-rule.

:math-rule a air:Belief-rule;
    air:label "addition rule";
        air:if { (1 2) math:sum 3 };
            air:then[air:assert {:MathAddition air:compliant-with :math_policy.}].

AIR Justifies

:JustifiesRulePolicy a air:Policy;
	air:rule :Rule1 .

:Rule1 a air:Belief-rule ;
       air:label "air:justifies with compliant policy";
       air:if { @forSome :LOG, :RULE.
                <http://dig.csail.mit.edu/2009/AIR/tutorial/examples/justifies_nested_log.n3> log:semantics :LOG .
                <http://dig.csail.mit.edu/2009/AIR/tutorial/examples/justifies_compliant_policy.n3> log:semantics :RULE .
                ((:LOG) (:RULE)) air:justifies { :nested_log air:compliant-with :nested_compliant_policy . } };
       air:then [ air:assert { :QComply air:compliant-with :JustifiesRulePolicy . } ] ;
       air:else [ air:assert { :QComply air:non-compliant-with :JustifiesRulePolicy . } ] .

Sparql Tutorial

Here we see a SPARQL query to dbpedia.org looking for db:areaLand for <http://dbpedia.org/resource/Boston>. Checks if :SIZE math:greaterThan "100", then compliant with TestPolicy.

:TestPolicy a air:RuleSet;
     air:rule :QueryDBPedia.

@forAll :RESULTS, :SIZE .

:QueryDBPedia a air:BeliefRule;
       air:if {
(<http://dbpedia.org/sparql> """ 
PREFIX db: <http://dbpedia.org/ontology/>
CONSTRUCT { <http://dbpedia.org/resource/Boston> db:areaLand ?area. }
WHERE { <http://dbpedia.org/resource/Boston> db:areaLand ?area }
""") sparql:queryEndpoint :RESULTS .
           :RESULTS log:includes { <http://dbpedia.org/resource/Boston> db:areaLand :SIZE }.
           :SIZE math:greaterThan "10000"
       };  
       air:then [ air:description ("Boston size: " :SIZE);
                  air:assert { :RESULT air:compliant-with :TestPolicy }
        ].

Log includes

@forAll :PERSON, :CITY.

:live_in_city_policy a air:RuleSet;
      air:rule :residency-rule.

:residency-rule a air:Belief-rule;
    rdfs:label "state residency rule";
    air:if {@forSome :LOG.
       <http://dig.csail.mit.edu/2009/AIR/tutorial/examples/demo.rdf> log:semantics :LOG.
       :LOG log:includes {:PERSON tamip:Lives_in_city :CITY} 
    };    
    air:then[air:description (:PERSON " lives in " :CITY);
            air:assert {:PERSON air:compliant-with :live_in_city_policy.}].