Skip to content

Commit 41aa59f

Browse files
committed
Refactored Causaloid context into Arc<RwLock>
Signed-off-by: Marvin Hansen <[email protected]>
1 parent e8da175 commit 41aa59f

File tree

24 files changed

+340
-56
lines changed

24 files changed

+340
-56
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deep_causality/src/alias/alias_function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44
*/
55
use crate::{AssumptionError, CausalityError, Context, PropagatingEffect};
6-
use std::sync::Arc;
6+
use std::sync::{Arc, RwLock};
77

88
// Fn aliases for assumable, assumption, & assumption collection
99
/// Function type for evaluating numerical values and returning a boolean result.
@@ -39,5 +39,5 @@ pub type CausalFn = fn(effect: &PropagatingEffect) -> Result<PropagatingEffect,
3939
pub type ContextualCausalFn<D, S, T, ST, SYM, VS, VT> =
4040
fn(
4141
effect: &PropagatingEffect,
42-
context: &Arc<Context<D, S, T, ST, SYM, VS, VT>>,
42+
context: &Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>,
4343
) -> Result<PropagatingEffect, CausalityError>;

deep_causality/src/types/causal_types/causaloid/getters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616
VS: Clone,
1717
VT: Clone,
1818
{
19-
pub fn context(&self) -> &Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>> {
19+
pub fn context(&self) -> &Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>> {
2020
&self.context
2121
}
2222

deep_causality/src/types/causal_types/causaloid/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
causal_type: CausaloidType,
3636
causal_fn: Option<CausalFn>,
3737
context_causal_fn: Option<ContextualCausalFn<D, S, T, ST, SYM, VS, VT>>,
38-
context: Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>>,
38+
context: Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>>,
3939
// The last calculated effect of this causaloid. It's an Option because a
4040
// causaloid may not have been evaluated yet.
4141
effect: ArcRWLock<Option<PropagatingEffect>>,
@@ -90,7 +90,7 @@ where
9090
pub fn new_with_context(
9191
id: IdentificationValue,
9292
context_causal_fn: ContextualCausalFn<D, S, T, ST, SYM, VS, VT>,
93-
context: Arc<Context<D, S, T, ST, SYM, VS, VT>>,
93+
context: Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>,
9494
description: &str,
9595
) -> Self {
9696
Causaloid {
@@ -140,7 +140,7 @@ where
140140
pub fn from_causal_collection_with_context(
141141
id: IdentificationValue,
142142
causal_coll: Arc<Vec<Causaloid<D, S, T, ST, SYM, VS, VT>>>,
143-
context: Arc<Context<D, S, T, ST, SYM, VS, VT>>,
143+
context: Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>,
144144
description: &str,
145145
) -> Self {
146146
Causaloid {
@@ -190,7 +190,7 @@ where
190190
pub fn from_causal_graph_with_context(
191191
id: IdentificationValue,
192192
causal_graph: Arc<CausaloidGraph<Causaloid<D, S, T, ST, SYM, VS, VT>>>,
193-
context: Arc<Context<D, S, T, ST, SYM, VS, VT>>,
193+
context: Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>,
194194
description: &str,
195195
) -> Self {
196196
Causaloid {

deep_causality/src/types/causal_types/causaloid/setters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
use crate::{Causaloid, Context, Datable, SpaceTemporal, Spatial, Symbolic, Temporal};
7-
use std::sync::Arc;
7+
use std::sync::{Arc, RwLock};
88

99
// Constructors
1010
#[allow(clippy::type_complexity)]
@@ -18,7 +18,7 @@ where
1818
VS: Clone,
1919
VT: Clone,
2020
{
21-
pub fn set_context(&mut self, context: Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>>) {
21+
pub fn set_context(&mut self, context: Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>>) {
2222
self.context = context;
2323
}
2424
}

deep_causality/src/types/csm_types/csm/eval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ where
143143
if let Some((ethos, tags)) = &self.effect_ethos {
144144
if let Some(context) = state.context() {
145145
let proposed_action = self.create_proposed_action(state, effect)?;
146-
let verdict = ethos.evaluate_action(&proposed_action, context, tags)?;
146+
let context_guard = context.read().unwrap();
147+
let verdict = ethos.evaluate_action(&proposed_action, &context_guard, tags)?;
147148

148149
if verdict.outcome() == TeloidModal::Impermissible {
149150
let explanation = ethos.explain_verdict(&verdict)?;

deep_causality/src/types/csm_types/csm_state/getter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
CausalState, Causaloid, Context, Datable, PropagatingEffect, SpaceTemporal, Spatial, Symbolic,
88
Temporal, UncertainParameter,
99
};
10-
use std::sync::Arc;
10+
use std::sync::{Arc, RwLock};
1111

1212
impl<D, S, T, ST, SYM, VS, VT> CausalState<D, S, T, ST, SYM, VS, VT>
1313
where
@@ -36,7 +36,7 @@ where
3636
}
3737

3838
#[allow(clippy::type_complexity)]
39-
pub fn context(&self) -> &Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>> {
39+
pub fn context(&self) -> &Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>> {
4040
self.causaloid.context()
4141
}
4242

deep_causality/src/types/model_types/model/getters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::{
77
Assumption, Causaloid, Context, Datable, Model, SpaceTemporal, Spatial, Symbolic, Temporal,
88
};
9-
use std::sync::Arc;
9+
use std::sync::{Arc, RwLock};
1010

1111
#[allow(clippy::type_complexity)]
1212
impl<D, S, T, ST, SYM, VS, VT> Model<D, S, T, ST, SYM, VS, VT>
@@ -35,7 +35,7 @@ where
3535
&self.causaloid
3636
}
3737

38-
pub fn context(&self) -> &Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>> {
38+
pub fn context(&self) -> &Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>> {
3939
&self.context
4040
}
4141
}

deep_causality/src/types/model_types/model/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
ModelBuildError, ModelValidationError, Symbolic,
1717
};
1818
use std::hash::Hash;
19-
use std::sync::Arc;
19+
use std::sync::{Arc, RwLock};
2020

2121
#[allow(clippy::type_complexity)]
2222
#[derive(Debug)]
@@ -35,7 +35,7 @@ where
3535
description: String,
3636
assumptions: Option<Arc<Vec<Assumption>>>,
3737
causaloid: Arc<Causaloid<D, S, T, ST, SYM, VS, VT>>,
38-
context: Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>>,
38+
context: Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>>,
3939
}
4040

4141
#[allow(clippy::type_complexity)]
@@ -55,7 +55,7 @@ where
5555
description: &str,
5656
assumptions: Option<Arc<Vec<Assumption>>>,
5757
causaloid: Arc<Causaloid<D, S, T, ST, SYM, VS, VT>>,
58-
context: Option<Arc<Context<D, S, T, ST, SYM, VS, VT>>>,
58+
context: Option<Arc<RwLock<Context<D, S, T, ST, SYM, VS, VT>>>>,
5959
) -> Self {
6060
Self {
6161
id,
@@ -114,7 +114,7 @@ where
114114
// The context is also owned now.
115115
let final_context_arc = if let Some(context) = context_opt {
116116
// Create the Arc for the context.
117-
let context_arc = Arc::new(context);
117+
let context_arc = Arc::new(RwLock::new(context));
118118

119119
final_causaloid.set_context(Some(Arc::clone(&context_arc)));
120120

deep_causality/src/utils_test/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
use crate::*;
66
use deep_causality_uncertain::Uncertain;
7-
use std::sync::Arc;
7+
use std::sync::{Arc, RwLock};
88

99
pub fn get_context() -> BaseContext {
1010
let id = 1;
@@ -212,12 +212,12 @@ pub fn get_test_causaloid_deterministic() -> BaseCausaloid {
212212

213213
pub fn get_test_causaloid_deterministic_with_context(context: BaseContext) -> BaseCausaloid {
214214
let id: IdentificationValue = 1;
215-
let context = Arc::new(context);
215+
let context = Arc::new(RwLock::new(context));
216216
let description = "Inverts any input";
217217

218218
fn causal_fn(
219219
effect: &PropagatingEffect,
220-
_context: &Arc<BaseContext>,
220+
_context: &Arc<RwLock<BaseContext>>,
221221
) -> Result<PropagatingEffect, CausalityError> {
222222
let obs = match effect {
223223
// If it's the Deterministic variant, extract the inner value.

0 commit comments

Comments
 (0)