Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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...</p> <p>The overall context of the issue is getting <em>ANY</em> 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. </p> <p>The big hurdle is that <em>Prolog is a 'Console' based interpretative environment</em> which prints to STDOUT, and while Android tolerates console printing activities, by default it routs ALL of that to <strong>/dev/null.</strong> </p> <p>So there are <strong><em>two</strong> sets of problems to address</em>: <strong>(1)</strong> Is there <em>ANY</em> Prolog portable to the Android Environment, and <strong>(2)</strong> How does one 'properly' handle the issue of <em>capturing the console output</em> when it's routed to <strong>/dev/null</strong>. </p> <p><em>Addressing (1)</em>: We settled on <strong>tuProlog</strong> <a href="http://alice.unibo.it/xwiki/bin/view/Tuprolog/" rel="nofollow noreferrer" title="[Site]">Site</a>, who's official source can be found: <a href="http://code.google.com/p/tuprolog/" rel="nofollow noreferrer" title="[Code Repository]">Google Code Repository - tuProlog</a>. 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 <strong>Open Source Java/Android</strong> and they have an <em>Android Prolog App</em> out with an update coming soon. <em>Interrogating their code was invaluable to finding a proper solution.</em></p> <p><em>Addressing (2)</em>: The links which added the most value to this research are these: <a href="https://stackoverflow.com/questions/1207281/java-how-do-i-read-from-printstream">Reading from PrintStream</a>, <a href="http://ostermiller.org/convert_java_outputstream_inputstream.html" rel="nofollow noreferrer">Convert Java OutputStream to InputStream</a>, and ultimately the most useful <a href="https://stackoverflow.com/questions/565508/convert-a-streamwriter-to-an-outputstream-in-java">StreamWriter to OutputStream</a>. </p> <p><em>Specifically, what's needed to be done is</em>: </p> <ul> <li>Create a <strong>ByteArrayOutputStream</strong> object to capture the Binary Data from the process of printing to the Console (<strong>System.out</strong>).</li> <li>Create <strong>PrintStream</strong> object (using the ByteArrayOutputStream) in which to set the <strong>System.setOut</strong> (which governs where the console output [<strong>System.out.println</strong>] goes to) </li> <li><strong>Reroute</strong> the System Output</li> <li><em>Capture the desired console print output to a string variable</em> </li> <li><em>Add that string</em> (in the case of this Android project) to a row of a listview</li> <li><em>Notify that Listview</em> the Data has changed</li> <li><strong>Reset</strong> the ByteArrayOutputStream object (to avoid concatenation)</li> </ul> <p><strong>Here's the Code</strong>:</p> <pre><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 </code></pre> <p><strong>Final Notes</strong>: 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 "<strong>Write</strong>" commands as well as other. </p> <p>The solving of <strong>Strict Prolog Queries</strong> is accomplished fairly easily by perusing the preferred methods established in their <em>users guide</em> ... developers can quickly gleam what they need from that. </p> <p>It's the capturing of Prolog Engine functions like the <strong>Write</strong>, <strong>Spy</strong> and <strong>Error</strong> Events that are harder to nail down (I eventually consulted w/the developers). For that you'll need to interrogate their Android Implementation of <strong>CUIConsole</strong> (as opposed to their Console implementation of <em>CUIConsole</em> , which 'is' different). </p> <p>In a nutshell the answer is this: <strong>(a)</strong> <em>establish a Listener</em> and then <strong>(b)</strong> <em>prepare for the event</em> to take place. </p> <p><strong>Here's the code:</strong></p> <pre><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 =&gt; SpyEvent Occured ** " ); System.out.println("** onSpy =&gt; SpyEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onOutput(OutputEvent e) { Log.d(TAG, "** LG'd: onOutput =&gt; OutputEvent Occured ** " ); System.out.println("** onOutput =&gt; OutputEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } @Override public void onWarning(WarningEvent e) { Log.d(TAG, "** LG'd: onWarning =&gt; WarningEvent Occured ** " ); System.out.println("** onWarning =&gt; WarningEvent Occured ** \n "); myChatListItems.add( e.getMsg() ); chatBotAdapter.notifyDataSetChanged(); } </code></pre> <p><strong>End Note:</strong> <em>For those interested in doing</em> <strong>"Prolog on Android"</strong>, 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload