@@ -13,17 +13,19 @@ defmodule ColouredFlow.Validators.Definition.ArcValidator do
1313 alias ColouredFlow.Definition.Arc
1414 alias ColouredFlow.Definition.ColouredPetriNet
1515 alias ColouredFlow.Definition.Transition
16+ alias ColouredFlow.Definition.Variable
1617 alias ColouredFlow.Validators.Exceptions.InvalidArcError
1718
1819 @ spec validate ( ColouredPetriNet . t ( ) ) ::
1920 { :ok , ColouredPetriNet . t ( ) } | { :error , InvalidArcError . t ( ) }
2021 def validate ( % ColouredPetriNet { } = cpnet ) do
2122 vars_and_consts = build_vars_and_consts ( cpnet )
2223 outputs = build_outputs ( cpnet )
24+ bound_vars_by_transition = build_bound_vars_by_transition ( cpnet )
2325
2426 cpnet . arcs
2527 |> Enum . find_value ( fn % Arc { } = arc ->
26- case validate_arc ( arc , vars_and_consts , outputs , cpnet ) do
28+ case validate_arc ( arc , vars_and_consts , outputs , bound_vars_by_transition ) do
2729 :ok -> nil
2830 { :error , reason } -> reason
2931 end
@@ -38,7 +40,7 @@ defmodule ColouredFlow.Validators.Definition.ArcValidator do
3840 % Arc { orientation: :p_to_t } = arc ,
3941 { vars , consts } ,
4042 _outputs ,
41- % ColouredPetriNet { }
43+ _bound_vars_by_transition
4244 ) do
4345 arc . expression . vars
4446 |> MapSet . new ( )
@@ -71,20 +73,14 @@ defmodule ColouredFlow.Validators.Definition.ArcValidator do
7173 % Arc { orientation: :t_to_p } = arc ,
7274 { _vars , consts } ,
7375 outputs ,
74- % ColouredPetriNet { } = cpnet
76+ bound_vars_by_transition
7577 ) do
76- bound_vars =
77- cpnet . arcs
78- |> Enum . flat_map ( fn
79- % Arc { orientation: :p_to_t } = arc -> arc . expression . vars
80- % Arc { } -> [ ]
81- end )
82- |> MapSet . new ( )
78+ bound_vars = Map . get ( bound_vars_by_transition , arc . transition , MapSet . new ( ) )
8379
8480 vars_and_consts =
8581 bound_vars
8682 |> MapSet . union ( consts )
87- |> MapSet . union ( Map . get ( outputs , arc . transition , [ ] ) )
83+ |> MapSet . union ( Map . get ( outputs , arc . transition , MapSet . new ( ) ) )
8884
8985 arc . expression . vars
9086 |> MapSet . new ( )
@@ -124,4 +120,15 @@ defmodule ColouredFlow.Validators.Definition.ArcValidator do
124120 { transition . name , MapSet . new ( transition . action . outputs ) }
125121 end )
126122 end
123+
124+ @ spec build_bound_vars_by_transition ( ColouredPetriNet . t ( ) ) ::
125+ % { Transition . name ( ) => MapSet . t ( Variable . name ( ) ) }
126+ defp build_bound_vars_by_transition ( % ColouredPetriNet { arcs: arcs } ) do
127+ arcs
128+ |> Stream . filter ( & ( & 1 . orientation == :p_to_t ) )
129+ |> Enum . group_by ( & & 1 . transition , & & 1 . expression . vars )
130+ |> Map . new ( fn { transition , vars_lists } ->
131+ { transition , vars_lists |> List . flatten ( ) |> MapSet . new ( ) }
132+ end )
133+ end
127134end
0 commit comments