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/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
Defined Under Namespace
Classes: Binding, Clause, ClauseMatchSideEffect, ClauseMatchSideEffects, Comparison, CompilationContext, Constraint, ContextNote, Definition, Enforcement, EntityType, Fact, FactType, Import, Literal, Negate, ObjectType, Operation, PresenceConstraint, Product, Quantifier, Query, Reciprocal, Reference, ReferenceMode, RingConstraint, SetComparisonConstraint, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, SubsetConstraint, Sum, Unit, ValueConstraint, ValueType, Vocabulary
Constant Summary
collapse
- LANGUAGES =
{
'en' => 'English',
'fr' => 'French',
'cn' => 'Mandarin'
}
Constants included
from ActiveFacts
VERSION
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Parser
#context, #parse, #parse_all
Constructor Details
#initialize(*a) ⇒ Compiler
Returns a new instance of Compiler.
27
28
29
30
31
32
33
|
# File 'lib/activefacts/cql/compiler.rb', line 27
def initialize *a
@filename = a.shift || "stdio"
super *a
@constellation = ActiveFacts::API::Constellation.new(ActiveFacts::Metamodel)
@language = nil
trace :file, "Parsing '#{@filename}'"
end
|
Instance Attribute Details
#vocabulary ⇒ Object
Returns the value of attribute vocabulary.
25
26
27
|
# File 'lib/activefacts/cql/compiler.rb', line 25
def vocabulary
@vocabulary
end
|
Instance Method Details
#compile(input) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/activefacts/cql/compiler.rb', line 68
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
@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
143
144
145
|
# File 'lib/activefacts/cql/compiler.rb', line 143
def compile_definition ast
ast.compile
end
|
#compile_file(filename) ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/activefacts/cql/compiler.rb', line 35
def compile_file filename
old_filename = @filename
@filename = filename
File.open(filename) do |f|
compile(f.read)
end
@filename = old_filename
@vocabulary
end
|
#compile_import(file, aliases) ⇒ Object
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
|
# File 'lib/activefacts/cql/compiler.rb', line 111
def compile_import file, aliases
saved_index = @index
saved_block = @block
saved_string = @string
saved_input_length = @input_length
saved_topic = @topic
old_filename = @filename
@filename = File.dirname(old_filename)+'/'+file+'.cql'
File.open(@filename) do |f|
topic_flood if @topic
@topic = @constellation.Topic(File.basename(@filename, '.cql'))
trace :import, "Importing #{@filename} as #{@topic.topic_name}" do
ok = parse_all(f.read, nil, &@block)
end
@topic = saved_topic
end
rescue => e
ne = StandardError.new("In #{@filename} #{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
@filename = old_filename
nil
end
|
#detect_language ⇒ Object
Load the appropriate natural language module
46
47
48
49
50
|
# File 'lib/activefacts/cql/compiler.rb', line 46
def detect_language
@filename =~ /.*\.(..)\.cql$/i
language_code = $1
@language = LANGUAGES[language_code] || 'English'
end
|
#include_language ⇒ Object
52
53
54
55
56
57
|
# File 'lib/activefacts/cql/compiler.rb', line 52
def include_language
detect_language unless @langage
require 'activefacts/cql/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
60
61
62
63
64
65
66
|
# File 'lib/activefacts/cql/compiler.rb', line 60
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
147
148
149
150
151
152
|
# File 'lib/activefacts/cql/compiler.rb', line 147
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
|