activefacts 0.8.16 → 0.8.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +15 -0
  2. data/Manifest.txt +10 -4
  3. data/bin/afgen +26 -20
  4. data/bin/cql +1 -1
  5. data/css/orm2.css +89 -9
  6. data/examples/CQL/CompanyDirectorEmployee.cql +4 -4
  7. data/examples/CQL/Genealogy.cql +5 -5
  8. data/examples/CQL/Metamodel.cql +121 -91
  9. data/examples/CQL/MonthInSeason.cql +2 -6
  10. data/examples/CQL/SeparateSubtype.cql +11 -9
  11. data/examples/CQL/ServiceDirector.cql +21 -33
  12. data/examples/CQL/Supervision.cql +0 -3
  13. data/examples/CQL/WindowInRoomInBldg.cql +10 -4
  14. data/examples/CQL/unit.cql +1 -1
  15. data/lib/activefacts.rb +1 -0
  16. data/lib/activefacts/cql/CQLParser.treetop +5 -1
  17. data/lib/activefacts/cql/Context.treetop +2 -7
  18. data/lib/activefacts/cql/Expressions.treetop +2 -2
  19. data/lib/activefacts/cql/FactTypes.treetop +37 -31
  20. data/lib/activefacts/cql/Language/English.treetop +21 -4
  21. data/lib/activefacts/cql/LexicalRules.treetop +59 -1
  22. data/lib/activefacts/cql/ObjectTypes.treetop +22 -12
  23. data/lib/activefacts/cql/Terms.treetop +13 -9
  24. data/lib/activefacts/cql/ValueTypes.treetop +30 -11
  25. data/lib/activefacts/cql/compiler.rb +34 -5
  26. data/lib/activefacts/cql/compiler/clause.rb +207 -116
  27. data/lib/activefacts/cql/compiler/constraint.rb +129 -105
  28. data/lib/activefacts/cql/compiler/entity_type.rb +49 -27
  29. data/lib/activefacts/cql/compiler/expression.rb +71 -42
  30. data/lib/activefacts/cql/compiler/fact.rb +70 -64
  31. data/lib/activefacts/cql/compiler/fact_type.rb +108 -57
  32. data/lib/activefacts/cql/compiler/query.rb +178 -0
  33. data/lib/activefacts/cql/compiler/shared.rb +13 -12
  34. data/lib/activefacts/cql/compiler/value_type.rb +10 -4
  35. data/lib/activefacts/cql/nodes.rb +1 -1
  36. data/lib/activefacts/cql/parser.rb +6 -2
  37. data/lib/activefacts/generate/absorption.rb +6 -3
  38. data/lib/activefacts/generate/cql.rb +140 -84
  39. data/lib/activefacts/generate/dm.rb +12 -6
  40. data/lib/activefacts/generate/help.rb +25 -6
  41. data/lib/activefacts/generate/helpers/oo.rb +195 -0
  42. data/lib/activefacts/generate/helpers/ordered.rb +589 -0
  43. data/lib/activefacts/generate/helpers/rails.rb +57 -0
  44. data/lib/activefacts/generate/html/glossary.rb +274 -54
  45. data/lib/activefacts/generate/json.rb +25 -22
  46. data/lib/activefacts/generate/null.rb +1 -0
  47. data/lib/activefacts/generate/rails/models.rb +244 -0
  48. data/lib/activefacts/generate/rails/schema.rb +185 -0
  49. data/lib/activefacts/generate/records.rb +1 -0
  50. data/lib/activefacts/generate/ruby.rb +51 -30
  51. data/lib/activefacts/generate/sql/mysql.rb +5 -3
  52. data/lib/activefacts/generate/sql/server.rb +8 -4
  53. data/lib/activefacts/generate/text.rb +1 -0
  54. data/lib/activefacts/generate/transform/surrogate.rb +209 -0
  55. data/lib/activefacts/generate/version.rb +1 -0
  56. data/lib/activefacts/input/orm.rb +234 -181
  57. data/lib/activefacts/mapping/rails.rb +122 -0
  58. data/lib/activefacts/persistence/columns.rb +34 -18
  59. data/lib/activefacts/persistence/foreignkey.rb +129 -71
  60. data/lib/activefacts/persistence/index.rb +42 -12
  61. data/lib/activefacts/persistence/reference.rb +37 -23
  62. data/lib/activefacts/persistence/tables.rb +53 -19
  63. data/lib/activefacts/registry.rb +11 -0
  64. data/lib/activefacts/support.rb +28 -10
  65. data/lib/activefacts/version.rb +1 -1
  66. data/lib/activefacts/vocabulary/extensions.rb +246 -117
  67. data/lib/activefacts/vocabulary/metamodel.rb +105 -65
  68. data/lib/activefacts/vocabulary/verbaliser.rb +226 -194
  69. data/spec/absorption_spec.rb +1 -0
  70. data/spec/cql/comparison_spec.rb +8 -8
  71. data/spec/cql/contractions_spec.rb +16 -43
  72. data/spec/cql/entity_type_spec.rb +2 -1
  73. data/spec/cql/expressions_spec.rb +2 -2
  74. data/spec/cql/fact_type_matching_spec.rb +4 -1
  75. data/spec/cql/parser/bad_literals_spec.rb +30 -30
  76. data/spec/cql/parser/entity_types_spec.rb +6 -6
  77. data/spec/cql/parser/expressions_spec.rb +25 -19
  78. data/spec/cql/samples_spec.rb +5 -4
  79. data/spec/cql_cql_spec.rb +2 -1
  80. data/spec/cql_dm_spec.rb +4 -0
  81. data/spec/cql_mysql_spec.rb +4 -0
  82. data/spec/cql_parse_spec.rb +2 -0
  83. data/spec/cql_ruby_spec.rb +4 -0
  84. data/spec/cql_sql_spec.rb +4 -0
  85. data/spec/cqldump_spec.rb +7 -4
  86. data/spec/helpers/parse_to_ast_matcher.rb +7 -3
  87. data/spec/helpers/test_parser.rb +2 -0
  88. data/spec/norma_cql_spec.rb +5 -2
  89. data/spec/norma_ruby_spec.rb +4 -1
  90. data/spec/norma_ruby_sql_spec.rb +4 -1
  91. data/spec/norma_sql_spec.rb +4 -1
  92. data/spec/norma_tables_spec.rb +2 -2
  93. data/spec/ruby_api_spec.rb +1 -1
  94. data/spec/spec_helper.rb +2 -0
  95. data/spec/transform_surrogate_spec.rb +59 -0
  96. metadata +70 -60
  97. data/TODO +0 -308
  98. data/lib/activefacts/cql/compiler/join.rb +0 -162
  99. data/lib/activefacts/generate/oo.rb +0 -176
  100. data/lib/activefacts/generate/ordered.rb +0 -602
