scjson 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/scjson/child_order.rb +95 -0
- data/lib/scjson/engine/context.rb +95 -127
- data/lib/scjson/engine.rb +9 -3
- data/lib/scjson/types.rb +220 -64
- data/lib/scjson/version.rb +1 -1
- data/lib/scjson.rb +37 -11
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e1e47599abbe98d5537ef303dd02b46064b192f2dd7dc1e2c261efaa2a970e14
|
|
4
|
+
data.tar.gz: 3a62dc58f0e2bdc68f14c0504dbbec148884afd83c9d53ceb3c54ebcd9e57fcc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cdf11a56763d9716539c0ab10fa39748d6b2a3a2217f06a0b23165bdf1db021f6cefaf6463ce040fff3b5ae646f3c762f0c0e14c7f4019fbf98b8a21bee06eb0
|
|
7
|
+
data.tar.gz: 48131c21d5cdfb774898c0f1bbee94b7a05907e95cf73b831c8ab81c14b97d647d4bde700ee0197e2f6148b57015ae2de31984f6c231915c6c72af6ad0ded049
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Capture and replay authored SCXML child sequences alongside grouped SCJSON fields.
|
|
4
|
+
# Agent Name: scjson-converter-parity
|
|
5
|
+
# Part of the scjson project. Licensed under the BSD 1-Clause License.
|
|
6
|
+
|
|
7
|
+
module Scjson
|
|
8
|
+
module ChildOrder
|
|
9
|
+
ACTIONS = %w[raise if foreach send script assign log cancel elseif else].freeze
|
|
10
|
+
ACTION_FIELDS = { 'if' => 'if_value', 'else' => 'else_value', 'raise' => 'raise_value' }.freeze
|
|
11
|
+
EXECUTABLE = %w[transition onentry onexit finalize foreach if].freeze
|
|
12
|
+
STRUCTURAL = %w[state parallel final].freeze
|
|
13
|
+
FIELDS = %w[execution_order state_order region_order].freeze
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def spec(owner)
|
|
17
|
+
return ['execution_order', ACTIONS] if EXECUTABLE.include?(owner)
|
|
18
|
+
return ['state_order', STRUCTURAL] if %w[scxml state].include?(owner)
|
|
19
|
+
return ['region_order', %w[state parallel]] if owner == 'parallel'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def extension_kind(qname)
|
|
23
|
+
raise ArgumentError, 'ordered extension requires a qname' unless qname.is_a?(String) && !qname.empty?
|
|
24
|
+
qname.start_with?('{') ? qname : "{}#{qname}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def check_markers(owner, order)
|
|
28
|
+
saw_else = false
|
|
29
|
+
order.each do |kind|
|
|
30
|
+
next unless %w[elseif else].include?(kind)
|
|
31
|
+
raise ArgumentError, 'invalid conditional marker sequence' if owner != 'if' || saw_else
|
|
32
|
+
saw_else = kind == 'else'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Return ordered children for each group of XML slots owned by this parent.
|
|
37
|
+
def slots(owner, map)
|
|
38
|
+
field, kinds = spec(owner)
|
|
39
|
+
misplaced = (map.keys & FIELDS) - [field]
|
|
40
|
+
raise ArgumentError, "#{misplaced.first} is invalid on #{owner}" unless misplaced.empty?
|
|
41
|
+
return {} unless field
|
|
42
|
+
explicit = map.key?(field)
|
|
43
|
+
if !explicit && field == 'execution_order'
|
|
44
|
+
%w[elseif else].each do |kind|
|
|
45
|
+
field_name = ACTION_FIELDS.fetch(kind, kind)
|
|
46
|
+
next unless map.key?(field_name)
|
|
47
|
+
value = map[field_name]
|
|
48
|
+
check_markers(owner, [kind] * (value.is_a?(Array) ? value.length : 1))
|
|
49
|
+
end
|
|
50
|
+
return {}
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
groups = {}
|
|
54
|
+
slots = {}
|
|
55
|
+
kinds.each do |kind|
|
|
56
|
+
key = ACTION_FIELDS.fetch(kind, kind)
|
|
57
|
+
next unless map.key?(key)
|
|
58
|
+
if explicit && field != 'execution_order' && !map[key].is_a?(Array)
|
|
59
|
+
raise ArgumentError, "#{field} requires child arrays"
|
|
60
|
+
end
|
|
61
|
+
values = map[key].is_a?(Array) ? map[key] : [map[key]]
|
|
62
|
+
raise ArgumentError, "#{field} requires child objects" unless values.all? { |v| v.is_a?(Hash) }
|
|
63
|
+
groups[kind] = values.map { |value| [kind, value] }
|
|
64
|
+
slots[key] = values.length
|
|
65
|
+
end
|
|
66
|
+
if field == 'execution_order' && map.key?('other_element')
|
|
67
|
+
extensions = map['other_element']
|
|
68
|
+
extensions = [extensions] unless extensions.is_a?(Array)
|
|
69
|
+
extensions.each do |child|
|
|
70
|
+
raise ArgumentError, 'expected an extension object' unless child.is_a?(Hash)
|
|
71
|
+
kind = extension_kind(child['qname'])
|
|
72
|
+
(groups[kind] ||= []) << [child['qname'], child]
|
|
73
|
+
end
|
|
74
|
+
slots['other_element'] = extensions.length
|
|
75
|
+
end
|
|
76
|
+
order = explicit ? map[field] : groups.flat_map { |kind, values| [kind] * values.length }
|
|
77
|
+
raise ArgumentError, "#{field} must be an array" unless order.is_a?(Array)
|
|
78
|
+
check_markers(owner, order) if field == 'execution_order'
|
|
79
|
+
indices = Hash.new(0)
|
|
80
|
+
sequence = order.map do |kind|
|
|
81
|
+
index = indices[kind]
|
|
82
|
+
indices[kind] += 1
|
|
83
|
+
groups.fetch(kind, [])[index] || (raise ArgumentError, "#{field} references an absent child: #{kind}")
|
|
84
|
+
end
|
|
85
|
+
raise ArgumentError, "#{field} omits a child" unless sequence.length == slots.values.sum
|
|
86
|
+
|
|
87
|
+
offset = 0
|
|
88
|
+
map.keys.each_with_object({}) do |key, result|
|
|
89
|
+
next unless slots.key?(key)
|
|
90
|
+
result[key] = sequence.slice(offset, slots[key])
|
|
91
|
+
offset += slots[key]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -18,6 +18,11 @@ module Scjson
|
|
|
18
18
|
# iterative scaffold toward full SCXML semantics.
|
|
19
19
|
#
|
|
20
20
|
class DocumentContext
|
|
21
|
+
OBJECT_PROPERTIES = %w[constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString
|
|
22
|
+
toString valueOf __proto__ __defineGetter__ __defineSetter__ __lookupGetter__ __lookupSetter__].freeze
|
|
23
|
+
ARRAY_PROPERTIES = %w[length at concat copyWithin entries every fill filter find findIndex findLast findLastIndex
|
|
24
|
+
flat flatMap forEach includes indexOf join keys lastIndexOf map pop push reduce reduceRight
|
|
25
|
+
reverse shift slice some sort splice toReversed toSorted toSpliced unshift values with].freeze
|
|
21
26
|
# @return [Hash{String=>Object}] Canonical scjson root map
|
|
22
27
|
attr_reader :root
|
|
23
28
|
# @return [Array<String>] Active state configuration
|
|
@@ -65,6 +70,14 @@ module Scjson
|
|
|
65
70
|
@internal_queue = []
|
|
66
71
|
@deferred_internal = []
|
|
67
72
|
@defer_done = true
|
|
73
|
+
completions = new_state_completions
|
|
74
|
+
until completions.empty?
|
|
75
|
+
event = "done.state.#{completions.shift}"
|
|
76
|
+
transitions = select_transitions_for_event(event)
|
|
77
|
+
next if transitions.empty?
|
|
78
|
+
apply_transition_set(transitions, cause: event)
|
|
79
|
+
completions.concat(new_state_completions)
|
|
80
|
+
end
|
|
68
81
|
# Schedule invocations for initial configuration
|
|
69
82
|
schedule_invocations_for_entered(@configuration)
|
|
70
83
|
end
|
|
@@ -76,8 +89,7 @@ module Scjson
|
|
|
76
89
|
def leaf_state_ids
|
|
77
90
|
leaves = []
|
|
78
91
|
@states.each do |id, node|
|
|
79
|
-
|
|
80
|
-
leaves << id unless has_children
|
|
92
|
+
leaves << id unless composite?(node)
|
|
81
93
|
end
|
|
82
94
|
leaves.sort
|
|
83
95
|
end
|
|
@@ -212,31 +224,29 @@ module Scjson
|
|
|
212
224
|
rescue StandardError
|
|
213
225
|
# ignore autoforward errors
|
|
214
226
|
end
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
227
|
+
# Consume this event once across the enabled regions before stabilizing.
|
|
228
|
+
tx_set = select_transitions_for_event(ev_name)
|
|
229
|
+
next if tx_set.empty?
|
|
230
|
+
|
|
231
|
+
e1, x1, f1, a1, d1 = apply_transition_set(tx_set, cause: ev_name)
|
|
232
|
+
entered.concat(e1)
|
|
233
|
+
exited.concat(x1)
|
|
234
|
+
fired.concat(f1)
|
|
235
|
+
action_log.concat(a1)
|
|
236
|
+
datamodel_delta.merge!(d1)
|
|
237
|
+
enqueue_done_events
|
|
238
|
+
flush_pending_done_invoke
|
|
239
|
+
0.upto(100) do
|
|
240
|
+
tx0 = select_transitions_for_event(nil)
|
|
241
|
+
break if tx0.empty?
|
|
242
|
+
e2, x2, f2, a2, d2 = apply_transition_set(tx0, cause: nil)
|
|
243
|
+
entered.concat(e2)
|
|
244
|
+
exited.concat(x2)
|
|
245
|
+
fired.concat(f2)
|
|
246
|
+
action_log.concat(a2)
|
|
247
|
+
datamodel_delta.merge!(d2)
|
|
225
248
|
enqueue_done_events
|
|
226
249
|
flush_pending_done_invoke
|
|
227
|
-
# After a set, process eventless transitions to quiescence
|
|
228
|
-
0.upto(100) do
|
|
229
|
-
tx0 = select_transitions_for_event(nil)
|
|
230
|
-
break if tx0.empty?
|
|
231
|
-
e2, x2, f2, a2, d2 = apply_transition_set(tx0, cause: nil)
|
|
232
|
-
entered.concat(e2)
|
|
233
|
-
exited.concat(x2)
|
|
234
|
-
fired.concat(f2)
|
|
235
|
-
action_log.concat(a2)
|
|
236
|
-
datamodel_delta.merge!(d2)
|
|
237
|
-
enqueue_done_events
|
|
238
|
-
flush_pending_done_invoke
|
|
239
|
-
end
|
|
240
250
|
end
|
|
241
251
|
end
|
|
242
252
|
|
|
@@ -483,6 +493,7 @@ module Scjson
|
|
|
483
493
|
# Update configuration: remove exited leaves and add targets (leaf-level model)
|
|
484
494
|
new_config = @configuration.dup
|
|
485
495
|
exit_order.each { |sid| new_config.delete(sid) }
|
|
496
|
+
@completed_states -= exit_order if @completed_states
|
|
486
497
|
# Ensure source is removed even if not in exit_order due to missing ids
|
|
487
498
|
new_config.delete(source_id)
|
|
488
499
|
targets.each { |tid| new_config << tid unless new_config.include?(tid) }
|
|
@@ -524,7 +535,6 @@ module Scjson
|
|
|
524
535
|
chosen_lca = choose_shallowest_ancestor(lcas)
|
|
525
536
|
exit_chain = path_up_exclusive(source_id, chosen_lca)
|
|
526
537
|
exit_set.concat(exit_chain)
|
|
527
|
-
seq = []
|
|
528
538
|
targets.each do |tid|
|
|
529
539
|
if @tag_type[tid] == :history
|
|
530
540
|
parent_id = @parent[tid]
|
|
@@ -541,18 +551,20 @@ module Scjson
|
|
|
541
551
|
end
|
|
542
552
|
end
|
|
543
553
|
remembered.each do |rid|
|
|
544
|
-
|
|
554
|
+
entry_sequences << path_down_from_lca(chosen_lca, rid)
|
|
545
555
|
end
|
|
546
556
|
else
|
|
547
|
-
|
|
557
|
+
entry_sequences << path_down_from_lca(chosen_lca, tid)
|
|
548
558
|
end
|
|
549
559
|
end
|
|
550
|
-
entry_sequences << seq
|
|
551
560
|
fired << { 'source' => source_id, 'targets' => targets, 'event' => cause, 'cond' => nil }
|
|
552
561
|
end
|
|
553
562
|
|
|
554
563
|
# Deduplicate exit set, deep->shallow
|
|
555
|
-
|
|
564
|
+
# Exiting a compound or parallel state also exits every active descendant.
|
|
565
|
+
active = @configuration.flat_map { |leaf| ancestors(leaf) }.uniq
|
|
566
|
+
exit_order = active.select { |sid| ancestors(sid).any? { |id| exit_set.include?(id) } }
|
|
567
|
+
.sort_by { |sid| -ancestors(sid).length }
|
|
556
568
|
# Execute onexit
|
|
557
569
|
exit_order.each do |sid|
|
|
558
570
|
a, d = run_onexit(sid)
|
|
@@ -563,6 +575,7 @@ module Scjson
|
|
|
563
575
|
# Update configuration: remove exit leaves; we'll add entered leaves after expansion
|
|
564
576
|
new_config = @configuration.dup
|
|
565
577
|
exit_order.each { |sid| new_config.delete(sid) }
|
|
578
|
+
@completed_states -= exit_order if @completed_states
|
|
566
579
|
|
|
567
580
|
# Record history for parents being exited based on previous configuration
|
|
568
581
|
record_history_for_exits(exit_order, @configuration)
|
|
@@ -579,33 +592,21 @@ module Scjson
|
|
|
579
592
|
entered_leaves = []
|
|
580
593
|
entry_sequences.each do |seq|
|
|
581
594
|
seq.each do |sid|
|
|
595
|
+
next if entered.include?(sid)
|
|
582
596
|
a, d = run_onentry(sid)
|
|
583
597
|
actions.concat(a)
|
|
584
598
|
delta.merge!(d)
|
|
585
599
|
entered << sid
|
|
586
600
|
end
|
|
587
|
-
#
|
|
601
|
+
# Only the destination takes its default entry. Its ancestors already
|
|
602
|
+
# have an explicit path to the target or remembered history state.
|
|
588
603
|
target_id = seq.last
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
else
|
|
596
|
-
# If history (deep): remembered leaves were appended as part of seq; if shallow: enter_descendants handled per remembered child in seq
|
|
597
|
-
# For shallow we should expand each remembered child; this happens implicitly since seq contains those child ids
|
|
598
|
-
# and we will expand below by calling enter_descendants on each non-history id in the sequence
|
|
599
|
-
end
|
|
600
|
-
# Expand descendants for any non-history ids in the sequence (covers shallow history children)
|
|
601
|
-
seq.each do |sid|
|
|
602
|
-
next if @tag_type[sid] == :history
|
|
603
|
-
leaves, bundle = enter_descendants(sid)
|
|
604
|
-
entered.concat(bundle[:entered])
|
|
605
|
-
actions.concat(bundle[:actions])
|
|
606
|
-
delta.merge!(bundle[:delta])
|
|
607
|
-
entered_leaves.concat(leaves)
|
|
608
|
-
end
|
|
604
|
+
next unless target_id
|
|
605
|
+
leaves, bundle = enter_descendants(target_id)
|
|
606
|
+
entered.concat(bundle[:entered])
|
|
607
|
+
actions.concat(bundle[:actions])
|
|
608
|
+
delta.merge!(bundle[:delta])
|
|
609
|
+
entered_leaves.concat(leaves)
|
|
609
610
|
end
|
|
610
611
|
|
|
611
612
|
# Merge entered leaves into configuration
|
|
@@ -666,55 +667,28 @@ module Scjson
|
|
|
666
667
|
end
|
|
667
668
|
|
|
668
669
|
def composite?(node)
|
|
669
|
-
|
|
670
|
+
%w[state parallel final].any? { |key| !wrap_list(node[key]).empty? }
|
|
670
671
|
end
|
|
671
672
|
|
|
672
673
|
def initial_child_ids_for(node)
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
end
|
|
685
|
-
# state: choose initial children or first
|
|
686
|
-
if node.key?('state')
|
|
687
|
-
initial_tokens = wrap_list(node['initial_attribute']).map(&:to_s)
|
|
688
|
-
if !initial_tokens.empty?
|
|
689
|
-
return initial_tokens
|
|
690
|
-
end
|
|
691
|
-
if node.key?('initial') && node['initial'].is_a?(Hash)
|
|
692
|
-
return wrap_list(node['initial']['transition']).flat_map { |t| wrap_list(t['target']).map(&:to_s) }
|
|
693
|
-
end
|
|
694
|
-
first = wrap_list(node['state']).map { |s| s['id'] }.compact.map(&:to_s).first
|
|
695
|
-
return first ? [first] : []
|
|
696
|
-
end
|
|
697
|
-
[]
|
|
674
|
+
owner = node.equal?(@root) ? 'scxml' : @tag_type.fetch(node['id'], :state).to_s
|
|
675
|
+
children = ChildOrder.slots(owner, node).values.flatten(1).filter_map { |_, child| child['id'] }
|
|
676
|
+
return children if owner == 'parallel'
|
|
677
|
+
explicit = wrap_list(node['initial_attribute'])
|
|
678
|
+
return explicit unless explicit.empty?
|
|
679
|
+
wrap_list(node['initial']).each do |initial|
|
|
680
|
+
return [initial] if initial.is_a?(String)
|
|
681
|
+
targets = wrap_list(initial['transition']).flat_map { |t| wrap_list(t['target']) }
|
|
682
|
+
return targets unless targets.empty?
|
|
683
|
+
end
|
|
684
|
+
children.first(1)
|
|
698
685
|
end
|
|
699
686
|
|
|
700
687
|
def initial_leaves_for_id(state_id)
|
|
701
688
|
node = @states[state_id]
|
|
702
689
|
return [] unless node
|
|
703
690
|
return [state_id] unless composite?(node)
|
|
704
|
-
|
|
705
|
-
if node.key?('parallel')
|
|
706
|
-
wrap_list(node['parallel']).each do |p|
|
|
707
|
-
wrap_list(p['state']).each do |s|
|
|
708
|
-
sid = s['id']
|
|
709
|
-
leaves.concat(initial_leaves_for_id(sid.to_s)) if sid
|
|
710
|
-
end
|
|
711
|
-
end
|
|
712
|
-
return leaves
|
|
713
|
-
end
|
|
714
|
-
initial_child_ids_for(node).each do |cid|
|
|
715
|
-
leaves.concat(initial_leaves_for_id(cid))
|
|
716
|
-
end
|
|
717
|
-
leaves
|
|
691
|
+
initial_child_ids_for(node).flat_map { |cid| initial_leaves_for_id(cid) }
|
|
718
692
|
end
|
|
719
693
|
|
|
720
694
|
# ---- Invoke / Finalize ----
|
|
@@ -725,6 +699,7 @@ module Scjson
|
|
|
725
699
|
wrap_list(node['invoke']).each do |inv|
|
|
726
700
|
next unless inv.is_a?(Hash)
|
|
727
701
|
iid = (inv['id'] && inv['id'].to_s)
|
|
702
|
+
next if iid && @invocations_by_state[sid].include?(iid)
|
|
728
703
|
unless iid && !iid.empty?
|
|
729
704
|
@invoke_seq += 1
|
|
730
705
|
iid = "i#{@invoke_seq}"
|
|
@@ -1248,8 +1223,8 @@ module Scjson
|
|
|
1248
1223
|
# boolean literals
|
|
1249
1224
|
return true if s.downcase == 'true'
|
|
1250
1225
|
return false if s.downcase == 'false'
|
|
1251
|
-
#
|
|
1252
|
-
if (m = s.match(
|
|
1226
|
+
# The SCXML state-membership predicate is case-sensitive.
|
|
1227
|
+
if (m = s.match(/^In\((.+)\)$/))
|
|
1253
1228
|
sid_raw = m[1].strip
|
|
1254
1229
|
sid = sid_raw
|
|
1255
1230
|
if (mm = sid_raw.match(/^['\"](.*)['\"]$/))
|
|
@@ -1257,6 +1232,7 @@ module Scjson
|
|
|
1257
1232
|
end
|
|
1258
1233
|
return in_state?(sid.to_s)
|
|
1259
1234
|
end
|
|
1235
|
+
return nil if s.match?(/\Ain\s*\(/i)
|
|
1260
1236
|
# JSON object/array literals
|
|
1261
1237
|
if (s.start_with?('[') && s.end_with?(']')) || (s.start_with?('{') && s.end_with?('}'))
|
|
1262
1238
|
begin
|
|
@@ -1360,6 +1336,17 @@ module Scjson
|
|
|
1360
1336
|
end
|
|
1361
1337
|
|
|
1362
1338
|
def member?(item, container)
|
|
1339
|
+
if @root['datamodel_attribute'] == 'ecmascript'
|
|
1340
|
+
key = item.nil? ? 'null' : item.to_s
|
|
1341
|
+
key = item.to_i.to_s if item.is_a?(Float) && item.finite? && item == item.to_i
|
|
1342
|
+
return container.key?(key) || OBJECT_PROPERTIES.include?(key) if container.is_a?(Hash)
|
|
1343
|
+
if container.is_a?(Array)
|
|
1344
|
+
return true if ARRAY_PROPERTIES.include?(key) || OBJECT_PROPERTIES.include?(key)
|
|
1345
|
+
return key.match?(/\A(?:0|[1-9][0-9]{0,9})\z/) && key.to_i < container.length
|
|
1346
|
+
end
|
|
1347
|
+
@internal_queue << { 'name' => 'error.execution', 'data' => nil }
|
|
1348
|
+
return false
|
|
1349
|
+
end
|
|
1363
1350
|
return false if container.nil?
|
|
1364
1351
|
if container.is_a?(Array)
|
|
1365
1352
|
return container.include?(item)
|
|
@@ -1488,40 +1475,26 @@ module Scjson
|
|
|
1488
1475
|
|
|
1489
1476
|
# ---- Completion (done.state.*) ----
|
|
1490
1477
|
def enqueue_done_events
|
|
1491
|
-
|
|
1492
|
-
done_ids.each do |sid|
|
|
1478
|
+
new_state_completions.each do |sid|
|
|
1493
1479
|
@internal_queue << ({ 'name' => "done.state.#{sid}", 'data' => nil })
|
|
1494
1480
|
end
|
|
1495
1481
|
end
|
|
1496
1482
|
|
|
1483
|
+
def new_state_completions
|
|
1484
|
+
done_ids = compute_done_states
|
|
1485
|
+
new_ids = done_ids - (@completed_states || [])
|
|
1486
|
+
@completed_states = done_ids
|
|
1487
|
+
new_ids
|
|
1488
|
+
end
|
|
1489
|
+
|
|
1497
1490
|
def compute_done_states
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
next unless node.is_a?(Hash)
|
|
1504
|
-
# only consider composite states
|
|
1505
|
-
if node.key?('state') || node.key?('parallel')
|
|
1506
|
-
# direct final children ids
|
|
1507
|
-
direct_finals = wrap_list(node['final']).map { |f| f.is_a?(Hash) ? f['id'] : nil }.compact.map(&:to_s)
|
|
1508
|
-
if !direct_finals.empty? && (finals & direct_finals).any?
|
|
1509
|
-
done << sid
|
|
1510
|
-
next
|
|
1511
|
-
end
|
|
1512
|
-
# parallel: all regions have a final descendant
|
|
1513
|
-
if node.key?('parallel')
|
|
1514
|
-
region_ids = wrap_list(node['parallel']).flat_map { |p| wrap_list(p['state']).map { |s| s['id'] }.compact.map(&:to_s) }
|
|
1515
|
-
if !region_ids.empty?
|
|
1516
|
-
all_done = region_ids.all? do |rid|
|
|
1517
|
-
finals.any? { |fid| is_ancestor?(rid, fid) }
|
|
1518
|
-
end
|
|
1519
|
-
done << sid if all_done
|
|
1520
|
-
end
|
|
1521
|
-
end
|
|
1522
|
-
end
|
|
1491
|
+
done = @configuration.filter_map { |leaf| @parent[leaf] if @tag_type[leaf] == :final }.uniq
|
|
1492
|
+
parallels = @states.keys.select { |id| @tag_type[id] == :parallel }
|
|
1493
|
+
parallels.sort_by { |id| -ancestors(id).length }.each do |id|
|
|
1494
|
+
regions = initial_child_ids_for(@states.fetch(id))
|
|
1495
|
+
done << id if !regions.empty? && (regions - done).empty?
|
|
1523
1496
|
end
|
|
1524
|
-
done
|
|
1497
|
+
done
|
|
1525
1498
|
end
|
|
1526
1499
|
|
|
1527
1500
|
def index_states(node, parent_id = nil, tag = nil)
|
|
@@ -1550,12 +1523,7 @@ module Scjson
|
|
|
1550
1523
|
end
|
|
1551
1524
|
return leaves.uniq
|
|
1552
1525
|
end
|
|
1553
|
-
|
|
1554
|
-
first_state = wrap_list(@root['state']).find { |s| s.is_a?(Hash) && s['id'] }
|
|
1555
|
-
if first_state && first_state['id']
|
|
1556
|
-
return initial_leaves_for_token(first_state['id'].to_s)
|
|
1557
|
-
end
|
|
1558
|
-
[]
|
|
1526
|
+
initial_child_ids_for(@root).flat_map { |id| initial_leaves_for_token(id) }
|
|
1559
1527
|
end
|
|
1560
1528
|
|
|
1561
1529
|
def initial_leaves_for_token(token)
|
data/lib/scjson/engine.rb
CHANGED
|
@@ -64,6 +64,12 @@ module Scjson
|
|
|
64
64
|
# ignore if not supported
|
|
65
65
|
end
|
|
66
66
|
leaves = leaf_only ? ctx.leaf_state_ids : nil
|
|
67
|
+
write_frame = lambda do |step, frame|
|
|
68
|
+
if ordering == 'scion'
|
|
69
|
+
%w[configuration enteredStates exitedStates].each { |key| frame[key] = frame.fetch(key).uniq.sort }
|
|
70
|
+
end
|
|
71
|
+
sink.write(JSON.generate({ step: step }.merge(frame)) + "\n")
|
|
72
|
+
end
|
|
67
73
|
# Step 0 snapshot
|
|
68
74
|
init = ctx.trace_init
|
|
69
75
|
if leaf_only && leaves
|
|
@@ -82,7 +88,7 @@ module Scjson
|
|
|
82
88
|
init['enteredStates'] = []
|
|
83
89
|
init['exitedStates'] = []
|
|
84
90
|
end
|
|
85
|
-
|
|
91
|
+
write_frame.call(0, init)
|
|
86
92
|
|
|
87
93
|
# Stream of events: from file or STDIN
|
|
88
94
|
stream = events_path ? File.open(events_path, 'r', encoding: 'utf-8') : $stdin
|
|
@@ -137,7 +143,7 @@ module Scjson
|
|
|
137
143
|
end
|
|
138
144
|
end
|
|
139
145
|
rec['firedTransitions'] = [] if omit_transitions
|
|
140
|
-
|
|
146
|
+
write_frame.call(step_no, rec)
|
|
141
147
|
step_no += 1
|
|
142
148
|
rescue StandardError
|
|
143
149
|
# ignore malformed
|
|
@@ -176,7 +182,7 @@ module Scjson
|
|
|
176
182
|
end
|
|
177
183
|
end
|
|
178
184
|
rec['firedTransitions'] = [] if omit_transitions
|
|
179
|
-
|
|
185
|
+
write_frame.call(step_no, rec)
|
|
180
186
|
step_no += 1
|
|
181
187
|
end
|
|
182
188
|
ensure
|