Skip to content

Commit 4d9e703

Browse files
committed
Updated CausaloidGraph with proper internal index structure and proper error handling.
1 parent 72a0c4a commit 4d9e703

File tree

5 files changed

+189
-84
lines changed

5 files changed

+189
-84
lines changed

deep_causality/src/errors/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ impl fmt::Display for ContextIndexError
4040
}
4141
}
4242

43+
#[derive(Debug)]
44+
pub struct CausalGraphIndexError(pub String);
45+
46+
impl Error for CausalGraphIndexError {}
47+
48+
impl fmt::Display for CausalGraphIndexError
49+
{
50+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51+
write!(f, "CausalGraphIndexError: {}", self.0)
52+
}
53+
}
54+
4355
#[derive(Debug)]
4456
pub struct CausalityError(pub String);
4557

deep_causality/src/prelude.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ pub use crate::csm::csm_state::CausalState;
66
pub use crate::csm::CSM;
77
//
88
// Error
9-
pub use crate::errors::ActionError;
10-
pub use crate::errors::BuildError;
11-
pub use crate::errors::CausalityError;
12-
pub use crate::errors::CausalityGraphError;
9+
pub use crate::errors::*;
1310
//
1411
// Extensions
1512
pub use crate::extensions::assumable::*;

deep_causality/src/protocols/causable_graph/mod.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
// Copyright (c) "2023" . Marvin Hansen <[email protected]> All rights reserved.
22
use std::collections::HashMap;
3-
use crate::errors::CausalityGraphError;
4-
use crate::prelude::{Causable, IdentificationValue, NodeIndex, NumericalValue};
3+
use crate::errors::{CausalGraphIndexError, CausalityGraphError};
4+
use crate::prelude::{Causable, IdentificationValue, NumericalValue};
55

