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.