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
@@ -6,7 +6,7 @@
6
6
  # Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
7
7
  #
8
8
  #module ActiveFacts
9
- $debug_indent = nil
9
+ $debug_indent = 0
10
10
  $debug_nested = false # Set when a block enables all enclosed debugging
11
11
  $debug_keys = nil
12
12
  $debug_available = {}
@@ -14,13 +14,15 @@
14
14
  def debug_initialize
15
15
  # First time, initialise the tracing environment
16
16
  $debug_indent = 0
17
- $debug_keys = {}
18
- if (e = ENV["DEBUG"])
19
- e.split(/[^_a-zA-Z0-9]/).each{|k| debug_enable(k) }
20
- if $debug_keys[:help]
21
- at_exit {
22
- $stderr.puts "---\nDebugging keys available: #{$debug_available.keys.map{|s| s.to_s}.sort*", "}"
23
- }
17
+ unless $debug_keys
18
+ $debug_keys = {}
19
+ if (e = ENV["DEBUG"])
20
+ e.split(/[^_a-zA-Z0-9]/).each{|k| debug_enable(k) }
21
+ if $debug_keys[:help]
22
+ at_exit {
23
+ $stderr.puts "---\nDebugging keys available: #{$debug_available.keys.map{|s| s.to_s}.sort*", "}"
24
+ }
25
+ end
24
26
  end
25
27
  end
26
28
  end
@@ -72,7 +74,9 @@
72
74
  end
73
75
 
74
76
  def debug_show(*args)
75
- debug_initialize unless $debug_indent
77
+ unless $debug_keys
78
+ debug_initialize
79
+ end
76
80
 
77
81
  enabled, key_to_show = debug_selected(args)
78
82
 
@@ -152,7 +156,7 @@ class Array
152
156
  end
153
157
 
154
158
  # Load the ruby debugger before everything else, if requested
155
- if debug :debug
159
+ if debug :debug or debug :firstaid
156
160
  begin
157
161
  require 'ruby-debug'
158
162
  Debugger.start # (:post_mortem => true) # Some Ruby versions crash on post-mortem debugging
@@ -167,4 +171,18 @@ if debug :debug
167
171
  true
168
172
  end
169
173
  end
174
+
175
+ if debug :firstaid
176
+ puts "Preparing first aid kit"
177
+ class ::Exception
178
+ alias_method :firstaid_initialize, :initialize
179
+
180
+ def initialize *args, &b
181
+ send(:firstaid_initialize, *args, &b)
182
+ puts "Stopped due to #{self.class}: #{message} at "+caller*"\n\t"
183
+ debugger
184
+ true # Exception thrown
185
+ end
186
+ end
187
+ end
170
188
  end
@@ -8,7 +8,7 @@ module ActiveFacts
8
8
  module Version
9
9
  MAJOR = 0
10
10
  MINOR = 8
11
- PATCH = 16
11
+ PATCH = 18
12
12
 
13
13
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
14
14
  end
@@ -6,15 +6,76 @@
6
6
  #
7
7
  module ActiveFacts
8
8
  module Metamodel
