首页 > 解决方案 > 如何在 Android/kotlin 中使用 CLIPS 规则引擎获取输出

问题描述

嗨,我是剪辑规则引擎的新手,必须在移动应用程序(Android/Kotlin)中的剪辑中进行跟踪。我们使用 CLIPS4android 库作为 JNI 的包装器。

当通过调用 clips.run() 执行规则时,如何从类文件中获取输出

类 RulesTest(filepath: String?) { private val clips: Environment fun stop() { clips.destroy() }

/**
 * Example of how to use "assert".
 * @throws CLIPSError
 */
@Throws(CLIPSError::class)
fun assertItems(products: List<Item>) {
    for (product in products) {
        Log.d(tag, "(Product " + "(productId " + product.ProductId + ")" + "(uomid " + product.UOMId + " )" + "(quantity " + product.Quantity + " ))")
        var InsertItem: String
        InsertItem = "(Product " + "(productId " + product.ProductId + ")" + "(uomid " + product.UOMId + " )" + "(quantity " + product.Quantity + " ))"
        clips.assertString(InsertItem)
    }
}

fun run() {
    clips.eval("(facts)");
    clips.run()
}

companion object {
    const val tag = "CLIPSProductRulesTest"
}

init {
    clips = Environment()
    clips.load(filepath)
    Log.d(tag, "Loading .clp...\n\n")
    clips.reset()
}

}

标签: androidkotlinrulesclips

解决方案


在 0.4 版本的 CLIPSJNI 中,您可以定义 Router 类的实现来捕获输出,然后您可以在实现的 print 方法中对输出做任何您想做的事情。

import CLIPSJNI.*;

class Example
  {  
   static class CaptureRouter implements Router
     {
      public int getPriority()
       {
        return 10;
       }

      public String getName()
        {
         return "grab";
        }

      public boolean query(
        String logName)
        {    
         if (logName.equals("wwarning") ||
             logName.equals("werror") ||
             logName.equals("wtrace") ||
             logName.equals("wdialog") ||
             logName.equals("wprompt") ||
             logName.equals("wdisplay") ||
             logName.equals("stdout"))
           { return true; }  

         return false;
        }

      public void print(
         String routerName,
         String printString)
         {
          System.out.print(printString);
         }

      public int getchar(
        String routerName)
        {
         return -1;
        }

      public int ungetchar(
        String routerName,
        int theChar)
        {
         return -1;
        }

      public boolean exit(
        int exitCode)
        {     
         return true; 
        }  

     }   

   public static void main(String args[])
     {  
      Environment clips;

      clips = new Environment();
      clips.addRouter(new CaptureRouter());

      clips.build("(defrule hello => (printout t \"Hello World!\" crlf))");
      clips.reset();
      clips.run();
     }  
  }

推荐阅读