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

Add support for serialization of OpticGraph

parent 75ec2b45
No related branches found
No related tags found
2 merge requests!11Resolve "Implement property system for nodes",!7Draft: Resolve "Implement rudimental serialization of OpticScenery"
Pipeline #7466 passed
......@@ -4,19 +4,19 @@ use crate::dottable::Dottable;
use crate::error::OpossumError;
use crate::light::Light;
use crate::lightdata::LightData;
use crate::optical::LightResult;
use crate::optical::{LightResult, OpticGraph};
use crate::properties::{Properties, Property, Proptype};
use crate::{optic_ports::OpticPorts, optical::OpticRef, optical::Optical};
use petgraph::prelude::{DiGraph, EdgeIndex, NodeIndex};
use petgraph::visit::EdgeRef;
use petgraph::{algo::*, Direction};
use serde_derive::Serialize;
use serde::Serialize;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
type Result<T> = std::result::Result<T, OpossumError>;
#[derive(Debug, Serialize, Clone)]
#[derive(Debug, Clone)]
/// A node that represents a group of other [`Optical`]s arranges in a subgraph.
///
/// All unconnected input and output ports of this subgraph could be used as ports of
......@@ -55,6 +55,24 @@ fn create_default_props() -> Properties {
prop: Proptype::Bool(false),
},
);
props.set(
"optic graph",
Property {
prop: Proptype::OpticGraph(OpticGraph::default()),
},
);
props.set(
"input port map",
Property {
prop: Proptype::String(("input port map").into()),
},
);
props.set(
"output port map",
Property {
prop: Proptype::String(("output poart map").into()),
},
);
props
}
......@@ -83,7 +101,9 @@ impl NodeGroup {
/// This command just adds an [`Optical`] but does not connect it to existing nodes in the (sub-)graph. The given node is
/// consumed (owned) by the [`NodeGroup`].
pub fn add_node<T: Optical + 'static>(&mut self, node: T) -> NodeIndex {
self.g.add_node(OpticRef(Rc::new(RefCell::new(node))))
let idx=self.g.add_node(OpticRef(Rc::new(RefCell::new(node))));
self.props.set("optic graph", Property { prop: Proptype::OpticGraph(OpticGraph(self.g.clone())) });
idx
}
/// Connect (already existing) nodes denoted by the respective `NodeIndex`.
///
......@@ -622,6 +642,13 @@ impl NodeGroup {
}
}
impl Serialize for NodeGroup {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer {
serializer.serialize_newtype_struct("props", self.properties())
}
}
impl Optical for NodeGroup {
fn node_type(&self) -> &str {
"group"
......
......@@ -2,8 +2,6 @@
use std::collections::HashMap;
use std::fmt::Debug;
use serde_derive::Serialize;
use crate::{
dottable::Dottable,
error::OpossumError,
......@@ -24,7 +22,7 @@ type Result<T> = std::result::Result<T, OpossumError>;
/// - none
/// - Outputs
/// - `out1`
#[derive(Default, Serialize)]
#[derive(Default)]
pub struct Source {
props: Properties,
}
......
use petgraph::prelude::DiGraph;
use serde::ser::SerializeStruct;
use serde::Serialize;
use crate::analyzer::AnalyzerType;
use crate::dottable::Dottable;
use crate::error::OpossumError;
use crate::light::Light;
use crate::lightdata::LightData;
use crate::nodes::NodeGroup;
use crate::optic_ports::OpticPorts;
......@@ -79,6 +81,17 @@ impl Debug for dyn Optical {
}
}
#[derive(Debug, Default, Clone)]
pub struct OpticGraph(pub DiGraph<OpticRef, Light>);
impl Serialize for OpticGraph {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer {
let g=self.0.clone();
serializer.collect_seq(g.node_weights())
}
}
#[derive(Debug, Clone)]
pub struct OpticRef(pub Rc<RefCell<dyn Optical>>);
......
......@@ -3,7 +3,7 @@ use std::collections::HashMap;
use serde::Serialize;
use serde_derive::Serialize;
use crate::{error::OpossumError, lightdata::LightData};
use crate::{error::OpossumError, lightdata::LightData, optical::OpticGraph};
#[derive(Default, Debug, Clone)]
pub struct Properties {
......@@ -59,5 +59,6 @@ pub enum Proptype {
I32(i32),
F64(f64),
Bool(bool),
LightData(Option<LightData>)
LightData(Option<LightData>),
OpticGraph(OpticGraph)
}
\ No newline at end of file
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