int main(int argc, char *argv[])
{ 

#ifdef MULTI_THREAD
  static th_context *p_th, *r_th;
#endif

  char init_string[MAXPATHLEN];
  int rcp, rcr;
  XSB_StrDefine(p_return_string);
  XSB_StrDefine(r_return_string);

  /* xsb_init_string relies on the calling program to pass the absolute or relative
     path name of the XSB installation directory. We assume that the current
     program is sitting in the directory ../examples/c_calling_xsb/
     To get installation directory, we strip 3 file names from the path. */
 
  strcpy(init_string,strip_names_from_path(xsb_executable_full_path(argv[0]),3));

  if (xsb_init_string(init_string)) {
    fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_error_type(xsb_get_main_thread()),
	    xsb_get_error_message(xsb_get_main_thread()));
    exit(XSB_ERROR);
  }

  p_th = xsb_get_main_thread();

  /* Create command to consult a file: edb.P, and send it. */
  if (xsb_command_string(p_th, "consult('edb.P').") == XSB_ERROR)
    fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(p_th),
	    xsb_get_error_message(p_th));

  xsb_ccall_thread_create(p_th,&r_th);

 if (xsb_command_string(r_th,"import thread_exit/0 from thread.") == XSB_ERROR)
    fprintf(stderr,"++Error exiting r: %s/%s\n",xsb_get_error_type(r_th),
	    xsb_get_error_message(r_th));

  rcp = xsb_query_string_string(p_th,"p(X,Y,Z).",&p_return_string,"|");
  rcr = xsb_query_string_string(r_th,"r(X,Y,Z).",&r_return_string,"|");

  while (rcp == XSB_SUCCESS && rcr == XSB_SUCCESS) {

    printf("Return p %s\n",(p_return_string.string));
    rcp = xsb_next_string(p_th, &p_return_string,"|");

    printf("Return r %s\n",(r_return_string.string));
    rcr = xsb_next_string(r_th, &r_return_string,"|");
  }

 if (rcp == XSB_ERROR) 
   fprintf(stderr,"++Query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th));
 if (rcr == XSB_ERROR) 
   fprintf(stderr,"++Query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th));

 // xsb_kill_thread(r_th);

 xsb_close(xsb_get_main_thread());      /* Close connection */
  return(0);
}
Example #2
0
/*******************************************************************
Error-throwing commands
*********************************************************************/
void *command_ps_err(void * arg) {
  int rc;
  th_context *p_th;
  p_th = (th_context *)arg;
  
  if ((rc = xsb_command_string(p_th,"test_p_err.")) == XSB_ERROR)
    printf("Planned command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th));
  else if ((rc = xsb_command_string(p_th,"test_p_err.")) == XSB_FAILURE)
    printf("++p_err failed improperly\n");
  return NULL;
}
Example #3
0
void *command_rs_fail(void * arg) {
  int rc;
  th_context *r_th;
  r_th = (th_context *)arg;
  
  if ((rc = xsb_command_string(r_th,"test_r_fail.")) == XSB_ERROR)
   printf("++Command Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th));
  else if ((rc = xsb_command_string(r_th,"test_r_fail.")) == XSB_FAILURE)
    printf("r_fail failed properly\n");
 return NULL;
}
Example #4
0
void *command_rs(void * arg) {
  int rc;
  th_context *r_th;
  r_th = (th_context *)arg;
  
  if ((rc = xsb_command_string(r_th,"test_r.")) == XSB_ERROR)
   printf("++Command Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th));
 return NULL;
}
Example #5
0
/*******************************************************************
Succeeding commands
*********************************************************************/
void *command_ps(void * arg) {
  int rc;
  th_context *p_th;
  p_th = (th_context *)arg;
  
  if ((rc = xsb_command_string(p_th,"test_p.")) == XSB_ERROR)
    printf("Command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th));
  return NULL;
}
Example #6
0
int main(int argc, char *argv[])
{ 

  int anslen,rc;
  int return_size = 15;
  char *return_string = malloc(return_size);

  int myargc = 1;
  char *myargv[1];

  myargv[0] = argv[1];

  if (xsb_init(myargc,myargv)) {
    fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_init_error_type(),
	    xsb_get_init_error_message());
    exit(XSB_ERROR);
  }

#ifdef MULTI_THREAD
  th_context *th = xsb_get_main_thread();
#define xsb_get_main_thread_macro() xsb_get_main_thread()
#else
#define xsb_get_main_thread_macro() 
#endif

  /* Create command to consult a file: edb.P, and send it. */
  if (xsb_command_string(CTXTc "consult('edb.P').") == XSB_ERROR)
    fprintf(stderr,"++Error consulting edb.P: %s/%s\n",
	    xsb_get_error_type(xsb_get_main_thread_macro()),
	    xsb_get_error_message(xsb_get_main_thread_macro()));

  rc = xsb_query_string_string_b(CTXTc "p(X,Y,Z).",return_string,return_size,&anslen,"|");

  while (rc == XSB_SUCCESS || rc == XSB_OVERFLOW) {
  
    if (rc == XSB_OVERFLOW) {
      printf("reallocating (%d)\n",anslen);
      return_string = (char *) realloc(return_string,anslen);
      return_size = anslen;
      rc = xsb_get_last_answer_string(CTXTc return_string,return_size,&anslen);
    }    

    printf("Return %s %d %d\n",return_string,return_size,anslen);
    rc = xsb_next_string_b(CTXTc return_string,return_size,&anslen,"|");
  }

 if (rc == XSB_ERROR) 
   fprintf(stderr,"++Query Error: %s/%s\n",xsb_get_error_type(xsb_get_main_thread_macro()),
	   xsb_get_error_message(xsb_get_main_thread_macro()));

  xsb_close(CTXT);
  return(0);
}
Example #7
0
int main(int argc, char *argv[])
{ 

  int rc;

  /* xsb_init_string relies on the calling program to pass the absolute or relative
     path name of the XSB installation directory. We assume that the current
     program is sitting in the directory ../examples/c_calling_xsb/
     To get installation directory, we strip 3 file names from the path. */

  int myargc = 1;
  char *myargv[1];
#ifdef WIN_NT
  VarString return_string, *rs;
  XSB_StrCreate(&rs);
  XSB_StrInit(rs);
  return_string = *rs;
#else
  XSB_StrDefine(return_string);
#endif

  myargv[0] = argv[1];

  if (xsb_init(myargc,myargv)) {
    fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_init_error_type(),xsb_get_init_error_message());
    exit(XSB_ERROR);
  }

#ifdef MULTI_THREAD
  th_context *th = xsb_get_main_thread();
#endif

  /* Create command to consult a file: edb.P, and send it. */
  if (xsb_command_string(CTXTc "consult('edb.P').") == XSB_ERROR)
    fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(CTXT),
	    xsb_get_error_message(CTXT));

  rc = xsb_query_string_string(CTXTc "p(X,Y,Z).",&return_string,"|");
  while (rc == XSB_SUCCESS) {
    printf("cvsreturn %s\n",(return_string.string));
    rc = xsb_next_string(CTXTc &return_string,"|");
  }

 if (rc == XSB_ERROR) 
   fprintf(stderr,"++Query Error: %s/%s\n",xsb_get_error_type(CTXT),xsb_get_error_message(CTXT));

  xsb_close(CTXT);      /* Close connection */
  return(0);
}
Example #8
0
int main(int argc, char *argv[])
{ 
  int rcode;
  int myargc = 3;
  char *myargv[3];

  /* xsb_init relies on the calling program to pass the absolute or relative
     path name of the XSB installation directory. We assume that the current
     program is sitting in the directory .../examples/c_calling_xsb/
     To get installation directory, we strip 3 file names from the path. */
  myargv[0] = strip_names_from_path(xsb_executable_full_path(argv[0]), 3);
  myargv[1] = "-n";
  myargv[2] = "-e writeln(hello). writeln(kkk).";

  /* Initialize xsb */
  xsb_init(myargc,myargv);  /* depend on user to put in right options (-n) */

  /* Create command to consult a file: ctest.P, and send it. */
  c2p_functor("consult",1,reg_term(1));
  c2p_string("ctest",p2p_arg(reg_term(1),1));
  if (xsb_command()) {
    printf("Error consulting ctest.P.\n");
    fflush(stdout);
  }

  if (xsb_command_string("consult(basics).")) {
    printf("Error (string) consulting basics.\n");
    fflush(stdout);
  }

  /* Create the query p(300,X,Y) and send it. */
  c2p_functor("p",3,reg_term(1));
  c2p_int(300,p2p_arg(reg_term(1),1));

  rcode = xsb_query();

  /* Print out answer and retrieve next one. */
  while (!rcode) {
    if (!(is_string(p2p_arg(reg_term(2),1)) & 
	  is_string(p2p_arg(reg_term(2),2))))
       printf("2nd and 3rd subfields must be atoms\n");
    else
      printf("Answer: %d, %s(%s), %s(%s)\n",
	     p2c_int(p2p_arg(reg_term(1),1)),
	     p2c_string(p2p_arg(reg_term(1),2)),
	     xsb_var_string(1),
	     p2c_string(p2p_arg(reg_term(1),3)),
	     xsb_var_string(2)
	     );
    fflush(stdout);
    rcode = xsb_next();
  }

  /* Create the string query p(300,X,Y) and send it, use higher-level
     routines. */

  xsb_make_vars(3);
  xsb_set_var_int(300,1);
  rcode = xsb_query_string("p(X,Y,Z).");

  /* Print out answer and retrieve next one. */
  while (!rcode) {
    if (!(is_string(p2p_arg(reg_term(2),2)) & 
	  is_string(p2p_arg(reg_term(2),3))))
       printf("2nd and 3rd subfields must be atoms\n");
    else
      printf("Answer: %d, %s, %s\n",
	     xsb_var_int(1),
	     xsb_var_string(2),
	     xsb_var_string(3)
	     );
    fflush(stdout);
    rcode = xsb_next();
  }



  /* Close connection */
  xsb_close();
  printf("cmain exit\n");
  return(0);
}
Example #9
0
int main(int argc, char *argv[])
{ 

  static th_context *main_th;
  static th_context *r_th;
  static th_context *q_th;
  pthread_t qthread_id,rthread_id;
  int qstatus, rstatus, rc;
  void *qreturn; void *rreturn;

  char init_string[MAXPATHLEN];

  pthread_mutex_init( &user_p_ready_mut, NULL ) ;
  pthread_mutex_init( &user_r_ready_mut, NULL ) ;

  /* xsb_init_string relies on the calling program to pass the absolute or relative
     path name of the XSB installation directory. We assume that the current
     program is sitting in the directory ../examples/c_calling_xsb/
     To get installation directory, we strip 3 file names from the path. */

  strcpy(init_string,strip_names_from_path(xsb_executable_full_path(argv[0]),3));

  if (xsb_init_string(init_string)) {
    fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_error_type(xsb_get_main_thread()),
	    xsb_get_error_message(xsb_get_main_thread()));
    exit(XSB_ERROR);
  }

  main_th = xsb_get_main_thread();

  /* Create command to consult a file: edb.P, and send it. */
  if (xsb_command_string(main_th, "consult('edb.P').") == XSB_ERROR)
    fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(main_th),
	    xsb_get_error_message(main_th));

  xsb_ccall_thread_create(main_th,&r_th);
  xsb_ccall_thread_create(main_th,&q_th);

  /*
  while (!(r_th->_xsb_ready))
    usleep(1000);

  while (!(q_th->_xsb_ready))
    usleep(1000);
  */

  pthread_create(&rthread_id,NULL,command_rs,r_th);
  pthread_create(&qthread_id,NULL,command_ps,q_th);
  pthread_create(&rthread_id,NULL,command_rs,r_th);
  pthread_create(&qthread_id,NULL,command_ps,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  pthread_create(&rthread_id,NULL,query_rs,r_th);
  pthread_create(&qthread_id,NULL,query_ps,q_th);
  pthread_create(&rthread_id,NULL,query_rs,r_th);
  pthread_create(&qthread_id,NULL,query_ps,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  printf("/******************************************/\n");

  pthread_create(&rthread_id,NULL,command_rs_fail,r_th);
  pthread_create(&qthread_id,NULL,command_ps_fail,q_th);
  pthread_create(&rthread_id,NULL,command_rs_fail,r_th);
  pthread_create(&qthread_id,NULL,command_ps_fail,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  pthread_create(&rthread_id,NULL,close_query_rs,r_th);
  pthread_create(&qthread_id,NULL,close_query_ps,q_th);
  pthread_create(&rthread_id,NULL,close_query_rs,r_th);
  pthread_create(&qthread_id,NULL,close_query_ps,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  printf("/******************************************/\n");

  pthread_create(&rthread_id,NULL,command_rs_err,r_th);
  pthread_create(&qthread_id,NULL,command_ps_err,q_th);
  pthread_create(&rthread_id,NULL,command_rs_err,r_th);
  pthread_create(&qthread_id,NULL,command_ps_err,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  pthread_create(&rthread_id,NULL,query_rs_err,r_th);
  pthread_create(&qthread_id,NULL,query_ps_err,q_th);
  pthread_create(&rthread_id,NULL,query_rs_err,r_th);
  pthread_create(&qthread_id,NULL,query_ps_err,q_th);

  rstatus = pthread_join(rthread_id,&rreturn);
  if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus);
  qstatus = pthread_join(qthread_id,&qreturn);
  if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus);

  if ((rc = xsb_command_string(r_th,"test_r.")) != XSB_SUCCESS)
    fprintf(stderr,"error on test_r: rc: %d\n",rc);

  if (xsb_command_string(r_th,"import thread_exit/0 from thread.") == XSB_ERROR)
    fprintf(stderr,"++Error exiting r: %s/%s\n",xsb_get_error_type(r_th),
	    xsb_get_error_message(r_th));

  xsb_kill_thread(r_th);
  //xsb_command_string(r_th,"thread_exit.");
 /*
 if (xsb_command_string(r_th,"thread_exit.") == XSB_ERROR)
    fprintf(stderr,"++Error exiting: %s/%s\n",xsb_get_error_type(r_th),
	    xsb_get_error_message(r_th));
 */
 xsb_close(xsb_get_main_thread());      /* Close connection */

 return(0);
}