9
+ class Vocabulary
10
+ def finalise
11
+ constellation.FactType.values.each do |fact_type|
12
+ if c = fact_type.check_and_add_spanning_uniqueness_constraint
13
+ debug :constraint, "Checking for existence of at least one uniqueness constraint over the roles of #{fact_type.default_reading.inspect}"
14
+ fact_type.check_and_add_spanning_uniqueness_constraint = nil
15
+ c.call
16
+ end
17
+ end
18
+ end
19
+
20
+ # This name does not yet exist (at least not as we expect it to).
21
+ # If it in fact does exist (but as the wrong type), complain.
22
+ # If it doesn't exist, but its name would cause existing fact type
23
+ # readings to be re-interpreted to a different meaning, complain.
24
+ # Otherwise return nil.
25
+ def check_valid_nonexistent_object_type_name name
26
+ if ot = valid_object_type_name(name)
27
+ raise "Cannot redefine #{ot.class.basename} #{name}"
28
+ end
29
+ end
30
+
31
+ def valid_object_type_name name
32
+ # Raise an exception if adding this name to the vocabulary would create anomalies
33
+ anomaly = constellation.Reading.detect do |r_key, reading|
34
+ expanded = reading.expand do |role_ref, *words|
35
+ words.map! do |w|
36
+ case
37
+ when w == nil
38
+ w
39
+ when w[0...name.size] == name
40
+ '_ok_'+w
41
+ when w[-name.size..-1] == name
42
+ w[-1]+'_ok_'
43
+ else
44
+ w
45
+ end
46
+ end
47
+
48
+ words
49
+ end
50
+ expanded =~ %r{\b#{name}\b}
51
+ end
52
+ raise "Adding new term '#{name}' would create anomalous re-interpretation of '#{anomaly.expand}'" if anomaly
53
+ @constellation.ObjectType[[identifying_role_values, name]]
54
+ end
55
+
56
+ # If this entity type exists, ok, otherwise check it's ok to add it
57
+ def valid_entity_type_name name
58
+ @constellation.EntityType[[identifying_role_values, name]] or
59
+ check_valid_nonexistent_object_type_name name
60
+ end
61
+
62
+ # If this entity type exists, ok, otherwise check it's ok to add it
63
+ def valid_value_type_name name
64
+ @constellation.ValueType[[identifying_role_values, name]] or
65
+ check_valid_nonexistent_object_type_name name
66
+ end
67
+ end
9
68
 
10
69
  class FactType
70
+ attr_accessor :check_and_add_spanning_uniqueness_constraint
71
+
11
72
  def all_reading_by_ordinal
12
73
  all_reading.sort_by{|reading| reading.ordinal}
13
74
  end
14
75
 
15
- def preferred_reading
16
- pr = all_reading_by_ordinal[0]
17
- raise "No reading for (#{all_role.map{|r| r.object_type.name}*", "})" unless pr
76
+ def preferred_reading negated = false
77
+ pr = all_reading_by_ordinal.detect{|r| !r.is_negative == !negated }
78
+ raise "No reading for (#{all_role.map{|r| r.object_type.name}*", "})" unless pr || negated
18
79
  pr
19
80
  end
20
81
 
@@ -37,21 +98,28 @@ module ActiveFacts
37
98
  end.flatten.compact.uniq
38
99
  end
39
100
 
101
+ def implicit_boolean_type vocabulary
102
+ @constellation.ImplicitBooleanValueType[[vocabulary.identifying_role_values, "_ImplicitBooleanValueType"]] ||
103
+ @constellation.ImplicitBooleanValueType(vocabulary.identifying_role_values, "_ImplicitBooleanValueType", :guid => :new)
104
+ end
105
+
40
106
  # This entity type has just objectified a fact type. Create the necessary ImplicitFactTypes with phantom roles
41
107
  def create_implicit_fact_type_for_unary
42
108
  role = all_role.single
43
- return if role.implicit_fact_type # Already exists
109
+ return if role.link_fact_type # Already exists
44
110
  # NORMA doesn't create an implicit fact type here, rather the fact type has an implicit extra role, so looks like a binary
45
111
  # We only do it when the unary fact type is not objectified
46
- implicit_fact_type = @constellation.ImplicitFactType(:new, :implying_role => role)
47
- entity_type = @entity_type || @constellation.ImplicitBooleanValueType(role.object_type.vocabulary, "_ImplicitBooleanValueType", :guid => :new)
48
- phantom_role = @constellation.Role(implicit_fact_type, 0, :object_type => entity_type, :guid => :new)
112
+ link_fact_type = @constellation.LinkFactType(:new, :implying_role => role)
113
+ entity_type = @entity_type || implicit_boolean_type(role.object_type.vocabulary)
114
+ phantom_role = @constellation.Role(link_fact_type, 0, :object_type => entity_type, :guid => :new)
49
115
  end
50
116
 
51
- def reading_preferably_starting_with_role role
117
+ def reading_preferably_starting_with_role role, negated = false
52
118
  all_reading_by_ordinal.detect do |reading|
53
- reading.text =~ /\{\d\}/ and reading.role_sequence.all_role_ref_in_order[$1.to_i].role == role
54
- end || preferred_reading
119
+ reading.text =~ /\{\d\}/ and
120
+ reading.role_sequence.all_role_ref_in_order[$1.to_i].role == role and
121
+ reading.is_negative == !!negated
122
+ end || preferred_reading(negated)
55
123
  end
56
124
 
57
125
  def all_role_in_order
@@ -76,6 +144,7 @@ module ActiveFacts
76
144
  end
77
145
 
78
146
  def is_mandatory
147
+ link_fact_type ||
79
148
  all_role_ref.detect{|rr|
80
149
  rs = rr.role_sequence
81
150
  rs.all_role_ref.size == 1 and
@@ -85,7 +154,6 @@ module ActiveFacts
85
154
  } ? true : false
86
155
  end
87
156
 
88
- # Return the RoleRef to this role from its fact type's preferred_reading
89
157
  def preferred_reference
90
158
  fact_type.preferred_reading.role_sequence.all_role_ref.detect{|rr| rr.role == self }
91
159
  end
@@ -103,19 +171,6 @@ module ActiveFacts
103
171
  end
104
172
  end
105
173
 
106
- def role_method
107
- if fact_type.all_role.size == 1
108
- # The object of an objectified unary has no boolean; the existence of the object is the fact
109
- return nil if fact_type.entity_type
110
- return preferred_role_name(role)
111
- end
112
- if fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
113
- # No method is available to indicate that the object is the specified subtype
114
- # In future, this might be implemented using the type discriminator
115
- return nil
116
- end
117
- end
118
-
119
174
  end
120
175
 
121
176
  class RoleRef
@@ -123,14 +178,14 @@ module ActiveFacts
123
178
  role_name
124
179
  end
125
180
 
126
- def preferred_role_ref
127
- role.fact_type.preferred_reading.role_sequence.all_role_ref.detect{|rr| rr.role == role}
181
+ def preferred_reference
182
+ role.preferred_reference
128
183
  end
129
184
 
130
- def role_name(joiner = "-")
185
+ def role_name(separator = "-")
131
186
  name_array =
132
187
  if role.fact_type.all_role.size == 1
133
- if role.fact_type.is_a?(ImplicitFactType)
188
+ if role.fact_type.is_a?(LinkFactType)
134
189
  "#{role.object_type.name} phantom for #{role.fact_type.role.object_type.name}"
135
190
  else
136
191
  role.fact_type.preferred_reading.text.gsub(/\{[0-9]\}/,'').strip.split(/\s/)
@@ -138,7 +193,7 @@ module ActiveFacts
138
193
  else
139
194
  role.role_name || [leading_adjective, role.object_type.name, trailing_adjective].compact.map{|w| w.split(/\s/)}.flatten
140
195
  end
141
- return joiner ? Array(name_array)*joiner : Array(name_array)
196
+ return separator ? Array(name_array)*separator : Array(name_array)
142
197
  end
143
198
 
144
199
  def cql_name
@@ -167,6 +222,11 @@ module ActiveFacts
167
222
  end
168
223
  end
169
224
 
225
+ class ObjectType
226
+ # Placeholder for the surrogate transform
227
+ attr_reader :injected_surrogate_role
228
+ end
229
+
170
230
  class ValueType
171
231
  def supertypes_transitive
172
232
  [self] + (supertype ? supertype.supertypes_transitive : [])
@@ -182,9 +242,27 @@ module ActiveFacts
182
242
  end
183
243
 
184
244
  class EntityType
245
+ def identification_is_inherited
246
+ preferred_identifier and
247
+ preferred_identifier.role_sequence.all_role_ref.detect{|rr| rr.role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) }
248
+ end
249
+
250
+ def assimilation
251
+ if rr = identification_is_inherited
252
+ rr.role.fact_type.assimilation
253
+ else
254
+ nil
255
+ end
256
+ end
257
+
185
258
  def preferred_identifier
