Skip to content
Snippets Groups Projects
Commit a55c59fb authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Add preliminary serializetion of graph edges.

parent b31376eb
No related branches found
No related tags found
1 merge request!7Draft: Resolve "Implement rudimental serialization of OpticScenery"
Pipeline #7491 passed
......@@ -56,7 +56,7 @@ fn create_default_props() -> Properties {
},
);
props.set(
"optic graph",
"graph",
Property {
prop: Proptype::OpticGraph(OpticGraph::default()),
},
......@@ -70,7 +70,7 @@ fn create_default_props() -> Properties {
props.set(
"output port map",
Property {
prop: Proptype::String(("output poart map").into()),
prop: Proptype::String(("output port map").into()),
},
);
props
......@@ -102,7 +102,7 @@ impl NodeGroup {
/// consumed (owned) by the [`NodeGroup`].
pub fn add_node<T: Optical + 'static>(&mut self, node: T) -> NodeIndex {
let idx=self.g.0.add_node(OpticRef(Rc::new(RefCell::new(node))));
self.props.set("optic graph", Property { prop: Proptype::OpticGraph(OpticGraph(self.g.0.clone())) });
self.props.set("graph", Property { prop: Proptype::OpticGraph(OpticGraph(self.g.0.clone())) });
idx
}
/// Connect (already existing) nodes denoted by the respective `NodeIndex`.
......@@ -171,8 +171,10 @@ impl NodeGroup {
let edge_index = self
.g.0
.add_edge(src_node, target_node, Light::new(src_port, target_port));
self.props.set("graph", Property { prop: Proptype::OpticGraph(OpticGraph(self.g.0.clone())) });
if is_cyclic_directed(&self.g.0) {
self.g.0.remove_edge(edge_index);
self.props.set("graph", Property { prop: Proptype::OpticGraph(OpticGraph(self.g.0.clone())) });
return Err(OpossumError::OpticScenery(
"connecting the given nodes would form a loop".into(),
));
......
......@@ -89,9 +89,15 @@ impl Serialize for OpticGraph {
where
S: serde::Serializer {
let g=self.0.clone();
serializer.collect_seq(g.node_weights())
let mut graph=serializer.serialize_struct("graph", 2)?;
let nodes=g.node_weights().map(|n| n.to_owned()).collect::<Vec<OpticRef>>();
graph.serialize_field("nodes", &nodes)?;
let edges=g.edge_weights().map(|n| (n.src_port(),n.target_port()).to_owned()).collect::<Vec<(&str,&str)>>();
graph.serialize_field("edges", &edges)?;
graph.end()
}
}
#[derive(Debug, Clone)]
pub struct OpticRef(pub Rc<RefCell<dyn Optical>>);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment