1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::{builders::method_builder::MutabilityMismatch, planner::Vertex};
use std::{
fmt::{Debug, Display},
ops::{Index, IndexMut},
sync::Arc,
};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MethodFailure {
NoSuchVariable(String),
WrongInputCount(usize, usize),
WrongOutputCount(usize, usize),
MutabilityMismatch(MutabilityMismatch),
TypeConversionFailure(&'static str, &'static str),
Custom(String),
}
pub type MethodResult<T> = Result<Vec<T>, MethodFailure>;
pub type MethodFunction<T> = Arc<dyn Fn(Vec<Arc<T>>) -> MethodResult<Arc<T>> + Send + Sync>;
pub trait MethodSpec: Vertex {
type Arg;
fn new(
name: String,
inputs: Vec<usize>,
outputs: Vec<usize>,
apply: MethodFunction<Self::Arg>,
) -> Self;
fn apply(&self, args: Vec<Arc<Self::Arg>>) -> MethodResult<Arc<Self::Arg>>;
fn name(&self) -> &str;
}
pub trait ConstraintSpec {
type Method: MethodSpec;
fn new(methods: Vec<Self::Method>) -> Self;
fn name(&self) -> &str;
fn methods(&self) -> &[Self::Method];
fn add_method(&mut self, m: Self::Method);
fn remove_method(&mut self, name: &str);
fn variables(&self) -> &[usize];
fn is_active(&self) -> bool;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PlanError {
Overconstrained,
}
impl Display for PlanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"No valid plan was found since the system is overconstrained"
)
}
}
pub trait ComponentSpec: Index<&'static str> + IndexMut<&'static str> {
type Value;
type Constraint: ConstraintSpec;
fn new(
name: String,
values: Vec<impl Into<Self::Value>>,
constraints: Vec<Self::Constraint>,
) -> Self;
fn n_variables(&self) -> usize;
fn n_constraints(&self) -> usize {
self.constraints().len()
}
fn constraints(&self) -> &[Self::Constraint];
fn constraints_mut(&mut self) -> &mut Vec<Self::Constraint>;
fn add_constraint(&mut self, constraint: Self::Constraint);
fn pop_constraint(&mut self) -> Option<Self::Constraint>;
fn remove_constraint(&mut self, idx: usize) -> Self::Constraint;
fn ranking(&self) -> Vec<usize>;
}