<?xml version="1.0"?>

<!--
Shows the application of defeasible rules to overcome conflicts and prioritize rules
-->
<rbsla>
<rulebase>


<defeasible>
  <oid>discount5.0</oid>
  <_head>
    <atom>
      <_opr><rel>discount</rel></_opr>
      <var>customer</var>
      <var>product</var>
      <ind>5.0 percent</ind>
    </atom>
  </_head>
  <_body>
    <and>
      <atom>
        <_opr><rel>premium</rel></_opr>
        <var>customer</var>
      </atom>
      <atom>
        <_opr><rel>regular</rel></_opr>
        <var>product</var>
      </atom>
    </and>
  </_body>
</defeasible>


<!--  Def 2
The discount for a customer buying a product is 7.5 percent
if the customer is premium and the product is luxury.
-->

<defeasible>
  <oid>discount7,5</oid>
  <_head>
    <atom>
      <_opr><rel>discount</rel></_opr>
      <var>customer</var>
      <var>product</var>
      <ind>7.5 percent</ind>
    </atom>
  </_head>
  <_body>
    <and>
      <atom>
        <_opr><rel>premium</rel></_opr>
        <var>customer</var>
      </atom>
      <atom>
        <_opr><rel>luxury</rel></_opr>
        <var>product</var>
      </atom>
    </and>
  </_body>
</defeasible>


<!--  Imp 3
A customer is premium if
their spending has been min 5000 euro in the previous year.

Notice that "previous year" can again be regarded as an individual constant
consisting of an entire phrase. It can also be regarded as a semi-formal construct
transitionally used in the process of formalizing a given English business rule in
a logic allowing backward temporal references.

Similarly, "min 5000 euro" can as well be regarded as an individual constant phrase.
It can also be regarded as a semi-formal construct ultimately to be replaced by a
'greater-than' comparison over numbers with units.
-->

<imp>
  <_head>
    <atom>
      <_opr><rel>premium</rel></_opr>
      <var>customer</var>
    </atom>
  </_head>
  <_body>
    <atom>
      <_opr><rel>spending</rel></_opr>
      <var>customer</var>
      <ind>min 5000 euro</ind>
      <ind>previous year</ind>
    </atom>
  </_body>
</imp>


<!--  Fact 1
A Porsche is luxury.
-->
 
<fact>
 <_head>
  <atom>
    <_opr><rel>luxury</rel></_opr>
    <ind>Porsche</ind>
  </atom>
 </_head>
</fact>


<!--  Fact 2
A Honda is regular.
-->

<fact>
 <_head>
  <atom>
    <_opr><rel>regular</rel></_opr>
    <ind>Honda</ind>
  </atom>
 </_head>
</fact>


<!--  Fact 3
Peter Miller's spending has been min 5000 euro in the previous year.

Notice that "min 5000 euro" and "previous year" directly unify with
the identical individual constant phrases of Imp 3. Thus, these
semi-formal constructs can be used for debugging the rules already
in an early phase of the formalization process.
-->

<fact>
 <_head>
  <atom>
    <_opr><rel>spending</rel></_opr>
    <ind>Peter Miller</ind>
    <ind>min 5000 euro</ind>
    <ind>previous year</ind>
  </atom>
 </_head>
</fact>


<!-- Integrity Constraint: Rule "discount7.5" and rule "discount5.0" are xor (mutal exclusive)-->
<fact>
 <_head>
  <atom>
    <_opr><rel>integrity</rel></_opr>
        <cterm>
        	<_opc><ctor>xor</ctor></_opc>
				<cterm>
			       	<_opc><ctor>discount</ctor></_opc>
	        	      <var>customer</var>
				      <var>product</var>
 				      <ind>5.0 percent</ind>
 				</cterm> 				
				<cterm>
			       	<_opc><ctor>discount</ctor></_opc>
	        	      <var>customer</var>
				      <var>product</var>
 				      <ind>7.5 percent</ind>
 				</cterm>
      </cterm>
  </atom>
 </_head>
</fact>

<!-- Rule "discount7.5" overrides "discount5.0"-->
<fact>
 <_head>
  <atom>
    <_opr><rel>overrides</rel></_opr>
    <ind>discount7.5</ind>
    <ind>discount5.0</ind>
  </atom>
 </_head>
</fact>

<!--  Query 1
Give the discount amounts for all customers buying any products.
-->

<query>
  <_body>
    <atom>
      <_opr><rel>defeasible</rel></_opr>
        <cterm>
        	<_opc><ctor>discount</ctor></_opc>				
		      <var>customer</var>
		      <var>product</var>
		      <var>amount</var>
		</cterm>
    </atom>
  </_body>
</query>


</rulebase>
</rbsla>