Class: OMF::Rete::Planner::PlanBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/omf_rete/planner/plan_builder.rb

Overview

This class builds all the possible plans for a given query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, store, opts = {}) ⇒ PlanBuilder

query – query consists of an array of tuple paterns with binding declarations store – store to attach source sets to



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/omf_rete/planner/plan_builder.rb', line 36

def initialize(query, store, opts = {})
  @store = store
  @opts = opts

  _parse_query(query)

  @complete_plans = []
  if (@source_cnt == 1)
    # only one source means a trivial plan, the source itself
    @complete_plans = @sources.to_a
  end

end

Instance Attribute Details

#planObject (readonly)

Returns the value of attribute plan.



31
32
33
# File 'lib/omf_rete/planner/plan_builder.rb', line 31

def plan
  @plan
end

#storeObject (readonly)

Returns the value of attribute store.



31
32
33
# File 'lib/omf_rete/planner/plan_builder.rb', line 31

def store
  @store
end

Instance Method Details

#best_planObject

Return plan with lowest cost



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/omf_rete/planner/plan_builder.rb', line 69

def best_plan()
  # best_plan = nil
  # lowest_cost = 9999999999
#
  # each_plan do |plan|
    # cost = plan.cost
    # if (cost < lowest_cost)
      # lowest_cost = cost
      # best_plan = plan
    # end
  # end
  best_plan = @complete_plans.min do |p1, p2|
    p1.cost <=> p2.cost
  end
  best_plan
end

#buildObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omf_rete/planner/plan_builder.rb', line 50

def build()
  level = 0
  maxLevels = @source_cnt + 10 # pull the emergency breaks sometimes
  while (@complete_plans.empty? && level < maxLevels) do
    _iterate()
    level += 1
  end
  if (@complete_plans.empty?)
    raise PlannerException.new("Can't create plan")
  end
  @complete_plans
end

#describe(out = STDOUT, offset = 0, incr = 2, sep = "\n") ⇒ Object



121
122
123
124
125
126
127
# File 'lib/omf_rete/planner/plan_builder.rb', line 121

def describe(out = STDOUT, offset = 0, incr = 2, sep = "\n")
  out << "\n=========\n"
  @complete_plans.each do |p|
    out << "------\n"
    p.describe(out, offset, incr, sep)
  end
end

#each_planObject



63
64
65
# File 'lib/omf_rete/planner/plan_builder.rb', line 63

def each_plan()
  @complete_plans.each do |p| yield(p) end
end

#materialize(projectPattern = nil, plan = nil, opts = nil, &block) ⇒ Object

Materialize the plan. Create all the relevant operations and tuple sets to realize a configuration for the respective query. Returns the result set.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/omf_rete/planner/plan_builder.rb', line 91

def materialize(projectPattern = nil, plan = nil, opts = nil, &block)
  unless block
    raise PlannerException.new("Missing block to process result tuples with")
  end
  unless plan
    plan = best_plan()
  end
  unless plan
    raise PlannerException.new("No plan to materialize");
  end
  if (plan.is_a?(SourcePlan))
    # This is really just a simple pattern on the store
    #plan = SimpleSourcePlan.new(plan)
    #plan.materialize(projectPattern, opts, &block)
    endS = _materialize_simple_plan(projectPattern, plan, opts, &block)
  else
    # this is the root of the plan
    if projectPattern
      description = projectPattern
    else
      description = plan.result_set.to_a.sort
    end
    frontS, endS = _materialize_result_stream(plan, projectPattern, opts, &block)
    plan.materialize(nil, frontS, opts, &block)
    #endS
  end
  FinalPlan.new(endS)
end