swig fixes for Android compatibility

This commit is contained in:
Vítor Santos Costa
2014-06-22 17:35:05 +01:00
parent 1f301ded89
commit 5b19ccf6a8
36 changed files with 2053 additions and 1861 deletions

View File

@@ -12,6 +12,7 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.content.res.AssetManager;
import android.widget.EditText;
import java.text.ParseException;
public class SwigSimple extends Activity
{
@@ -22,6 +23,39 @@ public class SwigSimple extends Activity
String str;
String buf;
void runQuery(String str)
{
try
{
YAPQuery q = eng.query( str );
YAPListTerm vs0 = q.namedVars();
// text.setText("");
if (vs0.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
int i=1;
while (q.next()) {
YAPListTerm vs = vs0;
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;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
@@ -44,7 +78,7 @@ public class SwigSimple extends Activity
text = (EditText)findViewById(R.id.EditText01);
outputText = (TextView)findViewById(R.id.OutputText);
outputText.setText("Application " + s + "\nPress 'Run' to start...\n");
outputText.setText("Application " + s + "\nPress 'Query' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
eng = new YAPEngine( );
@@ -57,24 +91,24 @@ public class SwigSimple extends Activity
}
}
public void onResetButtonClick(View view)
public void onClearButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onReseButtonClick called");
Log.i(TAG, "onClearButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
text.setText("");
text.setText("");
}
});
}
public void onRunButtonClick(View view)
public void onQueryButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onRunButtonClick called");
Log.i(TAG, "onQueryButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
@@ -82,36 +116,16 @@ public class SwigSimple extends Activity
scroller.fullScroll(ScrollView.FOCUS_DOWN);
str = text.getText().toString();
outputText.append("?- " + str);
YAPQuery q = eng.query( str );
YAPListTerm vs = q.namedVars();
// text.setText("");
if (vs.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
int i=1;
while (q.next()) {
// outputText.append(Integer.toString(i++) + ": " + vs.text() +"\n");
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();
Log.i(TAG, "onQueryButtonClick "+str + "\n");
runQuery(str);
}
});
}
public void onRunSelectionButtonClick(View view)
public void onQuerySelectionButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onRunButtonClick called");
Log.i(TAG, "onQuerySelectionButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
@@ -120,29 +134,9 @@ public class SwigSimple extends Activity
int startSelection = text.getSelectionStart();
int endSelection = text.getSelectionEnd();
str = text.getText().toString().substring( startSelection, endSelection );
outputText.append("?- " + str);
YAPQuery q = eng.query( str );
YAPListTerm vs = q.namedVars();
// text.setText("");
if (vs.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
while (q.next()) {
int i=1;
// outputText.append(Integer.toString(i++) + ": " + vs.text() +"\n");
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();
Log.i(TAG, "onQuerySelectionButtonClick "+str);
outputText.append("?- " + str + "\n");
runQuery(str);
}
});
}

View File

@@ -1,52 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="6" />
<Button
android:id="@+id/RunButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run..."
android:onClick="onRunButtonClick"
/>
<Button
android:id="@+id/RunSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:onClick="onRunSelectionButtonClick"
/>
<Button
android:id="@+id/ClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:onClick="onClearButtonClick"
/>
</LinearLayout>
<ScrollView
android:id="@+id/Scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/OutputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- Set MaxLegth EditText -->
</ScrollView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/EditText01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1.0"
android:lines="6" />
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent">
<Button android:id="@+id/QueryButton" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Query..."
android:onClick="onQueryButtonClick" />
<Button android:id="@+id/QuerySelectionButton"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Select" android:onClick="onQuerySelectionButtonClick" />
<Button android:id="@+id/ClearButton" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Reset"
android:onClick="onClearButtonClick" />
</LinearLayout>
</LinearLayout>
<ScrollView android:id="@+id/Scroller" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/OutputText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:singleLine = "false"/>
<!-- Set MaxLegth EditText -->
</ScrollView>
</LinearLayout>