186
259
  return @preferred_identifier if @preferred_identifier
187
260
  if fact_type
261
+ # When compiling a fact instance, the delayed creation of a preferred identifier might be necessary
262
+ if c = fact_type.check_and_add_spanning_uniqueness_constraint
263
+ fact_type.check_and_add_spanning_uniqueness_constraint = nil
264
+ c.call
265
+ end
188
266
 
189
267
  # For a nested fact type, the PI is a unique constraint over N or N-1 roles
190
268
  fact_roles = Array(fact_type.all_role)
@@ -369,12 +447,13 @@ module ActiveFacts
369
447
 
370
448
  # This entity type has just objectified a fact type. Create the necessary ImplicitFactTypes with phantom roles
371
449
  def create_implicit_fact_types
372
- fact_type.all_role.each do |role|
373
- next if role.implicit_fact_type # Already exists
374
- implicit_fact_type = @constellation.ImplicitFactType(:new, :implying_role => role)
375
- phantom_role = @constellation.Role(implicit_fact_type, 0, :object_type => self, :guid => :new)
450
+ fact_type.all_role.map do |role|
451
+ next if role.link_fact_type # Already exists
452
+ link_fact_type = @constellation.LinkFactType(:new, :implying_role => role)
453
+ phantom_role = @constellation.Role(link_fact_type, 0, :object_type => self, :guid => :new)
376
454
  # We could create a copy of the visible external role here, but there's no need yet...
