This commit is contained in:
Vitor Santos Costa 2016-06-28 23:35:45 +01:00
parent 06f5fa2614
commit d9b5a21a54
4 changed files with 182 additions and 110 deletions

View File

@ -12,26 +12,42 @@ SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(../yap.i PROPERTIES SWIG_FLAGS "-includeall")
configure_file ("setup.py.cmake" "setup.py" )
#SET( CMAKE_SWIG_OUTDIR "yap" )
SWIG_ADD_MODULE(yap python ../yap.i)
SWIG_LINK_LIBRARIES(yap ${PYTHON_LIBRARIES} Yap++ libYap)
SWIG_LINK_LIBRARIES(yap ${PYTHON_LIBRARIES} Yap++ libYap libpython)
set_target_properties ( ${SWIG_MODULE_yap_REAL_NAME} PROPERTIES NO_SONAME ON )
execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print( sysconfig.get_python_lib( plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}' ) )"
OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE )
# execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print( sysconfig.get_python_lib( plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}' ) )"
# OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
# OUTPUT_STRIP_TRAILING_WHITESPACE )
# get_filename_component ( _ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE )
# file ( RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH} )
get_filename_component ( _ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE )
file ( RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH} )
# set ( PYTHON_MODULE_PATH
# ${_REL_PYTHON_MODULE_PATH}
# )
set ( PYTHON_MODULE_PATH
${_REL_PYTHON_MODULE_PATH}
)
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
INSTALL ( FILES ${CMAKE_CURRENT_BINARY_DIR}/_yap.so DESTINATION ${PYTHON_MODULE_PATH} )
INSTALL ( FILES ${CMAKE_CURRENT_BINARY_DIR}/yap.py DESTINATION ${PYTHON_MODULE_PATH} )
INSTALL ( FILES ${CMAKE_CURRENT_SOURCE_DIR}/yapex.py DESTINATION ${PYTHON_MODULE_PATH} )
add_custom_target ( py2yap ALL
COMMAND ${PYTHON_EXECUTABLE} setup.py build -f
DEPENDS yap.py )
if ( DOCUMENTATION AND DOXYGEN_FOUND )
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install -f
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
# INSTALL ( FILES ${CMAKE_CURRENT_BINARY_DIR}/yap/_yap.so DESTINATION ${PYTHON_MODULE_PATH} )
# INSTALL ( FILES ${CMAKE_CURRENT_BINARY_DIR}/yap/yap.py DESTINATION ${PYTHON_MODULE_PATH} )
# INSTALL ( FILES ${CMAKE_CURRENT_SOURCE_DIR}/yap/yapex.py DESTINATION ${PYTHON_MODULE_PATH} )
if ( DOCUMENTATION AND DOXYGEN_FOUND )
set(CMAKE_SWIG_FLAGS -DDOXYGEN=${DOXYGEN_FOUND})
@ -45,7 +61,7 @@ set_target_properties ( ${SWIG_MODULE_yap_REAL_NAME} PROPERTIES NO_SONAME ON )
# generate .i from doxygen .xml
add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doxy2swig.py -n
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doxy2swig.py -n
${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml
${CMAKE_CURRENT_BINARY_DIR}/ftdi1_doc.i
DEPENDS ${CMAKE_BINARY_DIR}/doc/xml/ftdi_8c.xml

View File

@ -0,0 +1,30 @@
from setuptools import setup, Extension
setup(
name = "yap",
version = "0.1",
ext_modules=[Extension('_yap', [ 'yapPYTHON_wrap.cxx',
'${CMAKE_SOURCE_DIR}/packages/python/pl2py.c',
'${CMAKE_SOURCE_DIR}/packages/python/python.c',
'${CMAKE_SOURCE_DIR}/packages/python/py2pl.c',
'${CMAKE_SOURCE_DIR}/packages/python/pl2pl.c',
'${CMAKE_SOURCE_DIR}/packages/python/pypreds.c',
'${CMAKE_SOURCE_DIR}/packages/python/pybips.c'],
define_macros = [('MAJOR_VERSION', '1'),
('MINOR_VERSION', '0'),
('_YAP_NOT_INSTALLED_', '1')],
swig_opts=['-py3', '-c++','-I${CMAKE_SOURCE_DIR}/CXX'],
runtime_library_dirs=['/usr/local/lib'],
library_dirs=['../../..','../../../CXX'],
libraries=['Yap++','Yap'],
include_dirs=['../../..',
'${CMAKE_SOURCE_DIR}/H',
'${CMAKE_SOURCE_DIR}/H/generated',
'${CMAKE_SOURCE_DIR}/OPTYap',
'${CMAKE_SOURCE_DIR}/os',
'${CMAKE_SOURCE_DIR}/include',
'${CMAKE_SOURCE_DIR}/CXX', '.']
)],
py_modules = ['yap']
)
\

View File

@ -1,50 +0,0 @@
# python commands
import sys
import yap
#
# initialize engine
engine = yap.YAPEngine();
# engine = yap.YAPEngine(yap.YAPParams());
def query( s ):
q = engine.query(s)
while q.next():
vs = q.namedVars()
if vs.length() == 0:
print( "yes" )
return
else:
while vs.length() > 0:
eq = vs.car()
print( eq.getArg(1).text() + " = " + eq.getArg(2).text() )
vs = vs.cdr()
if q.deterministic():
return
s = input("next: ?")
if s.find(';') != 0 :
return
print( "no more answers" )
return
def live():
loop = True
while loop:
try:
s = input("?- ")
query( 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
live()

View File

@ -1,62 +1,130 @@
/* example.i */
%module(directors="1") yap
%module(directors="1") yap
// Language independent exception handler
// Language independent exception handler
%include exception.i
%include stdint.i
class YAPPredicate;
class YAPEngine;
%ignore *::operator[];
class YAPPredicate;
class YAPEngine;
#ifdef SWIGPYTHON
#define arity_t uintptr_t
%typemap(out) YAPTerm {
return term_to_python( $1.handle() );
}
%typemap(out) YAPListTerm {
return term_to_python( $1.handle() );
}
%typemap(out) YAPAtomTerm {
return term_to_python( $1.handle() );
}
if ($1.handle() == 0) {
return NULL;
}
}
%extend(out) YAPTerm {
YAPTerm & __getitem__ (size_t i) {
Term t0 = $self->term();
if (IsApplTerm(t0)) {
Functor f = FunctorOfTerm(t0);
if (!IsExtensionFunctor(f))
return *new YAPTerm(ArgOfTerm(i + 1, t0));
} else if (IsPairTerm(t0)) {
if (i==0)
return * new YAPTerm(HeadOfTerm(t0));
else if (i==1)
return * new YAPTerm(TailOfTerm(t0));
}
return * new YAPTerm();
}
}
%typemap(out) YAPIntegerTerm {
return term_to_python( $1.handle() );
}
Term t = $1.term();
Int j = IntegerOfTerm(t);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong(j);
#else
return PyLong_FromLong(j);
#endif
}
%typemap(out) YAPFloatTerm {
Term t = $1.term();
Int double j = FloatOfTerm(t);
return PyFloat_FromDouble(j);
}
// translate well-known names and existing
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPAtomTerm {
const char *s = RepAtom(AtomOfTerm($1.term()))->StrOfAE;
PyObject *p;
if ((p = AtomToPy( s))) {
return p;
}
}
// translate lists as Python Lists
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPListTerm {
Term l =$1.term(), *end;
PyObject *list;
Int len = Yap_SkipList( & l, &end );
$result = list = PyList_New( len );
for (Int i = 0 ; i < len ; i++) {
Term a = HeadOfTerm(l);
YAPTerm *argp1 = new YAPTerm( a );
PyObject *obj0 =
SWIG_NewPointerObj(SWIG_as_voidptr(argp1), SWIGTYPE_p_YAPTerm, 0 | 0 );
l = TailOfTerm(l);
PyList_SetItem( list, i, obj0 );
}
return list;
}
%exception YAPPredicate {
try {
$action
} catch (...) {
PyErr_SetString(PyExc_SyntaxError, "syntax error");
return NULL;
}
try {
$action
} catch (...) {
PyErr_SetString(PyExc_SyntaxError, "syntax error");
return NULL;
}
}
#endif
%exception query {
try {
$action
}
catch (YAPError YAP_SYMTAX_ERROR) {
SWIG_exception(SWIG_SyntaxError,"Syntax Error exception");
}
catch (...) {
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
}
}
try {
$action
}
catch (YAPError YAP_SYMTAX_ERROR) {
SWIG_exception(SWIG_SyntaxError,"Syntax Error exception");
}
catch (...) {
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
}
}
%exception next {
try {
$action
}
catch (...) {
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
}
try {
$action
}
catch (...) {
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
}
}
%{
/* Put header files here or function declarations like below */
%{
/* Put header files here or function declarations like below */
#define YAP_CPP_INTERFACE 1
@ -69,11 +137,11 @@
#define Yap_regp regcache
#endif
// we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't
}
// we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't
}
%}
%}
/* turn on director wrapping Callback */
@ -95,11 +163,19 @@
#ifdef SWIGJAVA
%javaexception("java.text.ParseException") YAPPredicate {
try {
$action
} catch (YAPError::SYNTAX_ERROR &e) {
$action
} catch (YAPError::SYNTAX_ERROR &e) {
jclass clazz = jenv->FindClass("java/text/ParseException");
jenv->ThrowNew(clazz, "Syntax error");
return $null;
}
}
}
}
#endif
%init %{
#ifdef SWIGPYTHON
init_python();
#endif
%}