【问题标题】:How to get output using CLIPS Rule Engine in Android/kotlin如何在 Android/kotlin 中使用 CLIPS 规则引擎获取输出
【发布时间】:2023-03-04 10:00:01
【问题描述】:

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

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

类规则测试(文件路径:字符串?){ 私人 val 剪辑:环境 有趣的停止(){ 剪辑.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()
}

}

【问题讨论】:

  • 不清楚您所说的“类文件输出”是什么意思。
  • @GaryRiley :实际上我们正在使用 CLIPS4android 库在 Android 中运行剪辑,见上面这是我正在使用的类
  • 我可以使用 Clips.run() 函数运行规则。但规则输出仅记录在 logcat 中。没有获取输出的函数。

标签: android kotlin rules clips


【解决方案1】:

在 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();
     }  
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多