RuleML 例子 - own.ruleml

http://ruleml.org/1.0/exa/Datalog/own.ruleml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://www.ruleml.org/1.0/relaxng/datalog_relaxed.rnc"?>
<!--<?xml-model href="http://www.ruleml.org/1.0/xsd/datalog.xsd" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>-->
<RuleML xmlns="http://ruleml.org/spec"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://ruleml.org/spec http://www.ruleml.org/1.0/xsd/datalog.xsd">

  <Assert mapClosure="universal">

    <!-- start XML comment ...

    这个例子的规则库中包含了4个规则.
    第一和第二个规则是推断; 第三和第四个规则是事实.

    In English:

   The first rule implies that a person owns an object 第一个规则推断:一个人拥有一个东西 if that person buys the object from a merchant and the person keeps the object.
   如果这个人从商人手中购买了这个东西,并且最终这个人拥有了这个东西 OrdLab Tree 如下: Implies~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * if * then * * * And~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Atom~~~~~~~~~~~~~~~~~~ | | * | | | | op * | | | | * | | Atom~~~~~~~~~~~~~~~~~~~~~~~~~~~ Atom~~~~~~~~~~~~~~~~~~ Rel Var Var * | | | * | | . . . op * | | | op * | | . . . * | | | * | | . . . Rel Var Var Var Rel Var Var own person object . . . . . . . . . . . . . . . . . . . . . buy person merchant object keep person object ... end XML comment
--> <Implies> <if> <!-- 申明 'And' 联结--> <And> <Atom> <op><Rel>buy</Rel></op> <Var>person</Var> <Var>merchant</Var> <Var>object</Var> </Atom> <Atom> <op><Rel>keep</Rel></op> <Var>person</Var> <Var>object</Var> </Atom> </And> </if> <then> <Atom> <op><Rel>own</Rel></op> <Var>person</Var> <Var>object</Var> </Atom> </then> </Implies> <!-- The second rule implies that a person buys an object from a merchant
    第二个规则的推断:一个人从商人手中购买一个东西 if the merchant sells the object to the person.
    如果商人将这个东西卖给这个人
--> <Implies> <if> <Atom> <op><Rel>sell</Rel></op> <Var>merchant</Var> <Var>person</Var> <Var>object</Var> </Atom> </if> <then> <Atom> <op><Rel>buy</Rel></op> <Var>person</Var> <Var>merchant</Var> <Var>object</Var> </Atom> </then> </Implies> <!-- The third rule is a fact that asserts that
  第三个规则是一个事实:John卖了XMLBible给Mary John sells XMLBible to Mary.
--> <Atom> <op><Rel>sell</Rel></op> <Ind>John</Ind> <Ind>Mary</Ind> <Ind>XMLBible</Ind> </Atom> <!-- The fourth rule is a fact that asserts that
    第四个规则是一个事实:Mary拥有XMLBible Mary keeps XMLBible. Observe that this fact is binary - i.e., there are two arguments for the relation. RDF viewed as a logical knowledge representation is, likewise, binary, although its arguments have type restrictions, e.g., the first must be a resource (basically, a URI).
  我们观察到这个事实是二元的:关系有两个参数。
  RDF被看作是一种合理的知识表现形式,同样的、二元的,虽然它的参数类型有限制,比如第一个必须是资源

  --> <Atom> <op><Rel>keep</Rel></op> <Ind>Mary</Ind> <Ind>XMLBible</Ind> </Atom> </Assert> </RuleML>

这个例子已经可以较完整的解释前面RuleML入门中描述的定义。

如果你感觉上面的定义是不是缺少了什么,那么下面是以上定义的完整体“扩展版本”:

<?xml version="1.0" encoding="utf-8"?>
<RuleML xmlns="http://ruleml.org/spec"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://ruleml.org/spec http://ruleml.org/1.0/xsd/datalog.xsd">

  <act index="1">
    <Assert mapClosure="universal">
      <formula>
        <Implies>
          <!-- explicit 'And' -->
          <if>
            <And>
              <formula>
                <Atom>
                  <op>
                    <Rel>buy</Rel>
                  </op>
                  <arg index="1">
                    <Var>person</Var>
                  </arg>
                  <arg index="2">
                    <Var>merchant</Var>
                  </arg>
                  <arg index="3">
                    <Var>object</Var>
                  </arg>
                </Atom>
              </formula>
              <formula>
                <Atom>
                  <op>
                    <Rel>keep</Rel>
                  </op>
                  <arg index="1">
                    <Var>person</Var>
                  </arg>
                  <arg index="2">
                    <Var>object</Var>
                  </arg>
                </Atom>
              </formula>
            </And>
          </if>
          <then>
            <Atom>
              <op>
                <Rel>own</Rel>
              </op>
              <arg index="1">
                <Var>person</Var>
              </arg>
              <arg index="2">
                <Var>object</Var>
              </arg>
            </Atom>
          </then>
        </Implies>
      </formula>
      <!-- The second rule implies that a person buys an object from a merchant
if the merchant sells the object to the person. -->
      <formula>
        <Implies>
          <if>
            <Atom>
              <op>
                <Rel>sell</Rel>
              </op>
              <arg index="1">
                <Var>merchant</Var>
              </arg>
              <arg index="2">
                <Var>person</Var>
              </arg>
              <arg index="3">
                <Var>object</Var>
              </arg>
            </Atom>
          </if>
          <then>
            <Atom>
              <op>
                <Rel>buy</Rel>
              </op>
              <arg index="1">
                <Var>person</Var>
              </arg>
              <arg index="2">
                <Var>merchant</Var>
              </arg>
              <arg index="3">
                <Var>object</Var>
              </arg>
            </Atom>
          </then>
        </Implies>
      </formula>
      <!-- The third rule is a fact that asserts that
John sells XMLBible to Mary. -->
      <formula>
        <Atom>
          <op>
            <Rel>sell</Rel>
          </op>
          <arg index="1">
            <Ind>John</Ind>
          </arg>
          <arg index="2">
            <Ind>Mary</Ind>
          </arg>
          <arg index="3">
            <Ind>XMLBible</Ind>
          </arg>
        </Atom>
      </formula>
      <!-- The fourth rule is a fact that asserts that
Mary keeps XMLBible.
 
Observe that this fact is binary - i.e., there are two arguments
for the relation. RDF viewed as a logical knowledge representation
is, likewise, binary, although its arguments have type restrictions,
e.g., the first must be a resource (basically, a URI). -->
      <formula>
        <Atom>
          <op>
            <Rel>keep</Rel>
          </op>
          <arg index="1">
            <Ind>Mary</Ind>
          </arg>
          <arg index="2">
            <Ind>XMLBible</Ind>
          </arg>
        </Atom>
      </formula>
    </Assert>
  </act>
</RuleML>

不过这样纯理论规范如何从实验室环境到真实生产环境还有较大距离,接下来的例子将展示一个可以被OO jDREW规则引擎执行的实实在在的例子。

 

 

posted @ 2014-02-03 01:12  完美主义  阅读(554)  评论(0编辑  收藏  举报