From 2fd735934ecb0cbc3cd47aedebbba5f783ab9c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 29 May 2013 16:49:35 +0200 Subject: [PATCH] Feature #1852: Improve grammar * Now grammar will look for attributes inside /VM/USER_TEMPLATE, /VM, and /VM/TEMPLATE, in that order * VMs without the attribute are not included in the average calc. * If the attribute is not found, the condition is false, instead of assuming a value of 0 --- flow/lib/grammar.rb | 37 +++++++++++++++++++++++++++++++------ flow/lib/grammar.treetop | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/flow/lib/grammar.rb b/flow/lib/grammar.rb index 1b44319df8..bdbf588cfd 100644 --- a/flow/lib/grammar.rb +++ b/flow/lib/grammar.rb @@ -294,7 +294,17 @@ module ElasticityGrammar module LogicCond1 def result(role) - comp_op.apply(left.result(role), right.result(role)) + l_res = left.result(role) + r_res = right.result(role) + + if l_res.nil? || r_res.nil? + # An attribute was not found, we return false instead + # of assuming a value of 0 + + return false + else + comp_op.apply(l_res, r_res) + end end end @@ -849,20 +859,35 @@ module ElasticityGrammar def result(role) nodes = role.get_nodes total = 0 + n_nodes = 0 + att = text_value.upcase # TODO: search in 'TEMPLATE' also? # TODO: what to do if value is not found nodes.each { |node| - if node && node['vm_info'] && node['vm_info']['VM']['USER_TEMPLATE'][text_value.upcase] - # TODO: Check value is numeric - total += (node['vm_info']['VM']['USER_TEMPLATE'][text_value.upcase]).to_f + if node && node['vm_info'] + if node['vm_info']['VM']['USER_TEMPLATE'][att] + total += (node['vm_info']['VM']['USER_TEMPLATE'][att]).to_f + n_nodes += 1 + elsif node['vm_info']['VM'][att] + total += (node['vm_info']['VM'][att]).to_f + n_nodes += 1 + elsif node['vm_info']['VM']['TEMPLATE'][att] + total += (node['vm_info']['VM']['TEMPLATE'][att]).to_f + n_nodes += 1 + end end } - Log.debug "ELAS", "Role #{role.name} attribute #{text_value.upcase} avg value of #{total / nodes.size}" + # The attribute wasn't found for any of the nodes + if n_nodes == 0 + return nil + end - return total / nodes.size + Log.debug "ELAS", "Role #{role.name} attribute #{att} avg value of #{total / n_nodes}" + + return total / n_nodes end end diff --git a/flow/lib/grammar.treetop b/flow/lib/grammar.treetop index f48bda6716..e290dc6c5f 100644 --- a/flow/lib/grammar.treetop +++ b/flow/lib/grammar.treetop @@ -53,7 +53,17 @@ grammar ElasticityGrammar # / left:operand space comp_op space right:operand { def result(role) - comp_op.apply(left.result(role), right.result(role)) + l_res = left.result(role) + r_res = right.result(role) + + if l_res.nil? || r_res.nil? + # An attribute was not found, we return false instead + # of assuming a value of 0 + + return false + else + comp_op.apply(l_res, r_res) + end end } / @@ -145,20 +155,35 @@ grammar ElasticityGrammar def result(role) nodes = role.get_nodes total = 0 + n_nodes = 0 + att = text_value.upcase # TODO: search in 'TEMPLATE' also? # TODO: what to do if value is not found nodes.each { |node| - if node && node['vm_info'] && node['vm_info']['VM']['USER_TEMPLATE'][text_value.upcase] - # TODO: Check value is numeric - total += (node['vm_info']['VM']['USER_TEMPLATE'][text_value.upcase]).to_f + if node && node['vm_info'] + if node['vm_info']['VM']['USER_TEMPLATE'][att] + total += (node['vm_info']['VM']['USER_TEMPLATE'][att]).to_f + n_nodes += 1 + elsif node['vm_info']['VM'][att] + total += (node['vm_info']['VM'][att]).to_f + n_nodes += 1 + elsif node['vm_info']['VM']['TEMPLATE'][att] + total += (node['vm_info']['VM']['TEMPLATE'][att]).to_f + n_nodes += 1 + end end } - Log.debug "ELAS", "Role #{role.name} attribute #{text_value.upcase} avg value of #{total / nodes.size}" + # The attribute wasn't found for any of the nodes + if n_nodes == 0 + return nil + end - return total / nodes.size + Log.debug "ELAS", "Role #{role.name} attribute #{att} avg value of #{total / n_nodes}" + + return total / n_nodes end } end