Class: Marty::RuleScriptSet
- Inherits:
-
Delorean::AbstractContainer
- Object
- Delorean::AbstractContainer
- Marty::RuleScriptSet
- Defined in:
- lib/marty/rule_script_set.rb
Instance Attribute Summary collapse
-
#pt ⇒ Object
readonly
Returns the value of attribute pt.
-
#sset ⇒ Object
readonly
Returns the value of attribute sset.
Class Method Summary collapse
- .body_lines ⇒ Object
- .body_start ⇒ Object
- .clear_cache ⇒ Object
- .grid_final_name(dgid) ⇒ Object
- .indent(s) ⇒ Object
- .node_name ⇒ Object
Instance Method Summary collapse
- #code_section_counts(ruleh) ⇒ Object
- #expand_grid_code(h, dgid, dgname, cache, extra_params) ⇒ Object
- #get_code(ruleh) ⇒ Object
- #get_engine(ruleh) ⇒ Object
- #get_parse_error_field(ruleh, exc) ⇒ Object
- #grid_code(ruleh) ⇒ Object
- #grid_init(ruleh) ⇒ Object
- #guard_code(ruleh) ⇒ Object
-
#initialize(pt) ⇒ RuleScriptSet
constructor
A new instance of RuleScriptSet.
- #paramify_h(h) ⇒ Object
- #parse_check(sname, body) ⇒ Object
- #result_code(ruleh) ⇒ Object
- #write_attr(k, v) ⇒ Object
- #write_code(attrs) ⇒ Object
Constructor Details
#initialize(pt) ⇒ RuleScriptSet
Returns a new instance of RuleScriptSet.
20 21 22 23 24 25 26 27 |
# File 'lib/marty/rule_script_set.rb', line 20 def initialize(pt) @pt = Mcfly.normalize_infinity(pt) # if pt is Infinity, we get a DEV Tag tag = Marty::Tag.cached_find_match(pt) @sset = Marty::ScriptSet.new(tag) super() end |
Instance Attribute Details
#pt ⇒ Object (readonly)
Returns the value of attribute pt.
2 3 4 |
# File 'lib/marty/rule_script_set.rb', line 2 def pt @pt end |
#sset ⇒ Object (readonly)
Returns the value of attribute sset.
2 3 4 |
# File 'lib/marty/rule_script_set.rb', line 2 def sset @sset end |
Class Method Details
.body_lines ⇒ Object
16 17 18 |
# File 'lib/marty/rule_script_set.rb', line 16 def self.body_lines self.body_start.count("\n") end |
.body_start ⇒ Object
13 14 15 |
# File 'lib/marty/rule_script_set.rb', line 13 def self.body_start "#{node_name}:\n" end |
.clear_cache ⇒ Object
4 5 6 |
# File 'lib/marty/rule_script_set.rb', line 4 def self.clear_cache @@engines, @@dengines, @@dengines_dt = {}, {}, nil end |
.grid_final_name(dgid) ⇒ Object
42 43 44 |
# File 'lib/marty/rule_script_set.rb', line 42 def self.grid_final_name(dgid) dgid.ends_with?("_grid") ? dgid + "_result" : dgid + "_grid_result" end |
.indent(s) ⇒ Object
178 179 180 |
# File 'lib/marty/rule_script_set.rb', line 178 def self.indent(s) s.gsub(/^/, ' '*4) end |
.node_name ⇒ Object
10 11 12 |
# File 'lib/marty/rule_script_set.rb', line 10 def self.node_name "Node" end |
Instance Method Details
#code_section_counts(ruleh) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/marty/rule_script_set.rb', line 117 def code_section_counts(ruleh) errs = {} errs[:grid_params] = grid_init(ruleh).count("\n") errs[:computed_guards] = guard_code(ruleh).count("\n") errs[:grids] = grid_code(ruleh).count("\n") errs[:results] = result_code(ruleh).count("\n") errs end |
#expand_grid_code(h, dgid, dgname, cache, extra_params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/marty/rule_script_set.rb', line 45 def (h, dgid, dgname, cache, extra_params) final_name = self.class.grid_final_name(dgid) if cache[dgname] h[final_name] = "#{cache[dgname]}" else h[dgid] = dgname h["#{dgid}_dgp__"] = "dgparams__ + \n" + self.class.indent(paramify_h(h)) lgde = "lookup_grid_h" h[final_name] = "Marty::DataGrid.#{lgde}(pt,#{dgid},#{dgid}_dgp__,true)" cache[dgname] = final_name end end |
#get_code(ruleh) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/marty/rule_script_set.rb', line 98 def get_code(ruleh) grid_i = grid_init(ruleh) grid_c = grid_code(ruleh) result_c = result_code(ruleh) guard_c = guard_code(ruleh) code = self.class.body_start + self.class.indent(grid_i + guard_c + grid_c + result_c) #puts '='*40 #puts ruleh["name"] #puts '-' #puts code #puts '-'*10 code end |
#get_engine(ruleh) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/marty/rule_script_set.rb', line 136 def get_engine(ruleh) begin # if rule is a str => importing a regular Script (i.e. not rule) return sset.get_engine(ruleh) if ruleh.is_a? String # on create rule doesn't have an id => don't cache return sset.parse_check("New RULE #{ruleh['name']}", get_code(ruleh)) unless ruleh["id"] rule_pfx = ruleh["classname"].demodulize # unique name for specific version of rule sname = "#{rule_pfx}_#{ruleh['group_id']}_#{ruleh['created_dt'].to_f}" # is it a dev posting? if Mcfly.is_infinity(pt) max_dt = ruleh["classname"].constantize.order("created_dt DESC"). limit(1).pluck(:created_dt).first @@dengines_dt ||= max_dt # reset dengine cache if an rule has changed @@dengines = {} if max_dt > @@dengines_dt engine = @@dengines[sname] return engine if engine @@dengines[sname] = sset.parse_check(sname, get_code(ruleh)) else engine = @@engines[[pt, sname]] return engine if engine @@engines[[pt, sname]] = sset.parse_check(sname, get_code(ruleh)) end rescue Delorean::ParseError => e f = get_parse_error_field(ruleh, e) raise "Error in rule '#{ruleh['name']}' field '#{f}': #{e}" end end |
#get_parse_error_field(ruleh, exc) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/marty/rule_script_set.rb', line 125 def get_parse_error_field(ruleh, exc) line = exc.line ? exc.line - self.class.body_lines : 0 errs = code_section_counts(ruleh) line_count = 0 errs.each do |k,v| line_count += v return k if line <= line_count end errs.keys.last end |
#grid_code(ruleh) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/marty/rule_script_set.rb', line 70 def grid_code(ruleh) dgcache = {} h = {} ruleh["grids"].each do |k, v| (h, k.ends_with?('_grid')?k:k+'_grid', %Q("#{v}"), dgcache, {}) end h.map { |k, v| write_attr(k, v) }.join("\n") + "\n" end |
#grid_init(ruleh) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/marty/rule_script_set.rb', line 88 def grid_init(ruleh) if ruleh["grids"].present? || ruleh["results"].keys.any?{|k|k.ends_with?("_grid")} write_code({ "pt" => :parameter, "dgparams__" => :parameter, }) else '' end end |
#guard_code(ruleh) ⇒ Object
80 81 82 |
# File 'lib/marty/rule_script_set.rb', line 80 def guard_code(ruleh) write_code(ruleh["computed_guards"]) end |
#paramify_h(h) ⇒ Object
37 38 39 40 |
# File 'lib/marty/rule_script_set.rb', line 37 def paramify_h(h) "{" + h.keys.reject{|k|k.ends_with?("__")}. map {|k| %Q("#{k}": #{k}) }.join(",\n") + "}" end |
#parse_check(sname, body) ⇒ Object
29 30 31 |
# File 'lib/marty/rule_script_set.rb', line 29 def parse_check(sname, body) sset.parse_check(sname, body) end |
#result_code(ruleh) ⇒ Object
84 85 86 |
# File 'lib/marty/rule_script_set.rb', line 84 def result_code(ruleh) write_code(ruleh["results"]) end |
#write_attr(k, v) ⇒ Object
33 34 35 |
# File 'lib/marty/rule_script_set.rb', line 33 def write_attr(k, v) k + (v == :parameter ? " =?" : " = #{v}") end |
#write_code(attrs) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/marty/rule_script_set.rb', line 58 def write_code(attrs) return '' if attrs.blank? newh = attrs.each_with_object({}) do |(k, v), h| if k.ends_with?("_grid") (h, k, v, {}, h) else h[k] = v end end newh.map { |k, v| write_attr(k, v) }.join("\n") + "\n" end |