@@ -51,37 +51,43 @@ defmodule ColouredFlow.EnabledBindingElements.Computation do
5151 binding_combinations = Binding . combine ( arc_bindings )
5252
5353 Enum . flat_map ( binding_combinations , fn binding ->
54- inputs
55- |> Enum . reduce_while ( [ ] , fn { arc , place } , acc ->
56- arc_binding = merge_constants ( binding , arc , constants )
57-
58- with (
59- { :ok , { coefficient , value } } <- eval_arc ( arc , arc_binding ) ,
60- colour_set = fetch_colour_set! ( place . colour_set , cpnet ) ,
61- of_type_context = build_of_type_context ( cpnet ) ,
62- { :ok , ^ value } <- ColourSet.Of . of_type ( value , colour_set . type , of_type_context ) ,
63- guard_binding = merge_constants ( binding , transition , constants ) ,
64- { :ok , true } <- eval_transition_guard ( transition , guard_binding ) ,
65- marking = get_marking ( place , markings ) ,
66- tokens = MultiSet . duplicate ( value , coefficient ) ,
67- true <- MultiSet . include? ( marking . tokens , tokens )
68- ) do
69- { :cont , [ % Marking { place: place . name , tokens: tokens } | acc ] }
70- else
71- _other -> { :halt , :error }
72- end
73- end )
74- |> case do
75- :error ->
76- [ ]
54+ build_binding_element ( inputs , binding , transition , constants , markings , cpnet )
55+ end )
56+ end
7757
78- to_consume ->
79- # binding here should not contain constants
80- [ BindingElement . new ( transition . name , binding , to_consume ) ]
81- end
58+ defp build_binding_element ( inputs , binding , transition , constants , markings , cpnet ) do
59+ case collect_consumption ( inputs , binding , transition , constants , markings , cpnet ) do
60+ :error -> [ ]
61+ to_consume -> [ BindingElement . new ( transition . name , binding , to_consume ) ]
62+ end
63+ end
64+
65+ defp collect_consumption ( inputs , binding , transition , constants , markings , cpnet ) do
66+ Enum . reduce_while ( inputs , [ ] , fn { arc , place } , acc ->
67+ try_consume ( arc , place , binding , transition , constants , markings , cpnet , acc )
8268 end )
8369 end
8470
71+ defp try_consume ( arc , place , binding , transition , constants , markings , cpnet , acc ) do
72+ arc_binding = merge_constants ( binding , arc , constants )
73+
74+ with (
75+ { :ok , { coefficient , value } } <- eval_arc ( arc , arc_binding ) ,
76+ colour_set = fetch_colour_set! ( place . colour_set , cpnet ) ,
77+ of_type_context = build_of_type_context ( cpnet ) ,
78+ { :ok , ^ value } <- ColourSet.Of . of_type ( value , colour_set . type , of_type_context ) ,
79+ guard_binding = merge_constants ( binding , transition , constants ) ,
80+ { :ok , true } <- eval_transition_guard ( transition , guard_binding ) ,
81+ marking = get_marking ( place , markings ) ,
82+ tokens = MultiSet . duplicate ( value , coefficient ) ,
83+ true <- MultiSet . include? ( marking . tokens , tokens )
84+ ) do
85+ { :cont , [ % Marking { place: place . name , tokens: tokens } | acc ] }
86+ else
87+ _other -> { :halt , :error }
88+ end
89+ end
90+
8591 @ spec reject_invalid_bandings ( [ [ [ BindingElement . binding ( ) ] ] ] , ColouredPetriNet . t ( ) ) ::
8692 [ [ [ BindingElement . binding ( ) ] ] ]
8793 defp reject_invalid_bandings ( bindings_list , cpnet ) do
0 commit comments