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

Commit

Permalink
add links
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Oct 30, 2024
1 parent 556fcb1 commit 8a16b76
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/faebryk/core/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def compile_and_load():
+ "# This file is auto-generated by nanobind.\n"
+ "# Do not edit this file directly; edit the corresponding\n"
+ "# C++ file instead.\n"
+ "from typing import overload\n"
# + "from typing import overload\n"
+ pyi_source.read_text(),
is_pyi=True,
)
Expand Down
28 changes: 14 additions & 14 deletions src/faebryk/core/cpp/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from typing import overload

class Graph:
def __init__(self) -> None: ...
def get_edges(self, arg: GraphInterface, /) -> set[GraphInterface]: ...
def get_edges(self, arg: GraphInterface, /) -> dict[GraphInterface, Link]: ...
def invalidate(self) -> None: ...
@property
def node_count(self) -> int: ...
Expand All @@ -20,13 +20,13 @@ class GraphInterface:
def __init__(self) -> None: ...
def __repr__(self) -> str: ...
def get_graph(self) -> Graph: ...
def get_edges(self) -> set[GraphInterface]: ...
def get_gif_edges(self) -> set[GraphInterface]: ...
@property
def edges(self) -> dict[GraphInterface, Link]: ...
@overload
def connect(self, arg: GraphInterface, /) -> None: ...
@overload
def connect(self, arg0: GraphInterface, arg1: Link, /) -> None: ...
@overload
def connect(self, arg0: GraphInterface, arg1: type, /) -> None: ...

class GraphInterfaceHierarchical(GraphInterface):
def __init__(self, is_parent: bool) -> None: ...
Expand All @@ -40,20 +40,20 @@ class GraphInterfaceSelf(GraphInterface):
class Link:
pass

class LinkDirect:
pass
class LinkDirect(Link):
def __init__(self) -> None: ...

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

class LinkParent:
pass
class LinkParent(Link):
def __init__(self) -> None: ...

class LinkPointer:
pass
class LinkPointer(Link):
def __init__(self) -> None: ...

class LinkSibling:
pass
class LinkSibling(LinkPointer):
def __init__(self) -> None: ...

def add(i: int, j: int = 1) -> int:
"""A function that adds two numbers"""
75 changes: 48 additions & 27 deletions src/faebryk/core/cpp/include/graph/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <nanobind/stl/pair.h>
#include <nanobind/stl/shared_ptr.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/unordered_map.h>
#include <nanobind/stl/unordered_set.h>
#include <sstream>
#include <unordered_map>
#include <vector>

namespace nb = nanobind;
Expand Down Expand Up @@ -50,14 +50,14 @@ class GraphInterface {
}

// Graph stuff
std::unordered_set<GI_ref_weak> get_edges();
std::unordered_set<GI_ref_weak> get_gif_edges();
std::unordered_map<GI_ref_weak, Link_ref> get_edges();

std::shared_ptr<Graph> get_graph() {
return this->G;
}

void connect(GI_ref_weak other);
void connect(GI_ref_weak other, nb::type_object link_type);
void connect(GI_ref_weak other, Link_ref link);

