需要从Android捕获选择性STDOUT以在listview中显示(Need to Capture selective STDOUT from Android for display in list

编程入门 行业动态 更新时间:2024-10-24 05:21:09
需要从Android捕获选择性STDOUT以在listview中显示(Need to Capture selective STDOUT from Android for display in listview)

我在Android中运行了一个嵌入式库[tuProlog(2p.jar)] Prolog推理引擎,带有自定义逻辑库,我可以在Android ListView中成功查询和显示(部分)结果。

显示的内容只是来自推理引擎本身的结果,而不是像Prolog'write'语句这样的辅助命令(默认情况下)写入STDOUT。

我需要在Android变量中将“写入”打印的结果捕获到STDOUT以显示给用户。 一般的想法(我没有与ListView结合实现)是模拟命令行交互,如果他们运行Java Based Prolog Interpreter终端接口,它将参与其中。

我想坚持知识工程的领域,而不是进入系统工程,以完成这个项目,所以我很感激任何有关这个问题的见解。

我的研究引导我作为进一步研究的途径,但系统的东西很快就超越了我的经验。

非常感谢提前....

I'm running the an embedded library [tuProlog (2p.jar)] Prolog Inference Engine in Android with custom logic bases which I can successfully query and display (some) of the results in an Android ListView.

What gets displayed is only the results from the inference engine itself, not ancillary commands like a Prolog 'write' statement which (by default) writes to STDOUT.

I need to capture the result of that 'write' printing to the STDOUT in an Android variable to display to the user. The general idea (which I'm not married to a ListView to implement) is to simulate the command line interaction which one would engage in if they ran a Java Based Prolog Interpreter terminal interface.

I'd like to stick to the realm of Knowledge Engineering rather than entering Systems Engineering in order to complete this project, so I would be grateful for any insights into this problem.

My research has lead me here as an avenue of further study, but the systems stuff quickly gets past my experience.

Big thanks in advance ....

最满意答案

经过我自己的研究和进一步讨论与tuProlog开发人员之后,我有一个解决这个问题的方法,我认为值得与这个社区分享...

该问题的整体背景是让任何 Prolog实现在“正确”Android上作为更有趣的应用程序(专家系统,游戏AI和自然语言接口)的基础架构,以后再开始。

最大的障碍是Prolog是一个基于“控制台”的解释环境 ,可以打印到STDOUT,虽然Android容忍控制台打印活动,但默认情况下它会将所有这些操作路由到/ dev / null。

因此,有两组问题需要解决: (1)是否有任何 Prolog可移植到Android环境,以及(2)当它被路由到/ dev / null时,如何“正确”处理捕获控制台输出的问题。

致辞(1) :我们选择了tuProlog 网站 ,其官方来源可以找到: Google Code Repository - tuProlog 。 他们设计了prolog,可以通过单个JAR文件嵌入,特别是针对Android。 他们是我们发现的唯一一个这样做的人,他们对开发人员“反应敏捷”。 他们的东西是开源Java / Android ,他们有一个Android Prolog应用程序 ,即将推出更新。 询问他们的代码对于找到合适的解决方案是非常宝贵的。

寻址(2) :为本研究增加最多价值的链接是: 从PrintStream读取 , 将Java OutputStream转换为InputStream ,最终将最有用的StreamWriter转换为OutputStream 。

具体来说,需要做的是

创建ByteArrayOutputStream对象以从打印到控制台( System.out )的过程中捕获二进制数据。 创建PrintStream对象(使用ByteArrayOutputStream),在其中设置System.setOut (它控制控制台输出[ System.out.println ]的去向) 重新调整系统输出 将所需的控制台打印输出捕获到字符串变量 将该字符串 (在此Android项目中)添加到listview的一行 通知Listview数据已更改 重置 ByteArrayOutputStream对象(以避免连接)

这是代码

// OutPutStream I/O Experimental Stuff PrintStream orgStream = System.out; // ByteArray Sub Experimental Stuff ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream psout = new PrintStream(baos, Boolean.TRUE); // Using autoFlush // Instantiate an instance of the Prolog Engine. //Do this only once because it's VERY expensive. Prolog engine; // ReRouting Print Streams // (Inside method we need to capture console output) System.setOut(orgStream); // Set the System Output Stream String myResult = baos.toString(); // returns the actual text myChatListItems.add(myResult); // Add Text to New ListView Row chatBotAdapter.notifyDataSetChanged(); // notify the Adapter to Refresh baos.reset(); // Reset the ByteArrayOutputStream System.setOut(orgStream); // RESET the System Output Stream

最后的注释 :tuProlog考虑了控制台打印问题并围绕它设计了这个特定的实现,使用Listeners和Events的组合来正确地处理Prolog“ Write ”命令以及其他命令的捕获。

通过仔细阅读用户指南中建立的首选方法,可以非常轻松地完成Strict Prolog查询的求解......开发人员可以快速了解他们需要的内容。

这是捕获Prolog引擎的功能,比如间谍错误事件更难确定(我最终咨询了开发人员)。 为此,您需要询问他们的CUIConsole的Android实现(而不是他们的CUIConsole的控制台实现,这是'不同')。

简而言之,答案是这样的: (a) 建立一个听众 ,然后(b) 为将要举办的活动做好准备

这是代码:

// Establish Prolog engine and it's appropriate listeners // [Warning, Output, and Spy] engine = new Prolog(); engine.addWarningListener(this); engine.addOutputListener(this); engine.addSpyListener(this); //// PROLOG CONSOLE OUTPUT MECHANISMS ******************************* @Override public void onSpy(SpyEvent e) { Log.d(TAG, "** LG'd onSpy => SpyEvent Occured ** " ); System.out.println("** onSpy => SpyEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onOutput(OutputEvent e) { Log.d(TAG, "** LG'd: onOutput => OutputEvent Occured ** " ); System.out.println("** onOutput => OutputEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onWarning(WarningEvent e) { Log.d(TAG, "** LG'd: onWarning => WarningEvent Occured ** " ); System.out.println("** onWarning => WarningEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); }

结束注意: 对于那些对 “Prolog on Android” 感兴趣的人 ,我非常乐意提供我编写的任何代码或我拥有的资源,以帮助您完成此过程。 请不要犹豫。

After a good bit of my own research and further discussions w/the tuProlog developers I have a solution to this question I think is worth sharing w/this community...

The overall context of the issue is getting ANY Prolog implementation to work on 'properly' Android as a foundational architecture for more interesting Apps (Expert Systems, Game AI, and Natural Language Interfaces) later on down the line.

The big hurdle is that Prolog is a 'Console' based interpretative environment which prints to STDOUT, and while Android tolerates console printing activities, by default it routs ALL of that to /dev/null.

So there are two sets of problems to address: (1) Is there ANY Prolog portable to the Android Environment, and (2) How does one 'properly' handle the issue of capturing the console output when it's routed to /dev/null.

Addressing (1): We settled on tuProlog Site, who's official source can be found: Google Code Repository - tuProlog. They have engineered prolog to be embedded by a single JAR file particularly for Android. They were the only one we found who did it, and they are 'responsive' to developers. Their stuff is Open Source Java/Android and they have an Android Prolog App out with an update coming soon. Interrogating their code was invaluable to finding a proper solution.

Addressing (2): The links which added the most value to this research are these: Reading from PrintStream, Convert Java OutputStream to InputStream, and ultimately the most useful StreamWriter to OutputStream.

Specifically, what's needed to be done is:

Create a ByteArrayOutputStream object to capture the Binary Data from the process of printing to the Console (System.out). Create PrintStream object (using the ByteArrayOutputStream) in which to set the System.setOut (which governs where the console output [System.out.println] goes to) Reroute the System Output Capture the desired console print output to a string variable Add that string (in the case of this Android project) to a row of a listview Notify that Listview the Data has changed Reset the ByteArrayOutputStream object (to avoid concatenation)

Here's the Code:

// OutPutStream I/O Experimental Stuff PrintStream orgStream = System.out; // ByteArray Sub Experimental Stuff ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream psout = new PrintStream(baos, Boolean.TRUE); // Using autoFlush // Instantiate an instance of the Prolog Engine. //Do this only once because it's VERY expensive. Prolog engine; // ReRouting Print Streams // (Inside method we need to capture console output) System.setOut(orgStream); // Set the System Output Stream String myResult = baos.toString(); // returns the actual text myChatListItems.add(myResult); // Add Text to New ListView Row chatBotAdapter.notifyDataSetChanged(); // notify the Adapter to Refresh baos.reset(); // Reset the ByteArrayOutputStream System.setOut(orgStream); // RESET the System Output Stream

Final Notes: tuProlog took into consideration the console printing problem and designed this particular implementation around it, using a combination of Listeners and Events to properly work around the capturing of Prolog "Write" commands as well as other.

The solving of Strict Prolog Queries is accomplished fairly easily by perusing the preferred methods established in their users guide ... developers can quickly gleam what they need from that.

It's the capturing of Prolog Engine functions like the Write, Spy and Error Events that are harder to nail down (I eventually consulted w/the developers). For that you'll need to interrogate their Android Implementation of CUIConsole (as opposed to their Console implementation of CUIConsole , which 'is' different).

In a nutshell the answer is this: (a) establish a Listener and then (b) prepare for the event to take place.

Here's the code:

// Establish Prolog engine and it's appropriate listeners // [Warning, Output, and Spy] engine = new Prolog(); engine.addWarningListener(this); engine.addOutputListener(this); engine.addSpyListener(this); //// PROLOG CONSOLE OUTPUT MECHANISMS ******************************* @Override public void onSpy(SpyEvent e) { Log.d(TAG, "** LG'd onSpy => SpyEvent Occured ** " ); System.out.println("** onSpy => SpyEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onOutput(OutputEvent e) { Log.d(TAG, "** LG'd: onOutput => OutputEvent Occured ** " ); System.out.println("** onOutput => OutputEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onWarning(WarningEvent e) { Log.d(TAG, "** LG'd: onWarning => WarningEvent Occured ** " ); System.out.println("** onWarning => WarningEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); }

End Note: For those interested in doing "Prolog on Android", I'd be very happy to make available any code I write or resource I have in order to help you along this process. Please don't hesitate to ask.

更多推荐

本文发布于:2023-08-01 02:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选择性   STDOUT   Android   listview   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!