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

OpticScenery: add example.

Scenery::connect_nodes return () instead of EdgeIndex since it's not
needed anaywhere.
parent 551d716d
No related branches found
No related tags found
1 merge request!7Draft: Resolve "Implement rudimental serialization of OpticScenery"
Pipeline #7422 passed
......@@ -40,14 +40,14 @@ fn main() -> Result<(), OpossumError> {
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot()?).unwrap();
// scenery.report();
scenery.report();
println!("");
let mut analyzer = AnalyzerEnergy::new(&scenery);
print!("Analyze...");
analyzer.analyze()?;
println!("Sucessful");
println!("");
//scenery.report();
scenery.report();
Ok(())
}
......@@ -28,7 +28,6 @@ fn main() -> Result<(), OpossumError> {
let i_d3 = scenery.add_node(EnergyMeter::default()); // Energy meter 2
scenery.connect_nodes(i_s, "out1", i_bs, "input1")?;
scenery.connect_nodes(i_bs, "out1_trans1_refl2", i_d1, "in1")?;
scenery.connect_nodes(i_bs, "out2_trans2_refl1", i_f, "front")?;
scenery.connect_nodes(i_f, "rear", i_d2, "in1")?;
......
......@@ -28,7 +28,10 @@ pub struct Dummy {
impl Default for Dummy {
fn default() -> Self {
Self { is_inverted: Default::default(), name: String::from("dummy") }
Self {
is_inverted: Default::default(),
name: String::from("dummy"),
}
}
}
impl Dummy {
......@@ -90,15 +93,15 @@ mod test {
use super::*;
#[test]
fn new() {
let node = Dummy::new("Test");
assert_eq!(node.name, "Test");
assert_eq!(node.inverted(), false);
let node = Dummy::new("Test");
assert_eq!(node.name, "Test");
assert_eq!(node.inverted(), false);
}
#[test]
fn default() {
let node = Dummy::default();
assert_eq!(node.name, "dummy");
assert_eq!(node.inverted(), false);
let node = Dummy::default();
assert_eq!(node.name, "dummy");
assert_eq!(node.inverted(), false);
}
#[test]
fn set_name() {
......
......@@ -9,7 +9,7 @@ use crate::lightdata::LightData;
use crate::nodes::NodeGroup;
use crate::optical::{LightResult, Optical};
use petgraph::algo::*;
use petgraph::prelude::{DiGraph, EdgeIndex, NodeIndex};
use petgraph::prelude::{DiGraph, NodeIndex};
use petgraph::visit::EdgeRef;
use petgraph::Direction::Incoming;
......@@ -19,6 +19,23 @@ type Result<T> = std::result::Result<T, OpossumError>;
///
/// All optical elements ([`Optical`]s) have to be added to this structure in order
/// to be considered for an analysis.
///
/// # Example
///
/// ```rust
/// use opossum::OpticScenery;
/// use opossum::nodes::Dummy;
/// use opossum::error::OpossumError;
///
/// fn main() -> Result<(), OpossumError> {
/// let mut scenery = OpticScenery::new();
/// scenery.set_description("OpticScenery demo");
/// let node1 = scenery.add_node(Dummy::new("dummy1"));
/// let node2 = scenery.add_node(Dummy::new("dummy2"));
/// scenery.connect_nodes(node1, "rear", node2, "front")
/// }
///
/// ```
#[derive(Default, Debug, Clone)]
pub struct OpticScenery {
g: DiGraph<Rc<RefCell<dyn Optical>>, Light>,
......@@ -47,7 +64,7 @@ impl OpticScenery {
src_port: &str,
target_node: NodeIndex,
target_port: &str,
) -> Result<EdgeIndex> {
) -> Result<()> {
if let Some(source) = self.g.node_weight(src_node) {
if !source.borrow().ports().outputs().contains(&src_port.into()) {
return Err(OpossumError::OpticScenery(format!(
......@@ -100,7 +117,7 @@ impl OpticScenery {
"connecting the given nodes would form a loop".into(),
));
}
Ok(edge_index)
Ok(())
}
fn src_node_port_exists(&self, src_node: NodeIndex, src_port: &str) -> bool {
self.g
......
......@@ -10,18 +10,6 @@ use std::collections::HashMap;
pub type LightResult = HashMap<String, Option<LightData>>;
type Result<T> = std::result::Result<T, OpossumError>;
// /// Creates a new [`OpticNode`]. The concrete type of the component must be given while using the `new` function.
// /// The node type ist a struct implementing the [`Optical`] trait. Since the size of the node type is not known at compile time it must be added as `Box<nodetype>`.
// ///
// /// # Examples
// ///
// /// ```rust
// /// use opossum::optic_node::OpticNode;
// /// use opossum::nodes::Dummy;
// ///
// /// let node=OpticNode::new("My node", Dummy::default());
// /// ```
/// This is the basic trait that must be implemented by all concrete optical components.
pub trait Optical: Dottable {
/// Sets the name of this [`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