@@ -4,16 +4,32 @@ pub fn cvir_json(program: &Program) -> String {
44 let mut w = JsonWriter :: new ( ) ;
55 w. obj_begin ( ) ;
66
7+ w. kv_str ( "cvir_version" , "0.2" ) ;
8+ w. comma_nl ( ) ;
79 w. key ( "items" ) ;
810 w. array_begin ( ) ;
9- for ( idx, item) in program. items . iter ( ) . enumerate ( ) {
10- if idx != 0 {
11+ let seed = program
12+ . items
13+ . iter ( )
14+ . find_map ( |item| match item {
15+ Item :: Seed ( s) => Some ( s. value ) ,
16+ _ => None ,
17+ } )
18+ . unwrap_or ( 0 ) ;
19+
20+ let mut first = true ;
21+ for item in program. items . iter ( ) {
22+ if matches ! ( item, Item :: Seed ( _) ) {
23+ continue ;
24+ }
25+ if !first {
1126 w. comma ( ) ;
1227 }
28+ first = false ;
1329 w. nl ( ) ;
14- emit_item ( & mut w, item) ;
30+ emit_item ( & mut w, item, seed ) ;
1531 }
16- if !program . items . is_empty ( ) {
32+ if !first {
1733 w. nl ( ) ;
1834 }
1935 w. array_end ( ) ;
@@ -24,7 +40,7 @@ pub fn cvir_json(program: &Program) -> String {
2440 w. finish ( )
2541}
2642
27- fn emit_item ( w : & mut JsonWriter , item : & Item ) {
43+ fn emit_item ( w : & mut JsonWriter , item : & Item , seed : u64 ) {
2844 w. obj_begin ( ) ;
2945 match item {
3046 Item :: Neuron ( d) => {
@@ -54,11 +70,42 @@ fn emit_item(w: &mut JsonWriter, item: &Item) {
5470 w. key ( "body" ) ;
5571 emit_assigns ( w, & d. body ) ;
5672 }
73+ Item :: Stimulus ( d) => {
74+ w. kv_str ( "kind" , "stimulus" ) ;
75+ w. comma_nl ( ) ;
76+ w. kv_str ( "layer" , & d. layer . name ) ;
77+ w. comma_nl ( ) ;
78+ w. key ( "model" ) ;
79+ emit_stimulus_model ( w, & d. model ) ;
80+ }
5781 Item :: Run ( d) => {
5882 w. kv_str ( "kind" , "run" ) ;
5983 w. comma_nl ( ) ;
6084 w. key ( "duration" ) ;
6185 emit_quantity ( w, & d. duration ) ;
86+ w. comma_nl ( ) ;
87+ w. key ( "step" ) ;
88+ if let Some ( step) = & d. step {
89+ emit_quantity ( w, step) ;
90+ } else {
91+ emit_quantity_value ( w, 1.0 , Some ( "ms" ) ) ;
92+ }
93+ w. comma_nl ( ) ;
94+ w. kv_u64 ( "seed" , seed) ;
95+ }
96+ Item :: Seed ( _) => { }
97+ }
98+ w. obj_end ( ) ;
99+ }
100+
101+ fn emit_stimulus_model ( w : & mut JsonWriter , model : & StimulusModel ) {
102+ w. obj_begin ( ) ;
103+ match model {
104+ StimulusModel :: Poisson { rate } => {
105+ w. kv_str ( "type" , "poisson" ) ;
106+ w. comma_nl ( ) ;
107+ w. key ( "rate" ) ;
108+ emit_quantity ( w, rate) ;
62109 }
63110 }
64111 w. obj_end ( ) ;
@@ -126,11 +173,15 @@ fn emit_expr(w: &mut JsonWriter, e: &Expr) {
126173}
127174
128175fn emit_quantity ( w : & mut JsonWriter , q : & Quantity ) {
176+ emit_quantity_value ( w, q. value , q. unit . as_ref ( ) . map ( |u| u. name . as_str ( ) ) ) ;
177+ }
178+
179+ fn emit_quantity_value ( w : & mut JsonWriter , value : f64 , unit : Option < & str > ) {
129180 w. obj_begin ( ) ;
130- w. kv_f64 ( "value" , q . value ) ;
131- if let Some ( u) = & q . unit {
181+ w. kv_f64 ( "value" , value) ;
182+ if let Some ( u) = unit {
132183 w. comma_nl ( ) ;
133- w. kv_str ( "unit" , & u . name ) ;
184+ w. kv_str ( "unit" , u ) ;
134185 }
135186 w. obj_end ( ) ;
136187}
0 commit comments