4

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

manlio
  • 18,345
  • 14
  • 76
  • 126
ProfVersaggi
  • 886
  • 13
  • 18
  • Glad I found your post. How much memory does tuProlog consume when it's running? I know the size of a consulted knowledge base will affect that number drastically, so I'm asking what the "base" memory consumption is before a significant consult takes place. – Robert Oschler Jan 14 '12 at 04:08
  • 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 Jan 15 '12 at 10:16
  • 1
    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 Jan 15 '12 at 12:11
  • Thanks for the tips. I downloaded 2p.jar when I first saw your post. Yes I'd like to know the setup tricks for the Prolog Engine. – Robert Oschler Jan 15 '12 at 13:37
  • 1
    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 Jan 15 '12 at 17:29
  • If you need anything else, just ask, I'll send you what I can if it'll help out .... – ProfVersaggi Jan 15 '12 at 17:30
  • Great, thanks. Anything to worry about when it comes to Android pausing/resuming the Activity or saving/restoring Activity data variables? – Robert Oschler Jan 15 '12 at 17:31
  • 1
    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 Jan 15 '12 at 19:51
  • if solve the STDOUT issue, please post so we can learn about it. Thanks again. – Robert Oschler Jan 15 '12 at 21:31
  • 1
    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 Jan 17 '12 at 23:16
  • Sound great. I look forward to seeing your answer. – Robert Oschler Jan 18 '12 at 03:50

1 Answers1

4

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.

Community
  • 1
  • 1
ProfVersaggi
  • 886
  • 13
  • 18
  • Hi ProfVersaggi can you please share a simple Prolog+android project. I read through this thread and implemented what you have written but still not getting any output. Here is my simple test project fo tuProlog in Android but I am getting System.err: FileNotFoundException. Link: http://stackoverflow.com/questions/10166942/running-prolog-in-android , using Listeners is not working either , may be I am using listeners at wrong place. I need tuProlog to export my NL interface for an app. – Yogesh Apr 16 '12 at 00:21
  • How about I post the source to my Android Dev site for you to download? Would that be useful? – ProfVersaggi Apr 22 '12 at 11:09
  • 1
    I'll try to do that as soon as possible, with a bit of luck I'll get it up by tomorrow and post a link to the site here. Stay tuned! ;-) – ProfVersaggi Apr 22 '12 at 11:39
  • 1
    To download the Eclipse (Helios) project Source Code, goto: http://www.versaggi.biz, Downloads Adrea, TuProlog Dev Project, Eclipse (Helios) Java Source Project, and finally to TuProlog Android Eclipse Porject Source. That should get you started. Please keep in mind that this is proof of concept code ONLY and is will be completely rewritten before the final version is released. Given that, it does work well enough for you to get some insights into how I did what I did. If you want any assistance at all just contact me directly and I'll be glad to help you along. :-) – ProfVersaggi Apr 24 '12 at 06:34