new version of python interface
This commit is contained in:
parent
728edb6b0a
commit
ee03af37fb
File diff suppressed because it is too large
Load Diff
@ -1,216 +0,0 @@
|
||||
/*************************************************************************
|
||||
* *
|
||||
* YAP Prolog *
|
||||
* *
|
||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||
* *
|
||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||
* *
|
||||
**************************************************************************
|
||||
* *
|
||||
* File: myddas_mysql.yap *
|
||||
* Last rev: *
|
||||
* mods: *
|
||||
* comments: MySQL Predicates *
|
||||
* *
|
||||
*************************************************************************/
|
||||
#ifdef MYDDAS_SQLITE3
|
||||
|
||||
:- module(myddas_sqlite3,[
|
||||
sqlite3_result_set/1,
|
||||
db_datalog_describe/1,
|
||||
db_datalog_describe/2,
|
||||
db_describe/3,
|
||||
db_describe/2,
|
||||
db_datalog_show_tables/1,
|
||||
db_datalog_show_tables/0,
|
||||
db_show_tables/2,
|
||||
db_show_tables/1,
|
||||
db_show_database/2,
|
||||
db_show_databases/2,
|
||||
db_show_databases/1,
|
||||
db_change_database/2,
|
||||
db_call_procedure/4,
|
||||
db_call_procedure/3
|
||||
]).
|
||||
|
||||
:- use_module(myddas,[
|
||||
db_sql/3
|
||||
]).
|
||||
|
||||
:- use_module(myddas_errors,[
|
||||
'$error_checks'/1
|
||||
]).
|
||||
|
||||
:- use_module(myddas_util_predicates,[
|
||||
'$get_value'/2,
|
||||
'$make_atom'/2,
|
||||
'$make_atom_args'/2,
|
||||
'$make_a_list'/2,
|
||||
'$write_or_not'/1
|
||||
]).
|
||||
|
||||
|
||||
%--------------------------------------------------------
|
||||
% Public Predicates
|
||||
%--------------------------------------------------------
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% sqlite3_result_set/1
|
||||
%
|
||||
%
|
||||
sqlite3_result_set(X):-
|
||||
var(X),!,
|
||||
get_value(sqlite3_result_set,X).
|
||||
sqlite3_result_set(use_result):-
|
||||
set_value(sqlite3_result_set,use_result).
|
||||
sqlite3_result_set(store_result):-
|
||||
set_value(sqlite3_result_set,store_result).
|
||||
%default value
|
||||
:- sqlite3_result_set(store_result).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% sqlite3_describe/2
|
||||
%
|
||||
%
|
||||
db_datalog_describe(Relation):-
|
||||
db_datalog_describe(myddas,Relation).
|
||||
db_datalog_describe(Connection,Relation) :-
|
||||
'$error_checks'(db_datalog_describe(Relation,Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
||||
sqlite3_result_set(Mode),
|
||||
c_sqlite3_query(SQL,ResultSet,Conn,Mode,_),
|
||||
c_sqlite3_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_describe/3
|
||||
% db_describe/2
|
||||
% gives the results of the DESCRIBE statement
|
||||
% by backtracking
|
||||
db_describe(Relation,TableInfo) :-
|
||||
db_describe(myddas,Relation,TableInfo).
|
||||
db_describe(Connection,Relation,tableinfo(A1,A2,A3,A4,A5,A6)) :-
|
||||
'$error_checks'(db_describe(Relation,Connection,_)),
|
||||
'$get_value'(Connection,Conn),
|
||||
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
||||
sqlite3_result_set(Mode),
|
||||
'$write_or_not'(SQL),
|
||||
c_sqlite3_query(SQL,ResultSet,Conn,Mode,_),
|
||||
!,c_sqlite3_row(ResultSet,6,[A1,A2,A3,A4,A5,A6]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_datalog_show_tables/1
|
||||
%
|
||||
%
|
||||
db_datalog_show_tables:-
|
||||
db_datalog_show_tables(myddas).
|
||||
db_datalog_show_tables(Connection) :-
|
||||
'$error_checks'(sqlite3_show_tables(Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
sqlite3_result_set(Mode),
|
||||
'$write_or_not'('.tables'),
|
||||
c_sqlite3_query('.tables',ResultSet,Conn,Mode,_),
|
||||
c_sqlite3_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_show_tables/2
|
||||
% db_show_tables/1
|
||||
% gives the results of the SHOW TABLES statement
|
||||
% by backtracking
|
||||
db_show_tables(Table) :-
|
||||
db_show_tables(myddas,Table).
|
||||
db_show_tables(Connection,table(Table)) :-
|
||||
'$error_checks'(sqlite3_show_tables(Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
sqlite3_result_set(Mode),
|
||||
'$write_or_not'('.tables'),
|
||||
c_sqlite3_query('.tables',ResultSet,Conn,Mode,_),
|
||||
!,c_sqlite3_row(ResultSet,1,[Table]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_show_database/2
|
||||
%
|
||||
%
|
||||
db_show_database(Connection,Database) :-
|
||||
'$error_checks'(db_show_database(Connection,Database)),
|
||||
'$get_value'(Connection,Con),
|
||||
c_sqlite3_get_database(Con,Database).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_show_databases/2
|
||||
%
|
||||
%
|
||||
db_show_databases(Connection,database(Databases)) :-
|
||||
%'$error_checks'(db_show_databases(Connection,Database)),
|
||||
'$get_value'(Connection,Conn),
|
||||
sqlite3_result_set(Mode),
|
||||
'$write_or_not'('SHOW DATABASES'),
|
||||
c_sqlite3_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
||||
!,c_sqlite3_row(ResultSet,1,[Databases]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_show_databases/1
|
||||
% TODO Error Checks
|
||||
%
|
||||
db_show_databases(Connection) :-
|
||||
'$error_checks'(sqlite3_show_databases(Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
sqlite3_result_set(Mode),
|
||||
'$write_or_not'('SHOW DATABASES'),
|
||||
c_sqlite3_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
||||
c_sqlite3_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_change_database/2
|
||||
%
|
||||
%
|
||||
db_change_database(Connection,Database) :-
|
||||
'$error_checks'(db_change_database(Connection,Database)),
|
||||
'$get_value'(Connection,Con),
|
||||
'$make_atom'(['USE ',Database],SQL),
|
||||
'$write_or_not'(SQL),
|
||||
c_sqlite3_change_database(Con,Database).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_call_procedure/4
|
||||
% db_call_procedure/3
|
||||
% db_call_procedure(+,+,+,?)
|
||||
% Only support in MySQL 5.0 an above
|
||||
% Called procedure must return results via MySQL result set
|
||||
db_call_procedure(Procedure,Args,Result) :-
|
||||
db_call_procedure(myddas,Procedure,Args,Result).
|
||||
db_call_procedure(Connection,Procedure,Args,LA) :-
|
||||
'$error_checks'(db_call_procedure(Connection,Procedure,Args,LA)),
|
||||
'$make_atom_args'(Args,ArgsSQL),
|
||||
'$make_atom'(['CALL ',Procedure,'(',ArgsSQL,')'],SQL),
|
||||
db_sql(Connection,SQL,LA).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
sqlite3_result_set(store_result).
|
||||
|
||||
#endif /* MYDDAS_SQLITE3 */
|
@ -2,7 +2,10 @@
|
||||
#CHECK: PythonLibs
|
||||
|
||||
set (PYTHON_SOURCES
|
||||
python.c)
|
||||
python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c )
|
||||
|
||||
set (PYTHON_HEADERS
|
||||
python.h)
|
||||
|
||||
#try to use Brew first
|
||||
#set ( PYTHON_LIBRARY /Anaconda/lib/libpython2.7.dylib )
|
||||
@ -39,8 +42,15 @@ if (PYTHONLIBS_FOUND) # PYTHONLIBS_FOUND - have the Python l
|
||||
|
||||
add_library (libpython SHARED ${PYTHON_SOURCES})
|
||||
|
||||
configure_file ("setup.py.cmake" "setup.py" )
|
||||
|
||||
target_link_libraries(libpython libYap ${PYTHON_LIBRARIES})
|
||||
|
||||
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
|
||||
|
||||
add_custom_target ( YAPex ALL
|
||||
COMMAND ${PYTHON_EXECUTABLE} setup.py build -f
|
||||
DEPENDS yapex.py )
|
||||
|
||||
set_target_properties (libpython PROPERTIES PREFIX "")
|
||||
|
||||
@ -55,5 +65,7 @@ if (PYTHONLIBS_FOUND) # PYTHONLIBS_FOUND - have the Python l
|
||||
DESTINATION ${libpl}
|
||||
)
|
||||
|
||||
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install -f
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
|
||||
|
||||
endif (PYTHONLIBS_FOUND)
|
||||
|
119
packages/python/pl2pl.c
Normal file
119
packages/python/pl2pl.c
Normal file
@ -0,0 +1,119 @@
|
||||
|
||||
#include "python.h"
|
||||
|
||||
static foreign_t array_to_python_list(term_t addr, term_t type, term_t szt,
|
||||
term_t py) {
|
||||
void *src;
|
||||
Py_ssize_t sz, i;
|
||||
int is_float;
|
||||
|
||||
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
|
||||
!PL_get_intptr(szt, &sz))
|
||||
return false;
|
||||
PyObject *list = PyList_New(sz);
|
||||
if (!list)
|
||||
return false;
|
||||
if (is_float) {
|
||||
double *v = (double *)src;
|
||||
for (i = 0; i < sz; i++) {
|
||||
PyObject *x = PyFloat_FromDouble(v[i]);
|
||||
PyList_SET_ITEM(list, i, x);
|
||||
}
|
||||
} else {
|
||||
YAP_Int *v = (YAP_Int *)src;
|
||||
for (i = 0; i < sz; i++) {
|
||||
PyObject *x = PyFloat_FromDouble(v[i]);
|
||||
PyList_SET_ITEM(list, i, x);
|
||||
}
|
||||
}
|
||||
if (PL_is_variable(py)) {
|
||||
return python_to_ptr(list, py);
|
||||
}
|
||||
return assign_to_symbol(py, list);
|
||||
}
|
||||
|
||||
static foreign_t array_to_python_tuple(term_t addr, term_t type, term_t szt,
|
||||
term_t py) {
|
||||
void *src;
|
||||
Py_ssize_t sz, i;
|
||||
int is_float;
|
||||
|
||||
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
|
||||
!PL_get_intptr(szt, &sz))
|
||||
return false;
|
||||
PyObject *list = PyTuple_New(sz);
|
||||
if (!list)
|
||||
return false;
|
||||
if (is_float) {
|
||||
double *v = (double *)src;
|
||||
|
||||
for (i = 0; i < sz; i++) {
|
||||
PyObject *x;
|
||||
x = PyFloat_FromDouble(v[i]);
|
||||
if (PyTuple_SetItem(list, i, x)) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int32_t *v = (int32_t *)src;
|
||||
PyObject *x;
|
||||
for (i = 0; i < sz; i++) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
x = PyInt_FromLong(v[i]);
|
||||
#else
|
||||
x = PyLong_FromLong(v[i]);
|
||||
#endif
|
||||
if (PyTuple_SetItem(list, i, x)) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (PL_is_variable(py)) {
|
||||
return python_to_ptr(list, py);
|
||||
}
|
||||
return assign_to_symbol(py, list);
|
||||
}
|
||||
|
||||
static foreign_t array_to_python_view(term_t addr, term_t type, term_t szt,
|
||||
term_t colt, term_t py) {
|
||||
void *src;
|
||||
Py_ssize_t sz, rows;
|
||||
int is_float;
|
||||
Py_ssize_t shape[2];
|
||||
|
||||
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
|
||||
!PL_get_intptr(szt, &sz) || !PL_get_intptr(colt, &rows))
|
||||
return false;
|
||||
Py_buffer buf;
|
||||
buf.buf = src;
|
||||
if (is_float) {
|
||||
buf.len = sz * sizeof(double);
|
||||
buf.itemsize = sizeof(double);
|
||||
} else {
|
||||
buf.len = sz * sizeof(YAP_Int);
|
||||
buf.itemsize = sizeof(YAP_Int);
|
||||
}
|
||||
buf.readonly = false;
|
||||
buf.format = NULL;
|
||||
buf.ndim = 2;
|
||||
buf.shape = shape;
|
||||
buf.strides = NULL;
|
||||
buf.suboffsets = NULL;
|
||||
PyObject *o = PyMemoryView_FromBuffer(&buf);
|
||||
if (!o) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
if (PL_is_variable(py)) {
|
||||
return python_to_ptr(o, py);
|
||||
}
|
||||
return assign_to_symbol(py, o);
|
||||
}
|
||||
|
||||
install_t install_pl2pl(void) {
|
||||
PL_register_foreign("array_to_python_list", 4, array_to_python_list, 0);
|
||||
PL_register_foreign("array_to_python_tuple", 4, array_to_python_tuple, 0);
|
||||
PL_register_foreign("array_to_python_view", 5, array_to_python_view, 0);
|
||||
}
|
153
packages/python/pl2py.c
Normal file
153
packages/python/pl2py.c
Normal file
@ -0,0 +1,153 @@
|
||||
|
||||
|
||||
#include "python.h"
|
||||
|
||||
|
||||
/**
|
||||
* term_to_python translates and evaluates from Prolog to Python
|
||||
*
|
||||
* @param t handle to Prolog term
|
||||
* @param t whether should try to evaluate evaluables.
|
||||
*
|
||||
* @return a Python object descriptor or NULL if failed
|
||||
*/
|
||||
PyObject *term_to_python(term_t t, bool eval) {
|
||||
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
|
||||
// am\n");
|
||||
YAP_Term yt = YAP_GetFromSlot(t);
|
||||
switch (PL_term_type(t)) {
|
||||
case PL_VARIABLE: {
|
||||
PyObject *o = NULL, *pArgs;
|
||||
if (eval)
|
||||
return NULL;
|
||||
if (PyObject_HasAttrString(py_Main, "V"))
|
||||
o = PyObject_GetAttrString(py_Main, "V");
|
||||
if (!o && PyObject_HasAttrString(py_Yapex, "V"))
|
||||
o = PyObject_GetAttrString(py_Yapex, "V");
|
||||
if (!o || !PyCallable_Check(o)) {
|
||||
return NULL;
|
||||
}
|
||||
pArgs = PyTuple_New(1);
|
||||
PyTuple_SET_ITEM(pArgs, 0, PyLong_FromLong(t));
|
||||
return PyObject_CallObject(o, pArgs);
|
||||
};
|
||||
case PL_ATOM: {
|
||||
YAP_Atom at = YAP_AtomOfTerm(yt);
|
||||
const char *s;
|
||||
PyObject *o;
|
||||
PyObject *c = NULL;
|
||||
|
||||
s = YAP_AtomName(at);
|
||||
if (strcmp(s, "true") == 0)
|
||||
return Py_True;
|
||||
if (strcmp(s, "false") == 0)
|
||||
return Py_False;
|
||||
if (strcmp(s, "none") == 0)
|
||||
return Py_None;
|
||||
if (strcmp(s, "[]") == 0)
|
||||
o = PyList_New(0);
|
||||
else if (strcmp(s, "{}") == 0)
|
||||
o = PyDict_New();
|
||||
/* return __main__,s */
|
||||
else if (PyObject_HasAttrString(py_Main, s)) {
|
||||
o = PyObject_GetAttrString(py_Main, s);
|
||||
} else {
|
||||
o = PyUnicode_FromString(s);
|
||||
if (eval)
|
||||
return NULL;
|
||||
if (PyObject_HasAttrString(py_Main, "A"))
|
||||
c = PyObject_GetAttrString(py_Main, "A");
|
||||
if (!c && PyObject_HasAttrString(py_Yapex, "A"))
|
||||
c = PyObject_GetAttrString(py_Yapex, "A");
|
||||
if (!c || !PyCallable_Check(c)) {
|
||||
return NULL;
|
||||
} else {
|
||||
PyObject *t = PyTuple_New(1);
|
||||
PyTuple_SET_ITEM(t, 0, PyUnicode_FromString(s));
|
||||
o = PyObject_CallObject(c, t);
|
||||
}
|
||||
}
|
||||
if (o) {
|
||||
Py_IncRef(o);
|
||||
}
|
||||
return o;
|
||||
} break;
|
||||
case PL_STRING: {
|
||||
char *s;
|
||||
if (!PL_get_chars(t, &s,
|
||||
REP_UTF8 | CVT_ATOM | CVT_STRING | BUF_DISCARDABLE)) {
|
||||
return NULL;
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (proper_ascii_string(s)) {
|
||||
return PyString_FromStringAndSize(s, strlen(s));
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
|
||||
// fprintf(stderr, "%s\n", s);
|
||||
return pobj;
|
||||
}
|
||||
} break;
|
||||
case PL_INTEGER: {
|
||||
int64_t j;
|
||||
if (!PL_get_int64_ex(t, &j))
|
||||
return NULL;
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
return PyInt_FromLong(j);
|
||||
#else
|
||||
return PyLong_FromLong(j);
|
||||
#endif
|
||||
}
|
||||
|
||||
case PL_FLOAT: {
|
||||
double fl;
|
||||
if (!PL_get_float(t, &fl))
|
||||
return NULL;
|
||||
return PyFloat_FromDouble(fl);
|
||||
}
|
||||
default: {
|
||||
term_t tail = PL_new_term_ref(), arg;
|
||||
size_t len, i;
|
||||
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
|
||||
PyObject *out;
|
||||
|
||||
arg = tail;
|
||||
out = PyList_New(len);
|
||||
if (!out)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!PL_get_list(t, arg, t)) {
|
||||
return NULL;
|
||||
}
|
||||
if (PyList_SetItem(out, i, term_to_python(arg, eval)) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
} else {
|
||||
functor_t fun;
|
||||
|
||||
if (!PL_get_functor(t, &fun))
|
||||
return NULL;
|
||||
if (eval)
|
||||
return compound_to_pyeval(t, fun);
|
||||
return compound_to_pytree(t, fun);
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *deref_term_to_python(term_t t) {
|
||||
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
|
||||
// am\n");
|
||||
YAP_Term yt = YAP_GetFromSlot(t);
|
||||
if (YAP_IsVarTerm(yt)) {
|
||||
char s[32];
|
||||
char *o = YAP_WriteBuffer(yt, s, 31, 0);
|
||||
return PyUnicode_FromString(o);
|
||||
}
|
||||
return term_to_python(t, false);
|
||||
}
|
256
packages/python/py2pl.c
Normal file
256
packages/python/py2pl.c
Normal file
@ -0,0 +1,256 @@
|
||||
|
||||
#include "python.h"
|
||||
|
||||
static foreign_t repr_term(const char *pVal, size_t sz, term_t t) {
|
||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
||||
PL_put_string_chars(t1, pVal);
|
||||
PL_cons_functor(to, FUNCTOR_pointer1, t1);
|
||||
Py_INCREF(pVal);
|
||||
PL_reset_term_refs(to);
|
||||
return PL_unify(t, to);
|
||||
}
|
||||
|
||||
foreign_t assign_to_symbol(term_t t, PyObject *e);
|
||||
|
||||
/**
|
||||
* assign_python assigns the Python RHS to a Prolog term LHS, ie LHS = RHS
|
||||
*
|
||||
* @param root Python environment
|
||||
* @param t left hand side, in Prolog, may be
|
||||
* - a Prolog variable, exports the term to Prolog, A <- RHS
|
||||
* - Python variable A, A <- RHS
|
||||
* - Python variable $A, A <- RHS
|
||||
* - Python string "A", A <- RHS
|
||||
* - Python array range
|
||||
* @param e the right-hand side
|
||||
*
|
||||
* @return -1 on failure.
|
||||
*
|
||||
* Note that this is an auxiliary routine to the Prolog
|
||||
*python_assign.
|
||||
*/
|
||||
int assign_python(PyObject *root, term_t t, PyObject *e) {
|
||||
switch (PL_term_type(t)) {
|
||||
case PL_VARIABLE:
|
||||
if (python_to_ptr(e, t))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
case PL_ATOM:
|
||||
return assign_to_symbol(t, e);
|
||||
case PL_STRING:
|
||||
case PL_INTEGER:
|
||||
case PL_FLOAT:
|
||||
return -1;
|
||||
case PL_TERM:
|
||||
if (PL_is_list(t)) {
|
||||
return -1;
|
||||
} else {
|
||||
functor_t fun;
|
||||
|
||||
if (!PL_get_functor(t, &fun))
|
||||
return -1;
|
||||
if (fun == FUNCTOR_dollar1) {
|
||||
if (!PL_get_arg(1, t, t))
|
||||
return -1;
|
||||
return assign_to_symbol(t, e);
|
||||
}
|
||||
if (fun == FUNCTOR_pointer1) {
|
||||
return -1;
|
||||
}
|
||||
if (fun == FUNCTOR_sqbrackets2) {
|
||||
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
|
||||
PyObject *lhs, *rhs;
|
||||
|
||||
if (!PL_get_arg(1, t, targ))
|
||||
return -1;
|
||||
lhs = term_to_python(targ, true);
|
||||
if (!PL_get_arg(2, t, targ) || !PL_is_list(targ) ||
|
||||
!PL_get_list(targ, trhs, targ))
|
||||
return -1;
|
||||
if (PL_is_functor(trhs, FUNCTOR_dot2)) {
|
||||
Py_ssize_t left, right;
|
||||
if (!PL_get_arg(1, trhs, targ))
|
||||
return -1;
|
||||
left = get_p_int(term_to_python(targ, true), 0);
|
||||
if (!PL_get_arg(2, trhs, targ))
|
||||
return -1;
|
||||
right = get_p_int(term_to_python(targ, true), PyObject_Size(lhs));
|
||||
if (!PySequence_Check(lhs))
|
||||
return -1;
|
||||
PL_reset_term_refs(targ);
|
||||
return PySequence_SetSlice(lhs, left, right, e);
|
||||
} else {
|
||||
rhs = term_to_python(trhs, true);
|
||||
PL_reset_term_refs(targ);
|
||||
return PyObject_SetItem(lhs, rhs, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
foreign_t assign_to_symbol(term_t t, PyObject *e) {
|
||||
char *s;
|
||||
PyErr_Clear();
|
||||
if (!PL_get_atom_chars(t, &s)) {
|
||||
wchar_t *w;
|
||||
atom_t at;
|
||||
size_t len;
|
||||
PyObject *attr;
|
||||
|
||||
if (!PL_get_atom(t, &at)) {
|
||||
return false;
|
||||
}
|
||||
if (!(w = PL_atom_wchars(at, &len)))
|
||||
return false;
|
||||
attr = PyUnicode_FromWideChar(w, wcslen(w));
|
||||
if (attr) {
|
||||
return PyObject_SetAttr(py_Main, attr, e) >= 0;
|
||||
} else {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
} else if (proper_ascii_string(s)) {
|
||||
return PyObject_SetAttrString(py_Main, s, e) >= 0;
|
||||
} else {
|
||||
PyObject *attr = PyUnicode_DecodeLatin1(s, strlen(s), NULL);
|
||||
if (!attr)
|
||||
return -1;
|
||||
return PyObject_SetAttr(py_Main, attr, e) >= 0;
|
||||
}
|
||||
}
|
||||
|
||||
foreign_t python_to_ptr(PyObject *pVal, term_t t) {
|
||||
Py_IncRef(pVal);
|
||||
return address_to_term(pVal, t);
|
||||
}
|
||||
|
||||
foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
if (pVal == Py_None) {
|
||||
return PL_unify_atom(t, ATOM_none);
|
||||
}
|
||||
if (PyBool_Check(pVal)) {
|
||||
if (PyObject_IsTrue(pVal)) {
|
||||
return PL_unify_atom(t, ATOM_true);
|
||||
} else {
|
||||
return PL_unify_atom(t, ATOM_false);
|
||||
}
|
||||
} else if (PyLong_Check(pVal)) {
|
||||
return PL_unify_int64(t, PyLong_AsLong(pVal));
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyInt_Check(pVal)) {
|
||||
return PL_unify_int64(t, PyInt_AsLong(pVal));
|
||||
#endif
|
||||
} else if (PyFloat_Check(pVal)) {
|
||||
return PL_unify_float(t, PyFloat_AsDouble(pVal));
|
||||
} else if (PyComplex_Check(pVal)) {
|
||||
bool rc;
|
||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref(),
|
||||
t2 = PL_new_term_ref();
|
||||
if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal)) ||
|
||||
!PL_put_float(t2, PyComplex_ImagAsDouble(pVal)) ||
|
||||
!PL_cons_functor(to, FUNCTOR_complex2, t1, t2)) {
|
||||
rc = FALSE;
|
||||
} else {
|
||||
rc = PL_unify(t, to);
|
||||
}
|
||||
PL_reset_term_refs(to);
|
||||
return rc;
|
||||
} else if (PyUnicode_Check(pVal)) {
|
||||
atom_t tmp_atom;
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
Py_ssize_t sz = PyUnicode_GetSize(pVal) + 1;
|
||||
wchar_t *ptr = malloc(sizeof(wchar_t) * sz);
|
||||
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, ptr, sz - 1);
|
||||
#else
|
||||
Py_ssize_t sz = PyUnicode_GetLength(pVal) + 1;
|
||||
wchar_t *ptr = malloc(sizeof(wchar_t) * sz);
|
||||
sz = PyUnicode_AsWideChar(pVal, ptr, sz);
|
||||
#endif
|
||||
tmp_atom = PL_new_atom_wchars(sz, ptr);
|
||||
free(ptr);
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
} else if (PyByteArray_Check(pVal)) {
|
||||
atom_t tmp_atom = PL_new_atom(PyByteArray_AsString(pVal));
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyString_Check(pVal)) {
|
||||
atom_t tmp_atom = PL_new_atom(PyString_AsString(pVal));
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
#endif
|
||||
} else if (PyTuple_Check(pVal)) {
|
||||
Py_ssize_t i, sz = PyTuple_Size(pVal);
|
||||
functor_t f = PL_new_functor(ATOM_t, sz);
|
||||
if (!PL_unify_functor(t, f))
|
||||
return FALSE;
|
||||
for (i = 0; i < sz; i++) {
|
||||
term_t to = PL_new_term_ref();
|
||||
if (!PL_unify_arg(i + 1, t, to))
|
||||
return FALSE;
|
||||
if (!python_to_term(PyTuple_GetItem(pVal, i), to))
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
} else if (PyList_Check(pVal)) {
|
||||
term_t to = PL_new_term_ref();
|
||||
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
|
||||
|
||||
for (i = 0; i < sz; i++) {
|
||||
if (!PL_unify_list(t, to, t) ||
|
||||
!python_to_term(PyList_GetItem(pVal, i), to))
|
||||
return FALSE;
|
||||
}
|
||||
return PL_unify_nil(t);
|
||||
} else if (PyDict_Check(pVal)) {
|
||||
Py_ssize_t pos = 0;
|
||||
term_t to = PL_new_term_ref(), ti = to;
|
||||
int left = PyDict_Size(pVal);
|
||||
PyObject *key, *value;
|
||||
|
||||
while (PyDict_Next(pVal, &pos, &key, &value)) {
|
||||
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint,
|
||||
tnew = PL_new_term_ref();
|
||||
/* do something interesting with the values... */
|
||||
if (!python_to_term(key, tkey)) {
|
||||
return FALSE;
|
||||
}
|
||||
if (!python_to_term(value, tval)) {
|
||||
return FALSE;
|
||||
}
|
||||
/* reuse */
|
||||
tint = tkey;
|
||||
if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) {
|
||||
return FALSE;
|
||||
}
|
||||
if (--left) {
|
||||
if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew))
|
||||
return FALSE;
|
||||
}
|
||||
if (!PL_unify(ti, tint))
|
||||
return FALSE;
|
||||
ti = tnew;
|
||||
}
|
||||
PL_cons_functor(to, FUNCTOR_curly1, to);
|
||||
return PL_unify(t, to);
|
||||
} else {
|
||||
PyObject *pValR = PyObject_Repr(pVal);
|
||||
if (pValR == NULL)
|
||||
return address_to_term(pVal, t);
|
||||
Py_ssize_t sz = PyUnicode_GetSize(pValR) + 1;
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
char *s = malloc(sizeof(char) * sz);
|
||||
PyObject *us = PyUnicode_EncodeUTF8((const Py_UNICODE *)pValR, sz, NULL);
|
||||
PyString_AsStringAndSize(us, &s, &sz);
|
||||
foreign_t rc = repr_term(s, sz, t);
|
||||
free((void *)s);
|
||||
return rc;
|
||||
#else
|
||||
// new interface
|
||||
char *s = PyUnicode_AsUTF8AndSize(pVal, &sz);
|
||||
return repr_term(s, sz, t);
|
||||
#endif
|
||||
}
|
||||
}
|
1289
packages/python/pybips.c
Normal file
1289
packages/python/pybips.c
Normal file
File diff suppressed because it is too large
Load Diff
691
packages/python/pypreds.c
Normal file
691
packages/python/pypreds.c
Normal file
@ -0,0 +1,691 @@
|
||||
|
||||
#include "python.h"
|
||||
|
||||
static int conj_size(term_t source) {
|
||||
if (PL_is_functor(source, FUNCTOR_comma2)) {
|
||||
term_t a1 = PL_new_term_ref(), a2 = PL_new_term_ref();
|
||||
if (PL_get_arg(1, source, a1) <= 0 || PL_get_arg(2, source, a2) <= 0)
|
||||
return -1;
|
||||
return conj_size(a1) + conj_size(a2);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int conj_copy(term_t target, PyObject *e, int pos) {
|
||||
if (PL_is_functor(target, FUNCTOR_comma2)) {
|
||||
term_t a1 = PL_new_term_ref(), a2 = PL_new_term_ref();
|
||||
if (PL_get_arg(1, target, a1) <= 0 || PL_get_arg(2, target, a2) <= 0)
|
||||
return -1;
|
||||
int p1 = conj_copy(a1, e, pos);
|
||||
return conj_copy(a2, e, p1);
|
||||
} else {
|
||||
assign_python(py_Main, target, PyTuple_GetItem(e, pos));
|
||||
return pos + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_f(term_t tmod, term_t fname, term_t tf) {
|
||||
char *s;
|
||||
size_t len;
|
||||
PyObject *pF, *pModule;
|
||||
|
||||
/* if an atom, fetch again */
|
||||
if (PL_is_atom(tmod)) {
|
||||
PyObject *pName;
|
||||
|
||||
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
pName = PyString_FromString(s);
|
||||
#else
|
||||
pName = PyUnicode_FromString(s);
|
||||
#endif
|
||||
if (pName == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
pModule = PyImport_Import(pName);
|
||||
} else if (!(pModule = term_to_python(tmod, true)))
|
||||
return FALSE;
|
||||
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
}
|
||||
pF = PyObject_GetAttrString(pModule, s);
|
||||
PyErr_Print();
|
||||
Py_DECREF(pModule);
|
||||
if (pF == NULL || !PyCallable_Check(pF)) {
|
||||
return FALSE;
|
||||
}
|
||||
printf("Module=%s ok\n", s);
|
||||
return python_to_ptr(pF, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_o(term_t tmod, term_t fname, term_t tf) {
|
||||
char *s;
|
||||
size_t len;
|
||||
PyObject *pO, *pModule;
|
||||
|
||||
pModule = term_to_python(tmod, true);
|
||||
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
}
|
||||
pO = PyObject_GetAttrString(pModule, s);
|
||||
if (pO == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
return python_to_ptr(pO, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_len(term_t tobj, term_t tf) {
|
||||
Py_ssize_t len;
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
len = PyObject_Length(o);
|
||||
return PL_unify_int64(tf, len);
|
||||
}
|
||||
|
||||
static foreign_t python_dir(term_t tobj, term_t tf) {
|
||||
PyObject *dir;
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
dir = PyObject_Dir(o);
|
||||
return python_to_ptr(dir, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
|
||||
PyObject *i;
|
||||
PyObject *o;
|
||||
PyObject *f;
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return false;
|
||||
if (!PySequence_Check(o))
|
||||
return false;
|
||||
i = term_to_python(tindex, true);
|
||||
if (i == NULL)
|
||||
return false;
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i);
|
||||
#else
|
||||
f = PyObject_CallMethodObjArgs(o, PyUnicode_FromString("getitem"), i);
|
||||
#endif
|
||||
return python_to_ptr(f, val);
|
||||
}
|
||||
|
||||
static foreign_t python_is(term_t tobj, term_t tf) {
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (!o)
|
||||
return FALSE;
|
||||
|
||||
return python_to_ptr(o, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_assign_item(term_t parent, term_t indx, term_t tobj) {
|
||||
PyObject *pF, *pI;
|
||||
|
||||
PyObject *p;
|
||||
|
||||
// get Scope ...
|
||||
pI = term_to_python(indx, true);
|
||||
// got Scope.Exp
|
||||
// get Scope ...
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
// get Scope ...
|
||||
pF = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
return false;
|
||||
} else if (PyObject_SetItem(p, pI, pF)) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** assign a tuple to something:
|
||||
*/
|
||||
static foreign_t python_assign_tuple(term_t t_lhs, term_t t_rhs) {
|
||||
PyObject *e = term_to_python(t_rhs, true);
|
||||
Py_ssize_t sz;
|
||||
functor_t f;
|
||||
if (!e || !PyTuple_Check(e)) {
|
||||
return -1;
|
||||
}
|
||||
sz = PyTuple_Size(e);
|
||||
switch (PL_term_type(t_lhs)) {
|
||||
case PL_VARIABLE:
|
||||
return PL_unify(t_lhs, t_rhs);
|
||||
case PL_ATOM:
|
||||
return assign_python(py_Main, t_rhs, e);
|
||||
case PL_TERM:
|
||||
if (PL_get_functor(t_lhs, &f)) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
// assign a tuple to a tuple
|
||||
if (PL_functor_name(f) == ATOM_t && ((sz = PL_functor_arity(f)))) {
|
||||
Py_ssize_t i;
|
||||
for (i = 0; i < sz; i++) {
|
||||
PL_get_arg(i + 1, t_lhs, targ);
|
||||
assign_python(py_Main, targ, PyTuple_GetItem(e, i));
|
||||
}
|
||||
} else if (PL_functor_name(f) == ATOM_comma) {
|
||||
int n = conj_size(t_lhs);
|
||||
if (n != sz)
|
||||
return -1;
|
||||
return conj_copy(t_lhs, e, 0);
|
||||
} else if (PL_functor_name(f) == ATOM_dot) { // vectors
|
||||
size_t len;
|
||||
term_t tail = PL_new_term_ref();
|
||||
|
||||
PL_skip_list(t_lhs, tail, &len);
|
||||
if (!PL_get_nil(tail))
|
||||
return -1;
|
||||
term_t arg = tail;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!PL_get_list(t_rhs, arg, t_rhs)) {
|
||||
return -1;
|
||||
}
|
||||
if (assign_python(py_Main, arg, PyTuple_GetItem(e, i)) < 0)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static foreign_t python_item(term_t parent, term_t indx, term_t tobj) {
|
||||
PyObject *pF, *pI;
|
||||
|
||||
PyObject *p;
|
||||
|
||||
// get Scope ...
|
||||
pI = term_to_python(indx, true);
|
||||
// got Scope.Exp
|
||||
// get Scope ...
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
return false;
|
||||
} else if ((pF = PyObject_GetItem(p, pI)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return address_to_term(pF, tobj);
|
||||
}
|
||||
|
||||
static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
PyObject *pF, *pI;
|
||||
|
||||
PyObject *p;
|
||||
|
||||
// get Scope ...
|
||||
pI = term_to_python(indx, true);
|
||||
// got Scope.Exp
|
||||
// get Scope ...
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
return false;
|
||||
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return address_to_term(pF, tobj);
|
||||
}
|
||||
|
||||
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
term_t tf) {
|
||||
PyObject *pF;
|
||||
PyObject *pArgs, *pKeywords;
|
||||
PyObject *pValue;
|
||||
int i, arity;
|
||||
atom_t aname;
|
||||
foreign_t out;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
pF = term_to_python(tin, true);
|
||||
if (pF == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (PL_is_atom(targs)) {
|
||||
pArgs = NULL;
|
||||
} else {
|
||||
|
||||
if (!PL_get_name_arity(targs, &aname, &arity)) {
|
||||
return FALSE;
|
||||
}
|
||||
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
|
||||
/* ignore (_) */
|
||||
pArgs = NULL;
|
||||
} else {
|
||||
|
||||
pArgs = PyTuple_New(arity);
|
||||
if (!pArgs)
|
||||
return FALSE;
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, targs, targ))
|
||||
return FALSE;
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (PL_is_atom(keywds)) {
|
||||
pKeywords = NULL;
|
||||
} else {
|
||||
pKeywords = term_to_python(keywds, true);
|
||||
}
|
||||
if (PyCallable_Check(pF)) {
|
||||
pValue = PyEval_CallObjectWithKeywords(pF, pArgs, pKeywords);
|
||||
// PyObject_Print(pF,stderr,0);fprintf(stderr, "\n");
|
||||
// PyObject_Print(pArgs,stderr,0);fprintf(stderr, " ");
|
||||
// PyObject_Print(pKeywords,stderr,0);fprintf(stderr, "\n");
|
||||
if (!pValue)
|
||||
PyErr_Print();
|
||||
else
|
||||
Py_IncRef(pValue);
|
||||
} else if (pArgs == NULL) {
|
||||
pValue = pF;
|
||||
|
||||
if (pF) {
|
||||
Py_IncRef(pValue);
|
||||
}
|
||||
} else {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
if (pArgs)
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL)
|
||||
return FALSE;
|
||||
out = python_to_ptr(pValue, tf);
|
||||
return out;
|
||||
}
|
||||
|
||||
static foreign_t python_assign(term_t name, term_t exp) {
|
||||
PyObject *e = term_to_python(exp, true);
|
||||
|
||||
if (e == NULL)
|
||||
return FALSE;
|
||||
return assign_python(py_Main, name, e) >= 0;
|
||||
}
|
||||
|
||||
static foreign_t python_assign_field(term_t source, term_t name, term_t exp) {
|
||||
PyObject *e = term_to_python(exp, true), *root = term_to_python(source, true);
|
||||
|
||||
if (e == NULL)
|
||||
return FALSE;
|
||||
return assign_python(root, name, e) >= 0;
|
||||
}
|
||||
|
||||
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
PyObject *pI, *pArgs, *pOut;
|
||||
PyObject *env;
|
||||
atom_t name;
|
||||
char *s;
|
||||
int i, arity;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if ((env = py_Builtin) == NULL) {
|
||||
// no point in even trying
|
||||
return false;
|
||||
}
|
||||
if (PL_get_name_arity(caller, &name, &arity)) {
|
||||
if (!(s = PL_atom_chars(name)))
|
||||
return false;
|
||||
if ((pI = PyObject_GetAttrString(env, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Prolog should make sure this never happens.
|
||||
return false;
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, caller, targ))
|
||||
return FALSE;
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
pArg = Py_None;
|
||||
} else {
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
if (PyTuple_SetItem(pArgs, i, pArg)) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pOut = PyObject_CallObject(pI, pArgs);
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pI);
|
||||
if (pOut == NULL) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
return python_to_ptr(pOut, out);
|
||||
}
|
||||
|
||||
static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
PyObject *o = term_to_python(obj, true), *pValue, *pArgs, *pF;
|
||||
atom_t name;
|
||||
char *s;
|
||||
int i, arity;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
if (PL_is_atom(f)) {
|
||||
if (!PL_get_atom_chars(f, &s))
|
||||
return FALSE;
|
||||
if ((pValue = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
return FALSE;
|
||||
}
|
||||
/* follow chains of the form a.b.c.d.e() */
|
||||
while (name == ATOM_dot && arity == 2) {
|
||||
term_t tleft = PL_new_term_ref();
|
||||
PyObject *lhs;
|
||||
|
||||
if (!PL_get_arg(1, f, tleft))
|
||||
return FALSE;
|
||||
lhs = term_to_python(tleft, true);
|
||||
if ((o = PyObject_GetAttr(o, lhs)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
if (!PL_get_arg(2, f, f))
|
||||
return FALSE;
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (!s)
|
||||
return false;
|
||||
if ((pF = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, f, targ))
|
||||
return FALSE;
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
pArgs = Py_None;
|
||||
}
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
}
|
||||
pValue = PyObject_CallObject(pF, pArgs);
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
|
||||
static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
PyObject *pF;
|
||||
atom_t name;
|
||||
char *s;
|
||||
int arity;
|
||||
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
} else {
|
||||
PyObject *p;
|
||||
|
||||
// got Scope.Exp
|
||||
// get Scope ...
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (arity == 1 && !strcmp(s, "()")) {
|
||||
if (!PL_get_arg(1, att, att))
|
||||
return false;
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
}
|
||||
if (!s || !p) {
|
||||
return false;
|
||||
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
|
||||
PyErr_Clear();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return address_to_term(pF, tobj);
|
||||
}
|
||||
|
||||
static foreign_t python_main_module(term_t mod) {
|
||||
return address_to_term(py_Main, mod);
|
||||
}
|
||||
|
||||
static foreign_t python_function(term_t tobj) {
|
||||
PyObject *obj = term_to_python(tobj, true);
|
||||
|
||||
return PyFunction_Check(obj);
|
||||
}
|
||||
|
||||
foreign_t python_builtin(term_t out) {
|
||||
return address_to_term(py_Builtin, out);
|
||||
}
|
||||
|
||||
static foreign_t python_run_file(term_t file) {
|
||||
char *s;
|
||||
size_t len;
|
||||
char si[256];
|
||||
s = si;
|
||||
if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
PyObject *PyFileObject = PyFile_FromString(si, "r");
|
||||
PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1);
|
||||
#else
|
||||
FILE *f = fopen(s, "r");
|
||||
if (f == NULL)
|
||||
return false;
|
||||
PyRun_SimpleFileEx(f, s, 1);
|
||||
#endif
|
||||
return TRUE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static foreign_t python_run_command(term_t cmd) {
|
||||
char *s;
|
||||
size_t len;
|
||||
char si[256];
|
||||
s = si;
|
||||
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
PyRun_SimpleString(s);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
char si[256], sf[256];
|
||||
size_t len = 255, len1 = 255;
|
||||
PyObject *pName, *pModule, *pFunc;
|
||||
PyObject *pArgs = NULL, *pValue;
|
||||
char *s;
|
||||
|
||||
s = si;
|
||||
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
|
||||
(s = sf) != NULL &&
|
||||
PL_get_nchars(fun, &len1, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
pName = PyString_FromString("rbm");
|
||||
#else
|
||||
// asssumes UTF-8
|
||||
pName = PyUnicode_FromString("rbm");
|
||||
#endif
|
||||
/* Error checking of pName left out */
|
||||
|
||||
pModule = PyImport_Import(pName);
|
||||
Py_DECREF(pName);
|
||||
|
||||
if (pModule != NULL) {
|
||||
pFunc = PyObject_GetAttrString(pModule, sf);
|
||||
/* pFunc is a new reference */
|
||||
|
||||
if (pFunc && PyCallable_Check(pFunc)) {
|
||||
pValue = PyObject_CallObject(pFunc, pArgs);
|
||||
if (pValue != NULL) {
|
||||
Py_DECREF(pValue);
|
||||
} else {
|
||||
Py_DECREF(pFunc);
|
||||
Py_DECREF(pModule);
|
||||
PyErr_Print();
|
||||
fprintf(stderr, "Call failed\n");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (PyErr_Occurred())
|
||||
PyErr_Print();
|
||||
fprintf(stderr, "Cannot find function \"%s\"\n", sf);
|
||||
}
|
||||
Py_XDECREF(pFunc);
|
||||
Py_DECREF(pModule);
|
||||
} else {
|
||||
PyErr_Print();
|
||||
fprintf(stderr, "Failed to load \"%s\"\n", s);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static foreign_t python_export(term_t t, term_t pl) {
|
||||
foreign_t rc = false;
|
||||
if (PL_is_functor(t, FUNCTOR_pointer1)) {
|
||||
void *ptr;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (!PL_get_arg(1, t, targ))
|
||||
return false;
|
||||
if (!PL_get_pointer(targ, &ptr))
|
||||
return false;
|
||||
Py_INCREF((PyObject *)ptr);
|
||||
/* return __main__,s */
|
||||
rc = python_to_term((PyObject *)ptr, pl);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int python_import(term_t mname, term_t mod) {
|
||||
char *s;
|
||||
size_t len;
|
||||
PyObject *pName, *pModule;
|
||||
|
||||
if (PL_is_list(mname)) {
|
||||
char *s2;
|
||||
char str[256];
|
||||
term_t arg = PL_new_term_ref();
|
||||
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &s) ||
|
||||
!PL_get_arg(2, mname, mname) || !PL_get_arg(1, mname, arg) ||
|
||||
!PL_get_atom_chars(arg, &s2))
|
||||
return FALSE;
|
||||
strcpy(str, s);
|
||||
strcat(str, ".");
|
||||
strcat(str, s2);
|
||||
s = str;
|
||||
} else if (!PL_get_nchars(mname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
pName = PyString_FromString(s);
|
||||
#else
|
||||
printf("Module=%s\n", s);
|
||||
pName = PyUnicode_FromString(s);
|
||||
#endif
|
||||
if (pName == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
pModule = PyImport_Import(pName);
|
||||
Py_DECREF(pName);
|
||||
if (pModule == NULL) {
|
||||
#if EXTRA_MESSSAGES
|
||||
if (PyErr_Occurred())
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
#endif
|
||||
return FALSE;
|
||||
}
|
||||
ActiveModules[active_modules++] = pModule;
|
||||
return python_to_ptr(pModule, mod);
|
||||
}
|
||||
|
||||
install_t install_pypreds(void) {
|
||||
PL_register_foreign("python_builtin_eval", 3, python_builtin_eval, 0);
|
||||
PL_register_foreign("python_builtin", 1, python_builtin, 0);
|
||||
PL_register_foreign("python_import", 2, python_import, 0);
|
||||
PL_register_foreign("python_f", 3, python_f, 0);
|
||||
PL_register_foreign("python_o", 3, python_o, 0);
|
||||
PL_register_foreign("python_len", 2, python_len, 0);
|
||||
PL_register_foreign("python_is", 2, python_is, 0);
|
||||
PL_register_foreign("python_dir", 2, python_dir, 0);
|
||||
PL_register_foreign("python_apply", 4, python_apply, 0);
|
||||
PL_register_foreign("python_index", 3, python_index, 0);
|
||||
PL_register_foreign("python_field", 3, python_field, 0);
|
||||
PL_register_foreign("python_assign", 2, python_assign, 0);
|
||||
PL_register_foreign("python_assign_field", 3, python_assign_field, 0);
|
||||
PL_register_foreign("python_assign_tuple", 2, python_assign_tuple, 0);
|
||||
PL_register_foreign("python_export", 2, python_export, 0);
|
||||
PL_register_foreign("python_function", 1, python_function, 0);
|
||||
PL_register_foreign("python_slice", 4, python_slice, 0);
|
||||
PL_register_foreign("python_item", 3, python_item, 0);
|
||||
PL_register_foreign("python_assign_item", 3, python_assign_item, 0);
|
||||
PL_register_foreign("python_run_file", 1, python_run_file, 0);
|
||||
PL_register_foreign("python_run_command", 1, python_run_command, 0);
|
||||
PL_register_foreign("python_run_script", 2, python_run_script, 0);
|
||||
PL_register_foreign("python_main_module", 1, python_main_module, 0);
|
||||
PL_register_foreign("python_import", 2, python_import, 0);
|
||||
PL_register_foreign("python_access", 3, python_access, 0);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
112
packages/python/python.h
Normal file
112
packages/python/python.h
Normal file
@ -0,0 +1,112 @@
|
||||
#ifdef _XOPEN_SOURCE
|
||||
#undef _XOPEN_SOURCE // python does its own thing
|
||||
#endif
|
||||
#include <Python.h>
|
||||
#include <SWI-Prolog.h>
|
||||
#ifdef HAVE_STAT
|
||||
#undef HAVE_STATa
|
||||
#endif
|
||||
#include <assert.h>
|
||||
|
||||
//@{
|
||||
|
||||
/** @brief Prolog to Python library
|
||||
*
|
||||
*
|
||||
* Please look at python.pl for more information, and to real.pl and real.c
|
||||
* for related work.
|
||||
*/
|
||||
|
||||
typedef YAP_Arity arity_t;
|
||||
|
||||
extern atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
|
||||
ATOM_comma, ATOM_builtin, ATOM_V, ATOM_A;
|
||||
|
||||
extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
|
||||
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
|
||||
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
|
||||
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
|
||||
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
|
||||
FUNCTOR_plus2, FUNCTOR_sub2, FUNCTOR_mul2, FUNCTOR_div2, FUNCTOR_hat2,
|
||||
FUNCTOR_colon2, FUNCTOR_comma2, FUNCTOR_equal2, FUNCTOR_sqbrackets2,
|
||||
FUNCTOR_dot2;
|
||||
|
||||
extern PyObject *py_Main;
|
||||
extern PyObject *py_Builtin;
|
||||
extern PyObject *py_Yapex;
|
||||
extern PyObject *py_F2P;
|
||||
|
||||
static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) {
|
||||
if (o == NULL)
|
||||
return def;
|
||||
if (PyLong_Check(o)) {
|
||||
return PyLong_AsLong(o);
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyInt_Check(o)) {
|
||||
return PyInt_AsLong(o);
|
||||
#endif
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
static inline foreign_t address_to_term(PyObject *pVal, term_t t) {
|
||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
||||
PL_put_pointer(t1, (void *)pVal);
|
||||
PL_cons_functor(to, FUNCTOR_pointer1, t1);
|
||||
Py_INCREF(pVal);
|
||||
foreign_t rc = PL_unify(t, to);
|
||||
PL_reset_term_refs(to);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static inline int proper_ascii_string(const char *s) {
|
||||
unsigned char c;
|
||||
|
||||
while ((c = *s++)) {
|
||||
if (c > 127)
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static inline PyObject *atom_to_python_string(term_t t) {
|
||||
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
|
||||
// am\n");
|
||||
char *s;
|
||||
if (!PL_get_atom_chars(t, &s))
|
||||
return NULL;
|
||||
/* return __main__,s */
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (proper_ascii_string(s)) {
|
||||
return PyString_FromStringAndSize(s, strlen(s));
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
|
||||
// fprintf(stderr, "%s\n", s);
|
||||
return pobj;
|
||||
}
|
||||
}
|
||||
|
||||
extern PyObject *compound_to_pyeval(term_t t, functor_t fun);
|
||||
extern PyObject *compound_to_pytree(term_t t, functor_t fun);
|
||||
|
||||
extern PyObject *term_to_python(term_t t, bool eval);
|
||||
extern foreign_t python_to_ptr(PyObject *pVal, term_t t);
|
||||
|
||||
foreign_t python_to_term(PyObject *pVal, term_t t);
|
||||
foreign_t assign_to_symbol(term_t t, PyObject *e);
|
||||
|
||||
int assign_python(PyObject *root, term_t t, PyObject *e);
|
||||
|
||||
extern foreign_t python_builtin(term_t out);
|
||||
|
||||
extern PyObject *ActiveModules[32];
|
||||
extern int active_modules;
|
||||
|
||||
extern install_t install_pypreds(void);
|
||||
extern install_t install_pl2pl(void);
|
||||
|
||||
extern bool init_python(void);
|
||||
|
||||
extern PyObject PyInit_yap(void);
|
@ -372,7 +372,6 @@ python_lhs((Exp1,Exp2), O) :- !,
|
||||
python_lhs(F, F).
|
||||
|
||||
start_python :-
|
||||
init_python,
|
||||
python_main_module(MRef),
|
||||
assert(python_mref_cache('__main__', MRef)),
|
||||
python_command('import sys'),
|
||||
@ -426,6 +425,5 @@ python_assign_field(C1.E, Obj) :-
|
||||
python_eval_term(C1, O1),
|
||||
python_assign_field(O1, E, Obj ).
|
||||
|
||||
:- initialization( use_foreign_library(foreign(libpython)), now ).
|
||||
:- initialization( use_foreign_library(foreign(libpython), init_python), now ).
|
||||
|
||||
:- initialization(start_python ).
|
||||
|
10
packages/python/setup.py.cmake
Normal file
10
packages/python/setup.py.cmake
Normal file
@ -0,0 +1,10 @@
|
||||
from setuptools import setup, Extension
|
||||
|
||||
|
||||
setup(
|
||||
name = "yapex",
|
||||
version = "0.1",
|
||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python' },
|
||||
py_modules = ['yapex']
|
||||
|
||||
)
|
3
packages/python/yap_kernel/__init__.py
Normal file
3
packages/python/yap_kernel/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
"""A Prolog kernel for Jupyter"""
|
||||
|
||||
__version__ = '0.0.1'
|
6
packages/python/yap_kernel/__main__.py
Normal file
6
packages/python/yap_kernel/__main__.py
Normal file
@ -0,0 +1,6 @@
|
||||
try:
|
||||
from ipykernel.kernelapp import IPKernelApp
|
||||
except ImportError:
|
||||
from IPython.kernel.zmq.kernelapp import IPKernelApp
|
||||
from .kernel import YAPKernel
|
||||
IPKernelApp.launch_instance(kernel_class=YAPKernel)
|
44
packages/python/yap_kernel/install.py
Normal file
44
packages/python/yap_kernel/install.py
Normal file
@ -0,0 +1,44 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from jupyter_client.kernelspec import install_kernel_spec
|
||||
except ImportError:
|
||||
from IPython.kernel.kernelspec import install_kernel_spec
|
||||
from IPython.utils.tempdir import TemporaryDirectory
|
||||
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": "yap",
|
||||
"mimetype": "text/x-prolog",
|
||||
"language": "prolog",
|
||||
"name": "yap",
|
||||
}
|
||||
|
||||
def install_my_kernel_spec(user=False):
|
||||
with TemporaryDirectory() as td:
|
||||
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
||||
with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_json, f, sort_keys=True)
|
||||
# TODO: Copy resources once they're specified
|
||||
|
||||
print('Installing IPython kernel spec')
|
||||
install_kernel_spec(td, 'yap', user=False, replace=True)
|
||||
|
||||
def _is_root():
|
||||
return True
|
||||
try:
|
||||
return os.geteuid() == 0
|
||||
except AttributeError:
|
||||
return False # assume not an admin on non-Unix platforms
|
||||
|
||||
def main(argv=[]):
|
||||
user = '--user' in argv or not _is_root()
|
||||
install_my_kernel_spec(user=user)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(argv=sys.argv)
|
1283
packages/python/yap_kernel/prolog.js
Normal file
1283
packages/python/yap_kernel/prolog.js
Normal file
File diff suppressed because it is too large
Load Diff
58
packages/python/yap_kernel/setup.py
Normal file
58
packages/python/yap_kernel/setup.py
Normal file
@ -0,0 +1,58 @@
|
||||
from distutils.command.install import install
|
||||
from distutils.core import setup
|
||||
from distutils import log
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
PY3 = sys.version_info[0] >= 3
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": " YAP-6.3" ,
|
||||
"language": "prolog",
|
||||
"name": "yap_kernel",
|
||||
}
|
||||
|
||||
class install_with_kernelspec(install):
|
||||
def run(self):
|
||||
install.run(self)
|
||||
from jupyter_client.kernelspec import install_kernel_spec
|
||||
from IPython.utils.tempdir import TemporaryDirectory
|
||||
with TemporaryDirectory() as td:
|
||||
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
||||
with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_json, f, sort_keys=True)
|
||||
log.info('Installing kernel spec')
|
||||
try:
|
||||
install_kernel_spec(td, 'yap_kernel', user=self.user,
|
||||
replace=True)
|
||||
except:
|
||||
install_kernel_spec(td, 'yap_kernel', user=not self.user,
|
||||
replace=True)
|
||||
|
||||
svem_flag = '--single-version-externally-managed'
|
||||
if svem_flag in sys.argv:
|
||||
# Die, setuptools, die.
|
||||
sys.argv.remove(svem_flag)
|
||||
|
||||
setup(name='yap_kernel',
|
||||
version='0.0.1',
|
||||
description='A simple YAP kernel for Jupyter/IPython',
|
||||
long_description="A simple YAP kernel for Jupyter/IPython, based on MetaKernel",
|
||||
url="https://github.com/vscosta/yap-6.3",
|
||||
author='Vitor Santos Costa, based on the metakernel from Douglas Blank',
|
||||
author_email='vsc@dcc.fc.up.pt',
|
||||
py_modules=['yap_kernel'],
|
||||
install_requires=["metakernel","yap"],
|
||||
cmdclass={'install': install_with_kernelspec},
|
||||
classifiers = [
|
||||
'Framework :: IPython',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Programming Language :: YAP :: 6.3',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Topic :: System :: Shells',
|
||||
]
|
||||
)
|
210
packages/python/yap_kernel/yap_kernel.py
Normal file
210
packages/python/yap_kernel/yap_kernel.py
Normal file
@ -0,0 +1,210 @@
|
||||
from __future__ import print_function
|
||||
|
||||
from metakernel import MetaKernel
|
||||
|
||||
import sys
|
||||
import signal
|
||||
import yap
|
||||
import yapex
|
||||
|
||||
import ipywidgets as widgets
|
||||
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, file=sys.stderr, **kwargs)
|
||||
|
||||
|
||||
class MetaKernelyap(MetaKernel):
|
||||
implementation = 'MetaKernel YAP'
|
||||
implementation_version = '1.0'
|
||||
language = 'text'
|
||||
language_version = '0.1'
|
||||
banner = "MetaKernel YAP"
|
||||
language_info = {
|
||||
'mimetype': 'text/prolog',
|
||||
'name': 'text',
|
||||
# ------ If different from 'language':
|
||||
'codemirror_mode': {
|
||||
"version": 2,
|
||||
"name": "prolog"
|
||||
},
|
||||
'pygments_lexer': 'prolog',
|
||||
'version' : "0.0.1",
|
||||
'file_extension': '.yap',
|
||||
'help_links': MetaKernel.help_links,
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
MetaKernel.__init__(self, **kwargs)
|
||||
self._start_yap(**kwargs)
|
||||
self.olines = ""
|
||||
self.ask = True
|
||||
|
||||
def _start_yap(self, **kwargs):
|
||||
# Signal handlers are inherited by forked processes, and we can't easily
|
||||
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
|
||||
# message handlers, we need to temporarily reset the SIGINT handler here
|
||||
# so that yap and its children are interruptible.
|
||||
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
try:
|
||||
self.engine = yap.YAPEngine()
|
||||
self.q = None
|
||||
self.engine.query("load_files(library(python), [])").command()
|
||||
banner = "YAP {0} Kernel".format(self.engine.version())
|
||||
self.olines = banner
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, sig)
|
||||
|
||||
# Register Yap function to write image data to temporary file
|
||||
#self.yapwrapper.run_command(image_setup_cmd)
|
||||
|
||||
def get_usage(self):
|
||||
return "This is the YAP kernel."
|
||||
|
||||
def query_prolog(self, s):
|
||||
|
||||
if not self.q:
|
||||
self.q = self.engine.query(s)
|
||||
if self.q.next():
|
||||
vs = self.q.namedVarsCopy()
|
||||
wrote = False
|
||||
if vs:
|
||||
i = 0
|
||||
for eq in vs:
|
||||
name = eq[0]
|
||||
bind = eq[1]
|
||||
if bind.isVar():
|
||||
var = yap.YAPAtom('$VAR')
|
||||
f = yap.YAPFunctor(var, 1)
|
||||
bind.unify(yap.YAPApplTerm(f, (name)))
|
||||
else:
|
||||
i = bind.numberVars(i, True)
|
||||
print(name.text() + " = " + bind.text())
|
||||
wrote = True
|
||||
print("yes")
|
||||
if self.q.deterministic():
|
||||
self.closeq()
|
||||
return
|
||||
print("No (more) answers")
|
||||
self.closeq()
|
||||
return
|
||||
|
||||
def closeq( self):
|
||||
if self.q:
|
||||
self.q.close()
|
||||
self.q = None
|
||||
|
||||
def do_execute_direct(self, code):
|
||||
if not code.strip():
|
||||
return ""
|
||||
lines = code.split("\n")
|
||||
interrupted = False
|
||||
self.doReset = True
|
||||
nlines = ""
|
||||
try:
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith('#'):
|
||||
# wait
|
||||
print( "comment")
|
||||
elif line.startswith('%'):
|
||||
# wait
|
||||
call_magic( line )
|
||||
elif line.endswith(';'):
|
||||
nlines += line.rstrip(';').rstrip()
|
||||
self.doReset = False
|
||||
break
|
||||
elif line.endswith('!'):
|
||||
nlines += line.rstrip('!').rstrip()
|
||||
self.ask = False
|
||||
self.doReset = False
|
||||
break
|
||||
else:
|
||||
line = line.rstrip()
|
||||
if line:
|
||||
nlines += line + "\n"
|
||||
if nlines != self.olines:
|
||||
self.closeq( )
|
||||
self.olines = nlines
|
||||
elif self.doReset:
|
||||
opt = widgets.ToggleButtons(
|
||||
description='Query Solutions:',
|
||||
options=['First', 'Next', 'All'],
|
||||
)
|
||||
print( opt )
|
||||
if opt == 'First':
|
||||
self.closeq( )
|
||||
elif opt == 'Next':
|
||||
self.doReset = False
|
||||
else:
|
||||
self.ask = False
|
||||
self.doReset = False
|
||||
self.query_prolog( nlines )
|
||||
while not self.ask and self.q:
|
||||
self.query_prolog( nlines )
|
||||
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
return
|
||||
except RuntimeError as err:
|
||||
print("YAP Execution Error: {0}".format(err))
|
||||
except ValueError:
|
||||
print("Could not convert data to an integer.")
|
||||
except KeyboardInterrupt:
|
||||
return 'stopped by user'
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
|
||||
def do_complete(self, code, cursor_pos):
|
||||
eprint( code, " -- ", str(cursor_pos) )
|
||||
# code = code[:cursor_pos]
|
||||
# default = {'matches': [], 'cursor_start': 0,
|
||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||
# 'status': 'ok'}
|
||||
|
||||
# if not code or code[-1] == ' ':
|
||||
# return default
|
||||
|
||||
# tokens = code.replace(';', ' ').split()
|
||||
# if not tokens:
|
||||
# return default
|
||||
|
||||
# matches = []
|
||||
# token = tokens[-1]
|
||||
# start = cursor_pos - len(token)
|
||||
|
||||
# if token[0] == '$':
|
||||
# # complete variables
|
||||
# cmd = 'compgen -A arrayvar -A export -A variable %s' % token[1:] # strip leading $
|
||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||
# completions = set(output.split())
|
||||
# # append matches including leading $
|
||||
# matches.extend(['$'+c for c in completions])
|
||||
# else:
|
||||
# # complete functions and builtins
|
||||
# cmd = 'compgen -cdfa %s' % token
|
||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||
# matches.extend(output.split())
|
||||
|
||||
# if not matches:
|
||||
# return default
|
||||
# matches = [m for m in matches if m.startswith(token)]
|
||||
|
||||
# return {'matches': sorted(matches), 'cursor_start': start,
|
||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||
# 'status': 'ok'}
|
||||
|
||||
|
||||
|
||||
|
||||
def repr(self, data):
|
||||
return repr(data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
from ipykernel.kernelapp import IPKernelApp
|
||||
except ImportError:
|
||||
from jupyter_client.zmq.kernelapp import IPKernelApp
|
||||
IPKernelApp.launch_instance(kernel_class=MetaKernelyap)
|
79
packages/python/yapex.py
Normal file
79
packages/python/yapex.py
Normal file
@ -0,0 +1,79 @@
|
||||
import yap
|
||||
|
||||
# this class is not being used
|
||||
# we rely on namedtuples instead.
|
||||
class T(tuple):
|
||||
|
||||
"""Represents a non-interned Prolog atom"""
|
||||
def __new__(self, s, tple):
|
||||
self.tuple.__new__(self, tple)
|
||||
self.name = s
|
||||
|
||||
def __repr__(self):
|
||||
return "yapex.T(" + self.name + " , " + tuple.__repr__(self) + ")"
|
||||
|
||||
def __str__(self):
|
||||
return str(self.name) + str(self.tuple)
|
||||
|
||||
|
||||
def query_prolog(engine, s):
|
||||
q = engine.query(s)
|
||||
ask = True
|
||||
while q.next():
|
||||
vs = q.namedVarsCopy()
|
||||
if vs:
|
||||
i = 0
|
||||
for eq in vs:
|
||||
name = eq[0]
|
||||
bind = eq[1]
|
||||
if bind.isVar():
|
||||
var = yap.YAPAtom('$VAR')
|
||||
f = yap.YAPFunctor(var, 1)
|
||||
bind.unify(yap.YAPApplTerm(f, (name)))
|
||||
else:
|
||||
i = bind.numberVars(i, True)
|
||||
print(name.text() + " = " + bind.text())
|
||||
print("yes")
|
||||
if q.deterministic():
|
||||
q.close()
|
||||
return
|
||||
if ask:
|
||||
s = input("more(;/y), all(!/a), no ?").lstrip()
|
||||
if s.startswith(';') or s.startswith('y'):
|
||||
continue
|
||||
elif s.startswith('#'):
|
||||
exec(s.lstrip('#'))
|
||||
elif s.startswith('a'):
|
||||
ask = False
|
||||
else:
|
||||
break
|
||||
print("No (more) answers")
|
||||
q.close()
|
||||
return
|
||||
|
||||
|
||||
def live():
|
||||
engine = yap.YAPEngine()
|
||||
loop = True
|
||||
while loop:
|
||||
try:
|
||||
s = input("?- ")
|
||||
if not s:
|
||||
loop = False
|
||||
query_prolog(engine, s)
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
return
|
||||
except RuntimeError as err:
|
||||
print("YAP Execution Error: {0}".format(err))
|
||||
except ValueError:
|
||||
print("Could not convert data to an integer.")
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
|
||||
#
|
||||
# initialize engine
|
||||
# engine = yap.YAPEngine();
|
||||
# engine = yap.YAPEngine(yap.YAPParams());
|
47
packages/python/yutils.py
Normal file
47
packages/python/yutils.py
Normal file
@ -0,0 +1,47 @@
|
||||
# python commands
|
||||
|
||||
import sys
|
||||
# import collections
|
||||
|
||||
# generated by swig
|
||||
import yap
|
||||
|
||||
# make sure Python knows about engine
|
||||
engine = None
|
||||
|
||||
# Mappings between functors and types
|
||||
#
|
||||
# they are used to have the same type f1or several occurences
|
||||
# of the same functor, and to ensure that an object is indeed
|
||||
# a Prolog compound term.
|
||||
#
|
||||
dictF2P = {}
|
||||
|
||||
|
||||
class A:
|
||||
"""Represents a non-interned Prolog atom"""
|
||||
def __init__(self, s):
|
||||
self.a = s
|
||||
|
||||
def __repr__(self):
|
||||
return "A(" + self.a + ")"
|
||||
|
||||
def __str__(self):
|
||||
return self.a
|
||||
|
||||
|
||||
class V:
|
||||
"""Wraps a term, or a reference to a logical variables"""
|
||||
def __init__(self, t):
|
||||
print(type(t))
|
||||
self.v = t
|
||||
|
||||
def __repr__(self):
|
||||
return "V(" + str(self.v) + ")"
|
||||
|
||||
def __str__(self):
|
||||
return engine.getTerm(self.v).text()
|
||||
|
||||
def handle(self):
|
||||
return self.v
|
||||
|
Reference in New Issue
Block a user