diff --git a/bin/main.ml b/bin/main.ml index dfc6bac..f44b47f 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -15,8 +15,7 @@ let () = Log.debug "book is:\n %s" (AddressBook.dump book); Log.debug "rules is:\n %s" (List.map Rule.dump rules |> String.concat "\n "); let scenarios = - List.map (fun rule -> Scenario.seq book rule) rules - |> List.map (fun seq -> Seq.take 10 seq |> List.of_seq) + List.map (fun rule -> Scenario.gen 10 book rule) rules |> List.concat in scenarios |> List.iter (fun scenario -> Log.debug "SC: %s" (Scenario.dump scenario)); diff --git a/bin/scenario.ml b/bin/scenario.ml index 9082cc6..6383066 100644 --- a/bin/scenario.ml +++ b/bin/scenario.ml @@ -47,11 +47,14 @@ let gen_aux book rule : (string * string * string) Gen.t = return (src_ip, Ip.to_string dst, "") end -let gen book rule = +let gen n book rule = let desc idx = !%"%s-%04d" rule.Rule.name (idx+1) in gen_aux book rule |> Gen.to_seq - |> Seq.mapi (fun idx (src_ip, dst_ip, url_domain) -> + |> Seq.take n + |> List.of_seq + |> list_uniques + |> List.mapi (fun idx (src_ip, dst_ip, url_domain) -> {src_ip; dst_ip; url_domain; description=(desc idx)}) let seq_addr addr = diff --git a/lib/common.ml b/lib/common.ml index 086cf59..4ed4f4b 100644 --- a/lib/common.ml +++ b/lib/common.ml @@ -22,6 +22,22 @@ let rec list_shuffle rand xs = | Some(x, xs) -> x :: list_shuffle rand xs | None -> [] +let rec elem x xs = + match xs with + | [] -> false + | y :: ys -> x = y || elem x ys + +let list_uniques l = + let rec helper l acc = + match l with + | [] -> acc + | x :: xs -> + if elem x acc then + helper xs acc + else + helper xs (x :: acc) + in List.rev (helper l []) + module SeqMonad = struct type 'a t = 'a Seq.t