Skip to content

Commit

Permalink
conform to go-metamodel cid encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
stackdump committed Feb 25, 2024
1 parent d6964c8 commit 19385e4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ impl<'a> FlowDsl for Builder<'a> {
}

fn arrow(&mut self, source: &str, target: &str, weight: i32) {
//TxNode::new(&self.net, label.to_string(), role.to_string(), pos)
assert!(weight > 0, "weight must be positive");
self.net.add_arc(source, target, Some(weight), None, None, None, None);
}

fn guard(&mut self, source: &str, target: &str, weight: i32) {
//TxNode::new(&self.net, label.to_string(), role.to_string(), pos)
assert!(weight > 0, "weight must be positive");
self.net.add_arc( source, target, None, None, None, Some(true), None);
}
}

Expand Down Expand Up @@ -74,11 +76,11 @@ mod tests {
let model = declare(model_test_code);
let vm = model.deref();
let state = vm.initial_vector();
let res = vm.transform(state, "bar", 1);
let res = vm.transform(&state, "bar", 1);

assert!(res.is_ok());
// FIXME actually make state changes
//assert_ne!(res.output, state);
assert_ne!(res.output, state);
println!("{:?}", res);
}
}
4 changes: 2 additions & 2 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Oid {
impl Oid {
pub fn new(bytes: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
let hash = MultihashDigest::digest(&Code::Sha2_256, bytes);
let cid = Cid::new_v1(0x12, hash);
let cid = Cid::new_v1(0x55, hash); // NOTE: always produces string starting with "zb2"
Ok(Self { cid })
}

Expand Down Expand Up @@ -38,6 +38,6 @@ mod tests {
let oid = Oid::new(DINING_PHILOSOPHERS.as_bytes()).expect("Failed to create Oid");
let oid_string = oid.to_string();
println!("oid_string: {:?}", oid_string);
assert_eq!(oid_string, "zUM7WNh744LkY5KqFcLXjP33St1HX5ajsu8KyqV6KfNiR87pU");
assert_eq!(oid_string, "zb2rhhAP4oqMEYFwLJ1UKgQrvBWsDkrvkY9Sn4HBVgfZ5ymNY");
}
}
12 changes: 10 additions & 2 deletions src/petri_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,16 @@ impl PetriNet {
return;
}

pub fn add_arc(&mut self, source: String, target: String, weight: Option<i32>, consume: Option<bool>, produce: Option<bool>, inhibit: Option<bool>, read: Option<bool>) {
self.arcs.push(Arrow { source, target, weight, consume, produce, inhibit, read });
pub fn add_arc(&mut self, source: &str, target: &str, weight: Option<i32>, consume: Option<bool>, produce: Option<bool>, inhibit: Option<bool>, read: Option<bool>) {
self.arcs.push(Arrow {
source: source.to_string(),
target: target.to_string(),
weight,
consume,
produce,
inhibit,
read
});
return;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/vasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ impl StateMachine {
let inhibit = arc.inhibit.unwrap_or(false);
let read = arc.read.unwrap_or(false);
if inhibit {
transitions.get_mut(&source).unwrap().guards.insert(target.clone(), Guard {
delta: vec![0; vector_size],
read: read,
});
// FIXME
// transitions.get_mut(&source).unwrap().guards.insert(target.clone(), Guard {
// delta: vec![0; vector_size],
// read: read,
// });
} else {
assert_ne!(produce, consume, "must be either produce or consume");
if consume {
transitions.get_mut(&source).unwrap().delta[model.places.get(&target).unwrap().offset as usize] = 0 - weight;
transitions.get_mut(&target).unwrap().delta[model.places.get(&source).unwrap().offset as usize] = 0 - weight;
} else {
transitions.get_mut(&source).unwrap().delta[model.places.get(&target).unwrap().offset as usize] = weight;
}
Expand Down Expand Up @@ -137,7 +138,7 @@ impl Transaction {
pub trait Vasm {
fn empty_vector(&self) -> Vector;
fn initial_vector(&self) -> Vector;
fn transform(&self, state: Vector, action: &str, multiple: i32) -> Transaction;
fn transform(&self, state: &Vector, action: &str, multiple: i32) -> Transaction;
}

pub fn declare(declaration: fn(&mut dyn FlowDsl)) -> Box<dyn Vasm> {
Expand All @@ -154,7 +155,7 @@ impl Vasm for StateMachine {
}

// REVIEW: test that this works properly
fn transform(&self, state: Vector, action: &str, multiple: i32) -> Transaction {
fn transform(&self, state: &Vector, action: &str, multiple: i32) -> Transaction {
let transition = self.transitions.get(action).unwrap_or_else(|| panic!("no transition for {}", action));
let mut output = state.clone();
for (i, v) in transition.delta.iter().enumerate() {
Expand Down

0 comments on commit 19385e4

Please sign in to comment.