swig-java

This commit is contained in:
Vitor Santos Costa 2016-01-07 16:02:08 +00:00
parent 8da8e0e472
commit 780f42210c
4 changed files with 122 additions and 119 deletions

View File

@ -117,13 +117,13 @@ protected:
/// It also communicates the array of arguments t[] /// It also communicates the array of arguments t[]
/// and the array of variables /// and the array of variables
/// back to yapquery /// back to yapquery
YAPPredicate(const char *s, Term &out, Term &vnames ) { YAPPredicate(const char *s, Term &out, Term &names ) {
CACHE_REGS CACHE_REGS
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
Term *outp; Term *outp;
out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &vnames ) ; out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &names ) ;
//extern char *s0; //extern char *s0;
//fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s); //fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
// Yap_DebugPlWrite(out); // Yap_DebugPlWrite(out);
// delete [] ns; // delete [] ns;

View File

@ -299,7 +299,7 @@ const char *YAPTerm::text() {
encoding_t enc = LOCAL_encoding; encoding_t enc = LOCAL_encoding;
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (!(os = Yap_TermToString(t, os, sze, &length, &enc, 0))) { RECOVER_MACHINE_REGS(); if (!(os = Yap_TermToString(Yap_GetFromSlot(t), os, sze, &length, &enc, 0))) { RECOVER_MACHINE_REGS();
return (char *)NULL; return (char *)NULL;
} }
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
@ -460,8 +460,9 @@ YAPQuery::YAPQuery(YAPPredicate p, YAPTerm ts[]): YAPPredicate(p.ap)
YAPListTerm YAPQuery::namedVars() { YAPListTerm YAPQuery::namedVars() {
CACHE_REGS CACHE_REGS
Term o = Yap_GetFromSlot( vnames ); Term o = vnames.term();
return YAPListTerm( o ); Yap_DebugPlWrite(names); printf("<<<<<<<<<<<<<<<<-------------------------\n");
return YAPListTerm( names ); // should be o
} }
bool YAPQuery::next() bool YAPQuery::next()
@ -469,13 +470,18 @@ bool YAPQuery::next()
CACHE_REGS CACHE_REGS
int result; int result;
Yap_DebugPlWrite(vnames.term()); fprintf(stderr,"++++++++++++++\n");
BACKUP_MACHINE_REGS(); BACKUP_MACHINE_REGS();
if (q_open != 1) return false; if (q_open != 1) return false;
if (setjmp(q_env)) if (setjmp(q_env))
return false; return false;
// don't forget, on success these guys must create slots // don't forget, on success these guys must create slots
if (this->q_state == 0) { if (this->q_state == 0) {
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)ap, q_g, &q_h); fprintf(stderr,"+++++ ap=%p +++++++++\n", ap);
Yap_DebugPlWrite(Yap_GetFromSlot(q_g)); fprintf(stderr,"+++++ ap +++++++++\n");
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)ap, q_g, &q_h);
Yap_DebugPlWrite(Yap_GetFromSlot(q_g)); fprintf(stderr,"+++++ ap=%d +++++++++\n", result);
} else { } else {
LOCAL_AllowRestart = q_open; LOCAL_AllowRestart = q_open;
result = (bool)YAP_RetryGoal(&q_h); result = (bool)YAP_RetryGoal(&q_h);
@ -752,7 +758,7 @@ void *YAPPrologPredicate::assertClause( YAPTerm clause, bool last, YAPTerm sourc
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
} }
return tref; return tref;
return 0; return 0;
} }
void* YAPPrologPredicate::retractClause( YAPTerm skeleton, bool all) { void* YAPPrologPredicate::retractClause( YAPTerm skeleton, bool all) {
return 0; return 0;

View File

@ -12,8 +12,9 @@
* uses an SWI-like status info internally. * uses an SWI-like status info internally.
*/ */
class YAPQuery: public YAPPredicate, open_query_struct { class YAPQuery: public YAPPredicate, open_query_struct {
Term vnames; YAPListTerm vnames;
YAPTerm goal; YAPTerm goal;
Term names;
Term t; Term t;
void initOpenQ(); void initOpenQ();
@ -39,8 +40,10 @@ public:
/// ///
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable /// It is given a string, calls the parser and obtains a Prolog term that should be a callable
/// goal. /// goal.
inline YAPQuery(const char *s): YAPPredicate(s, t, vnames) inline YAPQuery(const char *s): YAPPredicate(s, t, names)
{ {
vnames = YAPListTerm( names );
initQuery( t ); initQuery( t );
}; };

View File

@ -4,130 +4,124 @@ import java.io.* ;
//import YAP.* ; //import YAP.* ;
class JavaYAP { class JavaYAP {
YAPQuery q; YAPQuery q;
Boolean running = false, compute = true; YAPEngine eng;
YAPListTerm vs0; InputStreamReader istream = new InputStreamReader(System.in) ;
YAPEngine eng; BufferedReader bufRead = new BufferedReader(istream) ;
String string;
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
void runQuery(String str, Boolean more) Boolean runQuery(String str)
{ {
try try
{ {
// check if at initial query q = eng.query( str );
if (!running) { } catch(Exception e){
System.out.println("Exception thrown :" + e);
q = eng.query( str ); return false;
// get the uninstantiated query variables.
vs0 = q.namedVars();
running = true;
}
// start computing
compute = true;
Boolean rc = true;
// text.setText("");
if (vs0.nil()) {
if (compute && (rc = q.next())) {
System.out.println( "yes\n" );
running = compute = more;
} else {
System.out.println( "no\n" );
running = false;
compute = false;
}
} else {
int i = 0;
while (compute && (rc = q.next()) ) {
YAPListTerm vs = q.namedVars();
while(!vs.nil()){
YAPTerm eq = vs.car();
//outputText.append(Integer.toString(i) + ": " + eq.text() );
System.out.println(Integer.toString(i++));
System.out.println(":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
compute = more;
}
}
if ( !rc) {
q.close();
compute = true;
running = false;
}
} catch(Exception e){
System.out.println("Exception thrown :" + e);
q.close();
compute = true;
running = false;
}
}
public void loop()
{
eng = new YAPEngine( );
JavaCallback callback = new JavaCallback( string );
// set the Java Callback
eng.setYAPCallback(callback);
System.out.println("Welcome To a simple Java YAP Program");
try {
System.out.println("Query? ");
while ((string = bufRead.readLine()) != null) {
if (running) {
if (string == ";") {
runQuery(string, true);
} else
break;
} else {
runQuery(string, false);
}
if (running)
System.out.print("More Solutions enter ';' ");
else
System.out.println("Query? ");
}
}
catch (IOException err) {
System.out.println("Error reading line");
}
} }
return continueQuery();
}
Boolean continueQuery()
{
try
{
Boolean rc;
rc = q.next();
if (!rc) {
q.close();
return false;
}
YAPListTerm vs = q.namedVars();
System.out.println("Another one "+vs);
while(!vs.nil()){
YAPTerm eq = vs.car();
//outputText.append(Integer.toString(i) + ": " + eq.text() );
System.out.println(":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
return true;
} catch(Exception e){
System.out.println("Exception thrown :" + e);
q.close();
return false;
}
}
public void top_query()
{
eng = new YAPEngine( );
JavaCallback callback = new JavaCallback( "The man walked on the moon" );
// set the Java Callback
eng.setYAPCallback(callback);
System.out.println("Welcome To a simple Java YAP Program");
try {
String string;
Boolean rc;
System.out.println("Query? ");
// Get Query
while ((string = bufRead.readLine()) == null) {
// skip empty lines
}
// first solution
rc = runQuery(string);
// no first solution, exit loop
if (!rc) {
System.out.println("No Dice, but Better Luck Next Time ");
return;
}
// alreadyone solution
while (true) {
System.out.print("More Solutions enter ';' ");
while ((string = bufRead.readLine()) == null) {
}
if (string.charAt(0) != ';' )
return;
rc = continueQuery();
if (!rc)
return;
}
}
catch (IOException err) {
System.out.println("Error reading line");
}
}
public static void main(String args[]) public static void main(String args[])
{ {
System.loadLibrary("/Users/vsc/Yap/bins/t/packages/swig/java/libNative"); System.loadLibrary("Native");
JavaYAP y = new JavaYAP(); JavaYAP y = new JavaYAP();
y.loop();
System.out.println("Thanks for trying a simple Java YAP Program");
while( true ) {
y.top_query();
} }
// System.out.println("Thanks for trying a simple Java YAP Program");
}
} }
class JavaCallback extends YAPCallback class JavaCallback extends YAPCallback
{ {
String callbacks; String callbacks;
public JavaCallback( String outputText ) public JavaCallback( String outputText )
{ {
super(); super();
callbacks += outputText; callbacks += outputText;
} }
public void run(String s) public void run(String s)
{ {
//callbacks.append(s); //callbacks.append(s);
} }
private static final String TAG = "JavaCallback"; private static final String TAG = "JavaCallback";
} }