377
455
  # Nor is there a need for a presence constraint, readings, etc.
456
+ link_fact_type
378
457
  end
379
458
  end
380
459
  end
@@ -484,9 +563,13 @@ module ActiveFacts
484
563
 
485
564
  class ValueConstraint
486
565
  def describe
487
- "restricted to {"+
488
- all_allowed_range_sorted.map{|ar| ar.to_s(false) }*", "+
489
- "}"
566
+ "restricted to "+
567
+ ( if regular_expression
568
+ '/' + regular_expression + '/'
569
+ else
570
+ '{' + all_allowed_range_sorted.map{|ar| ar.to_s(false) }*', ' + '}'
571
+ end
572
+ )
490
573
  end
491
574
 
492
575
  def all_allowed_range_sorted
@@ -515,7 +598,7 @@ module ActiveFacts
515
598
 
516
599
  if min = value_range.minimum_bound
517
600
  min = min.value
518
- if min.is_a_string
601
+ if min.is_literal_string
519
602
  min_literal = min.literal.inspect.gsub(/\A"|"\Z/,"'") # Escape string characters
520
603
  else
521
604
  min_literal = min.literal
@@ -525,7 +608,7 @@ module ActiveFacts
525
608
  end
526
609
  if max = value_range.maximum_bound
527
610
  max = max.value
528
- if max.is_a_string
611
+ if max.is_literal_string
529
612
  max_literal = max.literal.inspect.gsub(/\A"|"\Z/,"'") # Escape string characters
530
613
  else
531
614
  max_literal = max.literal
@@ -541,8 +624,19 @@ module ActiveFacts
541
624
 
542
625
  class Value
543
626
  def to_s
