Class: ActiveFacts::CQL::Compiler::Query

Inherits:
ObjectType show all
Defined in:
lib/activefacts/cql/compiler/fact_type.rb

Overview

A fact type which is objectified (whether derived or not) is also an ObjectType

Direct Known Subclasses

FactType

Instance Attribute Summary collapse

Attributes inherited from ObjectType

#name

Attributes inherited from Definition

#constellation, #tree, #vocabulary

Instance Method Summary collapse

Methods inherited from Definition

#all_bindings_in_clauses, #build_all_steps, #build_step, #build_variables, #source

Constructor Details

#initialize(name, conditions = nil, returning = nil) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 9

def initialize name, conditions = nil, returning = nil
  super name
  @conditions = conditions
  @returning = returning || []
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



7
8
9
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 7

def conditions
  @conditions
end

#contextObject (readonly)

Exposed for testing purposes



6
7
8
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 6

def context
  @context
end

Instance Method Details

#compileObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 36

def compile
  # Match roles with players, and match clauses with existing fact types
  prepare_roles unless @context

  @context.left_contraction_allowed = true
  match_condition_fact_types

  # Build the query:
  unless @conditions.empty? and @returning.empty?
    trace :query, "building query for derived fact type (returning #{@returning}) with #{@conditions.size} conditions: (#{@conditions.map{|c|c.inspect}*', '})" do
      @query = build_variables(@conditions.flatten)
      @roles_by_binding = build_all_steps(@query, @conditions)
      @query.validate
      @query
    end
  end
  @context.left_contraction_allowed = false
  @query
end

#detect_projection_by_equality(condition) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 65

def detect_projection_by_equality condition
  return false unless condition.is_a?(Comparison)
  if is_projected_role(condition.e1)
    condition.project :left
  elsif is_projected_role(condition.e2)
    condition.project :right
  end
end

#inspectObject



19
20
21
22
23
24
25
26
27
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 19

def inspect
  "Query: " +
    if @conditions.empty?
      ''
    else
      'where ' + @conditions.map{|c| ((j=c.conjunction) ? j+' ' : '') + c.inspect}*' '
    end
  # REVISIT: @returning = returning
end

#is_projected_role(rr) ⇒ Object



74
75
76
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 74

def is_projected_role(rr)
  false
end

#match_condition_fact_typesObject



56
57
58
59
60
61
62
63
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 56

def match_condition_fact_types
  @conditions.each do |condition|
    trace :projection, "matching condition fact_type #{condition.inspect}" do
      fact_type = condition.match_existing_fact_type @context
      raise "Unrecognised fact type #{condition.inspect} in #{self.class}" unless fact_type
    end
  end
end

#prepare_roles(clauses = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 29

def prepare_roles clauses = nil
  trace :binding, "preparing roles" do
    @context ||= CompilationContext.new(@vocabulary)
    @context.bind clauses||[], @conditions, @returning
  end
end

#to_sObject



15
16
17
# File 'lib/activefacts/cql/compiler/fact_type.rb', line 15

def to_s
  inspect
end