66
pub trait CausableGraph<T>
77
where
88
T: Causable + PartialEq,
99
{
1010
// Root Node
11-
fn add_root_causaloid(&mut self, value: T) -> NodeIndex;
11+
fn add_root_causaloid(&mut self, value: T) -> usize;
1212
fn contains_root_causaloid(&self) -> bool;
1313
fn get_root_causaloid(&self) -> Option<&T>;
14-
fn get_root_index(&self) -> Option<NodeIndex>;
15-
fn get_last_index(&self) -> Result<NodeIndex, CausalityGraphError>;
14+
fn get_root_index(&self) -> Option<usize>;
15+
fn get_last_index(&self) -> Result<usize, CausalityGraphError>;
1616

1717
// Nodes
18-
fn add_causaloid(&mut self, value: T) -> NodeIndex;
19-
fn contains_causaloid(&self, index: NodeIndex) -> bool;
20-
fn get_causaloid(&self, index: NodeIndex) -> Option<&T>;
21-
fn remove_causaloid(&mut self, index: NodeIndex);
18+
fn add_causaloid(&mut self, value: T) -> usize;
19+
fn contains_causaloid(&self, index: usize) -> bool;
20+
fn get_causaloid(&self, index: usize) -> Option<&T>;
21+
fn remove_causaloid(&mut self, index: usize) -> Result<(), CausalGraphIndexError>
22+
;
2223

2324
// Edges
24-
fn add_edge(&mut self, a: NodeIndex, b: NodeIndex);
25-
fn add_edg_with_weight(&mut self, a: NodeIndex, b: NodeIndex, weight: u64);
25+
fn add_edge(&mut self, a: usize, b: usize) -> Result<(), CausalGraphIndexError>;
26+
fn add_edg_with_weight(&mut self, a: usize, b: usize, weight: u64) -> Result<(), CausalGraphIndexError>;
2627

27-
fn contains_edge(&self, a: NodeIndex, b: NodeIndex) -> bool;
28-
fn remove_edge(&mut self, a: NodeIndex, b: NodeIndex);
28+
fn contains_edge(&self, a: usize, b: usize) -> bool;
29+
fn remove_edge(&mut self, a: usize, b: usize) -> Result<(), CausalGraphIndexError>;
2930

3031
// Utils
3132
fn all_active(&self) -> bool;
@@ -60,7 +61,7 @@ pub trait CausableGraphReasoning<T>
6061
/// Returns: String representing the explanation or an error
6162
fn explain_subgraph_from_cause(
6263
&self,
63-
start_index: NodeIndex,
64+
start_index: usize,
6465
)
6566
-> Result<String, CausalityGraphError>;
6667

@@ -74,8 +75,8 @@ pub trait CausableGraphReasoning<T>
7475
/// Returns: String representing the explanation or an error
7576
fn explain_shortest_path_between_causes(
7677
&self,
77-
start_index: NodeIndex,
78-
stop_index: NodeIndex,
78+
start_index: usize,
79+
stop_index: usize,
7980
)
8081
-> Result<String, CausalityGraphError>;
8182

@@ -115,7 +116,7 @@ pub trait CausableGraphReasoning<T>
115116
/// a CausalityGraphError in case of failure.
116117
fn reason_subgraph_from_cause(
117118
&self,
118-
start_index: NodeIndex,
119+
start_index: usize,
119120
data: &[NumericalValue],
120121
data_index: Option<&HashMap<IdentificationValue, IdentificationValue>>,
121122
)
@@ -138,8 +139,8 @@ pub trait CausableGraphReasoning<T>
138139
/// a CausalityGraphError in case of failure.
139140
fn reason_shortest_path_between_causes(
140141
&self,
141-
start_index: NodeIndex,
142-
stop_index: NodeIndex,
142+
start_index: usize,
143+
stop_index: usize,
143144
data: &[NumericalValue],
144145
data_index: Option<&HashMap<IdentificationValue, IdentificationValue>>,
145146
)
@@ -153,7 +154,7 @@ pub trait CausableGraphReasoning<T>
153154
/// a CausalityGraphError in case of failure.
154155
fn reason_single_cause(
155156
&self,
156-
index: NodeIndex,
157+
index: usize,
157158
data: &[NumericalValue],
158159
)
159160
-> Result<bool, CausalityGraphError>;

deep_causality/src/types/context_types/context.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
use std::collections::HashMap;
44
use std::fmt::{Debug, Display, Formatter};
55
use petgraph::Directed;
6+
use petgraph::graph::{NodeIndex as GraphNodeIndex};
67
use petgraph::matrix_graph::MatrixGraph;
78
use crate::errors::ContextIndexError;
8-
use crate::prelude::{Contextuable, Contextoid, Datable, NodeIndex, SpaceTemporal, Spatial, Temporal, RelationKind, Identifiable};
9+
use crate::prelude::{Contextuable, Contextoid, Datable, SpaceTemporal, Spatial, Temporal, RelationKind, Identifiable};
10+
11+
type DefaultIx = u32;
12+
type NodeIndex<Ix = DefaultIx> = GraphNodeIndex<Ix>;
913

1014
//
1115
// Edge weights need to be numerical (u64) to make shortest path algo work.
@@ -185,7 +189,7 @@ impl<'l, D, S, T, ST> Contextuable<'l, D, S, T, ST> for Context<'l, D, S, T, ST>
185189
-> Result<(), ContextIndexError>
186190
{
187191
if !self.contains_node(index) {
188-
return Err(ContextIndexError("index not found".into()));
192+
return Err(ContextIndexError(format!("index {} not found", index)));
189193
};
190194

191195
let k = self.index_map.get(&index).unwrap();
@@ -209,11 +213,11 @@ impl<'l, D, S, T, ST> Contextuable<'l, D, S, T, ST> for Context<'l, D, S, T, ST>
209213
-> Result<(), ContextIndexError>
210214
{
211215
if !self.contains_node(a) {
212-
return Err(ContextIndexError("index a not found".into()));
216+
return Err(ContextIndexError(format!("index a {} not found", a)));
213217
};
214218

215219
if !self.contains_node(b) {
216-
return Err(ContextIndexError("index b not found".into()));
220+
return Err(ContextIndexError(format!("index b {} not found", b)));
217221
};
218222

219223
let k = self.index_map.get(&a).expect("index not found");

0 commit comments

Comments
 (0)