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

Add first structure for using OpticNodes

parent c9bcaa65
No related branches found
No related tags found
No related merge requests found
use opossum::optic_scenery::OpticScenery;
use opossum::nodes::node_dummy::NodeDummy;
use opossum::optic_node::OpticNode;
use opossum::optic_scenery::OpticScenery;
use std::fs::File;
use std::io::Write;
......@@ -9,7 +10,7 @@ fn main() {
scenery.set_description("OpticScenery demo".into());
println!("default opticscenery: {:?}", scenery);
println!("export to `dot` format: {}", scenery.to_dot());
scenery.add_node(OpticNode::new("my optic".into()));
scenery.add_node(OpticNode::new("my optic".into(), Box::new(NodeDummy)));
let path = "graph.dot";
let mut output = File::create(path).unwrap();
write!(output, "{}", scenery.to_dot()).unwrap();
......
......@@ -3,4 +3,6 @@
/// The basic structure containing the entire optical model
pub mod optic_scenery;
/// The basic structure representing an optical element
pub mod optic_node;
\ No newline at end of file
pub mod optic_node;
pub mod nodes;
\ No newline at end of file
pub mod node_dummy;
\ No newline at end of file
use crate::optic_node::Optical;
pub struct NodeDummy;
impl Optical for NodeDummy {
}
\ No newline at end of file
#[derive(Debug, Default)]
use std::fmt::Debug;
pub struct OpticNode {
name: String,
node: Box<dyn Optical>,
}
impl OpticNode {
/// Creates a new [`OpticNode`].
pub fn new(name: String) -> Self {
Self { name }
pub fn new(name: String, node: Box<dyn Optical>) -> Self {
Self { name, node }
}
/// Sets the name of this [`OpticNode`].
pub fn set_name(&mut self, name: String) {
......@@ -22,28 +23,36 @@ impl OpticNode {
}
}
impl Debug for OpticNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
pub trait Optical {}
#[cfg(test)]
mod test {
use crate::nodes::node_dummy::NodeDummy;
use super::OpticNode;
#[test]
fn new() {
let node = OpticNode::new("Test".into());
assert_eq!(node.name, "Test".to_owned());
let node = OpticNode::new("Test".into(), Box::new(NodeDummy));
assert_eq!(node.name, "Test".to_owned());
}
#[test]
fn set_name() {
let mut node = OpticNode::new("Test".into());
fn set_name() {
let mut node = OpticNode::new("Test".into(), Box::new(NodeDummy));
node.set_name("Test2".into());
assert_eq!(node.name, "Test2".to_owned())
}
#[test]
fn name() {
let node = OpticNode::new("Test".into());
fn name() {
let node = OpticNode::new("Test".into(), Box::new(NodeDummy));
assert_eq!(node.name(), "Test".to_owned())
}
#[test]
fn to_dot() {
let node = OpticNode::new("Test".into());
let node = OpticNode::new("Test".into(), Box::new(NodeDummy));
assert_eq!(node.to_dot(), " \"Test\"\n".to_owned())
}
}
......@@ -23,12 +23,12 @@ impl OpticScenery {
}
/// Export the optic graph into the `dot` format to be used in combination with the [`graphviz`](https://graphviz.org/) software.
pub fn to_dot(&self) -> String {
let mut dot_string="digraph {\n".to_owned();
let mut dot_string = "digraph {\n".to_owned();
dot_string.push_str(&format!(" label=\"{}\"\n", self.description));
for node in self.g.node_weights() {
dot_string+=&node.to_dot();
dot_string += &node.to_dot();
}
dot_string+="}";
dot_string += "}";
dot_string
}
/// Analyze this [`OpticScenery`] using a given OpticAnalyzer.
......@@ -50,41 +50,43 @@ mod test {
use super::*;
#[test]
fn new() {
let scenery= OpticScenery::new();
let scenery = OpticScenery::new();
assert_eq!(scenery.description, "".to_owned());
assert_eq!(scenery.g.edge_count(),0);
assert_eq!(scenery.g.node_count(),0);
assert_eq!(scenery.g.edge_count(), 0);
assert_eq!(scenery.g.node_count(), 0);
}
#[test]
fn add_node() {
let mut scenery= OpticScenery::new();
scenery.add_node(OpticNode::new("Test".into()));
assert_eq!(scenery.g.node_count(),1);
let mut scenery = OpticScenery::new();
scenery.add_node(OpticNode::new("Test".into(), Box::new(NodeDummy)));
assert_eq!(scenery.g.node_count(), 1);
}
#[test]
fn to_dot_empty() {
let mut scenery=OpticScenery::new();
let mut scenery = OpticScenery::new();
scenery.set_description("Test".into());
assert_eq!(scenery.to_dot(),"digraph {\n label=\"Test\"\n}");
assert_eq!(scenery.to_dot(), "digraph {\n label=\"Test\"\n}");
}
#[test]
fn to_dot_with_node() {
let mut scenery=OpticScenery::new();
let mut scenery = OpticScenery::new();
scenery.set_description("SceneryTest".into());
scenery.add_node(OpticNode::new("Test".into()));
assert_eq!(scenery.to_dot(),"digraph {\n label=\"SceneryTest\"\n Test\n}");
scenery.add_node(OpticNode::new("Test".into(), Box::new(NodeDummy)));
assert_eq!(
scenery.to_dot(),
"digraph {\n label=\"SceneryTest\"\n Test\n}"
);
}
#[test]
fn set_description() {
let mut scenery=OpticScenery::new();
let mut scenery = OpticScenery::new();
scenery.set_description("Test".into());
assert_eq!(scenery.description, "Test".to_owned())
}
#[test]
fn description() {
let mut scenery=OpticScenery::new();
let mut scenery = OpticScenery::new();
scenery.set_description("Test".into());
assert_eq!(scenery.description(), "Test".to_owned())
}
}
\ 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