进出入完善组织机构并加入导入人员和机构功能
554325746@qq.com
2019-08-07 07a66e53d2b4126c2004870d81a379d8ef0071da
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
%% Copyright (c) 2012, Magnus Klaar <klaar@ninenines.eu>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
%% @doc Query processing functions.
-module(glc_lib).
 
-export([
    reduce/1,
    matches/2,
    onoutput/1,
    onoutput/2
]).
 
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-undef(LET).
-ifdef(PROPER).
-include_lib("proper/include/proper.hrl").
-endif.
-endif.
 
%% @doc Return a reduced version of a query.
%% 
%% The purpose of this function is to ensure that a query filter
%% is in the simplest possible form. The query filter returned
%% from this function is functionally equivalent to the original.
reduce(Query) ->
    repeat(Query, fun(Q0) ->
        Q1 = repeat(Q0, fun flatten/1),
        Q2 = required(Q1),
        Q3 = repeat(Q2, fun flatten/1),
        Q4 = common(Q3),
        Q5 = repeat(Q4, fun flatten/1),
        Q6 = constants(Q5),
        Q6
    end).
 
 
%% @doc Test if an event matches a query.
%% This function is only intended to be used for testing purposes.
matches({any, Conds}, Event) ->
    lists:any(fun(Cond) -> matches(Cond, Event) end, Conds);
matches({all, Conds}, Event) ->
    lists:all(fun(Cond) -> matches(Cond, Event) end, Conds);
matches({null, Const}, _Event) ->
    Const;
