Skip to content

Commit

Permalink
Merge pull request #4 from ngeiswei/add-python-bindings
Browse files Browse the repository at this point in the history
Add python bindings (only atom types for now)
  • Loading branch information
ngeiswei authored Oct 21, 2020
2 parents b9cc4d8 451103a commit dba43ae
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 4 deletions.
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 114,10 @@ IF (NOT CXXTEST_FOUND)
ENDIF (NOT CXXTEST_FOUND)

# ----------------------------------------------------------
# This is required for Guile
# This is required for Guile, Python and Cython

include(OpenCogFindGuile)
include(OpenCogFindPython)

# ----------------------------------------------------------
# Octomap
Expand All @@ -142,8 143,12 @@ IF (NOT DEFINED ATOMSPACE_DATA_DIR)
SET (ATOMSPACE_DATA_DIR "${COGUTIL_DATA_DIR}")
ENDIF (NOT DEFINED ATOMSPACE_DATA_DIR)

INCLUDE("${ATOMSPACE_DATA_DIR}/cmake/OpenCogMacros.cmake")
INCLUDE("${ATOMSPACE_DATA_DIR}/cmake/OpenCogGuile.cmake")
# Add the 'cmake' directory from atomspace to search path (should be
# the same as cogutil, but we add it just in case)
LIST(APPEND CMAKE_MODULE_PATH ${ATOMSPACE_DATA_DIR}/cmake)
INCLUDE(OpenCogMacros)
INCLUDE(OpenCogGuile)
INCLUDE(OpenCogCython)

# ==========================================================
# Decide what to build, based on the packages found.
Expand Down Expand Up @@ -244,7 249,7 @@ FIND_PACKAGE(Doxygen)
# ===================================================================
# Show a summary of what we found, what we will do.

# SUMMARY_ADD("Cython bindings" "Cython (python) bindings" HAVE_CYTHON)
SUMMARY_ADD("Cython bindings" "Cython (python) bindings" HAVE_CYTHON)
SUMMARY_ADD("Doxygen" "Code documentation" DOXYGEN_FOUND)
SUMMARY_ADD("SpaceTime" "3D Space-Time object tracker" HAVE_OCTOMAP)
SUMMARY_ADD("Unit tests" "Unit tests" CXXTEST_FOUND)
Expand Down
4 changes: 4 additions & 0 deletions opencog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 1,6 @@

ADD_SUBDIRECTORY (spacetime)

IF (HAVE_CYTHON)
ADD_SUBDIRECTORY (cython)
ENDIF (HAVE_CYTHON)
6 changes: 6 additions & 0 deletions opencog/cython/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
#
ADD_SUBDIRECTORY (opencog)

# module init
file(MAKE_DIRECTORY opencog)
file(COPY opencog/__init__.py DESTINATION opencog)
17 changes: 17 additions & 0 deletions opencog/cython/README.md
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
# Python bindings for PLN

## Requirements

* Python 2.7, Python 3 recommended.
* Cython 0.14 or later. http://www.cython.org/

The bindings are written mostly using Cython, which allows writing
code that looks pythonic but gets compiled to C. It also makes it
trivial to access both Python objects and C objects without using a
bunch of extra Python library API calls.

## Package structure

Currently the package structure looks like this:

opencog.spacetime
58 changes: 58 additions & 0 deletions opencog/cython/opencog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,58 @@

# Need to use -fno-strict-aliasing when compiling cython code, in order
# to avoid nasty compiler warnings about aliasing. Cython explicitly
# performs aliasing, in order to emulate python object inheritance.
# See, for example,
# https://groups.google.com/forum/#!topic/cython-users/JV1-KvIUeIg
#
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")

INCLUDE_DIRECTORIES(
${ATOMSPACE_INCLUDE_DIR}
${PYTHON_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)

IF (${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3.0.0)
SET(CYTHON_FLAGS "-2" "-f" "-Wextra" # "-Werror"
"-I" "${ATOMSPACE_INCLUDE_DIR}/opencog/cython"
"-I" "${ATOMSPACE_INCLUDE_DIR}/opencog/cython/opencog")
ELSE ()
SET(CYTHON_FLAGS "-3" "-f" "-Wextra" # "-Werror"
"-I" "${ATOMSPACE_INCLUDE_DIR}/opencog/cython"
"-I" "${ATOMSPACE_INCLUDE_DIR}/opencog/cython/opencog")
ENDIF ()

# Use this as a guide:
# https://github.com/OpenKinect/libfreenect/blob/master/wrappers/python/CMakeLists.txt

##################### SpaceTime Types ##################

CYTHON_ADD_MODULE_PYX(spacetime
"spacetime_types.pyx"
)

list(APPEND ADDITIONAL_MAKE_CLEAN_FILES "spacetime_types.cpp")

# opencog.spacetime Python bindings
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})

ADD_LIBRARY(spacetime_cython SHARED
spacetime.cpp
)

ADD_DEPENDENCIES(spacetime_cython spacetime-types)

TARGET_LINK_LIBRARIES(spacetime_cython
spacetime-types
${ATOMSPACE_LIBRARIES}
${PYTHON_LIBRARIES}
)

SET_TARGET_PROPERTIES(spacetime_cython PROPERTIES
PREFIX ""
OUTPUT_NAME spacetime)

INSTALL (TARGETS spacetime_cython
DESTINATION "${PYTHON_DEST}")
2 changes: 2 additions & 0 deletions opencog/cython/opencog/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
8 changes: 8 additions & 0 deletions opencog/cython/opencog/spacetime.pyx
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
# Cython/distutils can only handle a single file as the source for
# a python module. Since it is helpful to be able to split the binding
# code into separate files, we just include them here.
#
# Note that the ordering of include statements may influence whether
# things work or not

include "spacetime_types.pyx"
10 changes: 10 additions & 0 deletions opencog/cython/opencog/spacetime_types.pyx
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
from opencog.atomspace import get_refreshed_types
from opencog.utilities import add_node, add_link

cdef extern :
void spacetime_types_init()

spacetime_types_init()
types = get_refreshed_types()

include "opencog/spacetime/atom-types/spacetime_types.pyx"

0 comments on commit dba43ae

Please sign in to comment.