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[]
/// and the array of variables
/// back to yapquery
YAPPredicate(const char *s, Term &out, Term &vnames ) {
YAPPredicate(const char *s, Term &out, Term &names ) {
CACHE_REGS
BACKUP_MACHINE_REGS();
Term *outp;
out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &vnames ) ;
//extern char *s0;
out = Yap_StringToTerm(s, strlen(s)+1, &LOCAL_encoding, 1200, &names ) ;
//extern char *s0;
//fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
// Yap_DebugPlWrite(out);
// delete [] ns;

View File

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

View File

@ -12,8 +12,9 @@
* uses an SWI-like status info internally.
*/
class YAPQuery: public YAPPredicate, open_query_struct {
Term vnames;
YAPListTerm vnames;
YAPTerm goal;
Term names;
Term t;
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
/// goal.
inline YAPQuery(const char *s): YAPPredicate(s, t, vnames)
inline YAPQuery(const char *s): YAPPredicate(s, t, names)
{
vnames = YAPListTerm( names );
initQuery( t );
};

View File

@ -4,130 +4,124 @@ import java.io.* ;
//import YAP.* ;
class JavaYAP {
YAPQuery q;
Boolean running = false, compute = true;
YAPListTerm vs0;
YAPEngine eng;
String string;
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
YAPQuery q;
YAPEngine eng;
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
void runQuery(String str, Boolean more)
{
try
{
// check if at initial query
if (!running) {
q = eng.query( str );
// 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");
}
Boolean runQuery(String str)
{
try
{
q = eng.query( str );
} catch(Exception e){
System.out.println("Exception thrown :" + e);
return false;
}
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[])
{
System.loadLibrary("/Users/vsc/Yap/bins/t/packages/swig/java/libNative");
public static void main(String args[])
{
System.loadLibrary("Native");
JavaYAP y = new JavaYAP();
y.loop();
System.out.println("Thanks for trying a simple Java YAP Program");
JavaYAP y = new JavaYAP();
while( true ) {
y.top_query();
}
// System.out.println("Thanks for trying a simple Java YAP Program");
}
}
class JavaCallback extends YAPCallback
{
String callbacks;
String callbacks;
public JavaCallback( String outputText )
{
super();
callbacks += outputText;
}
public JavaCallback( String outputText )
{
super();
callbacks += outputText;
}
public void run(String s)
{
//callbacks.append(s);
}
public void run(String s)
{
//callbacks.append(s);
}
private static final String TAG = "JavaCallback";
private static final String TAG = "JavaCallback";
}