假设我有一些普通 CLIPS事实(没有插槽):
(deffacts happyFacts
(happy Peter)
(happy Alex)
(happy Mary))
使用Java API(CLIPSJNI),我可以按如下方式检索这些事实:
MultifieldValue mv = (MultifieldValue) clips.eval("(find-all-facts ((?f happy)) TRUE)");
然后我可以按如下方式检索第一个单独的事实:
FactAddressValue fact = (FactAddressValue) mv.multifieldValue().get(0);
如果这个事实已经命名了插槽,我可以按如下方式获得字段数据:
fact.getFactSlot("someSlot")
但它没有插槽;那么,我如何检索其字段值(在这种情况下,Peter
)?
答案 0 :(得分:1)
对于CLIPSJNI 0.5,修改net_sf_cilpsrules_jni_Environment.c中的Java_net_sf_clipsrules_jni_Environment_getFactSlot以允许将空值作为插槽名称传递:
JNIEXPORT jobject JNICALL Java_net_sf_clipsrules_jni_Environment_getFactSlot(
JNIEnv *env,
jclass javaClass,
jobject javaEnv,
jlong clipsEnv,
jlong clipsFact,
jstring slotName)
{
jobject rv;
DATA_OBJECT theDO;
void *theCLIPSEnv = JLongToPointer(clipsEnv);
const char *cSlotName = NULL;
if (slotName != NULL)
{ cSlotName = (*env)->GetStringUTFChars(env,slotName,NULL); }
void *oldContext = SetEnvironmentContext(theCLIPSEnv,(void *) env);
EnvGetFactSlot(JLongToPointer(clipsEnv),JLongToPointer(clipsFact),(char *) cSlotName,&theDO);
if (slotName != NULL)
{ (*env)->ReleaseStringUTFChars(env,slotName,cSlotName); }
rv = ConvertDataObject(env,javaEnv,theCLIPSEnv,&theDO);
SetEnvironmentContext(JLongToPointer(clipsEnv),oldContext);
return rv;
}
然后,您可以传递空值以返回隐含的插槽:
public static void main(String args[])
{
Environment clips;
clips = new Environment();
clips.build("(deffacts happyFacts " +
" (happy Peter)" +
" (happy Alex)" +
" (happy Mary))");
clips.reset();
clips.eval("(facts)");
MultifieldValue mv = (MultifieldValue) clips.eval("(find-all-facts ((?f happy)) TRUE)");
FactAddressValue fact = (FactAddressValue) mv.multifieldValue().get(0);
try
{
System.out.println("Slot value is " + fact.getFactSlot(null));
}
catch (Exception e)
{
e.printStackTrace();
}
}