CLIPS - EnvAssertString vs EnvAssert

时间:2017-06-06 18:59:40

标签: c++ clips expert-system

EnvAssertEnvAssertString之间的功能是否存在差异? 我尝试使用两者来断言事实,但是当我使用EnvAssertString断言事实时相关规则被激活,并且当我使用EnvAssert断言事实时未激活相关规则!

这是我在constructs.clp文件中的定义:

(deftemplate Safety
    (slot SafetyStatus (type SYMBOL) (default True) (allowed-symbols True False))
    (slot DistanceToObject (type INTEGER) (default 0))
    (slot BatteryLevel (type INTEGER) (default 0)))

(defrule r11
    ?f <- (Safety (SafetyStatus True) (BatteryLevel ?bat) (DistanceToObject ?dist))
    (test (and (> ?bat 20) (> ?dist 3)))
    => 
    (modify ?f (SafetyStatus False)))

这是我的C ++代码段:

// The rule is activated if I use this line
EnvAssertString(theEnv, "(Safety (BatteryLevel 100)(DistanceToObject 4))");

// The rule is not activated if I used these lines
templatePtr = EnvFindDeftemplate(theEnv, "Safety");
newFact = EnvCreateFact(theEnv, templatePtr);
if (newFact == NULL) return -1;

theValue.type = INTEGER;
theValue.value = EnvAddLong(theEnv, 100);
EnvPutFactSlot(theEnv, newFact, "BatteryLevel", &theValue);

theValue.type = INTEGER;
theValue.value = EnvAddLong(theEnv, 4);
EnvPutFactSlot(theEnv, newFact, "DistanceToObject", &theValue);

EnvAssert(theEnv, newFact);

EnvRun(theEnv, -1L);

调用EnvAssertString断言以下内容:

==> f-1     (Safety (SafetyStatus nil) (DistanceToObject 4) (BatteryLevel 100))

在调用EnvAssert时断言以下内容:

==> f-1     (Safety (SafetyStatus True) (DistanceToObject 4) (BatteryLevel 100))

有人可以告诉我问题在哪里吗?

1 个答案:

答案 0 :(得分:0)

AssertString是Assert的包装器。基本上它解析字符串参数,创建适当类型的事实,分配指定的槽值,将默认值分配给未指定的槽,然后断言事实。在使用EnvAssert的示例中,您需要调用&#34; EnvAssignFactSlotDefaults(theEnv,newFact);&#34;以便将默认值分配给未指定的插槽。这将导致将值True分配给SafetyStatus插槽并允许r11规则匹配。