Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
add LinkDirectConditional
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 4, 2024
1 parent 5f12d14 commit 28d82e7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/faebryk/core/cpp/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file is auto-generated by nanobind.
# Do not edit this file directly; edit the corresponding
# C++ file instead.
from collections.abc import Set
from collections.abc import Callable, Set
from typing import overload

class Graph:
Expand Down Expand Up @@ -53,6 +53,11 @@ class Link:
class LinkDirect(Link):
def __init__(self) -> None: ...

class LinkDirectConditional(LinkDirect):
def __init__(
self, arg: Callable[[GraphInterface, GraphInterface], bool], /
) -> None: ...

class LinkNamedParent(LinkParent):
def __init__(self, arg: str, /) -> None: ...

Expand All @@ -69,7 +74,7 @@ class Node:
def __init__(self) -> None: ...
def get_graph(self) -> Graph: ...
@property
def self(self) -> GraphInterfaceSelf: ...
def self_gif(self) -> GraphInterfaceSelf: ...
@property
def children(self) -> GraphInterfaceHierarchical: ...
@property
Expand All @@ -89,3 +94,5 @@ class NodeNoParent(Exception):

def add(i: int, j: int = 1) -> int:
"""A function that adds two numbers"""

def call_python_function(func: Callable[[], int]) -> int: ...
1 change: 1 addition & 0 deletions src/faebryk/core/cpp/include/graph/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "util.hpp"
#include <nanobind/stl/function.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/shared_ptr.h>
Expand Down
22 changes: 21 additions & 1 deletion src/faebryk/core/cpp/include/graph/links.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,24 @@ class LinkSibling : public LinkPointer {
public:
LinkSibling();
LinkSibling(GI_ref_weak from, GraphInterfaceSelf *to);
};
};

class LinkDirectConditional : public LinkDirect {

public:
using FilterF = std::function<bool(GI_ref_weak from, GI_ref_weak to)>;

struct LinkFilteredException : public std::runtime_error {
LinkFilteredException(std::string msg)
: std::runtime_error(msg) {
}
};

private:
FilterF filter;

public:
LinkDirectConditional(FilterF filter);
LinkDirectConditional(FilterF filter, GI_ref_weak from, GI_ref_weak to);
void set_connections(GI_ref_weak from, GI_ref_weak to) override;
};
20 changes: 20 additions & 0 deletions src/faebryk/core/cpp/src/graph/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,23 @@ LinkSibling::LinkSibling()
LinkSibling::LinkSibling(GI_ref_weak from, GraphInterfaceSelf *to)
: LinkPointer(from, to) {
}

// LinkDirectConditional ----------------------------------------------------------------
LinkDirectConditional::LinkDirectConditional(FilterF filter)
: LinkDirect()
, filter(filter) {
}

LinkDirectConditional::LinkDirectConditional(FilterF filter, GI_ref_weak from,
GI_ref_weak to)
: LinkDirect(from, to)
, filter(filter) {
this->set_connections(from, to);
}

void LinkDirectConditional::set_connections(GI_ref_weak from, GI_ref_weak to) {
if (!this->filter(from, to)) {
throw LinkFilteredException("LinkDirectConditional filtered");
}
LinkDirect::set_connections(from, to);
}
11 changes: 10 additions & 1 deletion src/faebryk/core/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ int add(int i, int j) {
return i + j;
}

int call_python_function(std::function<int()> func) {
auto out = func();
printf("%d\n", out);
return out;
}

PYMOD(m) {
m.doc() = "faebryk core c++ module";

m.def("add", &add, "i"_a, "j"_a = 1, "A function that adds two numbers");
m.def("call_python_function", &call_python_function, "func"_a);

// Graph
using GI = GraphInterface;
Expand Down Expand Up @@ -80,11 +87,13 @@ PYMOD(m) {
nb::class_<LinkDirect, Link>(m, "LinkDirect").def(nb::init<>());
nb::class_<LinkPointer, Link>(m, "LinkPointer").def(nb::init<>());
nb::class_<LinkSibling, LinkPointer>(m, "LinkSibling").def(nb::init<>());
nb::class_<LinkDirectConditional, LinkDirect>(m, "LinkDirectConditional")
.def(nb::init<LinkDirectConditional::FilterF>());

// Node
FACTORY((nb::class_<Node>(m, "Node")
.def("get_graph", &Node::get_graph)
.def_prop_ro("self", &Node::get_self_gif)
.def_prop_ro("self_gif", &Node::get_self_gif)
.def_prop_ro("children", &Node::get_children_gif)
.def_prop_ro("parent", &Node::get_parent_gif)
.def("get_parent", &Node::get_parent)
Expand Down
13 changes: 11 additions & 2 deletions test/core/cpp/test_importcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@


def test_add():
from faebryk.core.cpp import add
from faebryk.core.cpp import add, call_python_function

assert add(1, 2) == 3
assert add(1) == 2

assert call_python_function(lambda: 1) == 1


def test_cnodes():
from faebryk.core.cpp import LinkNamedParent, Node

n1 = Node()
n2 = Node()

class _Node(Node): ...

n3 = _Node()

n1.children.connect(n2.parent, LinkNamedParent("test"))
print(n2)
print(n1)
print(n3)


def test_cobject():
Expand Down Expand Up @@ -46,4 +53,6 @@ def test_cobject():


if __name__ == "__main__":
test_cnodes()
test_add()
# test_cnodes()
# test_cobject()

0 comments on commit 28d82e7

Please sign in to comment.