-module(dfs_init_matrix).
-export([dfs_init_matrix/1]).
dfs_init_matrix(Matrix)->
My_Elem = get_elem(a,Matrix),
{Name, Node_Fault, PID, Neighbours, Current, _} = My_Elem,
New_My_Elem = {Name, Node_Fault, PID, Neighbours, Current, [root]},
Rest_Matrix = lists:delete(My_Elem, Matrix),
Stable_Matrix = [New_My_Elem],
Path = [root],
Iterative_List = [root],
My_Children = get_children(Iterative_List, Matrix, a),
[Next_Child|Other_Children] = My_Children,
Receive = do_init(Next_Child, Path, Stable_Matrix, Rest_Matrix, Matrix, Iterative_List, Other_Children),
Receive.
do_init(My_Name, Path, Stable_Matrix, Rest_Matrix, Matrix, It_List, My_Brothers) ->
My_Elem = get_elem(My_Name, Matrix),
{Name, NFP, PID, Nei, Cur, _} = My_Elem,
New_Path = lists:append(Path,[My_Name]),
Stable_My_Elem = {Name, NFP, PID, Nei, Cur, New_Path},
New_Stable_Matrix = lists:append(Stable_Matrix, [Stable_My_Elem]),
New_Rest_Matrix = lists:delete(My_Elem, Rest_Matrix),
New_It_List = lists:append(It_List,[My_Name]),
My_Children = get_children(New_It_List, Matrix, My_Name),
if
length(New_Rest_Matrix) > 0 ->
if
length(My_Children) > 0 ->
[Next_Child|Other_Children] = My_Children,
{New_New_Stable_Matrix, New_New_Rest_Matrix, New_New_It_List} = do_init(Next_Child, New_Path, New_Stable_Matrix, New_Rest_Matrix, Matrix, New_It_List, Other_Children);
true ->
New_New_Stable_Matrix = New_Stable_Matrix,
New_New_Rest_Matrix = New_Rest_Matrix,
New_New_It_List = New_It_List
end,
if
length(My_Brothers) > 0 ->
[Next_Brother|Other_Brothers] = My_Brothers,
{New_New_New_Stable_Matrix, New_New_New_Rest_Matrix, New_New_New_It_List} = do_init(Next_Brother, Path, New_New_Stable_Matrix, New_New_Rest_Matrix, Matrix, New_New_It_List, Other_Brothers);
true ->
New_New_New_Stable_Matrix = New_New_Stable_Matrix,
New_New_New_Rest_Matrix = New_New_Rest_Matrix,
New_New_New_It_List = New_New_It_List
end,
{New_New_New_Stable_Matrix,New_New_New_Rest_Matrix,New_New_New_It_List};
true ->
New_New_Stable_Matrix = lists:keysort(1,New_Stable_Matrix),
Return_Value = {New_It_List, New_New_Stable_Matrix},
io:format("READ THIS!!!~p~n",[Return_Value]),
Return_Value
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_children(Iterative_List, Matrix, Name)->
Elem = get_elem(Name,Matrix),
{_, _, _, Neighbours, _, _} = Elem,
Raw_Neighbours = raw_neighbours(Neighbours,[]),
rip_neighbours(Raw_Neighbours,Iterative_List).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_elem(Elem, Client_List)->
[First|Rest] = Client_List,
{Name, _,_,_,_,_} = First,
if
Name == Elem ->
First;
true ->
get_elem(Elem, Rest)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
raw_neighbours([],Neighbours)->
Neighbours;
raw_neighbours(Full_List,Neighbours)->
[First|Rest] = Full_List,
{Name,_} = First,
New_Neighbours = lists:append(Neighbours,[Name]),
raw_neighbours(Rest,New_Neighbours).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rip_neighbours(Raw_Neighbours,[])->
Raw_Neighbours;
rip_neighbours(Raw_Neighbours,Iterative_List)->
[First|Rest] = Iterative_List,
Bool = lists:member(First, Raw_Neighbours),
if
First == root ->
New_Raw_Neighbours = Raw_Neighbours;
true ->
if
Bool ->
New_Raw_Neighbours = lists:delete(First, Raw_Neighbours);
true ->
New_Raw_Neighbours = Raw_Neighbours
end
end,
rip_neighbours(New_Raw_Neighbours,Rest).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%