<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN" [
 <!ENTITY copyrightDates '1999, 2000, 2001'>
 <!ENTITY % METACOSM SYSTEM "../en.metacosm.ent">
 %METACOSM;
 ]>

<article lang="EN">
  <articleinfo>
    <title>Predicates RFC</title>
    <corpauthor>&author;</corpauthor>
    <revhistory>    
      <revision>
        <revnumber>M1</revnumber>   
        <date>July, 15th 2001</date>      
        <revremark>First version</revremark>
      </revision>
    </revhistory>
    <abstract>
      <simpara>This document is meant to replace
        Conditional Expressions RFC by refining and extending the
        initial concepts.</simpara>
    </abstract>
  </articleinfo>
  
    &license;
  
    &project;

  <sect1>
    <title>Goals</title>
    <para>The goal of this RFC is to define the concepts needed to the
      implementation of a generic and extensible Predicate system. This
      predicate system is meant to be used extensively by Metacosm's
      interaction engine, in particular for dynamic Actions.
    </para>
    <para>We present a possible solution from an implementation point
      of view.
    </para>
  </sect1>

  <sect1>
    <title>Requirements</title>
    <para>In the context of the interaction engine, we need to be able
      to define conditions guarding modifications on Entities. More
      specifically, we would like to be able to prevent the execution
      of an Action if the performing Entity is not strong enough, as
      an example.</para>
    <para>We'd also like to be able to define conditions that direct
      the execution path of Actions. If we consider those as
      state machines, we want to be able to guard transitions
      depending on conditions such as the result of a precedent
      Action.
    </para>
  </sect1>
  
  <sect1><title>Definitions</title>
    
    <glosslist>
      <glossentry id="predicate">
        <glossterm>Predicate</glossterm>
        <glossdef>
          <para>Something that is affirmed or denied of the subject in
            a proposition in logic. (Merriam-Webster's Collegiate
            Dictionary)</para>
        </glossdef>
        <glossdef>
          <para><emphasis>In Metacosm:</emphasis> An object evaluating
            a predicate on a set of Entities before to determine if
            manipulation on them are allowed. If an Entity manipulation
            (Action) is guarded by a Predicate, it can only be executed
            if and only if the guarding Predicate is validated (i.e., it
            is evaluated as true). A Predicate defines conditions to be
            verified on a set of Entities. Predicates are implemented as
            Command (design pattern) objects. As such, they can be
            serialized and reused in lots of different contexts without
            needed to be described again.
          </para>
        </glossdef>
      </glossentry>
      <glossentry id="predicate-attribute">
        <glossterm>Attribute (for a Predicate)</glossterm>
        <glossdef>
          <para>An inherent characteristic. (Merriam-Webster's Collegiate
            Dictionary)</para>
        </glossdef>
        <glossdef>
          <para><emphasis>In Metacosm:</emphasis> An attribute is a
            characteristic associated to a 
            <glossterm linkend="predicate">Predicate</glossterm>
            against which the particular aspect of the 
            <glossterm linkend="predicate">Predicable</glossterm>
            tested by this Predicate, is evaluated.
          </para>
        </glossdef>
      </glossentry>
      <glossentry id="predicable">
        <glossterm>Predicable</glossterm>
        <glossdef>
          <para>Something that may be predicated. (Merriam-Webster's Collegiate
            Dictionary)</para>
        </glossdef>
        <glossdef>
          <para><emphasis>In Metacosm:</emphasis> An object on which
            <glossterm linkend="predicate">Predicates</glossterm>
            can operate.</para>
        </glossdef>
      </glossentry>
      <glossentry id="operator">
        <glossterm>Operator</glossterm>
        <glossdef>
          <para><emphasis>In Metacosm:</emphasis> An object able to
            manipulate
            <glossterm linkend="predicate">Predicates</glossterm>. 
            In particular, an object able to combine several
            Predicates into one.</para>
        </glossdef>
      </glossentry>
    </glosslist>    
  </sect1>

  <sect1>
    <title>Description</title>
    <para>A Predicate is defined as:
      <itemizedlist>
        <listitem><simpara>a mnemonic name</simpara></listitem>
        <listitem><simpara>a human-readable description</simpara></listitem>
        <listitem><para>a formal definition</para></listitem>
      </itemizedlist></para>    

    <para>A Predicate is identified by its <emphasis>mnemonic
        name</emphasis> which is used to refer to a given Predicate. A
      Predicate's mnemonic is unique throughout a given Metacosm
      instance. Mnemonics are user- and editor-friendly since they
      describe the Predicate in a very concise way.</para>

    <para>The <emphasis>human-readable description</emphasis> expands
      on the mnemonic and precise what the Predicate is about.</para>
    
    <para>The <emphasis>formal definition</emphasis> specifies in an
      unambiguous way (i.e. using a well defined grammar) what condition
      the Predicate checks. See the Predicate grammar section for more
      details.</para>
  </sect1>

  <sect1>
    <title>Categories of Predicates</title>
    <para>Next, we categorize the different types of Predicates that
      we have identified.</para>
    <sect2>
      <title>Special Predicates</title>
      <sect3>
        <title>True Predicate</title>       
        <para>This Predicate is always evaluated to true.</para>
      </sect3>
      <sect3>
        <title>False Predicate</title>        
        <para>This Predicate is always evaluated to false.</para>
      </sect3>
    </sect2>
    <sect2>
      <title>Unary Predicates</title>
      <sect3>
        <title>Is Predicate</title>       
        <para>evaluated to true if the current Predicable is an Entity
          and has the same identity has the one defined by the
          Predicate's attribute.</para>
      </sect3>
      <sect3>
        <title>InfluencedBy Predicate</title>       
        <para>This Predicate is evaluated to true if the current
          Predicable is influenced by the Influence identified by the
          Predicate's attribute.</para>
      </sect3>
      <sect3>
        <title>Possess Predicate</title>        
        <para>This Predicate is evaluated to true if the Predicable is
          in possession of the Entity identified by the Predicate's
          attribute.</para>
      </sect3>
      <sect3>
        <title>CanPerform Predicate</title>       
        <para>This Predicate is evaluated to true if the Predicable
          can perform the Action identified by the Predicate's
          attribute.</para>
      </sect3>
    </sect2>
    <sect2>
      <title>Binary Predicates</title>
      <para>Binary Predicates are more complex Predicates which
        attributes are composed by a couple of values, named first and
        second argument respectively. Next, we detail binary
        Predicates.</para>
      <sect3>
        <title>LesserThan Predicate</title>
        <segmentedlist>
          <segtitle>First argument</segtitle>
          <segtitle>Second argument</segtitle>
          <seglistitem>
            <seg>Property name</seg>
            <seg>Threshold value</seg>
          </seglistitem>
        </segmentedlist>
        <para>
          This Predicate is evaluated to true if the current
          Predicable has a Property named like the first
          argument and this Property has a value that is lesser than
          the value given by the second argument.</para>
      </sect3>
      <sect3>
        <title>GreaterThan Predicate</title>
        <segmentedlist>
          <segtitle>First argument</segtitle>
          <segtitle>Second argument</segtitle>
          <seglistitem>
            <seg>Property name</seg>
            <seg>Threshold value</seg>
          </seglistitem>
        </segmentedlist>
        <para>
          This Predicate is evaluated to true if the current
          Predicable has a Property named like the first
          argument and this Property has a value that is greater than
          the value given by the second argument.</para>
      </sect3>
      <sect3>
        <title>EqualsTo Predicate</title>
        <segmentedlist>
          <segtitle>First argument</segtitle>
          <segtitle>Second argument</segtitle>
          <seglistitem>
            <seg>Property name</seg>
            <seg>Threshold value</seg>
          </seglistitem>
        </segmentedlist>
        <para>
          This Predicate is evaluated to true if the current
          Predicable has a Property named like the first
          argument and this Property has a value that is equals to
          the value given by the second argument.</para>
      </sect3>
      <sect3>
        <title>KnowsAbout Predicate</title>
        <segmentedlist>
          <segtitle>First argument</segtitle>
          <segtitle>Second argument</segtitle>
          <seglistitem>
            <seg>Entity identifier</seg>
            <seg>Level of knowledge</seg>
          </seglistitem>
        </segmentedlist>
        <para>
          This Predicate is evaluated to true if the current
          Predicable knows about the Entity identified by the first
          argument with a level given by the second argument.</para>
      </sect3>
      <sect3>
        <title>RelatesTo Predicate</title>
        <segmentedlist>
          <segtitle>First argument</segtitle>
          <segtitle>Second argument</segtitle>
          <seglistitem>
            <seg>Entity identifier</seg>
            <seg>Type of relation</seg>
          </seglistitem>
        </segmentedlist>
        <para>
          This Predicate is evaluated to true if the current
          Predicable is related to the Entity identified by the first
          argument by a link given by the second argument.</para>
      </sect3>
    </sect2>
  </sect1>

  <sect1><title>Predicate grammar</title>
  
    <para>We define a Predicate grammar that describes how Predicates
      can be combined to form more elaborate Predicates.
      <programlisting>
        predicate -> orPredicate
        orPredicate -> andPredicate <emphasis>or</emphasis>
                       orPredicate
        orPredicate -> andPredicate
        andPredicate -> simplePredicate <emphasis>and</emphasis> 
                        andPredicate
        andPredicate -> simplePredicate
        simplePredicate -> <emphasis>(</emphasis> orPredicate <emphasis>)</emphasis>
        simplePredicate -> notPredicate
        notPredicate -> <emphasis>not</emphasis> simplePredicate        
      </programlisting>
      <note>
        <simpara>Terminals are displayed like so: 
          <emphasis>terminal</emphasis>
        </simpara>
      </note>
      <note>
        <simpara>Note that this context-free grammar defines a
          precedence on rules. In the preceeding grammar,
          <emphasis>and</emphasis> has a higher precedence than
          <emphasis>or</emphasis>.
        </simpara>
      </note>
    </para>
    <para>Using this grammar, it is possible to define fairly complex
      combinations of Predicates that can describe complex
      pre-conditions. This set of Predicates is evaluated to a single
      Predicate according to the grammar rules.
    </para>
  </sect1>

  <sect1>
    <title>Examples</title>
    <note>
      <simpara>In the following, examples are title after their
        <emphasis>description</emphasis> (see Description section).
      </simpara>
    </note>
    <example>
      <title>Guards with a sword</title>
      <para>
        <segmentedlist>
          <segtitle>Mnemonic</segtitle>
          <segtitle>Definition</segtitle>
          <seglistitem>
            <seg>guard-sword</seg>
            <seg>(InfluencedBy Guard) and (Possess Sword)</seg>
          </seglistitem>
        </segmentedlist>
      </para>
    </example>
    <example>
      <title>Non-elven guards with a axe, knowing Raoul the baker</title>
      <para>
        <segmentedlist>
          <segtitle>Mnemonic</segtitle>
          <segtitle>Definition</segtitle>
          <seglistitem>
            <seg>guard-axe</seg>
            <seg>(InfluencedBy Guard) and not (InfluencedBy Elf) and (Possess Axe) and (KnowsAbout "Raoul the baker" a_little)</seg>
          </seglistitem>
        </segmentedlist>
      </para>
    </example>
    <example>
      <title>Able to bribe a sergeant</title>
      <para>
        <segmentedlist>
          <segtitle>Mnemonic</segtitle>
          <segtitle>Definition</segtitle>
          <seglistitem>
            <seg>able-bribe-sergeant</seg>
            <seg>(CanPerform bribe) and (GreatherThan bribe_skill 50%)</seg>
          </seglistitem>
        </segmentedlist>
      </para>
    </example>
  </sect1>
  
  <sect1><title>Implementation</title>
    
    <para>The generic form a 
      <glossterm linkend="predicate">Predicate</glossterm> is
      materialized by a generic interface that allows Predicates to be
      evaluated. Since Predicates can be used to guard the execution
      of Actions, their evaluation must take at least the same
      parameters defined by the Action interface. We thus define the
      following interface:
      <programlisting>
        public interface Predicate {
          boolean evaluate(Entity[] sources, 
          Entity[] targets,
          Object[] parameters);
        }
      </programlisting>
    </para>
    
    <para>This interface is associated with 
      <glossterm linkend="predicable">Predicable</glossterm>
      objects. This interface defines the methods needed by 
      <glossterm linkend="predicable">Predicable</glossterm> and
      <glossterm linkend="predicate">Predicate</glossterm> objects to
      communicate efficiently. Obvious Predicables are Actions
      but they are not restricted to these. We can also imagine
      that spells, Quests, IAs or other concepts could be implemented as
      Predicables.
      <programlisting>(to be defined)</programlisting>
    </para>
    
  </sect1>
  
</article>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-namecase-general:t
sgml-general-insert-case:lower
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:2
sgml-indent-data:t
sgml-parent-document:nil
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->