@@ -11,11 +11,11 @@
11
11
  #
12
12
  require 'activefacts/vocabulary'
13
13
  require 'activefacts/persistence'
14
- require 'activefacts/generate/oo'
14
+ require 'activefacts/generate/helpers/oo'
15
15
 
16
16
  module ActiveFacts
17
17
  module Generate
18
- class DM < OO #:nodoc:
18
+ class DM < Helpers::OO #:nodoc:
19
19
  # Generate SQL for DataMapper for an ActiveFacts vocabulary.
20
20
  # Invoke as
21
21
  # afgen --dm[=options] <file>.cql
@@ -61,7 +61,11 @@ module ActiveFacts
61
61
 
62
62
  def key_fields(ref, reverse = false)
63
63
  # Compute and return child_key and parent_key if necessary
64
- fk = ref.from.foreign_keys.detect{|k| k.reference == ref}
64
+ fk = ref.from.foreign_keys.detect{|k| k.jump_reference == ref}
65
+ unless fk
66
+ debugger
67
+ raise "foreign key not found for #{ref}"
68
+ end
65
69
  child_key = fk.from_columns.map{|c| column_name(c)}
66
70
  parent_key = fk.to_columns.map{|c| column_name(c)}
67
71
  if child_key != parent_key
@@ -130,7 +134,7 @@ module ActiveFacts
130
134
  puts " include DataMapper::Resource\n\n" unless supertype
131
135
 
132
136
  columns = o.columns
133
- o.references_from.each do |ref|
137
+ o.references_from.sort_by{|r| r.to_s}.each do |ref|
134
138
  # A (set of) columns
135
139
  if !columns
136
140
  # absorbed subtypes didn't have columns populated
@@ -141,7 +145,7 @@ module ActiveFacts
141
145
  # debugger if ref_columns.detect{|c| [:subtype, :supertype].include?(c.references[0].role_type)}
142
146
  ref_columns = columns.select{|c| c.references[0] == ref }
143
147
  # puts " \# #{ref.reading}:"
144
- ref_columns.each do |column|
148
+ ref_columns.sort_by{|column| column_name(column)}.each do |column|
145
149
  type, params, constraints = column.type
146
150
  length = params[:length]
147
151
  length &&= length.to_i
@@ -191,7 +195,7 @@ module ActiveFacts
191
195
 
192
196
  # Emit the "has n," associations
193
197
  # REVISIT: Need to use ActiveSupport to pluralise these names, or disable inflexion somehow.
194
- o.references_to.each do |ref|
198
+ o.references_to.sort_by{|r| [r.to_s, (r.from_role||r.to_role).ordinal] }.each do |ref|
195
199
  next unless is_model[ref.from]
196
200
  constraint = ''
197
201
  association_type =
@@ -271,3 +275,5 @@ module ActiveFacts
271
275
  end
272
276
  end
273
277
  end
278
+
279
+ ActiveFacts::Registry.generator('dm', ActiveFacts::Generate::DM)
@@ -14,16 +14,34 @@ module ActiveFacts
14
14
  class HELP
15
15
  private
16
16
  def initialize(vocabulary, *options)
17
+ generators = $:.
18
+ map{|path|
19
+ Dir[path+"/activefacts/generate/**/*.rb"].
20
+ reject{|p|
21
+ p =~ %r{/(transform|helpers)/}
22
+ }.
23
+ map{|p|
24
+ p.sub(%r{.*/activefacts/generate/}, '').sub(/\.rb/,'')
25
+ }
26
+ }
27
+ transformers = $:.
28
+ map{|path|
29
+ Dir[path+"/activefacts/generate/transform/**/*.rb"].
30
+ map{|p|
31
+ p.sub(%r{.*/activefacts/generate/}, '').sub(/\.rb/,'')
32
+ }
33
+ }
34
+
17
35
  puts %Q{
18
- Usage: afgen --generator[=options] file.inp[=options]
36
+ Usage: afgen [ --transformer[=options] ... ] [ --generator[=options] ... ] file.inp[=options]
19
37
  options are comma-separated lists. Use =help to get more information.
20
38
 
21
39
  Available generators are:
22
- #{$:.map{|path|
23
- Dir[path+"/activefacts/generate/**.rb"].map{|p|
24
- p.sub(%r{.*/}, '').sub(/\.rb/,'')
25
- }
26
- }.flatten.uniq.sort.join("\n\t")
40
+ #{generators.flatten.uniq.sort.join("\n\t")
41
+ }
42
+
43
+ Available transformers are:
44
+ #{transformers.flatten.uniq.sort.join("\n\t")
27
45
  }