// TODO remove, gives class vtable
Expand All @@ -76,17 +76,37 @@ class GraphInterface {
class Link {
GI_ref_weak from;
GI_ref_weak to;
bool setup = false;

protected:
Link()
: from(nullptr)
, to(nullptr)
, setup(false) {
}
Link(GI_ref_weak from, GI_ref_weak to)
: from(from)
, to(to) {
, to(to)
, setup(true) {
}

public:
std::pair<GI_ref_weak, GI_ref_weak> get_connections() {
if (!this->setup) {
throw std::runtime_error("link not setup");
}
return {this->from, this->to};
}

virtual void set_connections(GI_ref_weak from, GI_ref_weak to) {
this->from = from;
this->to = to;
this->setup = true;
}

bool is_setup() {
return this->setup;
}
};

/**
Expand All @@ -95,6 +115,9 @@ class Link {
*/
class LinkDirect : public Link {
public:
LinkDirect()
: Link() {
}
LinkDirect(GI_ref_weak from, GI_ref_weak to)
: Link(from, to) {
}
Expand All @@ -114,8 +137,10 @@ class Graph {
this->v.insert(gi);
}

void add_edge(GI_ref_weak from, GI_ref_weak to, Link_ref link) {
printf("add_edge from %s to %s\n", from->repr().c_str(), to->repr().c_str());
void add_edge(Link_ref link) {
auto [from, to] = link->get_connections();

// printf("add_edge from %s to %s\n", from->repr().c_str(), to->repr().c_str());

if (from->G.get() != this || to->G.get() != this) {
Graph *G_target = this;
Expand All @@ -127,7 +152,7 @@ class Graph {
} else {
throw std::runtime_error("neither node in graph");
}
printf("Merging graphs: %p ---> %p\n", G_source, G_target);
// printf("Merging graphs: %p ---> %p\n", G_source, G_target);

assert(G_source->v.size() > 0);
// make shared_ptr, while in scope
Expand All @@ -154,16 +179,18 @@ class Graph {
this->e_cache_simple.merge(other.e_cache_simple);
}

std::unordered_set<GI_ref_weak> get_edges(GI_ref_weak from) {
std::unordered_set<GI_ref_weak> get_gif_edges(GI_ref_weak from) {
return this->e_cache_simple[from];
}

std::unordered_map<GI_ref_weak, Link_ref> get_edges(GI_ref_weak from) {
return this->e_cache[from];
}

Graph() {
printf("Graph::Graph(): %p\n", this);
}

~Graph() {
printf("Graph::~Graph(): %p\n", this);
if (!this->invalidated) {
printf("WARNING: graph not invalidated\n");
// throw std::runtime_error("graph not invalidated");
Expand Down Expand Up @@ -199,7 +226,6 @@ class Graph {
}

void invalidate() {
printf("Invalidating graph: %p\n", this);
this->invalidated = true;
this->v.clear();
}
Expand All @@ -220,37 +246,32 @@ class Graph {
}
};

Set<GI_ref_weak> GraphInterface::get_edges() {
return this->G->get_edges(this);
Set<GI_ref_weak> GraphInterface::get_gif_edges() {
return this->G->get_gif_edges(this);
}

void GraphInterface::connect(GI_ref_weak other) {
auto link = std::make_shared<LinkDirect>(this, other);
this->connect(other, link);
std::unordered_map<GI_ref_weak, Link_ref> GraphInterface::get_edges() {
return this->G->get_edges(this);
}

void GraphInterface::connect(GI_ref_weak other, nb::type_object link_type) {
// printf("Link type: %s\n", link_type)
// TODO
if (!link_type.is_type()) {
throw std::runtime_error("not a type");
}
throw std::runtime_error("not implemented");
void GraphInterface::connect(GI_ref_weak other) {
auto link = std::make_shared<LinkDirect>(this, other);
this->connect(other, link);
this->G->add_edge(link);
}

void GraphInterface::connect(GI_ref_weak other, Link_ref link) {
this->G->add_edge(this, other, link);
if (link->is_setup()) {
throw std::runtime_error("link already setup");
}
link->set_connections(this, other);
this->G->add_edge(link);
}

GraphInterface::~GraphInterface() {
printf("%s::~GraphInterface(): %p\n", get_type_name(this).c_str(), this);
}

GraphInterface::GraphInterface()
: G(std::make_shared<Graph>()) {
printf("GraphInterface::GraphInterface(): %p\n", this);
}

void GraphInterface::register_graph(std::shared_ptr<GraphInterface> gi) {
Expand Down
31 changes: 30 additions & 1 deletion src/faebryk/core/cpp/include/links.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ class LinkParent : public Link {
GraphInterfaceHierarchical *child;

public:
LinkParent()
: Link()
, parent(nullptr)
, child(nullptr) {
}
LinkParent(GraphInterfaceHierarchical *from, GraphInterfaceHierarchical *to)
: Link(from, to) {
: Link(from, to)
, parent(nullptr)
, child(nullptr) {
this->set_connections(from, to);
}

void set_connections(GraphInterfaceHierarchical *from,
GraphInterfaceHierarchical *to) {
Link::set_connections(from, to);
if (from->get_is_parent() && !to->get_is_parent()) {
this->parent = from;
this->child = to;
Expand All @@ -26,10 +39,16 @@ class LinkParent : public Link {
}

GraphInterfaceHierarchical *get_parent() {
if (!this->is_setup()) {
throw std::runtime_error("link not setup");
}
return this->parent;
}

GraphInterfaceHierarchical *get_child() {
if (!this->is_setup()) {
throw std::runtime_error("link not setup");
}
return this->child;
}
};
Expand All @@ -38,6 +57,10 @@ class LinkNamedParent : public LinkParent {
std::string name;

public:
LinkNamedParent(std::string name)
: LinkParent()
, name(name) {
}
LinkNamedParent(std::string name, GraphInterfaceHierarchical *from,
GraphInterfaceHierarchical *to)
: LinkParent(from, to)
Expand All @@ -59,6 +82,9 @@ class LinkDirectShallow : public LinkDirect {
class LinkPointer : public Link {
// TODO
public:
LinkPointer()
: Link() {
}
LinkPointer(GI_ref_weak from, GI_ref_weak to)
: Link(from, to) {
}
Expand All @@ -69,6 +95,9 @@ class LinkPointer : public Link {
*/
class LinkSibling : public LinkPointer {
public:
LinkSibling()
: LinkPointer() {
}
LinkSibling(GI_ref_weak from, GI_ref_weak to)
: LinkPointer(from, to) {
}
Expand Down
3 changes: 1 addition & 2 deletions src/faebryk/core/cpp/include/nano.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ template <typename Func> new__(Func &&f) -> new__<Func>;
{ \
auto _ = pyclass; \
new__(newc).execute(_, ##__VA_ARGS__); \
\
}
}
28 changes: 14 additions & 14 deletions src/faebryk/core/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
#error "C++20 is required"
#endif

namespace nb = nanobind;
using namespace nb::literals;

#if EDITABLE
#define PYMOD(m) NB_MODULE(faebryk_core_cpp_editable, m)
#warning "EDITABLE"
#else
#define PYMOD(m) NB_MODULE(faebryk_core_cpp, m)
#endif

// #include <nanobind/nb_types.h>

namespace nb = nanobind;
using namespace nb::literals;
// -------------------------------------------------------------------------------------

int add(int i, int j) {
Expand All @@ -40,15 +41,12 @@ PYMOD(m) {
FACTORY(nb::class_<GI>(m, "GraphInterface")
.def("__repr__", &GI::repr)
.def("get_graph", &GI::get_graph)
.def("get_edges", &GI::get_edges)
.def("get_gif_edges", &GI::get_gif_edges)
.def_prop_ro("edges", &GI::get_edges)
.def("connect", nb::overload_cast<GI_ref_weak>(&GI::connect))
.def("connect", nb::overload_cast<GI_ref_weak, Link_ref>(&GI::connect))
.def("connect",
nb::overload_cast<GI_ref_weak, nb::type_object>(&GI::connect)),
.def("connect", nb::overload_cast<GI_ref_weak, Link_ref>(&GI::connect)),
&GraphInterface::factory);

nb::class_<Link>(m, "Link");

nb::class_<Graph>(m, "Graph")
.def(nb::init<>())
.def("get_edges", &Graph::get_edges)
Expand All @@ -69,9 +67,11 @@ PYMOD(m) {
&GraphInterfaceHierarchical::factory, "is_parent"_a);

// Links
nb::class_<LinkParent>(m, "LinkParent");
nb::class_<LinkNamedParent>(m, "LinkNamedParent");
nb::class_<LinkDirect>(m, "LinkDirect");
nb::class_<LinkPointer>(m, "LinkPointer");
nb::class_<LinkSibling>(m, "LinkSibling");
nb::class_<Link>(m, "Link");
nb::class_<LinkParent, Link>(m, "LinkParent").def(nb::init<>());
nb::class_<LinkNamedParent, LinkParent>(m, "LinkNamedParent")
.def(nb::init<std::string>());
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<>());
}
Loading

0 comments on commit 8a16b76

Please sign in to comment.