Newer
Older
use serde_json::json;
use crate::properties::{Properties, Property, Proptype};
use std::collections::HashMap;
type Result<T> = std::result::Result<T, OpossumError>;
/// A fake / dummy component without any optical functionality.
///
/// Any [`LightResult`] is directly forwarded without any modification. It is mainly used for
/// development and debugging purposes.
///
/// ## Optical Ports
/// - Inputs
/// - `front`
/// - Outputs
/// - `rear`
fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set(
"name",
Property {
prop: Proptype::String("dummy".into()),
},
);
props.set(
"inverted",
Property {
prop: Proptype::Bool(false),
},
);
props
}
impl Default for Dummy {
fn default() -> Self {
props: create_default_props(),
impl Dummy {
/// Creates a new [`Dummy`] with a given name.
pub fn new(name: &str) -> Self {
let mut props = create_default_props();
props.set(
"name",
Property {
prop: Proptype::String(name.into()),
},
);

Udo Eisenbarth
committed
Self { props }
self.props.set(
"name",
Property {
prop: Proptype::String(name.into()),
},
);
if let Some(value) = self.props.get("name") {
if let Proptype::String(name) = &value.prop {
let mut ports = OpticPorts::new();
ports.add_input("front").unwrap();
ports.add_output("rear").unwrap();

Udo Eisenbarth
committed
if self.properties().get_bool("inverted").unwrap().unwrap() {
ports.set_inverted(true)
}
fn analyze(
&mut self,
incoming_data: LightResult,
_analyzer_type: &AnalyzerType,
) -> Result<LightResult> {

Udo Eisenbarth
committed
if !self.inverted() {
if let Some(data) = incoming_data.get("front") {
Ok(HashMap::from([("rear".into(), data.clone())]))
} else {
Ok(HashMap::from([("rear".into(), None)]))
}
} else if let Some(data) = incoming_data.get("rear") {
Ok(HashMap::from([("front".into(), data.clone())]))
Ok(HashMap::from([("front".into(), None)]))
fn inverted(&self) -> bool {
self.properties().get_bool("inverted").unwrap().unwrap()
fn properties(&self) -> &Properties {
&self.props
fn set_property(&mut self, name: &str, prop: Property) -> Result<()> {
if self.props.set(name, prop).is_none() {
Err(OpossumError::Other("property not defined".into()))
} else {
Ok(())
}
}
fn report(&self) -> serde_json::Value {
json!({"type": self.node_type(),
"name": self.name()})
}
#[cfg(test)]
mod test {
use crate::{lightdata::{DataEnergy, LightData}, spectrum::create_he_ne_spectrum};
use super::*;
#[test]
fn new() {
assert_eq!(node.name(), "Test");
}
#[test]
fn default() {
assert_eq!(node.name(), "dummy");
}
#[test]
fn name() {
let mut node = Dummy::default();
assert_eq!(node.name(), "Test1")
}
#[test]
fn inverted() {
let mut node = Dummy::default();

Udo Eisenbarth
committed
node.set_property("inverted", true.into()).unwrap();
assert_eq!(node.inverted(), true)
}
#[test]
fn is_detector() {
let node = Dummy::default();
assert_eq!(node.is_detector(), false);
}
#[test]
fn node_type() {
let node = Dummy::default();
assert_eq!(node.node_type(), "dummy");
}
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#[test]
fn analyze_ok() {
let mut dummy = Dummy::default();
let mut input=LightResult::default();
let input_light=LightData::Energy(DataEnergy{spectrum:create_he_ne_spectrum(1.0)});
input.insert("front".into(), Some(input_light.clone()));
let output=dummy.analyze(input, &AnalyzerType::Energy);
assert!(output.is_ok());
let output=output.unwrap();
assert!(output.contains_key("rear".into()));
assert_eq!(output.len(),1);
let output=output.get("rear".into()).unwrap();
assert!(output.is_some());
let output=output.clone().unwrap();
assert_eq!(output, input_light);
}
#[test]
fn analyze_wrong() {
let mut dummy = Dummy::default();
let mut input=LightResult::default();
let input_light=LightData::Energy(DataEnergy{spectrum:create_he_ne_spectrum(1.0)});
input.insert("rear".into(), Some(input_light.clone()));
let output=dummy.analyze(input, &AnalyzerType::Energy);
assert!(output.is_ok());
let output=output.unwrap();
let output=output.get("rear".into()).unwrap();
assert!(output.is_none());
}
#[test]
fn analyze_inverse() {
let mut dummy = Dummy::default();
dummy.set_property("inverted", true.into()).unwrap();
let mut input=LightResult::default();
let input_light=LightData::Energy(DataEnergy{spectrum:create_he_ne_spectrum(1.0)});
input.insert("rear".into(), Some(input_light.clone()));
let output=dummy.analyze(input, &AnalyzerType::Energy);
assert!(output.is_ok());
let output=output.unwrap();
assert!(output.contains_key("front".into()));
assert_eq!(output.len(),1);
let output=output.get("front".into()).unwrap();
assert!(output.is_some());
let output=output.clone().unwrap();
assert_eq!(output, input_light);
}