如何编写JESS defrule"如果列表中不存在值,则打印出警告"

时间:2016-07-21 23:01:22

标签: conditional rule not-exists expert-system jess

我是JESS的新手,并且在Java中适度。

我有' cList'作为特殊商品清单(包括'必要'和'区域'商品), 和' xList'作为所有商品的清单(包括'必要'和'非必要'商品) 我想制定一个规则:如果' x'来自' xList'状态'必要'在' cList'中不存在然后打印"缺少基本商品' x'。#34;

要清楚,我的系统模式与Jess71p2中的pricing_engine示例相同。

  • cList与CatalogItems相同
  • xList与OrderItems相同

我从Java类声明deftemplate。 这是反击;

    (import com.aisy.entity.*)
    (deftemplate oldList(declare (from-class oldList)))
    (deftemplate xList  (declare (from-class xList)))
    (deftemplate cList  (declare (from-class cList)))

这里是xList.java

    package com.aisy.entity;
    import java.sql.Timestamp;
    public class xList {
            private Long id_commodity;
            private String commodity;
            private String essentiality;
            private Timestamp update;

        public xList() {
            super();
        }

        public xList(Long id_commodity, String commodity, String essentiality, Timestamp update) {
            super();
            this.id_commodity = id_commodity;
            this.commodity = commodity;
            this.essentiality = essentiality;
            this.update = update;
        }

        public Long getId_commodity() {
            return this.id_commodity;
        }

        public void setId_Commodity(Long id_commodity) {
            this.id_commodity = id_commodity;
        }

        public String getCommodity() {
            return this.commoditty;
        }

        ....getter and setter methods for the rest fields
    }

这里是cList.java;

    public class cList {
        private Long id_commodity;
        private String commodity;
        private String price;
        private Double final_price;

        public cList(Long id_commodity, String commodity, String price, Double final_price) {
            super();
            this.id_commodity = id_commodity;
            this.commodity = commodity;
            this.price = price;
            this.final_price = final_price;
        }

        public cList() {
            super();
        }

            ...common getter and setter methods for all fields

这里是warning.java;

    public class warning {
        private final String description;
        public warning(String aDescription) {
        description = aDescription;
        }

        public String getDescription() {
        return description;
        }
    }

这是规则;

    (defrule 
       (xList (commodity ?essential_commodity) (essentiality /essential/))
       (not(cList (commodity ?essential_commodity)))
        => (add ( new warning "Missing essential commodity" ?essential_commodity))
    )

我尝试将一些信息放到Database for xList和cList:

    xList("100000", "PEANUT", "2.45");
    xList("100003", "BREAD", "1.5");

    cList("100000", "PEANUT", "ESSENTIAL");
    cList("100002", "CAKE", "ESSENTIAL");
    cList("100003", "BREAD", "NON ESSENTIAL" );

由此,如果我运行我的规则引擎,结果应为

   "Missing essential commodity 100002"

但我目前的结果是:

    "Missing essential commodity 100000"
    "Missing essential commodity 100002"

如何解决此问题?

  

编辑:07/22/2016添加了xList.java和cList.java

2 个答案:

答案 0 :(得分:0)

确切的语法当然取决于您匹配的事实的确切性质。下面的规则是假设有很多" xList"事实,每个都列出了商品的名称及其在商品中的重要性"和" essentiality"插槽,分别只有一个cList事实,其中包含所有必需商品的列表。空白多字段(" $?")在基本商品之前和之后匹配任意数量的列表元素。

(defrule 
   (xList (commodity ?essential_commodity) (essentiality essential))
   (not (cList $? ?essential_commodity $?))
    =>
   (assert (warning
       (str-cat "Missing essential commodity" ?essential_commodity))))

答案 1 :(得分:0)

欧内斯特几乎在那里:

(defrule 
   (xList (commodity ?essential_commodity) (essentiality essential))
   (not (cList (commodity ?essential_commodity)))
=>
   (assert (warning (description ?essential_commodity)))
)

注意:调用不是列表但甚至不包含的类“-List”令人不安(并且确实让Ernest感到困惑)。类应该被称为somethingCommodity,字段名称可能被限制为“id”,“name”等。