From 4c9a7fc301b190b7eee89d2c499a4d0d1c535781 Mon Sep 17 00:00:00 2001 From: Mickael GUIBERT <mickael.guibert@cea.fr> Date: Wed, 6 Nov 2024 10:21:03 +0000 Subject: [PATCH] feat: Add SimpleGraphMatching binding function --- python_binding/graph/pybind_Matching.cpp | 51 ++++++++++++++++++++++++ python_binding/pybind_core.cpp | 2 + 2 files changed, 53 insertions(+) create mode 100644 python_binding/graph/pybind_Matching.cpp diff --git a/python_binding/graph/pybind_Matching.cpp b/python_binding/graph/pybind_Matching.cpp new file mode 100644 index 000000000..94f2471c3 --- /dev/null +++ b/python_binding/graph/pybind_Matching.cpp @@ -0,0 +1,51 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> +#include <memory> +#include <string> +#include "aidge/graph/Matching.hpp" +#include "aidge/graph/GraphView.hpp" +#include "aidge/graph/Node.hpp" +#include "aidge/utils/Types.h" +//#include "aidge/data/Data.hpp" + +namespace py = pybind11; +namespace Aidge { +void init_SinglePassGraphMatching(py::module& m) { + py::class_<Aidge::SinglePassGraphMatching::MatchingResult>(m,"MatchingResult") + .def(py::init<>()) + .def_readwrite("graph", &Aidge::SinglePassGraphMatching::MatchingResult::graph) + .def_readwrite("anchors", &Aidge::SinglePassGraphMatching::MatchingResult::anchors) + .def_readwrite("startNode", &Aidge::SinglePassGraphMatching::MatchingResult::startNode); + + py::class_<Aidge::SinglePassGraphMatching>(m, "SinglePassGraphMatching") + .def(py::init<std::shared_ptr<GraphView>>(), py::arg("graph")) + .def("match", + [](Aidge::SinglePassGraphMatching& self, const std::string& query, bool disjoint){ + // Note: Need to convert set to vector has MatchingResult is not hashable and + // set<MatchingResult> cannot be binded + std::set<Aidge::SinglePassGraphMatching::MatchingResult> set_res = self.match(query, disjoint); + std::vector<Aidge::SinglePassGraphMatching::MatchingResult> vec_res(set_res.begin(), set_res.end()); + return vec_res; + }, + py::arg("query"), py::arg("disjoint") = false, + R"mydelimiter( Matches a query by direct, single-pass parse and match. + :param query: The query string to search. + :param disjoint: If true, only keep the longest disjoint matches. + :return: A set of MatchingResult instances. + )mydelimiter"); + + + +} +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index 53e5ec0fe..2730f8e0b 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -82,6 +82,7 @@ void init_Node(py::module&); void init_GraphView(py::module&); void init_OpArgs(py::module&); void init_Connector(py::module&); +void init_SinglePassGraphMatching(py::module&); void init_GraphRegex(py::module&); void init_MatchSolution(py::module&); @@ -109,6 +110,7 @@ void init_Aidge(py::module& m) { init_GraphView(m); init_OpArgs(m); init_Connector(m); + init_SinglePassGraphMatching(m); init_OperatorImpl(m); init_Log(m); -- GitLab