Running prologue in Android

I am testing tuProlog on Android. I have Activity TuProlog, a Parser class for interacting with prolog code and data.pl, which contains a prolog. I can run it as a java project with console output, but I ran into an Android project related issue. For Android, I get a FileNotFoundException, although my data.pl file is copied to the project root, inside src and inside my package. I just want to get the result as a string and show the result in a TextView. Here are my codes

public class TuProlog extends Activity implements OnClickListener{ TextView tv; Button b1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.label); b1 = (Button)findViewById(R.id.button1); b1.setOnClickListener(this); } @Override public void onClick(View v) { Parser custom = new Parser(); String result = custom.parse(); tv.setText(result); } } public class Parser { Prolog engine; PrintStream orgStream = System.out; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream psout = new PrintStream(baos, Boolean.TRUE); // Using autoFlush String myResult ; public String parse() { engine = new Prolog(); try{ Theory t = new Theory(new FileInputStream("data.pl")); try{ engine.setTheory(t); try{ SolveInfo answer = engine.solve("likes(john,X)."); try{ Term derivative = answer.getTerm("X"); return myResult;; } catch (NoSolutionException e){ e.printStackTrace(); } catch (UnknownVarException e){ e.printStackTrace(); } } catch (MalformedGoalException e){ e.printStackTrace(); } } catch (InvalidTheoryException e){ e.printStackTrace(); } } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } return null; } @Override public void onSpy(SpyEvent e) { // TODO Auto-generated method stub Log.d("TAG", "** LG'd onSpy => SpyEvent Occured ** " ); System.out.println("** onSpy => SpyEvent Occured ** \n "); myResult = e.getMsg(); } @Override public void onOutput(OutputEvent e) { // TODO Auto-generated method stub Log.d("TAG", "** LG'd: onOutput => OutputEvent Occured ** " ); System.out.println("** onOutput => OutputEvent Occured ** \n "); myResult = e.getMsg(); } @Override public void onWarning(WarningEvent e) { // TODO Auto-generated method stub Log.d("TAG", "** LG'd: onWarning => WarningEvent Occured ** " ); System.out.println("** onWarning => WarningEvent Occured ** \n "); myResult = e.getMsg(); } } 

Data.pl

 likes(john,mary). likes(mary,wine). 

Here is my logcat output, I don't know about System.err

 04-15 18:51:25.480: W/System.err(23813): java.io.FileNotFoundException: /data.pl (No such file or directory) 04-15 18:51:25.484: W/System.err(23813): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 04-15 18:51:25.484: W/System.err(23813): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 04-15 18:51:25.484: W/System.err(23813): at java.io.FileInputStream.<init>(FileInputStream.java:80) 04-15 18:51:25.484: W/System.err(23813): at java.io.FileInputStream.<init>(FileInputStream.java:132) 04-15 18:51:25.484: W/System.err(23813): at com.tuprolog.alicia.Parser.parse(Parser.java:32) 04-15 18:51:25.484: W/System.err(23813): at com.tuprolog.alicia.TuProlog.onClick(TuProlog.java:51) 04-15 18:51:25.484: W/System.err(23813): at android.view.View.performClick(View.java:2485) 04-15 18:51:25.484: W/System.err(23813): at android.view.View$PerformClick.run(View.java:9080) 04-15 18:51:25.484: W/System.err(23813): at android.os.Handler.handleCallback(Handler.java:587) 
+1
android prolog tuprolog
source share
3 answers

I did this before and posted (very, very beta for concept only) the source code for the survey, see below.

To download the source code for the Eclipse (Helios) project, goto: versaggi.biz, download Adrea, the TuProlog Dev project, the Eclipse (Helios) Java Source project, and finally to TuProlog Android Eclipse Porject Source. That should get you started. Please keep in mind that this proof of concept code is ONLY and will be completely rewritten before the final version is released. Given this, it works well enough so that you can get an idea of ​​how I did what I did. If you need help, just contact me directly and I will be happy to help you. :-)

+1
source share

I get a FileNotFoundException, although my data.pl file is copied to the root of the project

But look at the error:

java.io.FileNotFoundException: /data.pl (There is no such file or directory)

He is trying to read data.pl from the root of the file system!

You can try using the FileInputStream(File) constructor for more control over the path. See, for example, this question on how to get the application directory.

+1
source share
  • In eclipse, create the "plFiles" folder in the res folder of your project.
  • Move the PL files to this folder (for example: res / plFiles / data.pl)
  • Use this activity to access this file using (a - current activity)

     InputStream in = a.getResources().openRawResource(R.plFiles.data); 
  • The above line returns an InputStream. Now you can use Scanner or BufferedReader or any reading class to your liking to continue parsing or reading the file.

Another way to access files is mentioned here ... Access to resource files in Android

+1
source share

All Articles