open BatStd open Printf open Util type itemPosition = Start | Intermediate type t = { item: Item.t; support: int; position: itemPosition; mutable children: t list; (* children: (t list) ref; *) (* children: (Item.t, latticeNode) BatHashtbl.t ref; *) } let itemPosition_to_string = function | Start -> "s" | Intermediate -> "i" let rec tree_to_string t = let children = t.children in let c = join ";" (List.map tree_to_string children) in sprintf "%d(%s) [%s]" t.item (itemPosition_to_string t.position) c let dumpTree t = printf "%s\n" (tree_to_string t) let demo_tree = { item = 0; support = 0; position = Start; children = [ { item = 1 ; support = 1; position = Start ; children = [] }; { item = 2 ; support = 1; position = Start ; children = [] }; { item = 3 ; support = 1; position = Start ; children = [ { item = 2 ; support = 1; position = Start ; children = [] }; ] }; ]; } let empty_lattice = { item = 0; support = 0; position = Start; children = []; } let item_to_node item position = { item = item; support = 0; position = position; children = []; } let rec sequence_to_path sequence = match sequence with | [] -> [] | itemset::remaining_sequence -> match itemset with | [] -> raise (Error "Invalid sequence") | start_item::inner_items -> let start_node = item_to_node start_item Start in let inner_nodes = List.map (fun i -> item_to_node i Intermediate) inner_items in (start_node::inner_nodes)@(sequence_to_path remaining_sequence) (* let rec insert_remaining_items lattice let rec insert_start_item children start_item inner_items remaining_sequence = match children with | [] -> let new_child = { item = start_item; support = 0; position = Start; children = [] } in insert_remaining_items new_child inner_items remaining_sequence | child::remaining_children -> if child.item = start_item and child.position = Start then let new_children = (insert_remaining_items child inner_items remaining_sequence)::remaining_children else insert_start_item remaining_children start_item inner_items remaining_sequence let rec insert_itemset lattice itemset remaining_sequence = match itemset with | [] -> insert lattice remaining_sequence | start_item::inner_items -> let new_children = insert_start_item lattice start_item inner_items remaining_sequence in lattice.children := new_children; lattice *) let rec insert_path_children children node remaining_nodes = match children with | [] -> let lattice = node in [ insert_path lattice remaining_nodes ] | child::remaining_children -> if (child.item = node.item) && (child.position = node.position) then (insert_path child remaining_nodes)::remaining_children else child::(insert_path_children remaining_children node remaining_nodes) and insert_path lattice path = match path with | [] -> lattice | node::remaining_nodes -> let children = lattice.children in let new_children = insert_path_children children node remaining_nodes in print "New children: "; print new_children; lattice.children <- new_children; print "New lattice: "; print lattice; lattice let rec insert lattice sequence = let itemsets, is_partial = sequence in let path = sequence_to_path itemsets in printf "Lattice: "; print lattice; printf "Sequence: "; print sequence; printf "Path:\n"; print path; let lattice = insert_path lattice path in printf "New lattice: "; print lattice; lattice (* let node_to_dot num node = "n_" ^ (string_of_int num) ^ "_" ^ (string_of_int node.item) ^ "_" ^ (if node.position = Start then "s" else "i") ^ "_" ^ (string_of_int node.support) let rec to_dot parent_id cur_id children = match children with | [] -> "" | child::remaining_children -> let child_id = cur_id + 1 in "n_" ^ (int_to_string parent_id) ^ "-> n_" ^ (int_to_string child_id) ^ ";\n" ^ (to_dot' child_id child_id let rec to_dot lattice = *)