How to implement UPPAAL external function files in calls to python scripts

159 views Asked by At

How to implement UPPAAL external function files in calls to python scripts. Functions that use python scripts in dynamically linked cpp files, such as Py_Initialize(), will report an error. Syntax checking shows that the xxx.so file cannot be found.

The external functions linked in the UPPAAL tool are as follows:

// Place global declarations here.
int a = 3;
int b = 5;

import "/home/lyt/Uppaal/project/case2/demo.so" {
    int linkPythonfunc(int a,int b);
};

External functions are written and compiled in c++, where python scripts are called:

#include<python3.8/Python.h>
#include<iostream>

using namespace std;

extern "C" int linkPythonfunc(int a,int b);

extern "C" int linkPythonfunc(int a,int b) {
      
      Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
      
      PyRun_SimpleString("import sys");
      PyRun_SimpleString("sys.path.append('./')");
      
      PyObject* pModule = PyImport_ImportModule("func");
      
      PyObject* pFunc = PyObject_GetAttrString(pModule,"add");
      
      PyObject* pArgs = PyTuple_New(2);
      
      PyTuple_SetItem(pArgs,0,Py_BuildValue("i",a));
      PyTuple_SetItem(pArgs,1,Py_BuildValue("i",b));
      
      PyObject* pReturn = PyEval_CallObject(pFunc,pArgs);
      
      int nResult;
      
      PyArg_Parse(pReturn, "i", &nResult);

      Py_Finalize();
      return nResult;
}

To compile a create a shared object file:

lyt@ubuntu:~/Uppaal/project/case2$ g++ -std=c++17 -fPIC -c -o demo.o demo.cpp -lpython3.8
lyt@ubuntu:~/Uppaal/project/case2$ gcc -shared -o demo.so demo.o

However, UPPAAL displays that ”Could not load library named /home/lyt/UPPAAL/project/case2/demo.so“

0

There are 0 answers