Class: ActiveFacts::CQL::Compiler
- Inherits:
-
Parser
- Object
- CQLParser
- Parser
- ActiveFacts::CQL::Compiler
show all
- Defined in:
- lib/activefacts/cql/compiler.rb,
lib/activefacts/cql/compiler/fact.rb,
lib/activefacts/cql/compiler/query.rb,
lib/activefacts/cql/compiler/clause.rb,
lib/activefacts/cql/compiler/shared.rb,
lib/activefacts/cql/compiler/informal.rb,
lib/activefacts/cql/compiler/fact_type.rb,
lib/activefacts/cql/compiler/constraint.rb,
lib/activefacts/cql/compiler/expression.rb,
lib/activefacts/cql/compiler/value_type.rb,
lib/activefacts/cql/compiler/entity_type.rb,
lib/activefacts/cql/compiler/transform_rule.rb
Defined Under Namespace
Classes: Aggregate, Binding, Clause, ClauseMatchSideEffect, ClauseMatchSideEffects, Comparison, CompilationContext, CompoundMatching, Constraint, ContextNote, Definition, Enforcement, EntityType, ExpressionTermList, Fact, FactType, Import, InformalDefinition, Literal, LogicalAnd, LogicalOr, Negate, Negation, ObjectType, Operation, PresenceConstraint, Product, Quantifier, Query, Reciprocal, Reference, ReferenceMode, RingConstraint, SetComparisonConstraint, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, SimpleMatching, SubsetConstraint, Sum, Ternary, TransformRule, Unit, ValueConstraint, ValueType, Vocabulary
Constant Summary
collapse
- LANGUAGES =
{
'en' => 'English',
'fr' => 'French',
'cn' => 'Mandarin'
}
- EXTENSIONS =
['fiml', 'fidl', 'fiql', 'cql']
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Parser
#context, #parse, #parse_all
Constructor Details
#initialize(filepath, options = {}) ⇒ Compiler
Returns a new instance of Compiler.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/activefacts/cql/compiler.rb', line 31
def initialize filepath, options = {}
@filepath = filepath
super()
if @constellation = options[:constellation]
@vocabulary = @constellation.Vocabulary.values[0]
@constellation.ValueType.values.each do |object_type|
context.object_type(object_type.name, "value type")
end
@constellation.EntityType.values.each do |object_type|
context.object_type(object_type.name, "entity type")
end
else
@constellation = ActiveFacts::API::Constellation.new(ActiveFacts::Metamodel)
end
@constellation = options[:constellation] || ActiveFacts::API::Constellation.new(ActiveFacts::Metamodel)
@constellation.loggers << proc{|*k| trace :apilog, k.inspect} if trace(:apilog)
@language = nil
@pending_import_topic = nil
@pending_import_role = ''
@pending_import_file_name = ''
trace :file, "Parsing '#{@filename}'"
end
|
Instance Attribute Details
#vocabulary ⇒ Object
Returns the value of attribute vocabulary.
29
30
31
|
# File 'lib/activefacts/cql/compiler.rb', line 29
def vocabulary
@vocabulary
end
|
Class Method Details
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/activefacts/cql/compiler/transform_rule.rb', line 25
def self.build_transform_target_refs context, targ_term_list, transform_rule
vocabulary_identifier = context.vocabulary.identifying_role_values
constellation = context.vocabulary.constellation
targ_term_list.flatten!
(0 ... targ_term_list.size).each do |idx|
ref = targ_term_list[idx]
if (target_ot = constellation.ObjectType[[vocabulary_identifier, ref.term]]).nil?
raise "Target object '#{ref.term}' of transformation must be a valid object type"
end
constellation.TransformTargetRef(
transform_rule, idx, :object_type => target_ot,
:leading_adjective => ref.leading_adjective, :trailing_adjective => ref.trailing_adjective
)
end
end
|
Instance Method Details
#compile(input) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/activefacts/cql/compiler.rb', line 104
def compile input
include_language
@string = input
ok = parse_all(@string, :definition) do |node|
trace :parse, "Parsed '#{node.text_value.gsub(/\s+/,' ').strip}'" do
trace :lex, (proc { node.inspect })
begin
ast = node.ast
next unless ast
trace :ast, ast.inspect
ast.tree = node
ast.constellation = @constellation
ast.vocabulary = @vocabulary
value = compile_definition ast
trace :definition, "Compiled to #{value.is_a?(Array) ? value.map{|v| v.verbalise}*', ' : value.verbalise}" if value
if value.is_a?(ActiveFacts::Metamodel::Topic)
topic_flood() if @topic
create_import_if_pending(value)
@topic = value
elsif ast.is_a?(Compiler::Vocabulary)
topic_flood() if @topic
@vocabulary = value
@topic = @constellation.Topic(@vocabulary.name)
end
rescue => e
start_line = @string.line_of(node.interval.first)
end_line = @string.line_of(node.interval.last-1)
lines = start_line != end_line ? "s #{start_line}-#{end_line}" : " #{start_line.to_s}"
ne = StandardError.new("at line#{lines} #{e.message.strip}")
ne.set_backtrace(e.backtrace)
raise ne
end
end
topic_flood() if @topic
end
raise failure_reason unless ok
vocabulary
end
|
#compile_definition(ast) ⇒ Object
216
217
218
|
# File 'lib/activefacts/cql/compiler.rb', line 216
def compile_definition ast
ast.compile
end
|
#compile_file(filepath) ⇒ Object
54
55
56
57
58
59
60
61
62
|
# File 'lib/activefacts/cql/compiler.rb', line 54
def compile_file filepath
old_filepath = @filepath
@filepath = filename
File.open(filepath) do |f|
compile(f.read)
end
@filepath = old_filepath
@vocabulary
end
|
#compile_import(file, import_role, aliases) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/activefacts/cql/compiler.rb', line 148
def compile_import file, import_role, aliases
saved_index = @index
saved_block = @block
saved_string = @string
saved_input_length = @input_length
old_filepath = @filepath
@file = file
@filepath = import_filepath(old_filepath, file)
compile_import_file(@filepath, import_role)
rescue => e
ne = StandardError.new("In #{@filepath} #{e.message.strip}")
ne.set_backtrace(e.backtrace)
raise ne
ensure
@block = saved_block
@index = saved_index
@input_length = saved_input_length
@string = saved_string
@filepath = old_filepath
nil
end
|
#compile_import_file(filepath, import_role) ⇒ Object
redefine in subsclass for different behaviour
183
184
185
186
187
188
|
# File 'lib/activefacts/cql/compiler.rb', line 183
def compile_import_file filepath, import_role
File.open(filepath) do |f|
compile_import_input(f.read, import_role)
end
end
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/activefacts/cql/compiler.rb', line 190
def compile_import_input input, import_role
topic_external_name = @file
if existing_topic = @constellation.Topic[[topic_external_name]]
trace :import, "Topic #{@topic.topic_name} has already been loaded, skip reload"
import = @constellation.Import(
topic: @topic, precursor_topic: existing_topic,
import_role: import_role, file_name: topic_external_name
)
else
saved_topic = @topic
topic_flood() if @topic
@pending_import_topic = saved_topic
@pending_import_role = import_role
@pending_import_file_name = topic_external_name
trace :import, "Importing #{@filepath} into #{@topic.topic_name}" do
ok = parse_all(input, nil, &@block)
end
@topic = saved_topic
end
end
|
#create_import_if_pending(new_topic) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/activefacts/cql/compiler.rb', line 87
def create_import_if_pending new_topic
if @pending_import_topic
trace :import, "Topic #{@pending_import_topic.topic_name} imports #{new_topic.topic_name} as #{@pending_import_role} from file #{@pending_import_file_name}"
@constellation.Import(
topic: @pending_import_topic,
precursor_topic: new_topic,
import_role: @pending_import_role,
file_name: @pending_import_file_name
)
@pending_import_topic = nil
@pending_import_role = ''
@pending_import_file_name = ''
end
end
|
#detect_language ⇒ Object
Load the appropriate natural language module
65
66
67
68
69
|
# File 'lib/activefacts/cql/compiler.rb', line 65
def detect_language
@filepath =~ /.*\.(..)\.cql$/i
language_code = $1
@language = LANGUAGES[language_code] || 'English'
end
|
#import_filepath(old_filepath, file) ⇒ Object
redefine in subsclass for different behaviour
173
174
175
176
177
178
179
180
|
# File 'lib/activefacts/cql/compiler.rb', line 173
def import_filepath(old_filepath, file)
filepath = ''
EXTENSIONS.each do |extension|
filepath = File.dirname(old_filepath)+'/'+file+".#{extension}"
break if File.exist?(filepath)
end
filepath
end
|
#include_language ⇒ Object
71
72
73
74
75
76
|
# File 'lib/activefacts/cql/compiler.rb', line 71
def include_language
detect_language unless @langage
require 'activefacts/cql/parser/Language/'+@language
language_module = ActiveFacts::CQL.const_get(@language)
extend language_module
end
|
#topic_flood ⇒ Object
Mark any new Concepts as belonging to this topic
79
80
81
82
83
84
85
|
# File 'lib/activefacts/cql/compiler.rb', line 79
def topic_flood
@constellation.Concept.each do |key, concept|
next if concept.topic
trace :topic, "Colouring #{concept.describe} with #{@topic.topic_name}"
concept.topic = @topic
end
end
|
#unit?(s) ⇒ Boolean
220
221
222
223
224
225
|
# File 'lib/activefacts/cql/compiler.rb', line 220
def unit? s
name = @constellation.Name[s]
units = (!name ? [] : Array(name.unit) + Array(name.plural_named_unit)).uniq
trace :units, "Looking for unit #{s}, got #{units.map{|u|u.name}.inspect}"
units.size > 0
end
|