Packages
bpe
6.4.0
13.5.22-aleph
11.4.16
11.4.15
11.4.14
11.4.13
9.9.7
9.9.6
8.12.4
8.12.3
8.12.1
8.12.0
retired
8.2.1
8.2.0
8.1.0
7.11.0
7.10.4
7.10.3
7.10.2
7.10.1
7.9.1
7.9.0
7.8.2
7.8.1
7.8.0
7.6.4
7.6.3
7.6.2
7.6.1
7.6.0
7.5.15
7.5.14
7.5.13
7.5.12
7.5.11
7.5.10
7.5.9
7.5.8
7.5.7
7.5.6
7.5.5
7.5.3
7.5.2
7.5.1
7.5.0
7.4.11
7.4.10
7.4.9
7.4.8
7.4.7
7.4.6
7.4.5
7.4.4
7.4.3
7.4.2
7.4.1
7.4.0
7.3.0
7.2.8
7.2.7
7.2.6
7.1.6
7.1.5
7.1.4
7.1.3
7.1.2
6.12.7
6.12.6
6.12.5
6.12.3
6.12.2
6.12.1
6.12.0
6.11.0
6.10.0
6.5.3
6.5.2
6.5.1
6.5.0
6.4.0
6.3.0
5.12.0
5.11.4
5.11.3
5.11.2
5.11.1
5.11.0
5.8.7
5.8.6
5.8.5
5.8.4
5.8.3
5.8.2
5.8.1
5.8.0
5.7.0
5.6.0
5.5.2
5.5.1
5.4.0
5.2.0
5.1.3
5.1.2
5.1.1
4.12.4
4.12.3
4.12.2
4.12.1
4.12.0
4.11.8
4.11.7
4.11.6
4.11.5
4.11.4
4.11.3
4.11.2
4.11.1
4.11.0
4.10.24
4.10.23
4.10.22
4.10.21
4.10.20
4.10.19
4.10.18
4.10.17
4.10.16
4.10.15
4.10.14
4.10.13
4.10.12
4.10.11
4.10.10
4.10.9
4.10.8
4.10.7
4.10.6
4.10.5
4.10.4
4.10.3
4.10.2
4.10.1
4.10.0
4.9.18
4.9.17
4.9.16
4.9.15
4.9.14
4.9.13
4.9.12
4.9.11
4.9.10
4.9.9
4.9.8
4.9.7
4.9.6
4.9.5
4.9.4
4.9.3
4.9.2
4.9.1
4.9.0
4.8.1
4.8.0
4.7.5
4.7.3
4.7.2
4.7.1
4.7.0
4.6.0
2.4.0
0.7.16
ERP/1: RTP GST WebRTC ICE SDP H.264 H.265 MP4 MPEG-2 HLS HEVC
Current section
Files
Jump to
Current section
Files
src/ext/bpe_xml.erl
-module(bpe_xml).
-include_lib("bpe/include/bpe.hrl").
-include_lib("xmerl/include/xmerl.hrl").
-compile(export_all).
-import(lists,[keyfind/3, keyreplace/4]).
-define(MODEL, 'http://www.omg.org/spec/BPMN/20100524/MODEL').
% explicit namespace for model elements
ns(#xmlElement{name=N, nsinfo=[], namespace=#xmlNamespace{default=?MODEL}}=E,S) ->
N1 = list_to_atom("bpmn:"++atom_to_list(N)),
{E#xmlElement{name=N1, nsinfo={"bpmn", N}}, S};
ns(#xmlElement{name=N, nsinfo=[], namespace=#xmlNamespace{default=[], nodes=Nds}}=E,S) ->
case lists:keyfind(?MODEL, 2, Nds) of
{P,_} ->
N1 = list_to_atom(P++":"++atom_to_list(N)),
{E#xmlElement{name=N1, nsinfo={P,N}},S};
false -> {E,S} end;
ns(E,S) -> {E,S}.
attr(E) -> [ {N,V} || #xmlAttribute{name=N,value=V} <- E].
find(E=[#xmlText{}, #xmlElement{name='bpmn:conditionExpression'} | _],[]) ->
[{X,[V],attr(A)} || #xmlElement{name=X,attributes=A,content=[#xmlText{value=V}]} <- E];
find(E=[#xmlText{}, #xmlElement{name='bpmn:flowNodeRef'} | _],[]) ->
[{X,[],{value,V}} || #xmlElement{name=X,content=[#xmlText{value=V}]} <- E];
find(E,[]) -> [{X,find(Sub,[]),attr(A)} || #xmlElement{name=X,attributes=A,content=Sub} <- E];
find(E, I) -> [{X,find(Sub,[]),attr(A)} || #xmlElement{name=X,attributes=A,content=Sub} <- E, X == I].
def() -> load("priv/sample.bpmn").
load(File) -> load(File, ?MODULE).
load(File,Module) ->
case xmerl_scan:file(File, [{hook_fun, fun ns/2}]) of
{error, R} -> {error, R};
{#xmlElement{name=N,content=C},_} ->
_E = {'bpmn:definitions',[{'bpmn:process',Elements,Attrs}],_} = {N,find(C,'bpmn:process'),attr(C)},
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
Proc = reduce(Elements,#process{id=Id,name=Name,module=Module}),
Tasks = fillInOut(Proc#process.tasks, Proc#process.flows),
Tasks1 = fixRoles(Tasks, Proc#process.roles),
Proc#process{ id=[],
tasks = Tasks1,
xml = filename:basename(File, ".bpmn"),
events = Proc#process.events } end.
reduce([], Acc) ->
Acc;
reduce([{'bpmn:task',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#task{id=Id,name=Name}|Tasks]});
reduce([{'bpmn:startEvent',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#beginEvent{id=Id,name=Name}|Tasks],beginEvent=Id});
reduce([{'bpmn:endEvent',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#endEvent{id=Id,name=Name}|Tasks],endEvent=Id});
reduce([{'bpmn:sequenceFlow',Body,Attrs}|T],#process{flows=Flows} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
Source = proplists:get_value(sourceRef,Attrs),
Target = proplists:get_value(targetRef,Attrs),
F = #sequenceFlow{id=Id,name=Name,source=Source,target=Target},
Flow = reduce(Body,F),
reduce(T,Process#process{flows=[Flow|Flows]});
reduce([{'bpmn:conditionExpression',Body,_Attrs}|T],#sequenceFlow{} = Flow) ->
reduce(T,Flow#sequenceFlow{condition = parse(hd(Body))});
reduce([{'bpmn:parallelGateway',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#gateway{id=Id,name=Name,type=parallel}|Tasks]});
reduce([{'bpmn:exclusiveGateway',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Default = proplists:get_value(default,Attrs,[]),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#gateway{id=Id,name=Name,type=exclusive,def=Default}|Tasks]});
reduce([{'bpmn:inclusiveGateway',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#gateway{id=Id,name=Name,type=inclusive}|Tasks]});
reduce([{'bpmn:complexGateway',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#gateway{id=Id,name=Name,type=complex}|Tasks]});
reduce([{'bpmn:gateway',_Body,Attrs}|T],#process{tasks=Tasks} = Process) ->
Id = proplists:get_value(id,Attrs),
Name = unicode:characters_to_binary(proplists:get_value(name,Attrs,[])),
reduce(T,Process#process{tasks=[#gateway{id=Id,name=Name}|Tasks]});
reduce([{'bpmn:laneSet',Lanes,_Attrs}|T], Process) ->
Roles = [ #role{ id = proplists:get_value(id,Att,[]),
tasks = [ Name || {_, [], {value, Name}} <- Tasks ],
name = unicode:characters_to_binary(proplists:get_value(name,Att,[]),utf16)
} || {_,Tasks,Att} <- Lanes],
% io:format("Lanes: ~p~n",[Lanes]),
% io:format("Roles: ~p~n",[Roles]),
reduce(T,Process#process{roles = Roles});
%%TODO? Maybe add support for those intries and remove them from this guard
reduce([{SkipType,_Body,_Attrs}|T],#process{} = Process)
when SkipType == 'bpmn:dataObjectReference';
SkipType == 'bpmn:dataObject';
SkipType == 'bpmn:association';
SkipType == 'bpmn:textAnnotation';
SkipType == 'bpmn:extensionElements' ->
skip,
reduce(T,Process).
%%TODO?: Maybe use incoming/outgoing from XML itself instead of fillInOut
fillInOut(Tasks, []) -> Tasks;
fillInOut(Tasks, [#sequenceFlow{id=Name,source=Source,target=Target}|Flows]) ->
Tasks1 = key_push_value(Name, #gateway.output, Source, #gateway.id, Tasks),
Tasks2 = key_push_value(Name, #gateway.input, Target, #gateway.id, Tasks1),
fillInOut(Tasks2, Flows).
key_push_value(Value, ValueKey, ElemId, ElemIdKey, List) ->
Elem = keyfind(ElemId, ElemIdKey, List),
RecName = hd(tuple_to_list(Elem)),
case RecName of
endEvent -> List;
_ -> keyreplace(ElemId, ElemIdKey, List,
setelement(ValueKey, Elem, [Value|element(ValueKey,Elem)])) end.
fixRoles(Tasks, []) -> Tasks;
fixRoles(Tasks, [#role{id=Id,name=_Name,tasks=XmlTasks}|Lanes]) -> fixRoles(update_roles(XmlTasks, Tasks, Id), Lanes).
update_roles([], AllTasks, _Role) -> AllTasks;
update_roles([TaskId|Rest], AllTasks, Role) ->
update_roles(Rest,key_push_value(Role, #task.roles, TaskId, #task.id, AllTasks),Role).
action({request,_,_},P) -> {reply,P}.
auth(_) -> true.
parse(Term) when is_binary(Term) ->
parse(binary_to_list(Term));
parse(Term) ->
{ok,Tokens,_} = erl_scan:string(Term),
{ok,AbsForm} = erl_parse:parse_exprs(Tokens),
{_,Value,_} = erl_eval:exprs(AbsForm, erl_eval:new_bindings()),
Value.