matches({Key, '<', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 < Term;
        false -> false
    end;
matches({Key, '=<', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 =< Term;
        false -> false
    end;
matches({Key, '=', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 =:= Term;
        false -> false
    end;
matches({Key, '!=', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 =/= Term;
        false -> false
    end;
matches({Key, '>', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 > Term;
        false -> false
    end;
matches({Key, '>=', Term}, Event) ->
    case gre:find(Key, Event) of
        {true, Term2} -> Term2 >= Term;
        false -> false
    end;
matches({Key, '*'}, Event) ->
    case gre:find(Key, Event) of
        {true, _} -> true;
        false -> false
    end;
matches({Key, '!'}, Event) ->
    not matches({Key, '*'}, Event).
 
%% @private Repeatedly apply a function to a query.
%% This is used for query transformation functions that must be applied
%% multiple times to yield the simplest possible version of a query.
repeat(Query, Fun) ->
    case Fun(Query) of
        Query -> Query;
        Query2 -> repeat(Query2, Fun)
    end.
 
 
%% @doc Return the output action of a query.
-spec onoutput(glc_ops:op()) -> output | no_return().
onoutput({_, '<', _}) ->
    output;
onoutput({_, '=<', _}) ->
    output;
onoutput({_, '=', _}) ->
    output;
onoutput({_, '>', _}) ->
    output;
onoutput({_, '>=', _}) ->
    output;
onoutput({_, '*'}) ->
    output;
onoutput({_, '!'}) ->
    output;
onoutput(Query) ->
    erlang:error(badarg, [Query]).
 
%% @doc Modify the output action of a query.
-spec onoutput(Action :: any(), Query :: glc_ops:op()) -> no_return().
onoutput(Action, Query) ->
    erlang:error(badarg, [Action, Query]).
 
 
%% @private Flatten a condition tree.
flatten({all, [Cond]}) ->
    Cond;
flatten({any, [Cond]}) ->
    Cond;
flatten({all, Conds}) ->
    flatten_all([flatten(Cond) || Cond <- Conds]);
flatten({any, [_|_]=Conds}) ->
    flatten_any([flatten(Cond) || Cond <- Conds]);
flatten({with, Cond, Action}) ->
    {with, flatten(Cond), Action};
flatten([{with, _Cond, _Action}|_] = I) ->
    [{with, flatten(Cond), Action} || {with, Cond, Action} <- I];
flatten(Other) ->
    valid(Other).
 
 
%% @private Flatten and remove duplicate members of an "all" filter.
flatten_all(Conds) ->
    {all, lists:usort(flatten_tag(all, Conds))}.
 
%% @private Flatten and remove duplicate members of an "any" filter.
flatten_any(Conds) ->
    {any, lists:usort(flatten_tag(any, Conds))}.
 
%% @private Common function for flattening "all" or "and" filters.
flatten_tag(Tag, [{Tag, Conds}|T]) ->
    Conds ++ flatten_tag(Tag, T);
flatten_tag(Tag, [H|T]) ->
    [H|flatten_tag(Tag, T)];
flatten_tag(_Tag, []) ->
    [].
 
%% @private Factor out required filters.
%%
%% Identical conditions may occur in all sub-filters of an "any" filter. These
%% filters can be tested once before testing the conditions that are unique to
%% each sub-filter.
%%
%% Assume that the input has been flattened first in order to eliminate all
%% occurances of an "any" filters being "sub-filters" of "any" filters.
required({any, [H|_]=Conds}) ->
    Init = ordsets:from_list(case H of {all, Init2} -> Init2; H -> [H] end),
    case required(Conds, Init) of
        nonefound ->
            Conds2 = [required(Cond) || Cond <- Conds],
            {any, Conds2};
        {found, Req} ->
            Conds2 = [required(deleteall(Cond, Req)) || Cond <- Conds],
            {all, [{all, Req}, {any, Conds2}]}
    end;
required({all, Conds}) ->
    {all, [required(Cond) || Cond <- Conds]};
required(Other) ->
    Other.
 
required([{all, Conds}|T], Acc) ->
    required(T, ordsets:intersection(ordsets:from_list(Conds), Acc));
required([{any, _}|_]=Cond, Acc) ->
    erlang:error(badarg, [Cond, Acc]);
required([H|T], Acc) ->
    required(T, ordsets:intersection(ordsets:from_list([H]), Acc));
required([], [_|_]=Req) ->
    {found, Req};
required([], []) ->
    nonefound.
 
%% @private Factor our common filters.
%%
%% Identical conditions may occur in some sub-filters of an "all" filter. These
%% filters can be tested once before testing the conditions that are unique to
%% each sub-filter. This is different from factoring out common sub-filters
%% in an "any" filter where the only those sub-filters that exist in all
%% members.
%%
%% Assume that the input has been flattened first in order to eliminate all
%% occurances of an "any" filters being "sub-filters" of "any" filters.
common({all, Conds}) ->
    case common_(Conds, []) of
        {found, Found} ->
            {all, [Found|[delete(Cond, Found) || Cond <- Conds]]};
        nonefound ->
            {all, [common(Cond) || Cond <- Conds]}
    end;
common({any, Conds}) ->
    {any, [common(Cond) || Cond <- Conds]};
common(Other) ->
    Other.
    
 
common_([{any, Conds}|T], Seen) ->
    Set = ordsets:from_list(Conds),
    case ordsets:intersection(Set, Seen) of
        [] -> common_(T, ordsets:union(Set, Seen));
        [H|_] -> {found, H}
    end;
common_([H|T], Seen) ->
    case ordsets:is_element(H, Seen) of
        false -> common_(T, ordsets:union(ordsets:from_list([H]), Seen));
        true -> {found, H}
    end;
common_([], _Seen) ->
    nonefound.
 
%% @private Delete all occurances of constants.
%%
%% An "all" or "any" filter may be reduced to a constant outcome when all
%% sub-filters has been factored out from the filter. In these cases the
%% filter can be removed from the query.
constants(Query) ->
    delete(Query, {null, true}).
    
 
 
%% @private Delete all occurances of a filter.
%%
%% Assume that the function is called because a filter is tested
%% by a parent filter. It is therefore safe to replace occurances
%% with a null filter that always returns true.
delete({all, Conds}, Filter) ->
    {all, [delete(Cond, Filter) || Cond <- Conds, Cond =/= Filter]};
delete({any, Conds}, Filter) ->
    {any, [delete(Cond, Filter) || Cond <- Conds, Cond =/= Filter]};
delete(Filter, Filter) ->
    {null, true};
delete(Cond, _Filter) ->
    Cond.
 
%% @private Delete all occurances of multiple filters.
deleteall(Filter, [H|T]) ->
    deleteall(delete(Filter, H), T);
deleteall(Filter, []) ->
    Filter.
 
 
 
%% @private Test if a term is a valid filter.
-spec is_valid(glc_ops:op()) -> boolean().
is_valid({Field, '<', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '=<', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '=', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '!=', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '>=', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '>', _Term}) when is_atom(Field) ->
    true;
is_valid({Field, '*'}) when is_atom(Field) ->
    true;
is_valid({Field, '!'}) when is_atom(Field) ->
    true;
is_valid({null, true}) ->
    true;
is_valid({null, false}) ->
    true;
is_valid(_Other) ->
    false.
 
%% @private Assert that a term is a valid filter.
%% If the term is a valid filter. The original term will be returned.
%% If the term is not a valid filter. A `badarg' error is thrown.
valid(Term) ->
    is_valid(Term) orelse erlang:error(badarg, [Term]),
    Term.
 
 
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
 
all_one_test() ->
    ?assertEqual(glc:eq(a, 1),
        glc_lib:reduce(glc:all([glc:eq(a, 1)]))
    ).
 
all_sort_test() ->
    ?assertEqual(glc:all([glc:eq(a, 1), glc:eq(b, 2)]),
        glc_lib:reduce(glc:all([glc:eq(b, 2), glc:eq(a, 1)]))
    ).
 
any_one_test() ->
    ?assertEqual(glc:eq(a, 1),
        glc_lib:reduce(glc:any([glc:eq(a, 1)]))
    ).
 
all_two_test() ->
    ?assertEqual(glc_lib:reduce(glc:all([glc:wc(a), glc:nf(b)])),
       glc_lib:reduce(glc:any([
                    glc:all([glc:wc(a)]), 
                    glc:all([glc:wc(a), glc:nf(b)])]))
    ).
 
any_sort_test() ->
    ?assertEqual(glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
        glc_lib:reduce(glc:any([glc:eq(b, 2), glc:eq(a, 1)]))
    ).
 
all_nest_test() ->
    ?assertEqual(glc:all([glc:eq(a, 1), glc:eq(b, 2)]),
        glc_lib:reduce(glc:all([glc:eq(a, 1), glc:all([glc:eq(b, 2)])]))
    ),
    ?assertEqual(glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]),
        glc_lib:reduce(glc:all([glc:eq(c, 3),
            glc:all([glc:eq(a, 1),
                glc:all([glc:eq(b, 2)])])]))
    ).
 
any_nest_test() ->
    ?assertEqual(glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
        glc_lib:reduce(glc:any([glc:eq(a, 1), glc:any([glc:eq(b, 2)])]))
    ),
    ?assertEqual(glc:any([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]),
        glc_lib:reduce(glc:any([glc:eq(c, 3),
            glc:any([glc:eq(a, 1),
                glc:any([glc:eq(b, 2)])])]))
    ).
 
all_equiv_test() ->
    ?assertEqual(glc:eq(a, 1),
        glc_lib:reduce(glc:all([glc:eq(a, 1), glc:eq(a, 1)]))
    ).
 
any_equiv_test() ->
    ?assertEqual(glc:eq(a, 1),
        glc_lib:reduce(glc:any([glc:eq(a, 1), glc:eq(a, 1)]))
    ).
 
any_required_test() ->
    ?assertEqual(
        glc:all([
            glc:any([glc:nf(d), glc:eq(b, 2), glc:eq(c, 3)]),
            glc:eq(a, 1)
        ]),
        glc_lib:reduce(
            glc:any([
                glc:all([glc:eq(a, 1), glc:nf(d)]),
                glc:all([glc:eq(a, 1), glc:eq(b, 2)]),
                glc:all([glc:eq(a, 1), glc:eq(c, 3)])]))
    ).
        
 
all_common_test() ->
    ?assertEqual(
        glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]),
        glc_lib:reduce(
            glc:all([
                glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
                glc:any([glc:eq(a, 1), glc:eq(c, 3)])]))
    ).
 
delete_from_all_test() ->
    ?assertEqual(
        glc:all([glc:eq(b,2)]),
        deleteall(
            glc:all([glc:eq(a, 1),glc:eq(b,2)]), [glc:eq(a, 1), glc:nf(a)])
    ).
 
delete_from_any_test() ->
    ?assertEqual(
        glc:any([glc:eq(b,2)]),
        deleteall(
            glc:any([glc:eq(a, 1),glc:eq(b,2)]), [glc:eq(a, 1), glc:wc(a)])
    ).
 
default_is_output_test_() ->
    [?_assertEqual(output, glc_lib:onoutput(glc:lt(a, 1))),
     ?_assertEqual(output, glc_lib:onoutput(glc:lte(a, 1))),
     ?_assertEqual(output, glc_lib:onoutput(glc:eq(a, 1))),
     ?_assertEqual(output, glc_lib:onoutput(glc:gt(a, 1))),
     ?_assertEqual(output, glc_lib:onoutput(glc:gte(a, 1))),
     ?_assertEqual(output, glc_lib:onoutput(glc:wc(a))),
     ?_assertEqual(output, glc_lib:onoutput(glc:nf(a)))
    ].
 
matches_test_() ->
    Event = gre:make([{a, 2}], [list]),
    [?_assertEqual(true, glc_lib:matches(glc:lt(a, 3), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:lte(a, 2), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:eq(a, 2), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:gt(a, 1), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:gte(a, 2), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:wc(a), Event)),
     ?_assertEqual(true, glc_lib:matches(glc:nf(b), Event)),
 
     ?_assertEqual(false, glc_lib:matches(glc:lt(a, 2), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:lte(a, 1), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:eq(a, 3), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:gt(a, 2), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:gte(a, 3), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:wc(b), Event)),
     ?_assertEqual(false, glc_lib:matches(glc:nf(a), Event))
    ].
 
-ifdef(PROPER).
 
 
prop_reduce_returns() ->
    ?FORALL(Query, glc_ops:op(),
        returns(fun() -> glc_lib:reduce(Query) end)).
 
reduce_returns_test() ->
    ?assert(proper:quickcheck(prop_reduce_returns())).
 
prop_matches_returns_boolean() ->
    ?FORALL({Query, Event}, {glc_ops:op(), [{atom(), term()}]},
        is_boolean(glc_lib:matches(Query, gre:make(Event, [list])))).
 
matches_returns_boolean_test() ->
    ?assert(proper:quickcheck(prop_matches_returns_boolean())).
 
returns(Fun) ->
    try Fun(),
        true
    catch _:_ ->
        false
    end.
 
-endif.
-endif.