Skip to content

Commit

Permalink
Major reorg on cmaboss
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-noel committed Oct 26, 2024
1 parent 1a58c06 commit e1528ba
Show file tree
Hide file tree
Showing 23 changed files with 1,513 additions and 1,027 deletions.
131 changes: 78 additions & 53 deletions engine/python/cmaboss/maboss_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,73 +45,98 @@
January-March 2020
*/

#ifndef MABOSS_CONFIG
#define MABOSS_CONFIG
#define PY_SSIZE_T_CLEAN
#include "maboss_cfg.h"
#include "maboss_net.h"
#include "popmaboss_net.h"

#include <Python.h>
#include <set>
#include "maboss_net.cpp"
#include "src/RunConfig.h"
#include "src/MaBEstEngine.h"

typedef struct {
PyObject_HEAD
RunConfig* config;
} cMaBoSSConfigObject;

static void cMaBoSSConfig_dealloc(cMaBoSSConfigObject *self)
{
delete self->config;
Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject * cMaBoSSConfig_new(PyTypeObject* type, PyObject *args, PyObject* kwargs)
{
Py_ssize_t nb_args = PyTuple_Size(args);

if (nb_args < 2) {
return NULL;
}

cMaBoSSNetworkObject * network = (cMaBoSSNetworkObject*) PyTuple_GetItem(args, 0);

cMaBoSSConfigObject* pyconfig;
pyconfig = (cMaBoSSConfigObject *) type->tp_alloc(type, 0);
pyconfig->config = new RunConfig();

try {
for (Py_ssize_t i = 1; i < nb_args; i++) {
PyObject* bytes = PyUnicode_AsUTF8String(PyTuple_GetItem(args, i));
pyconfig->config->parse(network->network, PyBytes_AsString(bytes));
Py_DECREF(bytes);
}

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

return (PyObject*) pyconfig;
}


static PyMethodDef cMaBoSSConfig_methods[] = {
PyMethodDef cMaBoSSConfig_methods[] = {
{NULL} /* Sentinel */
};

static PyTypeObject cMaBoSSConfig = []{
PyTypeObject cMaBoSSConfig = []{
PyTypeObject net{PyVarObject_HEAD_INIT(NULL, 0)};

net.tp_name = "cmaboss.cMaBoSSConfigObject";
net.tp_name = (std::string(module_name) + std::string("cMaBoSSConfigObject")).c_str();
net.tp_basicsize = sizeof(cMaBoSSConfigObject);
net.tp_itemsize = 0;
net.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
net.tp_doc = "cMaBoSS Network object";
net.tp_init = cMaBoSSConfig_init;
net.tp_new = cMaBoSSConfig_new;
net.tp_dealloc = (destructor) cMaBoSSConfig_dealloc;
net.tp_methods = cMaBoSSConfig_methods;
return net;
}();

#endif
void cMaBoSSConfig_dealloc(cMaBoSSConfigObject *self)
{
delete self->config;
Py_TYPE(self)->tp_free((PyObject *) self);
}

PyObject * cMaBoSSConfig_new(PyTypeObject* type, PyObject *args, PyObject* kwargs)
{
cMaBoSSConfigObject* py_config = (cMaBoSSConfigObject *) type->tp_alloc(type, 0);
py_config->config = new RunConfig();
return (PyObject*) py_config;
}
int cMaBoSSConfig_init(PyObject* self, PyObject *args, PyObject* kwargs)
{
PyObject * py_network = Py_None;
PyObject * config_file = Py_None;
PyObject * config_files = Py_None;
PyObject * config_str = Py_None;

const char *kwargs_list[] = {"network", "config_file", "config_files", "config_str", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "|OOOO", const_cast<char **>(kwargs_list),
&py_network, &config_file, &config_files, &config_str
))
return -1;

Network* network = NULL;

if (py_network != Py_None && PyObject_IsInstance(py_network, (PyObject*)&cMaBoSSNetwork))
{
network = ((cMaBoSSNetworkObject*) py_network)->network;

} else if (py_network != Py_None && PyObject_IsInstance(py_network, (PyObject*)&cPopMaBoSSNetwork))
{
network = ((cPopMaBoSSNetworkObject*) py_network)->network;

} else {
PyErr_SetString(PyBNException, "Invalid network object");
return -1;
}

cMaBoSSConfigObject* py_config = (cMaBoSSConfigObject *) self;

try
{
IStateGroup::reset(network);

if (config_file != Py_None)
{
py_config->config->parse(network, PyUnicode_AsUTF8(config_file));

} else if (config_files != Py_None)
{
for (int i = 0; i < PyList_Size(config_files); i++) {
PyObject* item = PyList_GetItem(config_files, i);
py_config->config->parse(network, PyUnicode_AsUTF8(item));
}

} else if (config_str != Py_None)
{
py_config->config->parseExpression(network, PyUnicode_AsUTF8(config_str));

}

} catch (BNException& e) {
py_config = NULL;
PyErr_SetString(PyBNException, e.getMessage().c_str());
return -1;
}
return 0;
}
64 changes: 64 additions & 0 deletions engine/python/cmaboss/maboss_cfg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
#############################################################################
# #
# BSD 3-Clause License (see https://opensource.org/licenses/BSD-3-Clause) #
# #
# Copyright (c) 2011-2020 Institut Curie, 26 rue d'Ulm, Paris, France #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are #
# met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its #
# contributors may be used to endorse or promote products derived from this #
# software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED #
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A #
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER #
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, #
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR #
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF #
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
# #
#############################################################################
Module:
maboss_cfg.h
Authors:
Vincent Noël <vincent.noel@curie.fr>
Date:
January-March 2020
*/

#ifndef MABOSS_CONFIG
#define MABOSS_CONFIG

#include "maboss_commons.h"

#include "src/RunConfig.h"

typedef struct {
PyObject_HEAD
RunConfig* config;
} cMaBoSSConfigObject;

void cMaBoSSConfig_dealloc(cMaBoSSConfigObject *self);
PyObject * cMaBoSSConfig_new(PyTypeObject* type, PyObject *args, PyObject* kwargs);
int cMaBoSSConfig_init(PyObject* self, PyObject *args, PyObject* kwargs);

#endif
21 changes: 16 additions & 5 deletions engine/python/cmaboss/maboss_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@
January-March 2020
*/

#define PY_SSIZE_T_CLEAN


#ifndef _COMMONS_H_
#define _COMMONS_H_

#include <Python.h>
#include "src/BooleanNetwork.h"
#include "src/MaBEstEngine.h"
#define PY_SSIZE_T_CLEAN

static PyObject *PyBNException = NULL;
#include <Python.h>
#include <structmember.h>

// I use these to define the name of the library, and the init function
// Not sure why we need this 2 level thingy... Came from https://stackoverflow.com/a/1489971/11713763
Expand All @@ -68,4 +66,17 @@ static PyObject *PyBNException = NULL;
#define MODULE_NAME NAME1(cmaboss_, MODULE_NODES)
#endif

extern PyObject *PyBNException;
extern const char module_name[];

extern PyTypeObject cMaBoSSNetwork;
extern PyTypeObject cMaBoSSConfig;
extern PyTypeObject cMaBoSSSim;
extern PyTypeObject cMaBoSSResult;
extern PyTypeObject cMaBoSSResultFinal;
extern PyTypeObject cPopMaBoSSSim;
extern PyTypeObject cPopMaBoSSNetwork;
extern PyTypeObject cPopMaBoSSResult;
extern PyTypeObject cMaBoSSNode;
extern PyTypeObject cMaBoSSParam;
#endif
66 changes: 45 additions & 21 deletions engine/python/cmaboss/maboss_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,35 @@
January-March 2020
*/

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL MABOSS_ARRAY_API
#include <Python.h>
#include <numpy/arrayobject.h>
#include "maboss_sim.cpp"
#include "popmaboss_sim.cpp"

#include "maboss_commons.h"
#include "maboss_node.h"
#include "maboss_sim.h"
#include "popmaboss_sim.h"
#include "maboss_res.h"
#include "maboss_resfinal.h"
#include "popmaboss_res.h"

#if ! defined (MAXNODES) || MAXNODES <= 64
const char module_name[] = "cmaboss";
#else
const char module_name[] = STR(MODULE_NAME);
#endif

PyObject* PyBNException = NULL;

/* define functions in module */
static PyMethodDef cMaBoSS[] =
PyMethodDef cMaBoSS[] =
{
{NULL, NULL, 0, NULL}
};

/* module initialization */
/* Python version 3*/
static struct PyModuleDef cMaBoSSDef =
struct PyModuleDef cMaBoSSDef =
{
PyModuleDef_HEAD_INIT,
#if ! defined (MAXNODES) || MAXNODES <= 64
Expand Down Expand Up @@ -107,12 +119,17 @@ MODULE_INIT_NAME(void)
if (PyType_Ready(&cPopMaBoSSNetwork) < 0){
return NULL;
}
if (PyType_Ready(&cPopMaBoSSConfig) < 0){

if (PyType_Ready(&cPopMaBoSSResult) < 0){
return NULL;
}
if (PyType_Ready(&cMaBoSSParam) < 0){
return NULL;
}
if (PyType_Ready(&cPopMaBoSSResult) < 0){
if (PyType_Ready(&cMaBoSSNode) < 0){
return NULL;
}

m = PyModule_Create(&cMaBoSSDef);

#if ! defined (MAXNODES) || MAXNODES <= 64
Expand All @@ -131,19 +148,26 @@ MODULE_INIT_NAME(void)
return NULL;
}

Py_INCREF(&cMaBoSSParam);
if (PyModule_AddObject(m, "MaBoSSParam", (PyObject *) &cMaBoSSParam) < 0) {
Py_DECREF(&cMaBoSSParam);
Py_DECREF(m);
return NULL;
}

Py_INCREF(&cPopMaBoSSSim);
if (PyModule_AddObject(m, "PopMaBoSSSim", (PyObject *) &cPopMaBoSSSim) < 0) {
Py_DECREF(&cPopMaBoSSSim);
Py_DECREF(m);
return NULL;
}

// Py_INCREF(&cMaBoSSNode);
// if (PyModule_AddObject(m, "MaBoSSNode", (PyObject *) &cMaBoSSNode) < 0) {
// Py_DECREF(&cMaBoSSNode);
// Py_DECREF(m);
// return NULL;
// }
Py_INCREF(&cMaBoSSNode);
if (PyModule_AddObject(m, "MaBoSSNode", (PyObject *) &cMaBoSSNode) < 0) {
Py_DECREF(&cMaBoSSNode);
Py_DECREF(m);
return NULL;
}

Py_INCREF(&cMaBoSSNetwork);
if (PyModule_AddObject(m, "MaBoSSNet", (PyObject *) &cMaBoSSNetwork) < 0) {
Expand All @@ -166,13 +190,6 @@ MODULE_INIT_NAME(void)
return NULL;
}

Py_INCREF(&cPopMaBoSSConfig);
if (PyModule_AddObject(m, "PopMaBoSSCfg", (PyObject *) &cPopMaBoSSConfig) < 0) {
Py_DECREF(&cPopMaBoSSConfig);
Py_DECREF(m);
return NULL;
}

Py_INCREF(&cMaBoSSResult);
if (PyModule_AddObject(m, "cMaBoSSResult", (PyObject *) &cMaBoSSResult) < 0) {
Py_DECREF(&cMaBoSSResult);
Expand All @@ -187,5 +204,12 @@ MODULE_INIT_NAME(void)
return NULL;
}

Py_INCREF(&cPopMaBoSSResult);
if (PyModule_AddObject(m, "cPopMaBoSSResult", (PyObject *) &cPopMaBoSSResult) < 0) {
Py_DECREF(&cPopMaBoSSResult);
Py_DECREF(m);
return NULL;
}

return m;
}
Loading

0 comments on commit e1528ba

Please sign in to comment.