diff --git a/cmake/OpenCogCython.cmake b/cmake/OpenCogCython.cmake index 475575727a..1c64df2156 100644 --- a/cmake/OpenCogCython.cmake +++ b/cmake/OpenCogCython.cmake @@ -2,22 +2,23 @@ # Additional arguments are dependencies MACRO(CYTHON_ADD_MODULE_PYX name) - SET(DEPENDS ${name}.pyx) + SET(DEPENDS ${name}.pyx) - IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd) - SET(DEPENDS ${DEPENDS} ${name}.pxd) - ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd) + IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd) + SET(DEPENDS ${DEPENDS} ${name}.pxd) + ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pxd) - # Allow the user to specify dependencies as optional arguments - SET(DEPENDS ${DEPENDS} ${ARGN}) + # Allow the user to specify dependencies as optional arguments + SET(DEPENDS ${DEPENDS} ${ARGN}) - ADD_CUSTOM_COMMAND( - OUTPUT ${name}.cpp - COMMAND ${CYTHON_EXECUTABLE} - ARGS ${CYTHON_FLAGS} -I ${PROJECT_BINARY_DIR} -o ${name}.cpp - --cplus ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pyx - DEPENDS ${DEPENDS} - COMMENT "Cythonizing ${name}.pyx") + ADD_CUSTOM_COMMAND( + OUTPUT ${name}.cpp + COMMAND ${CYTHON_EXECUTABLE} + ARGS ${CYTHON_FLAGS} -I ${PROJECT_BINARY_DIR} + -I ${CMAKE_CURRENT_SOURCE_DIR} -o ${name}.cpp + --cplus ${CMAKE_CURRENT_SOURCE_DIR}/${name}.pyx + DEPENDS ${DEPENDS} + COMMENT "Cythonizing ${name}.pyx") - list(APPEND ADDITIONAL_MAKE_CLEAN_FILES "${name}.cpp") + list(APPEND ADDITIONAL_MAKE_CLEAN_FILES "${name}.cpp") ENDMACRO(CYTHON_ADD_MODULE_PYX) diff --git a/examples/python/ground/ground.py b/examples/python/ground/ground.py index b631b29097..d92ab22634 100644 --- a/examples/python/ground/ground.py +++ b/examples/python/ground/ground.py @@ -2,7 +2,7 @@ from opencog.atomspace import AtomSpace, TruthValue from opencog.atomspace import types from opencog.type_constructors import * -from opencog.bindlink import execute_atom +from opencog.exec import execute_atom import mymodule as mm diff --git a/examples/python/stop_go.py b/examples/python/stop_go.py index 8055993de8..2f0590adc8 100755 --- a/examples/python/stop_go.py +++ b/examples/python/stop_go.py @@ -19,7 +19,7 @@ """ from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name -from opencog.bindlink import satisfaction_link +from opencog.exec import satisfaction_link from opencog.type_constructors import * from opencog.logger import Logger, log diff --git a/opencog/cython/CMakeLists.txt b/opencog/cython/CMakeLists.txt index adc472c735..d2fe70fe37 100644 --- a/opencog/cython/CMakeLists.txt +++ b/opencog/cython/CMakeLists.txt @@ -25,6 +25,9 @@ INCLUDE_DIRECTORIES( FILE(MAKE_DIRECTORY opencog) FILE(COPY opencog/__init__.py DESTINATION opencog) +# Legacy bindlink +FILE(COPY opencog/bindlink.py DESTINATION opencog) + ADD_LIBRARY(PythonEval PythonEval.cc ) diff --git a/opencog/cython/opencog/CMakeLists.txt b/opencog/cython/opencog/CMakeLists.txt index 5e5bdd6b35..da43f22bdf 100644 --- a/opencog/cython/opencog/CMakeLists.txt +++ b/opencog/cython/opencog/CMakeLists.txt @@ -21,7 +21,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ) -SET(CYTHON_FLAGS "-2" "-f") +SET(CYTHON_FLAGS "-3" "-f") # Use this as a guide: # https://github.com/OpenKinect/libfreenect/blob/master/wrappers/python/CMakeLists.txt @@ -85,6 +85,7 @@ SET_TARGET_PROPERTIES(atomspace_cython PROPERTIES INSTALL (FILES __init__.py atomspace.pxd + value_types.pxd DESTINATION "include/opencog/cython/opencog" ) @@ -132,36 +133,42 @@ SET_TARGET_PROPERTIES(utilities_cython PROPERTIES PREFIX "" OUTPUT_NAME utilities) -############################## bindlink ##################### +############################## bindlink (exec module) ##################### -CYTHON_ADD_MODULE_PYX(bindlink +CYTHON_ADD_MODULE_PYX(exec "atomspace.pxd" opencog_atom_types ) -ADD_LIBRARY(bindlink_cython - BindlinkStub.cc - bindlink.cpp +ADD_LIBRARY(exec_cython + "BindlinkStub.cc" + "exec.cpp" ) -TARGET_LINK_LIBRARIES(bindlink_cython +TARGET_LINK_LIBRARIES(exec_cython atomspace_cython atomspace ${PYTHON_LIBRARIES} ) -SET_TARGET_PROPERTIES(bindlink_cython PROPERTIES +SET_TARGET_PROPERTIES(exec_cython PROPERTIES PREFIX "" - OUTPUT_NAME bindlink) + OUTPUT_NAME exec) -INSTALL (TARGETS +### install the modules ### +INSTALL(TARGETS atomspace_cython - bindlink_cython + exec_cython logger_cython type_constructors utilities_cython DESTINATION "${PYTHON_DEST}") +# install the legacy bindlink module +INSTALL(FILES + "bindlink.py" + DESTINATION "${PYTHON_DEST}") + IF (HAVE_GUILE) ############################## scheme wrapper ##################### diff --git a/opencog/cython/opencog/atom.pyx b/opencog/cython/opencog/atom.pyx index 75662871ff..d8519e5d43 100644 --- a/opencog/cython/opencog/atom.pyx +++ b/opencog/cython/opencog/atom.pyx @@ -2,64 +2,64 @@ from cpython cimport PyLong_FromLongLong from cpython.object cimport Py_LT, Py_EQ, Py_GT, Py_LE, Py_NE, Py_GE from libcpp.set cimport set as cpp_set +# from atomspace cimport Atom + # Atom wrapper object cdef class Atom(Value): - @staticmethod - cdef Atom createAtom(const cHandle& handle): - return Atom(PtrHolder.create(handle)) - - def __init__(self, ptr_holder): - super(Atom, self).__init__(ptr_holder) + def __cinit__(self, PtrHolder ptr_holder, *args, **kwargs): self.handle = &((ptr_holder).shared_ptr) - # cache the results after first retrieval of - # immutable properties self._atom_type = None self._name = None self._outgoing = None + + @staticmethod + cdef Atom createAtom(const cHandle& handle): + return Atom(PtrHolder.create(handle)) cdef cHandle get_c_handle(Atom self): """Return C++ shared_ptr from PtrHolder instance""" return (self.ptr_holder.shared_ptr) - property atomspace: - def __get__(self): - cdef cAtomSpace* a = self.get_c_handle().get().getAtomSpace() - return AtomSpace_factory(a) - - property name: - def __get__(self): - cdef cAtom* atom_ptr - if self._name is None: - atom_ptr = self.handle.atom_ptr() - if atom_ptr == NULL: # avoid null-pointer deref - return None - if atom_ptr.is_node(): - self._name = atom_ptr.get_name().decode('UTF-8') - else: - self._name = "" - return self._name - - property tv: - def __get__(self): - cdef cAtom* atom_ptr = self.handle.atom_ptr() - cdef tv_ptr tvp + @property + def atomspace(self): + cdef cAtomSpace* a = self.get_c_handle().get().getAtomSpace() + return AtomSpace_factory(a) + + @property + def name(self): + cdef cAtom* atom_ptr + if self._name is None: + atom_ptr = self.handle.atom_ptr() if atom_ptr == NULL: # avoid null-pointer deref return None - tvp = atom_ptr.getTruthValue() - if (not tvp.get()): - raise AttributeError('cAtom returned NULL TruthValue pointer') - return createTruthValue(tvp.get().get_mean(), tvp.get().get_confidence()) - - def __set__(self, truth_value): - try: - assert isinstance(truth_value, TruthValue) - except AssertionError: - raise TypeError("atom.tv property needs a TruthValue object") - cdef cAtom* atom_ptr = self.handle.atom_ptr() - if atom_ptr == NULL: # avoid null-pointer deref - return - atom_ptr.setTruthValue(deref((truth_value)._tvptr())) + if atom_ptr.is_node(): + self._name = atom_ptr.get_name().decode('UTF-8') + else: + self._name = "" + return self._name + + @property + def tv(self): + cdef cAtom* atom_ptr = self.handle.atom_ptr() + cdef tv_ptr tvp + if atom_ptr == NULL: # avoid null-pointer deref + return None + tvp = atom_ptr.getTruthValue() + if (not tvp.get()): + raise AttributeError('cAtom returned NULL TruthValue pointer') + return createTruthValue(tvp.get().get_mean(), tvp.get().get_confidence()) + + @tv.setter + def tv(self, truth_value): + try: + assert isinstance(truth_value, TruthValue) + except AssertionError: + raise TypeError("atom.tv property needs a TruthValue object") + cdef cAtom* atom_ptr = self.handle.atom_ptr() + if atom_ptr == NULL: # avoid null-pointer deref + return + atom_ptr.setTruthValue(deref((truth_value)._tvptr())) def set_value(self, key, value): if not isinstance(key, Atom): @@ -90,30 +90,30 @@ cdef class Atom(Value): cdef vector[cHandle] handle_vector = atom_ptr.getOutgoingSet() return convert_handle_seq_to_python_list(handle_vector) - property out: - def __get__(self): - if self._outgoing is None: - atom_ptr = self.handle.atom_ptr() - if atom_ptr == NULL: # avoid null-pointer deref - return None - if atom_ptr.is_link(): - self._outgoing = self.get_out() - else: - self._outgoing = [] - return self._outgoing - - property arity: - def __get__(self): - return len(self.out) - - property incoming: - def __get__(self): - cdef vector[cHandle] handle_vector - cdef cAtom* atom_ptr = self.handle.atom_ptr() + @property + def out(self): + if self._outgoing is None: + atom_ptr = self.handle.atom_ptr() if atom_ptr == NULL: # avoid null-pointer deref return None - atom_ptr.getIncomingSet(back_inserter(handle_vector)) - return convert_handle_seq_to_python_list(handle_vector) + if atom_ptr.is_link(): + self._outgoing = self.get_out() + else: + self._outgoing = [] + return self._outgoing + + @property + def arity(self): + return len(self.out) + + @property + def incoming(self): + cdef vector[cHandle] handle_vector + cdef cAtom* atom_ptr = self.handle.atom_ptr() + if atom_ptr == NULL: # avoid null-pointer deref + return None + atom_ptr.getIncomingSet(back_inserter(handle_vector)) + return convert_handle_seq_to_python_list(handle_vector) def incoming_by_type(self, Type type): cdef vector[cHandle] handle_vector diff --git a/opencog/cython/opencog/atomspace.pxd b/opencog/cython/opencog/atomspace.pxd index 90264eadab..53b229d628 100644 --- a/opencog/cython/opencog/atomspace.pxd +++ b/opencog/cython/opencog/atomspace.pxd @@ -5,6 +5,7 @@ from libcpp.set cimport set as cpp_set from libcpp.string cimport string from cython.operator cimport dereference as deref + cdef extern from "Python.h": # Tacky hack to pass atomspace pointer to AtomSpace ctor. cdef void* PyLong_AsVoidPtr(object) @@ -55,12 +56,14 @@ cdef extern from "opencog/atoms/value/Value.h" namespace "opencog": cdef class PtrHolder: cdef shared_ptr[void] shared_ptr + @staticmethod cdef PtrHolder create(shared_ptr[void]& ptr) cdef class Value: cdef PtrHolder ptr_holder cdef cValuePtr get_c_value_ptr(self) + @staticmethod cdef Value create(cValuePtr& ptr) @@ -96,9 +99,9 @@ cdef extern from "opencog/atoms/truthvalue/SimpleTruthValue.h" namespace "openco bint operator!=(cTruthValue h) cdef class TruthValue(Value): - cdef _mean(self) - cdef _confidence(self) - cdef _count(self) + cdef strength_t _mean(self) + cdef confidence_t _confidence(self) + cdef count_t _count(self) cdef cTruthValue* _ptr(self) cdef tv_ptr* _tvptr(self) @@ -207,6 +210,7 @@ cdef extern from "opencog/atomspace/AtomSpace.h" namespace "opencog": cdef AtomSpace_factory(cAtomSpace *to_wrap) + cdef class AtomSpace: cdef cAtomSpace *atomspace cdef bint owns_atomspace @@ -215,6 +219,7 @@ cdef class AtomSpace: cdef create_python_value_from_c_value(const cValuePtr& value) + # FloatValue cdef extern from "opencog/atoms/value/FloatValue.h" namespace "opencog": cdef cppclass cFloatValue "opencog::FloatValue": @@ -222,6 +227,7 @@ cdef extern from "opencog/atoms/value/FloatValue.h" namespace "opencog": cFloatValue(const vector[double]& values) const vector[double]& value() const + # StringValue cdef extern from "opencog/atoms/value/StringValue.h" namespace "opencog": cdef cppclass cStringValue "opencog::StringValue": @@ -229,8 +235,12 @@ cdef extern from "opencog/atoms/value/StringValue.h" namespace "opencog": cStringValue(const vector[string]& values) const vector[string]& value() const + # LinkValue cdef extern from "opencog/atoms/value/LinkValue.h" namespace "opencog": cdef cppclass cLinkValue "opencog::LinkValue": cLinkValue(const vector[cValuePtr]& values) const vector[cValuePtr]& value() const + + +include "value_types.pxd" diff --git a/opencog/cython/opencog/atomspace.pyx b/opencog/cython/opencog/atomspace.pyx index 65ea89f018..33167f127e 100644 --- a/opencog/cython/opencog/atomspace.pyx +++ b/opencog/cython/opencog/atomspace.pyx @@ -5,11 +5,11 @@ # Note that the ordering of include statements may influence whether # things work or not -include "nameserver.pyx" -include "truth_value.pyx" -include "atomspace_details.pyx" -include "atom.pyx" include "value.pyx" +include "atomspace_details.pyx" +include "truth_value.pyx" include "float_value.pyx" include "string_value.pyx" +include "atom.pyx" +include "nameserver.pyx" include "link_value.pyx" diff --git a/opencog/cython/opencog/atomspace_details.pyx b/opencog/cython/opencog/atomspace_details.pyx index f943ef6d41..c029aba516 100644 --- a/opencog/cython/opencog/atomspace_details.pyx +++ b/opencog/cython/opencog/atomspace_details.pyx @@ -3,7 +3,8 @@ from libcpp.set cimport set as cpp_set from libcpp.vector cimport vector from cython.operator cimport dereference as deref, preincrement as inc -from atomspace cimport * +# from atomspace cimport * + # @todo use the guide here to separate out into a hierarchy # http://wiki.cython.org/PackageHierarchy diff --git a/opencog/cython/opencog/bindlink.py b/opencog/cython/opencog/bindlink.py new file mode 100644 index 0000000000..b76a63027c --- /dev/null +++ b/opencog/cython/opencog/bindlink.py @@ -0,0 +1,6 @@ +import warnings + +from opencog.exec import * + + +warnings.warn('opencog.bindlink has been renamed to opencog.exec, update your imports.', DeprecationWarning) diff --git a/opencog/cython/opencog/bindlink.pxd b/opencog/cython/opencog/exec.pxd similarity index 100% rename from opencog/cython/opencog/bindlink.pxd rename to opencog/cython/opencog/exec.pxd diff --git a/opencog/cython/opencog/bindlink.pyx b/opencog/cython/opencog/exec.pyx similarity index 53% rename from opencog/cython/opencog/bindlink.pyx rename to opencog/cython/opencog/exec.pyx index f82dd6403f..3676d4cbf2 100644 --- a/opencog/cython/opencog/bindlink.pyx +++ b/opencog/cython/opencog/exec.pyx @@ -1,26 +1,29 @@ -from opencog.atomspace cimport Atom, AtomSpace, TruthValue, PtrHolder +from opencog.atomspace cimport Atom, AtomSpace from opencog.atomspace cimport cAtomSpace, cTruthValue -from opencog.atomspace cimport tv_ptr, strength_t, count_t, shared_ptr -from opencog.atomspace cimport handle_cast -from cython.operator cimport dereference as deref +from opencog.atomspace cimport tv_ptr, strength_t, confidence_t, count_t from opencog.atomspace cimport create_python_value_from_c_value -from opencog.atomspace import is_a, types +from cython.operator cimport dereference as deref + +from opencog.type_constructors import TruthValue + def execute_atom(AtomSpace atomspace, Atom atom): if atom is None: raise ValueError("execute_atom atom is: None") - cdef cValuePtr c_value_ptr = c_execute_atom(atomspace.atomspace, - deref(atom.handle)) + cdef cValuePtr c_value_ptr = c_execute_atom( + atomspace.atomspace, deref(atom.handle) + ) return create_python_value_from_c_value(c_value_ptr) def evaluate_atom(AtomSpace atomspace, Atom atom): if atom is None: raise ValueError("evaluate_atom atom is: None") - cdef tv_ptr result_tv_ptr = c_evaluate_atom(atomspace.atomspace, - deref(atom.handle)) + cdef tv_ptr result_tv_ptr = c_evaluate_atom( + atomspace.atomspace, deref(atom.handle) + ) cdef cTruthValue* result_tv = result_tv_ptr.get() cdef strength_t strength = deref(result_tv).get_mean() - cdef strength_t confidence = deref(result_tv).get_confidence() + cdef confidence_t confidence = deref(result_tv).get_confidence() return TruthValue(strength, confidence) diff --git a/opencog/cython/opencog/float_value.pyx b/opencog/cython/opencog/float_value.pyx index 613f05e06f..f774756794 100644 --- a/opencog/cython/opencog/float_value.pyx +++ b/opencog/cython/opencog/float_value.pyx @@ -9,9 +9,6 @@ def createFloatValue(arg): cdef class FloatValue(Value): - def __init__(self, ptr_holder): - super(FloatValue, self).__init__(ptr_holder) - def to_list(self): return FloatValue.vector_of_doubles_to_list( &((self.get_c_value_ptr().get()).value())) diff --git a/opencog/cython/opencog/link_value.pyx b/opencog/cython/opencog/link_value.pyx index 10bf1855c3..678cb9219a 100644 --- a/opencog/cython/opencog/link_value.pyx +++ b/opencog/cython/opencog/link_value.pyx @@ -9,9 +9,6 @@ def createLinkValue(arg): cdef class LinkValue(Value): - def __init__(self, ptr_holder): - super(LinkValue, self).__init__(ptr_holder) - def to_list(self): return LinkValue.vector_of_values_to_list( &((self.get_c_value_ptr().get()).value())) diff --git a/opencog/cython/opencog/logger.pyx b/opencog/cython/opencog/logger.pyx index 5cc9f15c2d..e89fb1bea5 100644 --- a/opencog/cython/opencog/logger.pyx +++ b/opencog/cython/opencog/logger.pyx @@ -57,18 +57,25 @@ cdef class Logger: if self.not_singleton_logger: del self.clog def __init__(self): self.clog = &logger() - property NONE: - def __get__(self): return NONE - property ERROR: - def __get__(self): return ERROR - property WARN: - def __get__(self): return WARN - property INFO: - def __get__(self): return INFO - property DEBUG: - def __get__(self): return DEBUG - property FINE: - def __get__(self): return FINE + + @property + def NONE(self): return NONE + + @property + def ERROR(self): return ERROR + + @property + def WARN(self): return WARN + + @property + def INFO(self): return INFO + + @property + def DEBUG(self): return DEBUG + + @property + def FINE(self): return FINE + def set_component(self, c): self.clog.set_component(c) cdef _set_level(self,int lvl): diff --git a/opencog/cython/opencog/nameserver.pyx b/opencog/cython/opencog/nameserver.pyx index f30bccc332..1118838585 100644 --- a/opencog/cython/opencog/nameserver.pyx +++ b/opencog/cython/opencog/nameserver.pyx @@ -1,6 +1,6 @@ -from atomspace cimport (cNameServer, nameserver, NOTYPE, string, Type, - AtomSpace, PtrHolder) +# from atomspace cimport cNameServer, nameserver, NOTYPE, Type from libc.string cimport strcmp +from libcpp cimport string import sys diff --git a/opencog/cython/opencog/string_value.pyx b/opencog/cython/opencog/string_value.pyx index 048ad6c3c3..72b87bcf0b 100644 --- a/opencog/cython/opencog/string_value.pyx +++ b/opencog/cython/opencog/string_value.pyx @@ -9,9 +9,6 @@ def createStringValue(arg): cdef class StringValue(Value): - def __init__(self, ptr_holder): - super(StringValue, self).__init__(ptr_holder) - def to_list(self): return StringValue.vector_of_strings_to_list( &((self.get_c_value_ptr().get()).value())) diff --git a/opencog/cython/opencog/truth_value.pyx b/opencog/cython/opencog/truth_value.pyx index ced5f1620a..cadb963b22 100644 --- a/opencog/cython/opencog/truth_value.pyx +++ b/opencog/cython/opencog/truth_value.pyx @@ -1,11 +1,11 @@ from cython.operator cimport dereference as deref from libcpp.memory cimport shared_ptr -from atomspace cimport cTruthValue, cSimpleTruthValue, tv_ptr, Value +# from atomspace cimport cTruthValue, cSimpleTruthValue, tv_ptr, TruthValue, Value def createTruthValue(strength = 1.0, confidence = 1.0): cdef tv_ptr c_ptr c_ptr.reset(new cSimpleTruthValue(strength, confidence)) - return TruthValue(ptr_holder = PtrHolder.create(c_ptr)) + return TruthValue(ptr_holder=PtrHolder.create(c_ptr)) cdef class TruthValue(Value): """ The truth value represents the strength and confidence of @@ -20,37 +20,40 @@ cdef class TruthValue(Value): # exported in opencog.atomspace. To keep it work before proper fix # TruthValue constructor is modified to accept both old parameters # (strength and confidence) and new ptr_holder parameter. - def __init__(self, strength=1.0, confidence=1.0, ptr_holder = None): - cdef tv_ptr c_ptr - if ptr_holder is not None: - super(TruthValue, self).__init__(ptr_holder) - else: - c_ptr.reset(new cSimpleTruthValue(strength, confidence)) - super(TruthValue, self).__init__(PtrHolder.create(c_ptr)) - - property mean: - def __get__(self): return self._mean() - - property confidence: - def __get__(self): return self._confidence() - - property count: - def __get__(self): return self._count() - - cdef _mean(self): + # def __init__(self, strength_t strength=1.0, confidence_t confidence=1.0, PtrHolder ptr_holder=None): + # cdef tv_ptr c_ptr + # if ptr_holder is not None: + # super().__init__(ptr_holder) + # else: + # c_ptr.reset(new cSimpleTruthValue(strength, confidence)) + # super().__init__(PtrHolder.create(c_ptr)) + + @property + def mean(self): + return self._mean() + + @property + def confidence(self): + return self._confidence() + + @property + def count(self): + return self._count() + + cdef strength_t _mean(self): return self._ptr().get_mean() - cdef _confidence(self): + cdef confidence_t _confidence(self): return self._ptr().get_confidence() - cdef _count(self): + cdef count_t _count(self): return self._ptr().get_count() cdef cTruthValue* _ptr(self): return (self.get_c_value_ptr().get()) cdef tv_ptr* _tvptr(self): - return &(self.ptr_holder.shared_ptr) + return &((self.ptr_holder).shared_ptr) def truth_value_ptr_object(self): return PyLong_FromVoidPtr(self._tvptr()) diff --git a/opencog/cython/opencog/type_constructors.pyx b/opencog/cython/opencog/type_constructors.pyx index e7e5adcf92..eaa210db7e 100644 --- a/opencog/cython/opencog/type_constructors.pyx +++ b/opencog/cython/opencog/type_constructors.pyx @@ -6,35 +6,43 @@ # # This imports all the python wrappers for atom creation. # +from opencog.atomspace cimport strength_t, confidence_t import warnings + from opencog.atomspace import (createFloatValue, createLinkValue, - createStringValue, createTruthValue) -from opencog.atomspace import AtomSpace, types -from utilities import add_node, add_link -from utilities import get_default_atomspace, set_default_atomspace + createStringValue, createTruthValue) +from opencog.atomspace import types, AtomSpace +from opencog.utilities import add_node, add_link +from opencog.utilities import get_default_atomspace, set_default_atomspace + def set_type_ctor_atomspace(new_atomspace): warnings.warn('set_type_ctor_atomspace is deprecated, use set_default_atomspace instead', DeprecationWarning) return set_default_atomspace(new_atomspace) + def get_type_ctor_atomspace(): warnings.warn('get_type_ctor_atomspace is deprecated, use get_default_atomspace instead', DeprecationWarning) return get_default_atomspace() + include "opencog/atoms/atom_types/core_types.pyx" + def FloatValue(arg): return createFloatValue(arg) + def LinkValue(arg): return createLinkValue(arg) + def StringValue(arg): return createStringValue(arg) -def TruthValue(strength=1.0, confidence=1.0): - return createTruthValue(strength, confidence) +def TruthValue(strength_t strength=1.0, confidence_t confidence=1.0): + return createTruthValue(strength, confidence) diff --git a/opencog/cython/opencog/utilities.pyx b/opencog/cython/opencog/utilities.pyx index 13a31efc8e..e909565e00 100644 --- a/opencog/cython/opencog/utilities.pyx +++ b/opencog/cython/opencog/utilities.pyx @@ -1,9 +1,11 @@ -from contextlib import contextmanager from cython.operator cimport dereference as deref from opencog.atomspace cimport AtomSpace, Atom, TruthValue -from opencog.atomspace import create_child_atomspace from opencog.atomspace cimport cValuePtr, create_python_value_from_c_value from opencog.atomspace cimport AtomSpace_factory + +from contextlib import contextmanager +from opencog.atomspace import create_child_atomspace + import warnings diff --git a/opencog/cython/opencog/value.pyx b/opencog/cython/opencog/value.pyx index b0a709cec6..7aa9479bb9 100644 --- a/opencog/cython/opencog/value.pyx +++ b/opencog/cython/opencog/value.pyx @@ -1,4 +1,6 @@ from cpython.object cimport Py_EQ, Py_NE +from cython.operator cimport dereference as deref + cdef class PtrHolder: """C++ shared_ptr object wrapper for Python clients. Cython cannot create @@ -16,6 +18,8 @@ cdef class PtrHolder: cdef class Value: """C++ Value object wrapper for Python clients""" + def __cinit__(self, PtrHolder ptr_holder, *args, **kwargs): + self.ptr_holder = ptr_holder @staticmethod cdef Value create(cValuePtr& ptr): @@ -32,13 +36,13 @@ cdef class Value: """Return C++ shared_ptr from PtrHolder instance""" return (self.ptr_holder.shared_ptr) - property type: - def __get__(self): - return self.get_c_value_ptr().get().get_type() + @property + def type(self): + return self.get_c_value_ptr().get().get_type() - property type_name: - def __get__(self): - return get_type_name(self.type) + @property + def type_name(self): + return get_type_name(self.type) def is_atom(self): return is_a(self.type, types.Atom) diff --git a/opencog/cython/opencog/value_types.pxd b/opencog/cython/opencog/value_types.pxd new file mode 100644 index 0000000000..c0b210390f --- /dev/null +++ b/opencog/cython/opencog/value_types.pxd @@ -0,0 +1,22 @@ +cdef class FloatValue(Value): + @staticmethod + cdef vector[double] list_of_doubles_to_vector(list python_list) + + @staticmethod + cdef list vector_of_doubles_to_list(const vector[double]* cpp_vector) + + +cdef class StringValue(Value): + @staticmethod + cdef vector[string] list_of_strings_to_vector(list python_list) + + @staticmethod + cdef list vector_of_strings_to_list(const vector[string]* cpp_vector) + + +cdef class LinkValue(Value): + @staticmethod + cdef vector[cValuePtr] list_of_values_to_vector(list python_list) + + @staticmethod + cdef list vector_of_values_to_list(const vector[cValuePtr]* cpp_vector) diff --git a/tests/cython/PythonEvalUTest.cxxtest b/tests/cython/PythonEvalUTest.cxxtest index 52e6d12a9d..8addca27d9 100644 --- a/tests/cython/PythonEvalUTest.cxxtest +++ b/tests/cython/PythonEvalUTest.cxxtest @@ -189,7 +189,8 @@ public: // Define python functions for use by scheme. std::string eval_res = python->eval( - "from opencog.atomspace import Atom, TruthValue\n" + "from opencog.atomspace import Atom\n" + "from opencog.type_constructors import TruthValue\n" "def add_link(atom1, atom2):\n" " ATOMSPACE = atom1.atomspace\n" " link = ATOMSPACE.add_link(types.ListLink, [atom1, atom2])\n" diff --git a/tests/cython/atomspace/test_atom.py b/tests/cython/atomspace/test_atom.py index c15794f62a..7959b40ac5 100644 --- a/tests/cython/atomspace/test_atom.py +++ b/tests/cython/atomspace/test_atom.py @@ -1,8 +1,8 @@ import unittest from unittest import TestCase -from opencog.atomspace import AtomSpace, Atom, TruthValue -from opencog.bindlink import execute_atom +from opencog.atomspace import Atom +from opencog.exec import execute_atom from opencog.atomspace import types, is_a, get_type, get_type_name, create_child_atomspace diff --git a/tests/cython/atomspace/test_atomspace.py b/tests/cython/atomspace/test_atomspace.py index 5032bbb0a6..cb8fc42da3 100644 --- a/tests/cython/atomspace/test_atomspace.py +++ b/tests/cython/atomspace/test_atomspace.py @@ -1,7 +1,7 @@ from unittest import TestCase import opencog.atomspace -from opencog.atomspace import AtomSpace, TruthValue, Atom +from opencog.atomspace import Atom from opencog.atomspace import types, is_a, get_type, get_type_name, create_child_atomspace from opencog.type_constructors import * diff --git a/tests/cython/atomspace/test_callback_arity.py b/tests/cython/atomspace/test_callback_arity.py index 4d2706cab1..a2fea0acad 100644 --- a/tests/cython/atomspace/test_callback_arity.py +++ b/tests/cython/atomspace/test_callback_arity.py @@ -1,7 +1,9 @@ import unittest + from opencog.utilities import initialize_opencog, finalize_opencog from opencog.type_constructors import * -from opencog.bindlink import execute_atom +from opencog.exec import execute_atom + import __main__ diff --git a/tests/cython/atomspace/test_do_execute.py b/tests/cython/atomspace/test_do_execute.py index e23ac64782..0c9994ba39 100644 --- a/tests/cython/atomspace/test_do_execute.py +++ b/tests/cython/atomspace/test_do_execute.py @@ -2,7 +2,7 @@ import threading from opencog.type_constructors import * -from opencog.bindlink import execute_atom +from opencog.exec import execute_atom from opencog.utilities import initialize_opencog, finalize_opencog from opencog.utilities import push_default_atomspace, get_default_atomspace diff --git a/tests/cython/atomspace/test_floatvalue.py b/tests/cython/atomspace/test_floatvalue.py index f57dc4a429..ac7cd10829 100644 --- a/tests/cython/atomspace/test_floatvalue.py +++ b/tests/cython/atomspace/test_floatvalue.py @@ -1,6 +1,5 @@ import unittest -from opencog.atomspace import AtomSpace from opencog.type_constructors import * from opencog.utilities import initialize_opencog, finalize_opencog diff --git a/tests/cython/atomspace/test_linkvalue.py b/tests/cython/atomspace/test_linkvalue.py index d94c394f12..876a1e6dae 100644 --- a/tests/cython/atomspace/test_linkvalue.py +++ b/tests/cython/atomspace/test_linkvalue.py @@ -1,6 +1,5 @@ import unittest -from opencog.atomspace import AtomSpace from opencog.type_constructors import * from opencog.utilities import initialize_opencog, finalize_opencog diff --git a/tests/cython/atomspace/test_linkvalue_containing_atoms.py b/tests/cython/atomspace/test_linkvalue_containing_atoms.py index 54c3c21835..1bd0df325c 100644 --- a/tests/cython/atomspace/test_linkvalue_containing_atoms.py +++ b/tests/cython/atomspace/test_linkvalue_containing_atoms.py @@ -1,7 +1,6 @@ import unittest import re -from opencog.atomspace import AtomSpace from opencog.type_constructors import * from opencog.utilities import initialize_opencog, finalize_opencog diff --git a/tests/cython/atomspace/test_stringvalue.py b/tests/cython/atomspace/test_stringvalue.py index 8327bf3981..32ffd21221 100644 --- a/tests/cython/atomspace/test_stringvalue.py +++ b/tests/cython/atomspace/test_stringvalue.py @@ -1,6 +1,5 @@ import unittest -from opencog.atomspace import AtomSpace from opencog.type_constructors import * from opencog.utilities import initialize_opencog, finalize_opencog diff --git a/tests/cython/bindlink/test_bindlink.py b/tests/cython/bindlink/test_bindlink.py index e63872ba3a..cb3b47bef7 100644 --- a/tests/cython/bindlink/test_bindlink.py +++ b/tests/cython/bindlink/test_bindlink.py @@ -1,8 +1,8 @@ import unittest import os -from opencog.atomspace import AtomSpace, TruthValue, Atom, types -from opencog.bindlink import execute_atom, evaluate_atom +from opencog.atomspace import Atom, types +from opencog.exec import execute_atom, evaluate_atom from opencog.type_constructors import * diff --git a/tests/cython/bindlink/test_functions.py b/tests/cython/bindlink/test_functions.py index 1712db2bba..fb90ef815a 100644 --- a/tests/cython/bindlink/test_functions.py +++ b/tests/cython/bindlink/test_functions.py @@ -1,7 +1,7 @@ # # Test Functions -from opencog.atomspace import types, Atom, TruthValue +from opencog.atomspace import types, Atom from opencog.type_constructors import * diff --git a/tests/cython/guile/test_exception.py b/tests/cython/guile/test_exception.py index 9163c0ca7f..f4248a70db 100644 --- a/tests/cython/guile/test_exception.py +++ b/tests/cython/guile/test_exception.py @@ -1,7 +1,9 @@ import unittest + from opencog.utilities import initialize_opencog, finalize_opencog +from opencog.atomspace import AtomSpace from opencog.type_constructors import * -from opencog.bindlink import evaluate_atom +from opencog.exec import evaluate_atom from opencog.scheme_wrapper import scheme_eval import __main__ @@ -9,7 +11,6 @@ # All of these tests try to make sure that python doesn't # crash when a C++ exception is thrown. class TestExceptions(unittest.TestCase): - def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) @@ -20,22 +21,23 @@ def tearDown(self): def test_bogus_scheme(self): try: - code = '''(Get (Concept "a") (Concept "a") (Concept "a"))''' - scheme_eval(self.space, code) - self.assertFalse("call should fail") + code = """(Get (Concept "a") (Concept "a") (Concept "a"))""" + scheme_eval(self.space, code) + self.assertFalse("call should fail") except RuntimeError as e: - # Use `nosetests3 --nocapture` to see this print... - print("The exception message is " + str(e)) - self.assertTrue("Expecting" in str(e)) + # Use `nosetests3 --nocapture` to see this print... + print("The exception message is " + str(e)) + self.assertTrue("Expecting" in str(e)) def test_bogus_path(self): try: - code = '''(load-from-path "/blargle/Betelgeuse")''' - scheme_eval(self.space, code) - self.assertFalse("call should fail") + code = """(load-from-path "/blargle/Betelgeuse")""" + scheme_eval(self.space, code) + self.assertFalse("call should fail") except RuntimeError as e: - # Use `nosetests3 --nocapture` to see this print... - print("The exception message is " + str(e)) - self.assertTrue("Unable to find" in str(e)) + # Use `nosetests3 --nocapture` to see this print... + print("The exception message is " + str(e)) + self.assertTrue("Unable to find" in str(e)) + # ===================== END OF FILE ================= diff --git a/tests/cython/guile/test_pattern.py b/tests/cython/guile/test_pattern.py index 180cbbe2ec..1cd2999bd7 100644 --- a/tests/cython/guile/test_pattern.py +++ b/tests/cython/guile/test_pattern.py @@ -1,6 +1,7 @@ from unittest import TestCase -from opencog.atomspace import AtomSpace, TruthValue, Atom +from opencog.atomspace import AtomSpace, Atom +from opencog.type_constructors import TruthValue from opencog.atomspace import types, is_a, get_type, get_type_name from opencog.scheme_wrapper import scheme_eval, scheme_eval_h import os diff --git a/tests/cython/utilities/utilities.py b/tests/cython/utilities/utilities.py index 7aaff9f421..663b47a597 100644 --- a/tests/cython/utilities/utilities.py +++ b/tests/cython/utilities/utilities.py @@ -1,7 +1,7 @@ from opencog.atomspace import AtomSpace, types from opencog.utilities import initialize_opencog, finalize_opencog -from opencog.bindlink import execute_atom +from opencog.exec import execute_atom import opencog.scheme_wrapper as scheme from opencog.scheme_wrapper import scheme_eval from opencog.type_constructors import * diff --git a/tests/scm/scm-python-arity.scm b/tests/scm/scm-python-arity.scm index 3fe189b66a..c26e7c751c 100644 --- a/tests/scm/scm-python-arity.scm +++ b/tests/scm/scm-python-arity.scm @@ -14,7 +14,8 @@ ; Define a python func returning a TV (python-eval " -from opencog.atomspace import AtomSpace, TruthValue, types +from opencog.atomspace import AtomSpace, types +from opencog.type_constructors import TruthValue # Twiddle some atoms in the atomspace diff --git a/tests/scm/scm-python-shared-atomspace.scm b/tests/scm/scm-python-shared-atomspace.scm index 6a2fb5b9d3..aa79193f5f 100644 --- a/tests/scm/scm-python-shared-atomspace.scm +++ b/tests/scm/scm-python-shared-atomspace.scm @@ -16,8 +16,8 @@ ; Define a python func returning a TV (python-eval " -from opencog.atomspace import AtomSpace, TruthValue, types -from opencog.type_constructors import get_type_ctor_atomspace +from opencog.atomspace import AtomSpace, types +from opencog.type_constructors import get_type_ctor_atomspace, TruthValue # Twiddle some atoms in the atomspace