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

Apply all properties to a node during d13n

parent ace314fc
No related branches found
No related tags found
1 merge request!16Implement basic serialization / deserialization of OPOSSUM models
Pipeline #7560 failed
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use opossum::{optical::OpticRef, nodes::Dummy, error::OpossumError}; use opossum::{optical::OpticRef, nodes::{Dummy, Detector}, error::OpossumError};
fn main() -> Result<(), OpossumError>{ fn main() -> Result<(), OpossumError>{
let optic_ref=OpticRef(Rc::new(RefCell::new(Dummy::default()))); let optic_ref=OpticRef(Rc::new(RefCell::new(Detector::default())));
let serialized= serde_yaml::to_string(&optic_ref).unwrap(); let serialized= serde_yaml::to_string(&optic_ref).unwrap();
......
...@@ -10,6 +10,9 @@ mod reference; ...@@ -10,6 +10,9 @@ mod reference;
mod source; mod source;
mod spectrometer; mod spectrometer;
use std::cell::RefCell;
use std::rc::Rc;
pub use beam_splitter::BeamSplitter; pub use beam_splitter::BeamSplitter;
pub use detector::Detector; pub use detector::Detector;
pub use dummy::Dummy; pub use dummy::Dummy;
...@@ -24,3 +27,14 @@ pub use energy_meter::Metertype; ...@@ -24,3 +27,14 @@ pub use energy_meter::Metertype;
pub use spectrometer::Spectrometer; pub use spectrometer::Spectrometer;
pub use spectrometer::SpectrometerType; pub use spectrometer::SpectrometerType;
use crate::error::OpossumError;
use crate::optical::OpticRef;
pub fn create_node_ref(node_type: &str) -> Result<OpticRef,OpossumError> {
match node_type {
"dummy" => Ok(OpticRef(Rc::new(RefCell::new(Dummy::default())))),
"detector" => Ok(OpticRef(Rc::new(RefCell::new(Detector::default())))),
_ => Err(OpossumError::Other(format!("cannot create node type {}", node_type)))
}
}
\ No newline at end of file
...@@ -3,7 +3,7 @@ use crate::dottable::Dottable; ...@@ -3,7 +3,7 @@ use crate::dottable::Dottable;
use crate::error::OpossumError; use crate::error::OpossumError;
use crate::light::Light; use crate::light::Light;
use crate::lightdata::LightData; use crate::lightdata::LightData;
use crate::nodes::{Dummy, NodeGroup}; use crate::nodes::{create_node_ref, Dummy, NodeGroup};
use crate::optic_ports::OpticPorts; use crate::optic_ports::OpticPorts;
use crate::properties::{Properties, Property}; use crate::properties::{Properties, Property};
use core::fmt::Debug; use core::fmt::Debug;
...@@ -74,6 +74,16 @@ pub trait Optical: Dottable { ...@@ -74,6 +74,16 @@ pub trait Optical: Dottable {
fn set_property(&mut self, _name: &str, _prop: Property) -> Result<()> { fn set_property(&mut self, _name: &str, _prop: Property) -> Result<()> {
Ok(()) Ok(())
} }
fn set_properties(&mut self, properties: &Properties) -> Result<()> {
let own_properties= self.properties().props.clone();
for prop in properties.props.iter() {
if own_properties.contains_key(prop.0) {
self.set_property(prop.0, prop.1.clone())?;
}
}
Ok(())
}
} }
impl Debug for dyn Optical { impl Debug for dyn Optical {
...@@ -212,9 +222,15 @@ impl<'de> Deserialize<'de> for OpticRef { ...@@ -212,9 +222,15 @@ impl<'de> Deserialize<'de> for OpticRef {
} }
} }
} }
let _node_type = node_type.ok_or_else(|| de::Error::missing_field("type"))?; let node_type = node_type.ok_or_else(|| de::Error::missing_field("type"))?;
let _nanos = properties.ok_or_else(|| de::Error::missing_field("properties"))?; let properties =
Ok(OpticRef(Rc::new(RefCell::new(Dummy::default())))) properties.ok_or_else(|| de::Error::missing_field("properties"))?;
let node =
create_node_ref(node_type).map_err(|e| de::Error::custom(e.to_string()))?;
node.0
.borrow_mut()
.set_properties(&properties).map_err(|e| de::Error::custom(e.to_string()))?;
Ok(node)
} }
} }
deserializer.deserialize_struct("OpticRef", FIELDS, OpticRefVisitor) deserializer.deserialize_struct("OpticRef", FIELDS, OpticRefVisitor)
......
use std::collections::HashMap; use std::collections::HashMap;
use serde::Serialize; // use serde::Serialize;
use serde_derive::{Serialize, Deserialize}; use serde_derive::{Serialize, Deserialize};
use crate::{error::OpossumError, lightdata::LightData, optical::OpticGraph}; use crate::{error::OpossumError, lightdata::LightData, optical::OpticGraph};
#[derive(Default, Serialize, Deserialize, Debug, Clone)] #[derive(Default, Serialize, Deserialize, Debug, Clone)]
pub struct Properties { pub struct Properties {
props: HashMap<String, Property> pub props: HashMap<String, Property>
} }
impl Properties { impl Properties {
......
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