Class: Twb::FieldCalculation

Inherits:
Object
  • Object
show all
Defined in:
lib/twb/fieldcalculation.rb

Constant Summary collapse

@@tableCalcs =
[ 'FIRST',        'INDEX', 'LAST', 'SIZE',
  'LOOKUP',       'PREVIOUS_VALUE',
  'RANK',         'RANK_DENSE',    'RANK_MODIFIED', 'RANK_PERCENTILE', 'RANK_UNIQUE',
  'RUNNING_AVG',  'RUNNING_COUNT', 'RUNNING_MAX',   'RUNNING_MIN',     'RUNNING_SUM',
  'SCRIPT_BOOL',  'SCRIPT_INT',    'SCRIPT_REAL',   'SCRIPT_STR',
  'TOTAL',
  'WINDOW_SUM',   'WINDOW_AVG',    'WINDOW_COUNT',  
  'WINDOW_MIN',   'WINDOW_MEDIAN', 'WINDOW_MAX',   'WINDOW_PERCENTILE',
  'WINDOW_STDEV', 'WINDOW_STDEVP',
  'WINDOW_CORR',  'WINDOW_COVAR',  'WINDOW_COVARP', 
  'WINDOW_VAR',   'WINDOW_VARP'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calcField, datasource = nil) ⇒ FieldCalculation

Returns a new instance of FieldCalculation.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/twb/fieldcalculation.rb', line 50