28
46
 
29
47
  inp is the name of a file input handler. Available input handlers are:
@@ -43,3 +61,4 @@ inp is the name of a file input handler. Available input handlers are:
43
61
  end
44
62
  end
45
63
 
64
+ ActiveFacts::Registry.generator('help', ActiveFacts::Generate::HELP)
@@ -0,0 +1,195 @@
1
+ #
2
+ # ActiveFacts Generators.
3
+ # Base class for generators of class libraries in any object-oriented language that supports the ActiveFacts API.
4
+ #
5
+ # Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
6
+ #
7
+ require 'activefacts/vocabulary'
8
+ require 'activefacts/generate/helpers/ordered'
9
+
10
+ module ActiveFacts
11
+ module Generate
12
+ module Helpers
13
+ # Base class for generators of object-oriented class libraries for an ActiveFacts vocabulary.
14
+ class OO < OrderedDumper #:nodoc:
15
+ def constraints_dump(constraints_used)
16
+ # Stub, not needed.
17
+ end
18
+
19
+ def value_type_banner
20
+ end
21
+
22
+ def value_type_end
23
+ end
24
+
25
+ def entity_type_dump(o)
26
+ @object_types_dumped[o] = true
27
+ pi = o.preferred_identifier
28
+
29
+ supers = o.supertypes
30
+ if (supers.size > 0)
31
+ # Ignore identification by a supertype:
32
+ pi = nil if pi && pi.role_sequence.all_role_ref.detect{|rr| rr.role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) }
33
+ subtype_dump(o, supers, pi)
34
+ else
35
+ non_subtype_dump(o, pi)
36
+ end
37
+ @constraints_used[pi] = true
38
+ end
39
+
40
+ # Dump the roles for an object type (excluding the roles of a fact type which is objectified)
41
+ def roles_dump(o)
42
+ o.all_role.
43
+ select{|role|
44
+ role.fact_type.all_role.size <= 2 &&
45
+ !role.fact_type.is_a?(ActiveFacts::Metamodel::LinkFactType)
46
+ }.
47
+ sort_by{|role|
48
+ preferred_role_name(role.fact_type.all_role.select{|r2| r2 != role}[0] || role, o)
49
+ }.each{|role|
50
+ role_dump(role)
51
+ }
52
+ end
53
+
54
+ def role_dump(role)
55
+ return if role.fact_type.entity_type
56
+
57
+ fact_type = role.fact_type
58
+ if fact_type.all_role.size == 1
59
+ unary_dump(role, preferred_role_name(role))
60
+ return
61
+ elsif fact_type.all_role.size != 2
62
+ # Shouldn't come here, except perhaps for an invalid model
63
+ return # ternaries and higher are always objectified
64
+ end
65
+
66
+ # REVISIT: TypeInheritance
67
+ if fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
68
+ # debug "Ignoring role #{role} in #{fact_type}, subtype fact type"
69
+ # REVISIT: What about secondary subtypes?
70
+ # REVISIT: What about dumping the relational mapping when using separate tables?
71
+ return
72
+ end
73
+
74
+ return unless role.is_functional
75
+
76
+ other_role = fact_type.all_role.select{|r| r != role}[0]
77
+ other_role_name = preferred_role_name(other_role)
78
+ other_player = other_role.object_type
79
+
80
+ # It's a one_to_one if there's a uniqueness constraint on the other role:
81
+ one_to_one = other_role.is_functional
82
+ return if one_to_one &&
83
+ !@object_types_dumped[other_role.object_type]
84
+
85
+ # Find role name:
86
+ role_method = preferred_role_name(role)
87
+ other_role_method = one_to_one ? role_method : "all_"+role_method
88
+ # puts "---"+role.role_name if role.role_name
89
+ if other_role_name != other_player.name.gsub(/ /,'_').snakecase and
90
+ role_method == role.object_type.name.gsub(/ /,'_').snakecase
91
+ other_role_method += "_as_#{other_role_name}"
92
+ end
93
+
94
+ role_name = role_method
95
+ role_name = nil if role_name == role.object_type.name.gsub(/ /,'_').snakecase
96
+
97
+ binary_dump(role, other_role_name, other_player, role.is_mandatory, one_to_one, nil, role_name, other_role_method)
98
+ end
99
+
100
+ def preferred_role_name(role, is_for = nil)
101
+ return "" if role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
102
+
103
+ if is_for && role.fact_type.entity_type == is_for && role.fact_type.all_role.size == 1
104
+ return role.object_type.name.gsub(/[- ]/,'_').snakecase
105
+ end
106
+
107
+ # debug "Looking for preferred_role_name of #{describe_fact_type(role.fact_type, role)}"
108
+ reading = role.fact_type.preferred_reading
109
+ preferred_role_ref = reading.role_sequence.all_role_ref.detect{|reading_rr|
110
+ reading_rr.role == role
111
+ }
112
+
113
+ # Unaries are a hack, with only one role for what is effectively a binary:
114
+ if (role.fact_type.all_role.size == 1)
115
+ return (role.role_name && role.role_name.snakecase) ||
116
+ reading.text.gsub(/ *\{0\} */,'').gsub(/[- ]/,'_').downcase
117
+ end
118
+
119
+ # debug "\tleading_adjective=#{(p=preferred_role_ref).leading_adjective}, role_name=#{role.role_name}, role player=#{role.object_type.name}, trailing_adjective=#{p.trailing_adjective}"
120
+ role_words = []
121
+ role_name = role.role_name
122
+ role_name = nil if role_name == ""
123
+
124
+ # REVISIT: Consider whether NOT to use the adjective if it's a prefix of the role_name
125
+ la = preferred_role_ref.leading_adjective
126
+ role_words << la.gsub(/[- ]/,'_') if la && la != "" and !role.role_name
127
+
128
+ role_words << (role_name || role.object_type.name.gsub(/[- ]/,'_'))
129
+ # REVISIT: Same when trailing_adjective is a suffix of the role_name
130
+ ta = preferred_role_ref.trailing_adjective
131
+ role_words << ta.gsub(/[- ]/,'_') if ta && ta != "" and !role_name
132
+ n = role_words.map{|w| w.gsub(/([a-z])([A-Z]+)/,'\1_\2').downcase}*"_"
133
+ # debug "\tresult=#{n}"
134
+ n.gsub(' ','_')
135
+ end
136
+
137
+ def skip_fact_type(f)
138
+ # REVISIT: There might be constraints we have to merge into the nested entity or subtype. These will come up as un-handled constraints.
139
+ !f.entity_type ||
140
+ f.is_a?(ActiveFacts::Metamodel::TypeInheritance)
141
+ end
142
+
143
+ # An objectified fact type has internal roles that are always "has_one":
144
+ def fact_roles_dump(fact_type)
145
+ fact_type.all_role.sort_by{|role|
146
+ preferred_role_name(role, fact_type.entity_type)
147
+ }.each{|role|
148
+ role_name = preferred_role_name(role, fact_type.entity_type)
149
+ one_to_one = role.all_role_ref.detect{|rr|
150
+ rr.role_sequence.all_role_ref.size == 1 &&
151
+ rr.role_sequence.all_presence_constraint.detect{|pc|
152
+ pc.max_frequency == 1
153
+ }
154
+ }
155
+ as = role_name != role.object_type.name.gsub(/ /,'_').snakecase ? "_as_#{role_name}" : ""
156
+ raise "Fact #{fact_type.describe} type is not objectified" unless fact_type.entity_type
157
+ other_role_method = (one_to_one ? "" : "all_") +
158
+ fact_type.entity_type.name.gsub(/ /,'_').snakecase +
159
+ as
160
+ binary_dump(role, role_name, role.object_type, true, one_to_one, nil, nil, other_role_method)
161
+ }
162
+ end
163
+
164
+ def entity_type_banner
165
+ end
166
+
167
+ def entity_type_group_end
168
+ end
169
+
170
+ def append_ring_to_reading(reading, ring)
171
+ # REVISIT: debug "Should override append_ring_to_reading"
172
+ end
173
+
174
+ def fact_type_banner
175
+ end
176
+
177
+ def fact_type_end
178
+ end
179
+
180
+ def constraint_banner
181
+ # debug "Should override constraint_banner"
182
+ end
183
+
184
+ def constraint_end
185
+ # debug "Should override constraint_end"
186
+ end
187
+
188
+ def constraint_dump(c)
189
+ # debug "Should override constraint_dump"
190
+ end
191
+
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,589 @@
1
+ #
2
+ # ActiveFacts Generators.
3
+ # Generation support superclass that sequences entity types to avoid forward references.
4
+ #
5
+ # Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
6
+ #
7
+ require 'activefacts/api'
8
+
9
+ module ActiveFacts
10
+ module Generate #:nodoc:
11
+ module Helpers #:nodoc:
12
+ class OrderedDumper #:nodoc:
13
+ # Base class for generators of object-oriented class libraries for an ActiveFacts vocabulary.
14
+ def initialize(vocabulary, *options)
15
+ @vocabulary = vocabulary
16
+ @vocabulary = @vocabulary.Vocabulary.values[0] if ActiveFacts::API::Constellation === @vocabulary
17
+ options.each{|option| set_option(option) }
18
+ end
19
+
20
+ def set_option(option)
21
+ end
22
+
23
+ def puts(*a)
24
+ @out.puts *a
25
+ end
26
+
27
+ def print(*a)
28
+ @out.print *a
29
+ end
30
+
31
+ def generate(out = $>)
32
+ @out = out
33
+ vocabulary_start(@vocabulary)
34
+
35
+ build_indices
36
+ @object_types_dumped = {}
37
+ @fact_types_dumped = {}
38
+ units_dump()
39
+ value_types_dump()
40
+ entity_types_dump()
41
+ fact_types_dump()
42
+ constraints_dump(@constraints_used)
43
+ vocabulary_end
44
+ end
45
+
46
+ def build_indices
47
+ @presence_constraints_by_fact = Hash.new{ |h, k| h[k] = [] }
48
+ @ring_constraints_by_fact = Hash.new{ |h, k| h[k] = [] }
49
+
50
+ @vocabulary.all_constraint.each { |c|
51
+ case c
52
+ when ActiveFacts::Metamodel::PresenceConstraint
53
+ fact_types = c.role_sequence.all_role_ref.map{|rr| rr.role.fact_type}.uniq # All fact types spanned by this constraint
54
+ if fact_types.size == 1 # There's only one, save it:
55
+ # debug "Single-fact constraint on #{fact_types[0].guid}: #{c.name}"
56
+ (@presence_constraints_by_fact[fact_types[0]] ||= []) << c
57
+ end
58
+ when ActiveFacts::Metamodel::RingConstraint
59
+ (@ring_constraints_by_fact[c.role.fact_type] ||= []) << c
60
+ else
61
+ # debug "Found unhandled constraint #{c.class} #{c.name}"
62
+ end
63
+ }
64
+ @constraints_used = {}
65
+ end
66
+
67
+ def units_dump
68
+ done_banner = false
69
+ units = @vocabulary.all_unit.to_a.sort_by{|u| u.name.gsub(/ /,'')}
70
+ while units.size > 0
71
+ i = 0
72
+ while i < units.size
73
+ unit = units[i]
74
+ i += 1
75
+
76
+ # Skip this one if the precursors haven't yet been dumped:
77
+ next if unit.all_derivation_as_derived_unit.detect{|d| units.include?(d.base_unit) }
78
+
79
+ # Even if we skip, we're done with this unit
80
+ units.delete(unit)
81
+ i -= 1
82
+
83
+ # Skip value-type derived units
84
+ next if unit.name =~ /\^/
85
+
86
+ if !done_banner
87
+ done_banner = true
88
+ units_banner
89
+ end
90
+ unit_dump(unit)
91
+ end
92
+ end
93
+ units_end if done_banner
94
+ end
95
+
96
+ def value_types_dump
97
+ done_banner = false
98
+ @value_type_dumped = {}
99
+ @vocabulary.all_object_type.sort_by{|o| o.name.gsub(/ /,'')}.each{|o|
100
+ next unless o.is_a?(ActiveFacts::Metamodel::ValueType)
101
+
102
+ value_type_banner unless done_banner
103
+ done_banner = true
104
+
105
+ value_type_chain_dump(o)
106
+ @object_types_dumped[o] = true
107
+ }
108
+ value_type_end if done_banner
109
+ end
110
+
111
+ # Ensure that supertype gets dumped first
112
+ def value_type_chain_dump(o)
113
+ return if @value_type_dumped[o]
114
+ value_type_chain_dump(o.supertype) if (o.supertype && !@value_type_dumped[o.supertype])
115
+ value_type_dump(o) if o.name != "_ImplicitBooleanValueType"
116
+ @value_type_dumped[o] = true
117
+ end
118
+
119
+ # Try to dump entity types in order of name, but we need
120
+ # to dump ETs before they're referenced in preferred ids
121
+ # if possible (it's not always, there may be loops!)
122
+ def entity_types_dump
123
+ # Build hash tables of precursors and followers to use:
124
+ @precursors, @followers = *build_entity_dependencies
125
+
126
+ done_banner = false
127
+ sorted = @vocabulary.all_object_type.select{|o|
128
+ o.is_a?(ActiveFacts::Metamodel::EntityType) # and !o.fact_type
129
+ }.sort_by{|o| o.name.gsub(/ /,'')}
130
+ panic = nil
131
+ while true do
132
+ count_this_pass = 0
133
+ skipped_this_pass = 0
134
+ sorted.each{|o|
135
+ next if @object_types_dumped[o] # Already done
136
+
137
+ # Can we do this yet?
138
+ if (o != panic and # We don't *have* to do it (panic mode)
139
+ (p = @precursors[o]) and # There might be...
140
+ p.size > 0) # precursors - still blocked
141
+ skipped_this_pass += 1
142
+ next
143
+ end
144
+
145
+ entity_type_banner unless done_banner
146
+ done_banner = true
147
+
148
+ # We're going to emit o - remove it from precursors of others:
149
+ (@followers[o]||[]).each{|f|
150
+ @precursors[f] -= [o]
151
+ }
152
+ count_this_pass += 1
153
+ panic = nil
154
+
155
+ if (o.fact_type)
156
+ fact_type_dump_with_dependents(o.fact_type)
157
+ released_fact_types_dump(o)
158
+ else
159
+ entity_type_dump(o)
160
+ released_fact_types_dump(o)
161
+ end
162
+
163
+ entity_type_group_end
164
+ }
165
+
166
+ # Check that we made progress if there's any to make:
167
+ if count_this_pass == 0 && skipped_this_pass > 0
168
+ if panic # We were already panicing... what to do now?
169
+ # This won't happen again unless the above code is changed to decide it can't dump "panic".
170
+ raise "Unresolvable cycle of forward references: " +
171
+ (bad = sorted.select{|o| EntityType === o && !@object_types_dumped[o]}).map{|o| o.name }.inspect +
172
+ ":\n\t" + bad.map{|o|
173
+ o.name +
174
+ ": " +
175
+ @precursors[o].map{|p| p.name}.uniq.inspect
176
+ } * "\n\t" + "\n"
177
+ else
178
+ # Find the object that has the most followers and no fwd-ref'd supertypes:
179
+ # This selection might be better if we allow PI roles to be fwd-ref'd...
180
+ panic = sorted.
181
+ select{|o| !@object_types_dumped[o] }.
182
+ sort_by{|o|
183
+ f = @followers[o] || [];
184
+ o.supertypes.detect{|s| !@object_types_dumped[s] } ? 0 : -f.size
185
+ }[0]
186
+ # debug "Panic mode, selected #{panic.name} next"
187
+ end
188
+ end
189
+
190
+ break if skipped_this_pass == 0 # All done.
191
+
192
+ end
193
+ end
194
+
195
+ def identified_by(o, pi)
196
+ # Different adjectives might be used for different readings.
197
+ # Here, we must find the role_ref containing the adjectives that we need for each identifier,
198
+ # which will be attached to the uniqueness constraint on this object in the binary FT that
199
+ # attaches that identifying role.
200
+ identifying_role_refs = pi.role_sequence.all_role_ref.sort_by{|role_ref| role_ref.ordinal}
201
+
202
+ # We need to get the adjectives for the roles from the identifying fact's preferred readings:
203
+ identifying_facts = ([o.fact_type]+identifying_role_refs.map{|rr| rr.role.fact_type }).compact.uniq
204
+
205
+ identification = identified_by_roles_and_facts(o, identifying_role_refs, identifying_facts)
206
+
207
+ identification
208
+ end
209
+
210
+ def describe_fact_type(fact_type, highlight = nil)
211
+ (fact_type.entity_type ? fact_type.entity_type.name : "")+
212
+ describe_roles(fact_type.all_role, highlight)
213
+ end
214
+
215
+ def describe_roles(roles, highlight = nil)
216
+ "("+
217
+ roles.map{|role| role.object_type.name + (role == highlight ? "*" : "")}*", "+
218
+ ")"
219
+ end
220
+
221
+ def describe_role_sequence(role_sequence)
222
+ "("+
223
+ role_sequence.all_role_ref.map{|role_ref| role_ref.role.object_type.name }*", "+
224
+ ")"
225
+ end
226
+
227
+ # This returns an array of two hash tables each keyed by an EntityType.
228
+ # The values of each hash entry are the precursors and followers (respectively) of that entity.
229
+ def build_entity_dependencies
230
+ @vocabulary.all_object_type.inject([{},{}]) { |a, o|
231
+ if o.is_a?(ActiveFacts::Metamodel::EntityType)
232
+ precursor = a[0]
233
+ follower = a[1]
234
+ blocked = false
235
+ pi = o.preferred_identifier
236
+ if pi
237
+ pi.role_sequence.all_role_ref.each{|rr|
238
+ role = rr.role
239
+ player = role.object_type
240
+ # REVISIT: If we decide to emit value types on demand, need to remove this:
241
+ next unless player.is_a?(ActiveFacts::Metamodel::EntityType)
242
+ # player is a precursor of o
243
+ (precursor[o] ||= []) << player if (player != o)
244
+ (follower[player] ||= []) << o if (player != o)
245
+ }
246
+ end
247
+ if o.fact_type
248
+ o.fact_type.all_role.each do |role|
249
+ next unless role.object_type.is_a?(ActiveFacts::Metamodel::EntityType)
250
+ (precursor[o] ||= []) << role.object_type
251
+ (follower[role.object_type] ||= []) << o
252
+ end
253
+ end
254
+
255
+ # Supertypes are precursors too:
256
+ subtyping = o.all_type_inheritance_as_supertype
257
+ next a if subtyping.size == 0
258
+ subtyping.each{|ti|
259
+ # debug ti.class.roles.verbalise; debug "all_type_inheritance_as_supertype"; exit
260
+ s = ti.subtype
261
+ (precursor[s] ||= []) << o
262
+ (follower[o] ||= []) << s
263
+ }
264
+ # REVISIT: Need to use this to order ValueTypes after their supertypes
265
+ # else
266
+ # o.all_value_type_as_supertype.each { |s|
267
+ # (precursor[s] ||= []) << o
268
+ # (follower[o] ||= []) << s
269
+ # }
270
+ end
271
+ a
272
+ }
273
+ end
274
+
275
+ # Dump all fact types for which all precursors (of which "o" is one) have been emitted:
276
+ def released_fact_types_dump(o)
277
+ roles = o.all_role
278
+ begin
279
+ progress = false
280
+ roles.map(&:fact_type).uniq.select{|fact_type|
281
+ # The fact type hasn't already been dumped but all its role players have
282
+ !@fact_types_dumped[fact_type] &&
283
+ !fact_type.is_a?(ActiveFacts::Metamodel::LinkFactType) &&
284
+ !fact_type.all_role.detect{|r| !@object_types_dumped[r.object_type] } &&
285
+ !fact_type.entity_type &&
286
+ derivation_precursors_complete(fact_type)
287
+ # REVISIT: A derived fact type must not be dumped before its dependent fact types have
288
+ }.sort_by{|fact_type|
289
+ fact_type_key(fact_type)
290
+ }.each{|fact_type|
291
+ fact_type_dump_with_dependents(fact_type)
292
+ # Objectified Fact Types may release additional fact types
293
+ roles += fact_type.entity_type.all_role.sort_by{|role| role.ordinal} if fact_type.entity_type
294
+ progress = true
295
+ }
296
+ end while progress
297
+ end
298
+
299
+ def derivation_precursors_complete(fact_type)
300
+ pr = fact_type.preferred_reading
301
+ return true unless jr = pr.role_sequence.all_role_ref.to_a[0].play
302
+ query = jr.variable.query
303
+ return false if query.all_step.detect{|js| !@fact_types_dumped[js.fact_type] }
304
+ return false if query.all_variable.detect{|jn| !@object_types_dumped[jn.object_type] }
305
+ true
306
+ end
307
+
308
+ def skip_fact_type(f)
309
+ # REVISIT: There might be constraints we have to merge into the nested entity or subtype.
310
+ # These will come up as un-handled constraints:
311
+ pcs = @presence_constraints_by_fact[f]
312
+ return true if f.is_a?(ActiveFacts::Metamodel::TypeInheritance)
313
+ return false if f.entity_type && !@object_types_dumped[f.entity_type]
314
+ pcs && pcs.size > 0 && !pcs.detect{|c| !@constraints_used[c] }
315
+ end
316
+
317
+ # Dump one fact type.
318
+ # Include as many as possible internal constraints in the fact type readings.
319
+ def fact_type_dump_with_dependents(fact_type)
320
+ @fact_types_dumped[fact_type] = true
321
+ # debug "Trying to dump FT again" if @fact_types_dumped[fact_type]
322
+ return if skip_fact_type(fact_type)
323
+
324
+ if (et = fact_type.entity_type) &&
325
+ (pi = et.preferred_identifier) &&
326
+ pi.role_sequence.all_role_ref.detect{|rr| rr.role.fact_type != fact_type }
327
+ # debug "Dumping objectified FT #{et.name} as an entity, non-fact PI"
328
+ entity_type_dump(et)
329
+ released_fact_types_dump(et)
330
+ return
331
+ end
332
+
333
+ fact_constraints = @presence_constraints_by_fact[fact_type]
334
+
335
+ # debug "for fact type #{fact_type.to_s}, considering\n\t#{fact_constraints.map(&:to_s)*",\n\t"}"
336
+ # debug "#{fact_type.name} has readings:\n\t#{fact_type.readings.map(&:name)*"\n\t"}"
337
+ # debug "Dumping #{fact_type.guid} as a fact type"
338
+
339
+ # Fact types that aren't nested have no names
340
+ name = fact_type.entity_type && fact_type.entity_type.name
341
+
342
+ fact_type_dump(fact_type, name)
343
+
344
+ # REVISIT: Go through the residual constraints and re-process appropriate readings to show them
345
+
346
+ @fact_types_dumped[fact_type] = true
347
+ @object_types_dumped[fact_type.entity_type] = true if fact_type.entity_type
348
+ end
349
+
350
+ # Dump fact types.
351
+ def fact_types_dump
352
+ # REVISIT: Uniqueness on the LHS of a binary can be coded using "distinct"
353
+
354
+ # The only fact types that can be remaining are those involving only value types,
355
+ # since we dumped every fact type as soon as all relevant entities were dumped.
356
+ # Iterate over all fact types of all value types, looking for these strays.
357
+
358
+ done_banner = false
359
+ fact_collection = @vocabulary.constellation.FactType
360
+ fact_collection.keys.select{|fact_id|
361
+ fact_type = fact_collection[fact_id] and
362
+ !fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) and
363
+ !fact_type.is_a?(ActiveFacts::Metamodel::LinkFactType) and
364
+ !@fact_types_dumped[fact_type] and
365
+ !skip_fact_type(fact_type) and
366
+ !fact_type.all_role.detect{|r| r.object_type.is_a?(ActiveFacts::Metamodel::EntityType) }
367
+ }.sort_by{|fact_id|
368
+ fact_type = fact_collection[fact_id]
369
+ fact_type_key(fact_type)
370
+ }.each{|fact_id|
371
+ fact_type = fact_collection[fact_id]
372
+
373
+ fact_type_banner unless done_banner
374
+ done_banner = true
375
+ fact_type_dump_with_dependents(fact_type)
376
+ }
377
+
378
+ # REVISIT: Find out why some fact types are missed during entity dumping:
379
+ @vocabulary.constellation.FactType.values.select{|fact_type|
380
+ !fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) &&
381
+ !fact_type.is_a?(ActiveFacts::Metamodel::LinkFactType)
382
+ }.sort_by{|fact_type|
383
+ fact_type_key(fact_type)
384
+ }.each{|fact_type|
385
+ next if @fact_types_dumped[fact_type]
386
+ # debug "Not dumped #{fact_type.verbalise}(#{fact_type.all_role.map{|r| r.object_type.name}*", "})"
387
+ fact_type_banner unless done_banner
388
+ done_banner = true
389
+ fact_type_dump_with_dependents(fact_type)
390
+ }
391
+
392
+ fact_type_end if done_banner
393
+ # unused = constraints - @constraints_used.keys
394
+ # debug "residual constraints are\n\t#{unused.map(&:to_s)*",\n\t"}"
395
+
396
+ @constraints_used
397
+ end
398
+
399
+ def fact_instances_dump
400
+ @vocabulary.fact_types.each{|f|
401
+ # Dump the instances:
402
+ f.facts.each{|i|
403
+ raise "REVISIT: Not dumping fact instances"
404
+ debug "\t\t"+i.to_s
405
+ }
406
+ }
407
+ end
408
+
409
+ # Arrange for objectified fact types to appear in order of name, after other fact types.
410
+ # Facts are ordered alphabetically by the names of their role players,
411
+ # then by preferred_reading (subtyping fact types have no preferred_reading).
412
+ def fact_type_key(fact_type)
413
+ role_names =
414
+ if (pr = fact_type.preferred_reading)
415
+ pr.role_sequence.
416
+ all_role_ref.
417
+ sort_by{|role_ref| role_ref.ordinal}.
418
+ map{|role_ref| [ role_ref.leading_adjective, role_ref.role.object_type.name, role_ref.trailing_adjective ].compact*"-" } +
419
+ [pr.text]
420
+ else
421
+ fact_type.all_role.map{|role| role.object_type.name }
422
+ end
423
+
424
+ (fact_type.entity_type ? [fact_type.entity_type.name] : [""]) + role_names
425
+ end
426
+
427
+ def role_ref_key(role_ref)
428
+ [ role_ref.leading_adjective, role_ref.role.object_type.name, role_ref.trailing_adjective ].compact*"-" +
429
+ " in " +
430
+ role_ref.role.fact_type.preferred_reading.expand
431
+ end
432
+
433
+ def constraint_sort_key(c)
434
+ case c
435
+ when ActiveFacts::Metamodel::RingConstraint
436
+ [ 1,
437
+ c.ring_type,
438
+ c.role.object_type.name,
439
+ c.other_role.object_type.name,
440
+ c.name||""
441
+ ]
442
+ when ActiveFacts::Metamodel::SetExclusionConstraint
443
+ [ 2+(c.is_mandatory ? 0 : 1),
444
+ c.all_set_comparison_roles.map{|scrs|
445
+ scrs.role_sequence.all_role_ref.map{|rr|
446
+ role_ref_key(rr)
447
+ }
448
+ },
449
+ c.name||""
450
+ ]
451
+ when ActiveFacts::Metamodel::SetEqualityConstraint
452
+ [ 4,
453
+ c.all_set_comparison_roles.map{|scrs|
454
+ scrs.role_sequence.all_role_ref.map{|rr|
455
+ role_ref_key(rr)
456
+ }
457
+ },
458
+ c.name||""
459
+ ]
460
+ when ActiveFacts::Metamodel::SubsetConstraint
461
+ [ 5,
462
+ [c.superset_role_sequence, c.subset_role_sequence].map{|rs|
463
+ rs.all_role_ref.map{|rr|
464
+ role_ref_key(rr)
465
+ }
466
+ },
467
+ c.name||""
468
+ ]
469
+ when ActiveFacts::Metamodel::PresenceConstraint
470
+ [ 6,
471
+ c.role_sequence.all_role_ref.map{|rr|
472
+ role_ref_key(rr)
473
+ },
474
+ c.name||""
475
+ ]
476
+ end
477
+ end
478
+
479
+ def constraints_dump(except = {})
480
+ heading = false
481
+ @vocabulary.all_constraint.reject{|c| except[c]}.sort_by{ |c| constraint_sort_key(c) }.each do|c|
482
+ # Skip some PresenceConstraints:
483
+ if c.is_a?(ActiveFacts::Metamodel::PresenceConstraint)
484
+ # Skip uniqueness constraints that cover all roles of a fact type, they're implicit
485
+ fact_types = c.role_sequence.all_role_ref.map{|rr| rr.role.fact_type}.uniq
486
+ if fact_types.size == 1 &&
487
+ !c.role_sequence.all_role_ref.detect{|rr| rr.play } &&
488
+ c.max_frequency == 1 && # Uniqueness
489
+ fact_types[0].all_role.size == c.role_sequence.all_role_ref.size
490
+ next
491
+ end
492
+
493
+ # Skip internal PresenceConstraints over TypeInheritances:
494
+ next if c.role_sequence.all_role_ref.size == 1 &&
495
+ fact_types[0].is_a?(ActiveFacts::Metamodel::TypeInheritance)
496
+ end
497
+
498
+ constraint_banner unless heading
499
+ heading = true
500
+
501
+ # Skip presence constraints on value types:
502
+ # next if ActiveFacts::PresenceConstraint === c &&
503
+ # ActiveFacts::ValueType === c.object_type
504
+ constraint_dump(c)
505
+ end
506
+ constraint_end if heading
507
+ end
508
+
509
+ def vocabulary_start(vocabulary)
510
+ debug "Should override vocabulary_start"
511
+ end
512
+
513
+ def vocabulary_end
514
+ debug "Should override vocabulary_end"
515
+ end
516
+
517
+ def units_banner
518
+ end
519
+
520
+ def units_end
521
+ end
522
+
523
+ def unit_dump unit
524
+ end
525
+
526
+ def value_type_banner
527
+ debug "Should override value_type_banner"
528
+ end
529
+
530
+ def value_type_end
531
+ debug "Should override value_type_end"
532
+ end
533
+
534
+ def value_type_dump(o)
535
+ debug "Should override value_type_dump"
536
+ end
537
+
538
+ def entity_type_banner
539
+ debug "Should override entity_type_banner"
540
+ end
541
+
542
+ def entity_type_group_end
543
+ debug "Should override entity_type_group_end"
544
+ end
545
+
546
+ def non_subtype_dump(o, pi)
547
+ debug "Should override non_subtype_dump"
548
+ end
549
+
550
+ def subtype_dump(o, supertypes, pi = nil)
551
+ debug "Should override subtype_dump"
552
+ end
553
+
554
+ def append_ring_to_reading(reading, ring)
555
+ debug "Should override append_ring_to_reading"
556
+ end
557
+
558
+ def fact_type_banner
559
+ debug "Should override fact_type_banner"
560
+ end
561
+
562
+ def fact_type_end
563
+ debug "Should override fact_type_end"
564
+ end
565
+
566
+ def fact_type_dump(fact_type, name)
567
+ debug "Should override fact_type_dump"
568
+ end
569
+
570
+ def constraint_banner
571
+ debug "Should override constraint_banner"
572
+ end
573
+
574
+ def constraint_end
575
+ debug "Should override constraint_end"
576
+ end
577
+
578
+ def constraint_dump(c)
579
+ debug "Should override constraint_dump"
580
+ end
581
+
582
+ end
583
+
584
+ def dump(vocabulary, out = $>)
585
+ OrderedDumper.new(vocabulary).dump(out)
586
+ end
587
+ end
588
+ end
589
+ end