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