aboutsummaryrefslogtreecommitdiffstats
path: root/lib/appmon/src/appmon_dg.erl
blob: f53defa94623b57798397be5b14a26580d292d00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%------------------------------------------------------------
%%
%% Digraph handling for process view GUI. Feeble attempt at data
%% separation. Provides functional interface to the data structures
%% vdata and edata, v for vertex and e for edge.
%%
%%------------------------------------------------------------
-module(appmon_dg).

-include("appmon_dg.hrl").

%% Exports for vertices
-export([get/3, get/2, set/4, av/3, add/4, del/2, visited/3]).

%% Exports for edges
-export([eget/2, eget/3, eset/4, eadd/4, edel/2, ae/3]).

%% Exports for convenience
-export([print_dg/1]).


%%------------------------------------------------------------


eget(all, DG) ->
    digraph:edges(DG).

eget(data, DG, E) ->
    case digraph:edge(DG, E) of
	{_, _V1, _V2, Data} -> Data;
	_Other    -> false
    end;
eget(edge, DG, {V1, V2}) ->
    case digraph:edge(DG, {V1, V2}) of
	{E, W1, W2, ED} -> {E, W1, W2, ED};
	Other ->
	    case digraph:edge(DG, {V2, V1}) of
		{E, W1, W2, ED} -> {E, W1, W2, ED};
		Other -> false
	    end
    end;

%% Weight in edge name
eget(edge, DG, {V1, V2, Weight}) ->
    case digraph:edge(DG, {V1, V2, Weight}) of
	{E, W1, W2, ED} -> {E, W1, W2, ED};
	_Other -> false
    end;
eget(in, DG, V) ->
    efilter(digraph:in_edges(DG, V)).

efilter(Es) -> 
    lists:filter(fun({_V1, _V2, primary}) -> true;
		 (_E) -> false end,
		 Es).

eset(ref, DG, E, Ref) ->
    {E2, _V1, _V2, D} = eget(edge, DG, E),
    update_e(DG, E2, D#edata{ref=Ref});
eset(line, DG, E, Line) ->
    {E2, _V1, _V2, D} = eget(edge, DG, E),
    update_e(DG, E2, D#edata{line=Line}).

edel(DG, E) ->
    digraph:del_edge(DG, E).

eadd(DG, E, D, Ref) ->
    case eget(edge, DG, E) of
	{_, _, _, ED} when is_record(ED, edata), ED#edata.ref == Ref ->
	    known;
	{_, _, _, ED} when is_record(ED, edata), ED#edata.ref /= Ref ->
	    update_e(DG, E, ED#edata{ref=Ref}),
	    updated;
	_Other ->
	    ae(DG, E, D)
    end.

ae(DG, {V1, V2, Weight}, D) ->
    digraph:add_edge(DG, {V1, V2, Weight}, V1, V2, D).

update_e(DG, {V1, V2, Weight}, D) ->
    digraph:del_edge(DG, {V1, V2, Weight}),
    digraph:add_edge(DG, {V1, V2, Weight}, V1, V2, D).

%% Filter destination vertex from a list of edges
vfilter(Vs) ->
    lists:map(fun({_V1, V2, _Weight}) -> V2;
	      ({_V1, V2}) -> V2
	     end, Vs).

get(all, DG) ->
    digraph:vertices(DG).

get(data, DG, {V1, V2}) ->
    case digraph:edge(DG, {V1, V2}) of
	{_,_,_,Data} -> Data;
	_Other    -> false
    end;
get(data, DG, V) ->
    case digraph:vertex(DG, V) of
	{_,Data} -> Data;
	_Other    -> false
    end;

%% Return all children of vertex V (those which V has edges to)
get(out, DG, V) ->
    vfilter(efilter(digraph:out_edges(DG, V)));
get(in, DG, V) ->
    digraph:in_neighbours(DG, V);
get(edges, DG, V) ->
    digraph:edges(DG, V);
get(w, DG, V) ->
    Data = get(data, DG, V),
    Data#vdata.width;
get(x, DG, V) ->
    Data = get(data, DG, V),
    Data#vdata.x.

set(type, DG, V, Type) ->
    D = get(data, DG, V),
    av(DG, V, D#vdata{type=Type});

set(ref, DG, V, Ref) ->
    D = get(data, DG, V),
    av(DG, V, D#vdata{ref=Ref});

set(y, DG, V, Y) ->
    D = get(data, DG, V),
    av(DG, V, D#vdata{y=Y});

set(data, DG, V, D) when is_record(D, vdata)->
    av(DG, V, D);

set(x, DG, V, X) ->
    D = get(data, DG, V),
    if  D#vdata.x /= X -> 
	    av(DG, V, D#vdata{x=X});
	true -> true
    end.

visited(DG, {V1, V2}, Ref) ->			% for edge
    D = eget(data, DG, {V1, V2}),
    if  is_record(D, edata), D#edata.ref == Ref -> true;
	true -> false
    end;
visited(DG, V, Ref) ->
    D = get(data, DG, V),
    if  is_record(D, vdata), D#vdata.ref == Ref -> true;
	true -> false
    end.

add(DG, V, D, Ref) ->
    case get(data, DG, V) of
	D2 when is_record(D2, vdata), D2#vdata.ref==Ref -> 
	    io:format("Ooops in ~p:add vertex~n", [?MODULE]),
	    known;
	D2 when is_record(D2, vdata) -> 
	    %%io:format("~p touch vertex ~p~n", [self(), V]),
	    set(ref, DG, V, Ref), 
	    set(type, DG, V, D#vdata.type), 
	    save_coords(DG, V),
	    updated;
	_Other -> 
	    av(DG, V, D), added
    end.

save_coords(DG, V) ->
    D = get(data, DG, V),
    D2 = D#vdata{origx=D#vdata.x, origy=D#vdata.y},
    av(DG, V, D2).

del(DG, V) ->
    digraph:del_vertex(DG, V).


av(DG, V, D) ->
    digraph:add_vertex(DG, V, D).

print_dg(DG) ->
    io:format("Vertices:~n", []),
    lists:foreach(fun(V) -> io:format("  ~p ~p~n", 
				      [V, get(data, DG, V)]) end,
		  get(all, DG)),
    io:format("Edges:~n", []),
    lists:foreach(fun(V) -> io:format("  ~p ~p~n",
				      [V, eget(edge, DG, V)]) end,
		  eget(all, DG)),
    true.