def initialize(calcField, datasource=nil)
  raise ArgumentError.new("FieldCalculation must be initialized with a CalculatedField, has been provided with a #{calcField.class}") if calcField.class != Twb::CalculatedField
  @field = calcField
  calcNode = calcField.node
  @istableCalc = false
  # puts "FieldCalculation calcNode.nil? :: #{calcNode.nil?} "
  unless calcNode.nil?
    @node         = calcNode.at_xpath('./calculation')
    @fieldNode    = calcField.node
    @dataSource   = datasource
    @class        = attribText(@node, 'class')
    @remoteFields = {}
    if 'categorical-bin'.eql? @class
      # <calculation class='categorical-bin' 
      #              column='[Calculation_507569757376950272]' 
      #              default='&quot;Other&quot;' 
      #              new-bin='true'>
      @techname  = @node.attribute('column').text.gsub(/^\[|\]$/,'')   # assumes the column attribute exists
      @caption   = attribText(@fieldNode, 'caption')
      uiname     = if datasource.nil?
                      @techName
                   else
                      datasource.fieldUIName(@techname)
                   end
      @uiname    = "#{uiname} <<group>>"
      @has_formula  = true
      @formula      = "grouped <<#{uiname}>> values"
      @formulaLines = [ @formula ] 
      @formulaFlat  = @formula 
      @comments     = [ ]
      @is_lod       = false
    else
      @caption   = calcField.caption
      @techname  = calcField.name
      @uiname    = calcField.uiname
      #--
      @scopeIsolation = attribText(@node, 'scope-isolation') 
      #-- Formula --
      @has_formula    = @node.has_attribute?('formula')
      if @has_formula
        @formula      = @node.attribute('formula').text.gsub(/\r\n/,"\n")
        @formulaUC    = @formula.upcase
        @formulaLines = formula.split(/\n|\r\n/)
        @formulaFlat  = flattenFormula(@formulaLines)
        @comments     = getComments(@formulaLines)
        @lodCodePos   = @formula =~ /^[ ]*{[ ]*(fixed|include|exclude)[ ]*/i
        @is_lod       = !lodCodePos.nil? && @lodCodePos >= 0
        @is_tableCalc = @@tableCalcs.any? { |tc| @formulaUC.include?(tc) }  #assessTableCalc @formula
        # puts "#{@lodCodePos} \t #{@is_lod} \t #{@is_lod.class} \t => #{@formula}"
      end
    end
  end
end

Instance Attribute Details

#calcFieldsObject (readonly)

Returns the value of attribute calcFields.



34
35
36
# File 'lib/twb/fieldcalculation.rb', line 34

def calcFields
  @calcFields
end

#captionObject (readonly)

Returns the value of attribute caption.



23
24
25
# File 'lib/twb/fieldcalculation.rb', line 23

def caption
  @caption
end

#classObject (readonly)

Returns the value of attribute class.



33
34
35
# File 'lib/twb/fieldcalculation.rb', line 33

def class
  @class
end

#commentsObject (readonly)

Returns the value of attribute comments.



35
36
37
# File 'lib/twb/fieldcalculation.rb', line 35

def comments
  @comments
end

#dataSourceObject (readonly)

Returns the value of attribute dataSource.



22
23
24
# File 'lib/twb/fieldcalculation.rb', line 22

def dataSource
  @dataSource
end

#fieldObject (readonly)

Returns the value of attribute field.



22
23
24
# File 'lib/twb/fieldcalculation.rb', line 22

def field
  @field
end

#fieldNodeObject (readonly)

Returns the value of attribute fieldNode.



22
23
24
# File 'lib/twb/fieldcalculation.rb', line 22

def fieldNode
  @fieldNode
end

#fieldsObject (readonly)

Returns the value of attribute fields.



34
35
36
# File 'lib/twb/fieldcalculation.rb', line 34

def fields
  @fields
end

#formulaObject (readonly)

Returns the value of attribute formula.



25
26
27
# File 'lib/twb/fieldcalculation.rb', line 25

def formula
  @formula
end

#formulaFlatObject (readonly)

Returns the value of attribute formulaFlat.



28
29
30
# File 'lib/twb/fieldcalculation.rb', line 28

def formulaFlat
  @formulaFlat
end

#formulaFlatResolvedObject (readonly)

Returns the value of attribute formulaFlatResolved.



29
30
31
# File 'lib/twb/fieldcalculation.rb', line 29

def formulaFlatResolved
  @formulaFlatResolved
end

#formulaLinesObject (readonly)

Returns the value of attribute formulaLines.



30
31
32
# File 'lib/twb/fieldcalculation.rb', line 30

def formulaLines
  @formulaLines
end

#formulaResolvedObject (readonly)

Returns the value of attribute formulaResolved.



27
28
29
# File 'lib/twb/fieldcalculation.rb', line 27

def formulaResolved
  @formulaResolved
end

#formulaResolvedLinesObject (readonly)

Returns the value of attribute formulaResolvedLines.



30
31
32
# File 'lib/twb/fieldcalculation.rb', line 30

def formulaResolvedLines
  @formulaResolvedLines
end

#formulaUCObject (readonly)

Returns the value of attribute formulaUC.



26
27
28
# File 'lib/twb/fieldcalculation.rb', line 26

def formulaUC
  @formulaUC
end

#has_formulaObject (readonly)

Returns the value of attribute has_formula.



24
25
26
# File 'lib/twb/fieldcalculation.rb', line 24

def has_formula
  @has_formula
end

#is_lodObject (readonly)

Returns the value of attribute is_lod.



32
33
34
# File 'lib/twb/fieldcalculation.rb', line 32

def is_lod
  @is_lod
end

#is_tableCalcObject (readonly)

Returns the value of attribute is_tableCalc.



31
32
33
# File 'lib/twb/fieldcalculation.rb', line 31

def is_tableCalc
  @is_tableCalc
end

#lodCodePosObject (readonly)

Returns the value of attribute lodCodePos.



32
33
34
# File 'lib/twb/fieldcalculation.rb', line 32

def lodCodePos
  @lodCodePos
end

#nodeObject (readonly)

Returns the value of attribute node.



22
23
24
# File 'lib/twb/fieldcalculation.rb', line 22

def node
  @node
end

#remoteFieldsObject (readonly)

Returns the value of attribute remoteFields.



34
35
36
# File 'lib/twb/fieldcalculation.rb', line 34

def remoteFields
  @remoteFields
end

#scopeIsolationObject (readonly)

Returns the value of attribute scopeIsolation.



33
34
35
# File 'lib/twb/fieldcalculation.rb', line 33

def scopeIsolation
  @scopeIsolation
end

#technameObject (readonly)

Returns the value of attribute techname.



23
24
25
# File 'lib/twb/fieldcalculation.rb', line 23

def techname
  @techname
end

#uinameObject (readonly)

Returns the value of attribute uiname.



23
24
25
# File 'lib/twb/fieldcalculation.rb', line 23

def uiname
  @uiname
end

Instance Method Details

#attribText(node, attribute) ⇒ Object

def assessTableCalc formula

@@tableCalcs.any? { |tc| string.include?(tc) }

end



108
109
110
# File 'lib/twb/fieldcalculation.rb', line 108

def attribText(node, attribute)
  node.attribute(attribute).nil? ? nil : node.attribute(attribute).text
end

#enlineResolvedFormulaObject



156
157
# File 'lib/twb/fieldcalculation.rb', line 156

def enlineResolvedFormula
end

#flattenFormula(lines) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/twb/fieldcalculation.rb', line 168

def flattenFormula lines
  formula = ''
  lines.each do |line|
      line.strip
      formula += ' ' + line.gsub(/\/\/.*/, '') # unless line =~ /^[ ]*\/\//
  end
  return formula.strip
end

#flattenResolvedFormulaObject



163
164
165
166
# File 'lib/twb/fieldcalculation.rb', line 163

def flattenResolvedFormula
  formula = formulaResolved
  formula.gsub(/\n/, '  ')
end

#getComments(lines) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/twb/fieldcalculation.rb', line 178

def getComments lines
  comments = ''
  lines.each do |line|
    if line =~ /\/\// then
      comments += ' ' + line.gsub(/^.*\/\//,'// ')
    end
  end
  return comments.strip
end

#parseFormFieldsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/twb/fieldcalculation.rb', line 113

def parseFormFields
  @fields     = Set.new []
  @calcFields = SortedSet.new []
  formula = @formulaFlat
  if !formula.nil? && formula.include?('[') && formula.include?(']')
      noSqLits =  formula.gsub( /'[\[\.\]]+'/, ' ')
      flatForm = noSqLits.gsub( /\n/, ' ')
      stripFrt = flatForm.gsub( /^[^\[]*[\[]/    , '['      )
      stripBck = stripFrt.gsub( /\][^\]]+$/      , ']'      )
      stripMid = stripBck.gsub( /\][^\]]{2,}\[/  , ']]..[[' )
      stripCom = stripMid.gsub( /\][ ]*,[ ]*\[/  , ']]..[[' )
      stripFns = stripMid.gsub( /\][ ]*[\*\/+\-><,=][ ]*\[/  , ']]..[[' )
      fields   = stripFns.split(']..[')
      fields.each { |field| @fields.add field.gsub(/^\[|\]$/, '')}
      fields.each do |field| 
        cf = CalculationField.new( field.gsub(/^\[|\]$/, ''), @dataSource, @dataSource.workbook )
        @calcFields.add cf
      end
  end
end

#resolveFormulaObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/twb/fieldcalculation.rb', line 138

def resolveFormula
  formula = @formula
  parseFormFields # - extracts the fields from the formula; as persisted they're the internal names
 # puts "::++ #{formula}"
  @calcFields.each do |calcField|
    if calcField.techUIdiff
     # puts ":::: #{calcField.techCode}  //  #{calcField.uiCode}"
      formula = formula.gsub(calcField.techCode,calcField.uiCode)
     # puts ":--: #{formula}"
    end
  end
  return formula
end