android - 如何在 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()
}
}
解决方案
在 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();
}
}
推荐阅读
- python - 是否可以在 Dash Plotly 下拉菜单中的数据库 (POSTGRES) 中的多个表之间进行选择?
- sql-server - SQL Server 动态 Where 子句 - 通配符、多个值或 1 个值的组合
- html - 格式标题 ng-database/ng-template
- sql - Oracle 对象关系 - 如何在 PLSQL 中获取 REF(SELF)
- node.js - 在 Azure 中运行全栈 Web 应用程序时出现错误(POST 500 Internal Server Error)
- c# - 在 WebView2 中触发 onbeforeunload
- r - GAM 中的张量平滑预测极值
- java - 如何使用 java 将 Int8Array 保存到本地文件
- node.js - Mongoose Populate 返回 ObjectID 而不是内容
- android - 如何在 Glide 监听器中获取 http responseCode?