544
- (is_a_string ? literal.inspect.gsub(/\A"|"\Z/,"'") : literal) + (unit ? " " + unit.name : "")
627
+ if is_literal_string
628
+ "'"+
629
+ literal.
630
+ inspect. # Use Ruby's inspect to generate necessary escapes
631
+ gsub(/\A"|"\Z/,''). # Remove surrounding quotes
632
+ gsub(/'/, "\\'") + # Escape any single quotes
633
+ "'"
634
+ else
635
+ literal
636
+ end +
637
+ (unit ? " " + unit.name : "")
545
638
  end
639
+
546
640
  def inspect
547
641
  to_s
548
642
  end
@@ -580,37 +674,37 @@ module ActiveFacts
580
674
  end
581
675
  end
582
676
 
583
- class JoinStep
677
+ class Step
584
678
  def describe
585
- "JoinStep " +
586
- "#{is_outer && 'maybe '}" +
587
- (is_unary_step ? " (unary) " : "from #{input_join_role.describe} ") +
588
- "#{is_anti && 'not '}" +
589
- "to #{output_join_role.describe} " +
679
+ "Step " +
680
+ "#{is_optional ? 'maybe ' : ''}" +
681
+ (is_unary_step ? '(unary) ' : "from #{input_play.describe} ") +
682
+ "#{is_disallowed ? 'not ' : ''}" +
683
+ "to #{output_play.describe} " +
590
684
  "over " +
591
685
  (is_objectification_step ? 'objectification ' : '') +
592
686
  "'#{fact_type.default_reading}'"
593
687
  end
594
688
 
595
689
  def is_unary_step
596
- # Preserve this in case we have to use a real join_node for the phantom
597
- input_join_role == output_join_role
690
+ # Preserve this in case we have to use a real variable for the phantom
691
+ input_play == output_play
598
692
  end
599
693
 
600
694
  def is_objectification_step
601
- fact_type.is_a?(ImplicitFactType)
695
+ fact_type.is_a?(LinkFactType)
602
696
  end
603
697
 
604
- def all_join_role
605
- [input_join_role, output_join_role].uniq + all_incidental_join_role.to_a
698
+ def all_play
699
+ [input_play, output_play].uniq + all_incidental_play.to_a
606
700
  end
607
701
 
608
702
  def external_fact_type
609
- fact_type.is_a?(ImplicitFactType) ? fact_type.role.fact_type : fact_type
703
+ fact_type.is_a?(LinkFactType) ? fact_type.role.fact_type : fact_type
610
704
  end
611
705
  end
612
706
 
613
- class JoinNode
707
+ class Variable
614
708
  def describe
615
709
  object_type.name +
616
710
  (subscript ? "(#{subscript})" : '') +
@@ -618,79 +712,79 @@ module ActiveFacts
618
712
  (value ? ' = '+value.to_s : '')
619
713
  end
620
714
 
621
- def all_join_step
622
- all_join_role.map do |jr|
623
- jr.all_join_step_as_input_join_role.to_a +
624
- jr.all_join_step_as_output_join_role.to_a
715
+ def all_step
716
+ all_play.map do |jr|
717
+ jr.all_step_as_input_play.to_a +
718
+ jr.all_step_as_output_play.to_a
625
719
  end.
626
720
  flatten.
627
721
  uniq
628
722
  end
629
723
  end
630
724
 
631
- class JoinRole
725
+ class Play
632
726
  def describe
633
- "#{role.object_type.name} JN#{join_node.ordinal}" +
727
+ "#{role.object_type.name} JN#{variable.ordinal}" +
634
728
  (role_ref ? " (projected)" : "")
635
729
  end
636
730
 
637
- def all_join_step
638
- (all_join_step_as_input_join_role.to_a +
639
- all_join_step_as_output_join_role.to_a +
640
- [join_step]).flatten.compact.uniq
731
+ def all_step
732
+ (all_step_as_input_play.to_a +
733
+ all_step_as_output_play.to_a +
734
+ [step]).flatten.compact.uniq
641
735
  end
642
736
  end
643
737
 
644
- class Join
738
+ class Query
645
739
  def show
646
740
  steps_shown = {}
647
- debug :join, "Displaying full contents of Join #{guid}" do
648
- all_join_node.sort_by{|jn| jn.ordinal}.each do |join_node|
649
- debug :join, "#{join_node.describe}" do
650
- join_node.all_join_step.
651
- each do |join_step|
652
- next if steps_shown[join_step]
653
- steps_shown[join_step] = true
654
- debug :join, "#{join_step.describe}"
741
+ debug :query, "Displaying full contents of Query #{guid}" do
742
+ all_variable.sort_by{|jn| jn.ordinal}.each do |variable|
743
+ debug :query, "#{variable.describe}" do
744
+ variable.all_step.
745
+ each do |step|
746
+ next if steps_shown[step]
747
+ steps_shown[step] = true
748
+ debug :query, "#{step.describe}"
655
749
  end
656
- join_node.all_join_role.each do |join_role|
657
- debug :join, "role of #{join_role.describe} in '#{join_role.role.fact_type.default_reading}'"
750
+ variable.all_play.each do |play|
751
+ debug :query, "role of #{play.describe} in '#{play.role.fact_type.default_reading}'"
658
752
  end
659
753
  end
660
754
  end
661
755
  end
662
756
  end
663
757
 
664
- def all_join_step
665
- all_join_node.map{|jn| jn.all_join_step.to_a}.flatten.uniq
758
+ def all_step
759
+ all_variable.map{|jn| jn.all_step.to_a}.flatten.uniq
666
760
  end
667
761
 
668
- # Check all parts of this join for validity
762
+ # Check all parts of this query for validity
669
763
  def validate
670
764
  show
671
765
  return
672
766
 
673
- # Check the join nodes:
674
- join_steps = []
675
- join_nodes = all_join_node.sort_by{|jn| jn.ordinal}
676
- join_nodes.each_with_index do |join_node, i|
677
- raise "Join node #{i} should have ordinal #{join_node.ordinal}" unless join_node.ordinal == i
678
- raise "Join Node #{i} has missing object_type" unless join_node.object_type
679
- join_node.all_join_role do |join_role|
680
- raise "Join Node for #{object_type.name} includes role played by #{join_role.object_type.name}" unless join_role.object_type == object_type
767
+ # Check the variables:
768
+ steps = []
769
+ variables = all_variable.sort_by{|jn| jn.ordinal}
770
+ variables.each_with_index do |variable, i|
771
+ raise "Variable #{i} should have ordinal #{variable.ordinal}" unless variable.ordinal == i
772
+ raise "Variable #{i} has missing object_type" unless variable.object_type
773
+ variable.all_play do |play|
774
+ raise "Variable for #{object_type.name} includes role played by #{play.object_type.name}" unless play.object_type == object_type
681
775
  end
682
- join_steps += join_node.all_join_step
776
+ steps += variable.all_step
683
777
  end
684
- join_steps.uniq!
685
-
686
- # Check the join steps:
687
- join_steps.each do |join_step|
688
- raise "Join Step has missing fact type" unless join_step.fact_type
689
- raise "Join Step has missing input node" unless join_step.input_join_role
690
- raise "Join Step has missing output node" unless join_step.output_join_role
691
- if (role = input_join_role).role.fact_type != fact_type or
692
- (role = output_join_role).role.fact_type != fact_type
693
- raise "Join Step has role #{role.describe} which doesn't belong to the fact type '#{fact_type.default_reading}' it traverses"
778
+ steps.uniq!
779
+
780
+ # Check the steps:
781
+ steps.each do |step|
782
+ raise "Step has missing fact type" unless step.fact_type
783
+ raise "Step has missing input node" unless step.input_play
784
+ raise "Step has missing output node" unless step.output_play
785
+ if (role = input_play).role.fact_type != fact_type or
786
+ (role = output_play).role.fact_type != fact_type
787
+ raise "Step has role #{role.describe} which doesn't belong to the fact type '#{fact_type.default_reading}' it traverses"
694
788
  end
695
789
  end
696
790
 
@@ -698,10 +792,10 @@ module ActiveFacts
698
792
  end
699
793
  end
700
794
 
701
- class ImplicitFactType
795
+ class LinkFactType
702
796
  def default_reading
703
797
  # There are two cases, where role is in a unary fact type, and where the fact type is objectified
704
- # If a unary fact type is objectified, only the ImplicitFactType for the objectification is asserted
798
+ # If a unary fact type is objectified, only the LinkFactType for the objectification is asserted
705
799
  if objectification = implying_role.fact_type.entity_type
706
800
  "#{objectification.name} involves #{implying_role.object_type.name}"
707
801
  else
@@ -709,6 +803,20 @@ module ActiveFacts
709
803
  end
710
804
  end
711
805
 
806
+ def add_reading implicit_reading
807
+ @readings ||= []
808
+ @readings << implicit_reading
809
+ end
810
+
811
+ def all_reading
812
+ @readings ||=
813
+ [ ImplicitReading.new(
814
+ self,
815
+ implying_role.fact_type.entity_type ? "{0} involves {1}" : implying_role.fact_type.default_reading+" Boolean"
816
+ )
817
+ ]
818
+ end
819
+
712
820
  # This is only used for debugging, from RoleRef#describe
713
821
  class ImplicitReading
714
822
  attr_accessor :fact_type, :text
@@ -726,8 +834,8 @@ module ActiveFacts
726
834
  @role = role
727
835
  @role_sequence = role_sequence
728
836
  end
729
- def join_node; nil; end
730
- def join_role; nil; end
837
+ def variable; nil; end
838
+ def play; nil; end
731
839
  def leading_adjective; nil; end
732
840
  def trailing_adjective; nil; end
733
841
  def describe
@@ -760,19 +868,12 @@ module ActiveFacts
760
868
  @fact_type.default_reading
761
869
  end
762
870
  end
763
-
764
- def all_reading
765
- [@reading ||= ImplicitReading.new(
766
- self,
767
- implying_role.fact_type.entity_type ? "{0} involves {1}" : implying_role.fact_type.default_reading+" Boolean"
768
- )]
769
- end
770
871
  end
771
872
 
772
- # Some joins must be over the proximate roles, some over the counterpart roles.
873
+ # Some queries must be over the proximate roles, some over the counterpart roles.
773
874
  # Return the common superclass of the appropriate roles, and the actual roles
774
- def self.join_roles_over roles, options = :both # Or :proximate, :counterpart
775
- # If we can stay inside this objectified FT, there's no join:
875
+ def self.plays_over roles, options = :both # Or :proximate, :counterpart
876
+ # If we can stay inside this objectified FT, there's no query:
776
877
  roles = Array(roles) # To be safe, in case we get a role collection proxy
777
878
  return nil if roles.size == 1 or
778
879
  options != :counterpart && roles.map{|role| role.fact_type}.uniq.size == 1
@@ -788,11 +889,11 @@ module ActiveFacts
788
889
  counterpart_role_supertypes =
789
890
  if fact_type.all_role.size > 2
790
891
  possible_roles = fact_type.all_role.select{|r| d_c_o && d_c_o[1].include?(r.object_type) }
791
- if possible_roles.size == 1 # Only one candidate matches the types of the possible join nodes
892
+ if possible_roles.size == 1 # Only one candidate matches the types of the possible variables
792
893
  counterpart_role = possible_roles[0]
793
894
  d_c_o[1] # No change
794
895
  else
795
- # puts "#{constraint_type} #{name}: Awkward, try counterpart-role join on a >2ary '#{fact_type.default_reading}'"
896
+ # puts "#{constraint_type} #{name}: Awkward, try counterpart-role query on a >2ary '#{fact_type.default_reading}'"
796
897
  # Try all roles; hopefully we don't have two roles with a matching candidate here:
797
898
  # Find which role is compatible with the existing supertypes, if any
798
899
  if d_c_o
@@ -815,7 +916,7 @@ module ActiveFacts
815
916
  if fact_type.entity_type
816
917
  objectification_role_supertypes =
817
918
  fact_type.entity_type.supertypes_transitive+object_type.supertypes_transitive
818
- objectification_role = role.implicit_fact_type.all_role.single # Find the phantom role here
919
+ objectification_role = role.link_fact_type.all_role.single # Find the phantom role here
819
920
  else
820
921
  objectification_role_supertypes = counterpart_role_supertypes
821
922
  objectification_role = counterpart_role
@@ -835,12 +936,12 @@ module ActiveFacts
835
936
  d_c_o
836
937
  end # inject
837
938
 
838
- # Discount a subtype join over an object type that's not a player here,
839
- # if we can use an objectification join to an object type that is:
939
+ # Discount a subtype step over an object type that's not a player here,
940
+ # if we can use an objectification step to an object type that is:
840
941
  if counterpart_sups.size > 0 && obj_sups.size > 0 && counterpart_sups[0] != obj_sups[0]
841
- debug :join, "ambiguous join, could be over #{counterpart_sups[0].name} or #{obj_sups[0].name}"
942
+ debug :query, "ambiguous query, could be over #{counterpart_sups[0].name} or #{obj_sups[0].name}"
842
943
  if !roles.detect{|r| r.object_type == counterpart_sups[0]} and roles.detect{|r| r.object_type == obj_sups[0]}
843
- debug :join, "discounting #{counterpart_sups[0].name} in favour of direct objectification"
944
+ debug :query, "discounting #{counterpart_sups[0].name} in favour of direct objectification"
844
945
  counterpart_sups = []
845
946
  end
846
947
  end
@@ -898,7 +999,7 @@ module ActiveFacts
898
999
  "its #{identifying_instance.verbalise(context)}"
899
1000
  else
900
1001
  identifying_role_refs.map do |rr|
901
- rr = rr.preferred_role_ref
1002
+ rr = rr.preferred_reference
902
1003
  [ (l = rr.leading_adjective) ? l+"-" : nil,
903
1004
  rr.role.role_name || rr.role.object_type.name,
904
1005
  (t = rr.trailing_adjective) ? l+"-" : nil
@@ -917,5 +1018,33 @@ module ActiveFacts
917
1018
  end
918
1019
  end
919
1020
 
1021
+ class ContextNote
1022
+ def verbalise(context=nil)
1023
+ ' (' +
1024
+ ( if all_context_according_to
1025
+ 'according to '
1026
+ all_context_according_to.map do |act|
1027
+ act.agent.agent_name+', '
1028
+ end.join('')
1029
+ end
1030
+ ) +
1031
+ context_note_kind.gsub(/_/, ' ') +
1032
+ ' ' +
1033
+ discussion +
1034
+ ( if agreement
1035
+ ', as agreed ' +
1036
+ (agreement.date ? ' on '+agreement.date+' ' : '') +
1037
+ 'by '
1038
+ agreement.all_context_agreed_by.map do |acab|
1039
+ acab.agent.agent_name+', '
1040
+ end.join('')
1041
+ else
1042
+ ''
1043
+ end
1044
+ ) +
1045
+ ')'
1046
+ end
1047
+ end
1048
+
920
1049
  end
921
1050
  end