Allow get_dependencies to check dependencies of multiple variables.
[whenjobs.git] / lib / whenstate.ml
index ddbc302..2bb030d 100644 (file)
@@ -77,6 +77,36 @@ let set_variable t name value =
 let copy_variables old t =
   { t with variables = StringMap.fold StringMap.add old.variables t.variables }
 
+let copy_prev_state old t =
+  let is_explicit jobname =
+    String.length jobname < 4 || String.sub jobname 0 4 <> "job$"
+  in
+
+  let prev_variables = StringMap.fold (
+    fun jobname _ map ->
+      try
+        if not (is_explicit jobname) then raise Not_found;
+        (* See if we can find a job with the same name in the old state. *)
+        let old_vars = StringMap.find jobname old.prev_variables in
+        StringMap.add jobname old_vars map
+      with
+        Not_found -> map
+  ) t.jobmap t.prev_variables in
+
+  let prev_eval_result = StringMap.fold (
+    fun jobname _ map ->
+      try
+        if not (is_explicit jobname) then  raise Not_found;
+        (* See if we can find a job with the same name in the old state. *)
+        let old_result = StringMap.find jobname old.prev_eval_result in
+        StringMap.add jobname old_result map
+      with
+        Not_found -> map
+  ) t.jobmap t.prev_eval_result in
+
+  { t with
+    prev_variables = prev_variables; prev_eval_result = prev_eval_result }
+
 let get_variable t name =
   try StringMap.find name t.variables with Not_found -> T_string ""
 
@@ -93,8 +123,21 @@ let get_variable_names t =
 
 let nr_jobs t = List.length t.jobs
 
-let get_dependencies t name =
-  let jobnames = try StringMap.find name t.dependencies with Not_found -> [] in
+let get_dependencies t names =
+  (* Get all job names that depend on these variables. *)
+  let jobnames =
+    List.map (
+      fun name ->
+        try StringMap.find name t.dependencies with Not_found -> []
+    ) names in
+
+  (* Flatten the list and remove duplicates. *)
+  let set = List.fold_left (
+    fun set jn -> StringSet.add jn set
+  ) StringSet.empty (List.flatten jobnames) in
+  let jobnames = StringSet.elements set in
+
+  (* Convert job names to jobs. *)
   List.map (fun jn ->
     try
       let j = StringMap.find jn t.jobmap in
@@ -115,7 +158,7 @@ let get_everyjobs t =
   List.filter (function { job_cond = Every_job _ } -> true | _ -> false) t.jobs
 
 let get_job t jobname =
-  try StringMap.find jobname t.jobmap with Not_found -> assert false
+  StringMap.find jobname t.jobmap
 
 let evaluate_whenjob ?(onload = false) t job =
   match job with