4
votes

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

1
Excellent Question and one that I faced and fixed .... In short, tuProlog is small, fast (if setup right) and efficient. It's engineered to be exactly that. My app loads the engine, and 2 separate and decent size knowledge bases along w/the Android App it lives in and is .89MB of memory. One think to factor into this is that I implemented a BIG memory hog of a thread to hold 2 Logic bases as well .... most apps may not require that ... There are 2 distinct tricks required in setting up the Prolog Engine and the resulting Thread to make sure it works ... I'd share them w/you if you need them.ProfVersaggi
One more thing ... I used the 2p.jar library which included EVERYTHING and the kitchen sink, including stuff not remotely required for the Android platform. The correct lib is tuprolog.jar contains the core API, the Agent application and default libraries, which I suspect makes it a good bit smaller.ProfVersaggi
Here they are ... (1) Put this in just below the Class definition of your activity so it gets instantiated before the app screen .... // Instantiate an instance of the Prolog Engine. // Do this only once because it's VERY expensive. Prolog engine = new Prolog(); (2) This is how I load a logicbase w/out using an external file ... LoadLogicBase family_LoadLogicBase = new LoadLogicBase(theory_family, "add"); You MUST use a thread and the 100000 parameter is the key: Thread thread_B = new Thread(new ThreadGroup("B"), family_LoadLogicBase, "Load LogicBase" ,100000); thread_B.start();ProfVersaggi
Not really ... 2p.jar handles tolerates that well. The big problem is the console output from the prolog 'write' statements ... Android reroutes all STDOUT to /dev/null so there simply isn't any. I'm researching a workaround for that now but I'm not quite there yet. :-(ProfVersaggi
After many hours of research and questions to the tuProlog Development team I do have a solution to getting the writes to work in the Android system where they route all of the STDOUT to /dev/null. However it's too big to include in a comment so I'll try to answer my own question and craft how to do it in there. In a nutshell the infrastructure of tuProlog takes this issue into account and makes some very elegant adjustments for it in the form of a listener/event pair which used STDOUT but escapes the Android lock down. It is possible to capture N use STDOUT in Android, more on that later.ProfVersaggi

1 Answers

4
votes

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.