yap4py stuff

This commit is contained in:
Vitor Santos Costa 2017-07-01 23:25:22 +01:00
parent d0b2924322
commit ee9e596368
2 changed files with 63 additions and 5 deletions

View File

@ -77,7 +77,8 @@ add_custom_target( YAP4PY_SETUP_DIRS
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
)
add_custom_target( YAP4PY ALL
add_custom_target( YAP4PY ALL
COMMAND ${PYTHON_EXECUTABLE} -m pip uninstall -y YAP4PY
COMMAND ${SWIG_EXECUTABLE} -python -modern -c++ -py3 -DX_API -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/H/generated -I${CMAKE_SOURCE_DIR}/OPTYap -I../../.. -o yap_wrap.cpp yap.i
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
@ -85,8 +86,9 @@ DEPENDS YAP4PY_SETUP)
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f dist yap4py
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})"
install(CODE "execute_process(
COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f packages/python/swig/dist YAP4PY
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})"
DEPENDS Py4YAP ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${dlls} )
install(FILES ${PROLOG_SOURCES} DESTINATION ${libpl})

View File

@ -7,11 +7,12 @@ from collections import namedtuple
from yap import *
class Engine( YAPEngine ):
def __init__(self, args=None):
def __init__(self, args=None,**kwargs):
# type: (object) -> object
if not args:
args = YAPEngineArgs()
args = EngineArgs(**kwargs)
yap_lib_path = os.path.dirname(__file__)
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
args.setYapLibDir(yap_lib_path)
@ -32,11 +33,55 @@ class Engine( YAPEngine ):
class EngineArgs( YAPEngineArgs ):
""" Interface to Engine Options class"""
def __init__(self, args=None,**kwargs):
super().__init__()
class Predicate( YAPPredicate ):
""" Interface to Generic Predicate"""
class Predicate:
"""Goal is a predicate instantiated under a specific environment """
def __init__( self, name, args, module=None, engine = None):
self = namedtuple( name, args )
if module:
self.p = YAPPredicate( name, len(self), module )
else:
self.p = YAPPredicate( name, len(self) )
self.e = engine
def goals( self, engine):
self.e = engine
def __iter__(self):
return PrologTableIter(self.e, self.p)
def holds(self):
return self.e.run(self._make_())
class PrologTableIter:
def __init__(self, e, goal):
try:
self.e = e
self.q = e.YAPQuery(goal)
except:
print('Error')
def __iter__(self):
# Iterators are iterables too.
# Adding this functions to make them so.
return self
def next(self):
if self.q.next():
return goal
else:
self.q.close()
self.q = None
raise StopIteration()
class PrologPredicate( YAPPrologPredicate ):
""" Interface to Prolog Predicate"""
@ -163,6 +208,17 @@ def live(**kwargs):
#
#
def boot_yap(**kwargs):
args = EngineArgs(**kwarg)
yap_lib_path = os.path.dirname(__file__)
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
args.setYapLibDir(yap_lib_path)
args.setSavedState(os.path.join(yap_lib_path,"startup.yss"))
engine = YAPEngine(args)
engine.goal( set_prolog_flag('verbose', 'silent' ) )
engine.goal( use_module(library('yapi') ) )
return engine
if __name__ == "__main__":
engine = boot_yap()
handler = numbervars