| 
									
										
										
										
											2014-07-09 23:28:10 -05:00
										 |  |  | package pt.up.fc.dcc.yap;
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import android.app.Activity;
 | 
					
						
							|  |  |  | import android.os.Bundle;
 | 
					
						
							|  |  |  | import android.view.View;
 | 
					
						
							|  |  |  | import android.widget.TextView;
 | 
					
						
							|  |  |  | import android.widget.ScrollView;
 | 
					
						
							|  |  |  | import android.text.method.ScrollingMovementMethod;
 | 
					
						
							| 
									
										
										
										
											2014-06-04 22:07:37 +01:00
										 |  |  | import android.content.pm.PackageManager;
 | 
					
						
							|  |  |  | import android.content.pm.PackageInfo;
 | 
					
						
							|  |  |  | import  android.content.pm.PackageManager.NameNotFoundException; 
 | 
					
						
							|  |  |  | import  android.util.Log;
 | 
					
						
							|  |  |  | import android.content.res.AssetManager;
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | import 	android.widget.EditText;
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | import 	java.text.ParseException;
 | 
					
						
							| 
									
										
										
										
											2014-07-15 18:57:48 -05:00
										 |  |  | import org.sqlite.database.sqlite.SQLiteDatabase;
 | 
					
						
							|  |  |  | import org.sqlite.database.sqlite.SQLiteStatement;
 | 
					
						
							|  |  |  | import org.sqlite.database.sqlite.SQLiteDatabaseCorruptException;
 | 
					
						
							|  |  |  | import org.sqlite.database.sqlite.SQLiteOpenHelper;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import org.sqlite.database.DatabaseErrorHandler;
 | 
					
						
							|  |  |  | class DoNotDeleteErrorHandler implements DatabaseErrorHandler {
 | 
					
						
							|  |  |  |   private static final String TAG = "DoNotDeleteErrorHandler";
 | 
					
						
							|  |  |  |   public void onCorruption(SQLiteDatabase dbObj) {
 | 
					
						
							|  |  |  |     Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2014-07-09 23:28:10 -05:00
										 |  |  | public class JavaYap extends Activity
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | {
 | 
					
						
							| 
									
										
										
										
											2014-07-09 23:28:10 -05:00
										 |  |  | 	TextView outputText = null;
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	ScrollView scroller = null;
 | 
					
						
							| 
									
										
										
										
											2014-06-04 22:07:37 +01:00
										 |  |  | 	YAPEngine eng = null;
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	EditText text;
 | 
					
						
							|  |  |  | 	String str;
 | 
					
						
							|  |  |  | 	String buf;
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 	void runQuery(String str) 
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		try
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			YAPQuery q = eng.query( str );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 14:20:22 -05:00
										 |  |  | 			if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  | 				Log.i(TAG, "onQueryButtonClick called");
 | 
					
						
							|  |  |  | 			} 	
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 			YAPListTerm vs0 = q.namedVars();
 | 
					
						
							| 
									
										
										
										
											2014-07-15 00:54:45 -05:00
										 |  |  | 			Boolean rc;
 | 
					
						
							| 
									
										
										
										
											2014-07-14 00:03:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 			// text.setText("");
 | 
					
						
							|  |  |  | 			if (vs0.nil()) {
 | 
					
						
							| 
									
										
										
										
											2014-09-19 14:20:22 -05:00
										 |  |  | 				if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  | 					Log.i(TAG, "q=");
 | 
					
						
							|  |  |  | 				} 	
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 				if (q.next()) {
 | 
					
						
							|  |  |  | 					outputText.append( "yes\n" );
 | 
					
						
							|  |  |  | 				} else {
 | 
					
						
							|  |  |  | 					outputText.append( "no\n" );
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			} else {
 | 
					
						
							|  |  |  | 				int i=1;
 | 
					
						
							| 
									
										
										
										
											2014-09-19 14:20:22 -05:00
										 |  |  | 				if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  | 					Log.i(TAG, "q=");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} 	
 | 
					
						
							| 
									
										
										
										
											2014-07-14 00:03:08 -05:00
										 |  |  | 				while (rc = q.next()) {
 | 
					
						
							| 
									
										
										
										
											2014-09-19 14:20:22 -05:00
										 |  |  | 					if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  | 						Log.i(TAG, "q=");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					} 	
 | 
					
						
							|  |  |  | 				YAPListTerm vs = vs0;
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 					while(!vs.nil()){
 | 
					
						
							|  |  |  | 						YAPTerm eq = vs.car();
 | 
					
						
							|  |  |  | 						//outputText.append(Integer.toString(i) + ": " + eq.text() );
 | 
					
						
							|  |  |  | 						outputText.append(Integer.toString(i++) + ":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
 | 
					
						
							|  |  |  | 						vs = vs.cdr();
 | 
					
						
							|  |  |  | 					}	
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			q.close();
 | 
					
						
							|  |  |  | 		} catch(Exception e){
 | 
					
						
							|  |  |  | 			outputText.append("Exception thrown  :" + e);
 | 
					
						
							|  |  |  | 			return;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	/** Called when the activity is first created. */
 | 
					
						
							|  |  |  | 	@Override
 | 
					
						
							|  |  |  | 	public void onCreate(Bundle savedInstanceState)
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		String s = null;
 | 
					
						
							|  |  |  | 		super.onCreate(savedInstanceState);
 | 
					
						
							|  |  |  | 		setContentView(R.layout.main);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		try {
 | 
					
						
							|  |  |  | 			PackageManager m = getPackageManager();
 | 
					
						
							|  |  |  | 			s = getPackageName();
 | 
					
						
							|  |  |  | 			PackageInfo p = m.getPackageInfo(s, 0);
 | 
					
						
							|  |  |  | 			//s = p.applicationInfo.dataDir;
 | 
					
						
							|  |  |  | 			AssetManager mgr = getResources().getAssets();
 | 
					
						
							|  |  |  | 			load(mgr);
 | 
					
						
							|  |  |  | 		} catch(NameNotFoundException e) { 
 | 
					
						
							|  |  |  | 			Log.e(TAG, "Couldn't find package  information in PackageManager", e); 
 | 
					
						
							|  |  |  | 		} 
 | 
					
						
							|  |  |  | 		Log.i(TAG, "mgr=" +mgr); 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		text = (EditText)findViewById(R.id.EditText01); 
 | 
					
						
							|  |  |  | 		outputText = (TextView)findViewById(R.id.OutputText);
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 		outputText.setText("Application " + s + "\nPress 'Query' to start...\n");
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 		outputText.setMovementMethod(new ScrollingMovementMethod());
 | 
					
						
							|  |  |  | 		scroller = (ScrollView)findViewById(R.id.Scroller);
 | 
					
						
							|  |  |  | 		eng = new YAPEngine(   );  
 | 
					
						
							|  |  |  | 		Log.i(TAG, "engine done");
 | 
					
						
							|  |  |  | 		JavaCallback callback = new JavaCallback( outputText );
 | 
					
						
							|  |  |  | 		// set the Java Callback	
 | 
					
						
							|  |  |  | 		eng.setYAPCallback(callback);
 | 
					
						
							|  |  |  | 		if (BuildConfig.DEBUG) {
 | 
					
						
							|  |  |  | 			Log.i(TAG, "callback done");
 | 
					
						
							|  |  |  | 		} 	
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 	public void onClearButtonClick(View view)
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		if (BuildConfig.DEBUG) {
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 			Log.i(TAG, "onClearButtonClick called");
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 		} 	
 | 
					
						
							|  |  |  | 		// Ensure scroll to end of text
 | 
					
						
							|  |  |  | 		scroller.post(new Runnable() {
 | 
					
						
							|  |  |  | 			public void run() {
 | 
					
						
							|  |  |  | 				scroller.fullScroll(ScrollView.FOCUS_DOWN);
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 				text.setText("");	
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		});
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 	public void onQueryButtonClick(View view)
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		if (BuildConfig.DEBUG) {
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 			Log.i(TAG, "onQueryButtonClick called");
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 		} 	
 | 
					
						
							|  |  |  | 		// Ensure scroll to end of text
 | 
					
						
							|  |  |  | 		scroller.post(new Runnable() {
 | 
					
						
							|  |  |  | 			public void run() {
 | 
					
						
							|  |  |  | 				scroller.fullScroll(ScrollView.FOCUS_DOWN);
 | 
					
						
							|  |  |  | 				str = text.getText().toString();
 | 
					
						
							|  |  |  | 				outputText.append("?- " + str);
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 				Log.i(TAG, "onQueryButtonClick "+str + "\n");
 | 
					
						
							|  |  |  | 				runQuery(str);
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		});
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 	public void onQuerySelectionButtonClick(View view)
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 	{
 | 
					
						
							|  |  |  | 		if (BuildConfig.DEBUG) {
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 			Log.i(TAG, "onQuerySelectionButtonClick called");
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 		} 	
 | 
					
						
							|  |  |  | 		// Ensure scroll to end of text
 | 
					
						
							|  |  |  | 		scroller.post(new Runnable() {
 | 
					
						
							|  |  |  | 			public void run() {
 | 
					
						
							|  |  |  | 				scroller.fullScroll(ScrollView.FOCUS_DOWN);
 | 
					
						
							|  |  |  | 				int startSelection = text.getSelectionStart();
 | 
					
						
							|  |  |  | 				int endSelection = text.getSelectionEnd();
 | 
					
						
							|  |  |  | 				str = text.getText().toString().substring( startSelection, endSelection );
 | 
					
						
							| 
									
										
										
										
											2014-06-22 17:35:05 +01:00
										 |  |  | 				Log.i(TAG, "onQuerySelectionButtonClick "+str);
 | 
					
						
							|  |  |  | 				outputText.append("?- " + str + "\n");
 | 
					
						
							|  |  |  | 				runQuery(str);
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		});
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** static constructor */
 | 
					
						
							|  |  |  | 	static {
 | 
					
						
							|  |  |  | 		System.loadLibrary("android");
 | 
					
						
							|  |  |  | 		System.loadLibrary("log");
 | 
					
						
							|  |  |  | 		System.loadLibrary("gmp");
 | 
					
						
							| 
									
										
										
										
											2014-07-15 18:57:48 -05:00
										 |  |  | 		System.loadLibrary("sqliteX");
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 		System.loadLibrary("example");
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	private static native void load(AssetManager mgr);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	private AssetManager mgr;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-09 23:28:10 -05:00
										 |  |  | 	private static final String TAG = "JavaYap";
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class JavaCallback extends YAPCallback
 | 
					
						
							|  |  |  | {
 | 
					
						
							|  |  |  | 	TextView output;
 | 
					
						
							|  |  |  | 	
 | 
					
						
							|  |  |  |   public JavaCallback( TextView outputText )
 | 
					
						
							|  |  |  |   {
 | 
					
						
							|  |  |  |     super();
 | 
					
						
							|  |  |  |     output =  outputText;
 | 
					
						
							|  |  |  |     Log.i(TAG, "java callback init");
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public void run()
 | 
					
						
							|  |  |  |   {
 | 
					
						
							| 
									
										
										
										
											2014-06-19 15:10:11 +01:00
										 |  |  |       Log.i(TAG, "java callback ");
 | 
					
						
							|  |  |  |       System.out.println("JavaCallback.run() ");
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  |   
 | 
					
						
							| 
									
										
										
										
											2014-06-19 15:10:11 +01:00
										 |  |  |   public void run(String s)
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  |   {
 | 
					
						
							| 
									
										
										
										
											2014-06-19 15:10:11 +01:00
										 |  |  |     Log.i(TAG, "java callback ");
 | 
					
						
							|  |  |  |     output.append(s);
 | 
					
						
							| 
									
										
										
										
											2014-06-11 19:31:24 +01:00
										 |  |  |  }
 | 
					
						
							|  |  |  |   
 | 
					
						
							|  |  |  |   private static final String TAG = "JavaCallback";
 | 
					
						
							| 
									
										
										
										
											2014-05-28 00:07:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | }
 |