需要捕捉选择性STDOUT从Android电子在列表视图显示 [英] Need to Capture selective STDOUT from Android for display in listview

查看:176
本文介绍了需要捕捉选择性STDOUT从Android电子在列表视图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

什么获取显示仅在从推理引擎本身的结果,像Prolog的写陈述(默认)写到标准输出辅助命令。

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.

我需要捕捉在Android变量,它写打印到标准输出的结果来显示给用户。总的想法(这我没有结婚到ListView来实现),主要是模拟命令行交互哪一个会搞,如果他们跑了一个基于Java的Prolog的国米preTER终端接口。

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开发我有一个解决这个问题,我觉得好一点是值得分享瓦特/这个社会。 ..

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...

问题的整体背景下越来越的任意的序言实施的正常的Andr​​oid作为一个基础架构,更有趣的应用程序(专家系统,游戏AI,和自然语言界面)以后下了线。

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.

最大的障碍是的 Prolog是一个控制台基于跨pretative环境的它打印到标准输出,和而Android容忍控制台打印活动,默认情况下它击溃所有这一切都为< STRONG>的/ dev / null的。

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.

因此​​,有两个套​​来解决问题(1)有没有的任意的序言移植到Android的环境,和(2)一个人如何妥善处理问题的捕获控制台输出的时,它的路由为的/ 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.

寻址(1)的:我们对入驻的 tuProlog 网站,谁的官方消息可以发现:的谷歌code库 - tuProlog 。他们已设计序言要埋入由特别为Android一个JAR文件。他们是我们发现谁做的唯一的一个,他们是'敏感'给开发商。他们的东西是开源的Java / Android版,然后他们有一个的序言的Andr​​oid应用程序的用的更新即将到来。 讯问他们的code是无价找到一个妥善的解决办法。

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.

寻址(2)的:它增加了最大的价值,以这项研究的链接是这些:的从PrintStream的阅读将Java的OutputStream到的InputStream ,最终最有用的<一个href="http://stackoverflow.com/questions/565508/convert-a-streamwriter-to-an-outputstream-in-java">StreamWriter到的OutputStream 。

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.

具体而言,有什么需要做的是的:

  • 创建一个 ByteArrayOutputStream 对象捕捉二进制数据 从打印到控制台(的System.out )。
  • 的过程
  • 创建的PrintStream 对象(使用ByteArrayOutputStream)的 其设置在 System.setOut (其管辖那里的控制台输出[的System.out.println ]进入)
  • 重路由系统输出
  • 拍摄所需的控制台打印输出到一个字符串变量
  • 添加该字符串的(在这个Android项目的情况下),以一排列表视图的
  • 通知列表视图的数据发生了变化
  • 重置的ByteArrayOutputStream对象(避免串联)
  • 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)

这里的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 

最后说明: tuProlog考虑到控制台打印问题并设计围绕它这个特殊处理,使用监听器和事件的结合,正确解决的Prolog的捕获命令以及其他。

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.

的解决严格的Prolog查询被细读已在自己的用户指南中的preferred方法来完成很容易的...开发人员可以快速地闪烁着他们需要什么从那个。

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.

它是Prolog的引擎功能,捕捉喜欢的 间谍错误事件是难以确定下来(我最终协商W¯¯ /开发者)。对于你需要询问他们的安卓实施 CUIConsole (而不是他们的控制台实施的 CUIConsole 的,这'是'不同)。

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).

在简单地说,答案是这样的:(一) 建立一个监听器的再(二) prepare事件的发生。

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

这里的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();

}

尾注: 对于那些有兴趣做序言在Android上,我会很乐意提供任何code我写或资源我有,以帮助您沿着这一过程。请不要犹豫,问。

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.

这篇关于需要捕捉选择性STDOUT从Android电子在列表视图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