Class: ParseDecision::Plugin::PreDecisionGuideline

Inherits:
Plugin
  • Object
show all
Defined in:
lib/parse_decision/plugin/pre_decision_guideline.rb

Overview

####################################################################### Pre-Decision Guideline XML plugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#apply_template, #apply_templates, #write_to_file

Constructor Details

#initializePreDecisionGuideline

Returns a new instance of PreDecisionGuideline.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 23

def initialize()
  $LOG.debug "PreDecisionGuideline::initialize"
  @fnameTemplate    = "@INDEX@-@[email protected]"
  @searchStrPpms    = "<PARAMS><_DATA_SET"
  @searchStrGdl     = "<Guideline "
  @searchStrGdl2    = "******Guideline*"
  @searchStrGdlEnd  = "<Decision GuidelineId"
  @searchRulesEnd   = "</Decision>"
  @ppmData      = ""
  @ruleData     = []

  @openTag      = "<@TAG@_DATA>\n"
  @closeTag     = "</@TAG@_DATA>\n"
  @actualCloseTag = ""
  @lineCount    = 0
  @chunkSize    = 1000
  @outfile      = "PreDecision"
end

Instance Attribute Details

#outfileObject (readonly)

Returns the value of attribute outfile.



21
22
23
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 21

def outfile
  @outfile
end

#ppmDataObject (readonly)

Returns the value of attribute ppmData.



20
21
22
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 20

def ppmData
  @ppmData
end

Instance Method Details

#check_for_ppms_or_rule_start(context, ln) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 57

def check_for_ppms_or_rule_start(context, ln)
  if ln.include?(@searchStrPpms)
    @ppmData = ln
    # XML Tidy doesn't like underscores at the beginning attribute names, take care of it here.
    @ppmData.gsub!(/_DATA_SET/, "DATA_SET")
    @ppmData.gsub!(/_Name/, "Name")
    @ppmData.gsub!(/_Value/, "Value")
    return true
  end

  if ln.include?(@searchStrGdl) || ln.include?(@searchStrGdl2)
    context.state = :preDecisionGdl
    @ruleData.clear
    open_discard_element
    @ruleData << ln
    return true
  end

  return false
end

#close_discard_elementObject



90
91
92
93
94
95
96
97
98
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 90

def close_discard_element
  # Only output the closing element if discarding is actually on.
  # This could get called multiple times after discarding has been turned off.
  if !@discarding.nil? && @discarding
    @discarding = false
    @ruleData << "-->\n"   # The leading element tag is not valid XML (no quotes around attrib params).
    @ruleData << "</DISCARD_BY_PARSER>\n"
  end
end

#close_out_rules_file(context, ln) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 149

def close_out_rules_file(context, ln)
  @ruleData << ln

  File.open(context.outputPath(@outfile), "a") do |f|
    write_to_file(f,@ruleData)
    write_to_file(f, @actualCloseTag)
  end
end

#execute(context, ln) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 42

def execute(context, ln)
  #$LOG.debug "PreDecisionGuideline::execute"

  case context.state
  when :app
    return check_for_ppms_or_rule_start context, ln

  when :preDecisionGdl
    return process_rule_data context, ln

  else
    return false
  end # case
end

#open_discard_elementObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 78

def open_discard_element
  # The log can contain a lot of junk here. This prevents it from being a valid
  # XML doc. To account for this, we'll create a DISCARD element for easy folding
  # and put everything into an HTML comment block.

  # We need to track when discarding is on, so we only turn it off once.
  @discarding = true

  @ruleData << "<DISCARD_BY_PARSER>\n"
  @ruleData << "<!-- "   # The leading element tag is not valid XML (no quotes around attrib params).
end

#process_rule_data(context, ln) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 100

def process_rule_data(context, ln)
  # Create the rules data file if the Guideline end tag is found
  if ln.include?(@searchStrGdlEnd)
    close_discard_element
    setup_rules_file context, ln
    return true
  end

  # Close the rules data file if the rules end tag is found
  if ln.include?(@searchRulesEnd)
    close_out_rules_file context, ln
    context.state = :app
    return true
  end

  # Haven't found the start or end of the rules here,
  # we must be somewhere in the middle.
  # Store the data so it can be written to file later.
  write_rules_data context, ln

  return true
end

#setup_rules_file(context, ln) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 123

def setup_rules_file(context, ln)
  @ruleData << ln

  # Default guideline name
  gdlName = "PreDecision"

  # String#match acts weird so using RegEx/MatchData here.
  m = /\sGuidelineName="([^"]+)/.match(ln)
  if m[1].length > 1
    gdlName = m[1]
    gdlName = context.createValidName(gdlName)
  end

  @outfile = apply_templates(@fnameTemplate, {"@INDEX@"=>context.indexStr, "@GDL@"=>gdlName})

  # Store the closing tag for later.
  @actualCloseTag = apply_template(@closeTag, "@TAG@", gdlName)

  puts "Creating Gdl Rules file: #{@outfile}" if context.verbose

  File.open(context.outputPath(@outfile), "w") do |f|
    write_to_file(f, apply_template(@openTag, "@TAG@", gdlName))
    write_to_file(f,@ppmData)
  end
end

#write_rules_data(context, ln) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/parse_decision/plugin/pre_decision_guideline.rb', line 158

def write_rules_data(context, ln)
  @ruleData << ln
  @lineCount += 1

  if(@lineCount > @chunkSize)
    puts "Writing rule data chunk." if context.verbose
    File.open(context.outputPath(@outfile), "a") do |f|
      write_to_file(f,@ruleData)
    end
    @lineCount = 0
    @ruleData.clear
  end
end