Module: FluentPath
- Defined in:
- lib/fluentpath/parse.rb,
lib/fluentpath/evaluate.rb,
lib/fluentpath/expression.rb
Defined Under Namespace
Classes: Expression
Constant Summary collapse
- @@reserved =
['all','not','empty','exists','where','select','extension','startsWith','contains','in','distinct','=','!=','<=','>=','<','>','and','or','xor','+','-','/','*','toInteger','implies','children','first','last','tail','count','substring']
- @@context =
Hash.new
- @@parent =
nil
Class Method Summary collapse
-
.atom(token) ⇒ Object
This method converts a token within an expression to a native number (if applicable) otherwise it considers it to be a symbol.
-
.build_tree(tokens) ⇒ Object
This method builds an Abstract Syntax Tree (AST) from a flat list of tokens.
-
.convertToBoolean(value) ⇒ Object
Convert nils and empty Arrays to false Everything else is true.
-
.eval(tree, data) ⇒ Object
evaluate a parsed expression given some context data.
-
.evaluate(expression, hash, parent = nil) ⇒ Object
This is the entry point to using the FluentPath class.
-
.get(key, hash) ⇒ Object
Get a value from a hash, with some special handling of self references.
- .parse(expression) ⇒ Object
- .reassemble_strings(tokens) ⇒ Object
-
.tokenize(expression) ⇒ Object
This method tokenizes the expression into a flat array of tokens.
Class Method Details
.atom(token) ⇒ Object
This method converts a token within an expression to a native number (if applicable) otherwise it considers it to be a symbol.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/fluentpath/parse.rb', line 94 def self.atom(token) # check if it is a number value = token begin value = Float(token) rescue value = token value = token.to_sym if @@reserved.include?(token) value = true if token=='true' value = false if token=='false' end value end |
.build_tree(tokens) ⇒ Object
This method builds an Abstract Syntax Tree (AST) from a flat list of tokens
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fluentpath/parse.rb', line 69 def self.build_tree(tokens) return if tokens.empty? tree = [] while tokens.length > 0 token = tokens.delete_at(0) if '(' == token # sub expression tree << FluentPath::Expression.new(build_tree(tokens)) elsif ')' == token return tree elsif '.' != token tree << atom(token) end end # post-processing tree.each_with_index do |token,index| if token==:extension # 'extension' can be a path or a function call (if followed by a block) next_token = tree[index+1] tree[index] = 'extension' if next_token.nil? || !next_token.is_a?(FluentPath::Expression) end end tree end |
.convertToBoolean(value) ⇒ Object
Convert nils and empty Arrays to false Everything else is true.
58 59 60 61 62 63 64 65 |
# File 'lib/fluentpath/evaluate.rb', line 58 def self.convertToBoolean(value) return false if value.nil? return false if value.is_a?(Array) && value.empty? return false if value.is_a?(Hash) && value.empty? return false if value==:null return false if value==false return true end |
.eval(tree, data) ⇒ Object
evaluate a parsed expression given some context data
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/fluentpath/evaluate.rb', line 68 def self.eval(tree,data) tree = tree.tree if tree.is_a?(FluentPath::Expression) # --------------- OPERATOR PRECEDENCE ------------------ #01 . (path/function invocation) #02 [] (indexer) #03 unary + and - #04: *, /, div, mod #05: +, -, #06: | #07: >, <, >=, <= #08: is, as #09: =, ~, !=, !~ #10: in, contains #11: and #12: xor, or #13: implies # evaluate all the data at this level functions = [:where,:select,:extension,:children,:first,:last,:tail] size = -1 substitutions = 1 while(tree.length!=size || substitutions > 0) substitutions = 0 FHIR.logger.debug "DATA: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(String) && !(node.start_with?("'") && node.end_with?("'")) array_index = nil if node.include?('[') && node.end_with?(']') array_index = node[node.index('[')..-1].gsub(/\[|\]/,'') t = get(array_index,data) t = array_index.to_i if(t.nil? || t==:null) array_index = t node = node[0..node.index('[')-1] end if previous_node.is_a?(Hash) || previous_node.is_a?(Array) tree[index] = get(node,previous_node) tree[previous_index] = nil if !previous_index.nil? elsif !previous_node.is_a?(FluentPath::Expression) tree[index] = get(node,data) end if array_index && tree[index].is_a?(Array) tree[index] = tree[index][array_index] end FHIR.logger.debug "V===> #{tree}" elsif node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) case node when :where # the previous node should be data (as Array or Hash) # the next node should be a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "Where function requires a block." end previous_node = [] if previous_node==:null if previous_node.is_a?(Array) previous_node.keep_if do |item| sub = eval(block.clone,item) convertToBoolean(sub) end tree[index] = previous_node tree[previous_index] = nil if !previous_index.nil? elsif previous_node.is_a?(Hash) sub = eval(block,previous_node) if convertToBoolean(sub) tree[index] = previous_node tree[previous_index] = nil if !previous_index.nil? else tree[index] = {} tree[previous_index] = nil if !previous_index.nil? end else raise "Where function not applicable to #{previous_node.class}: #{previous_node}" end break when :select # select is equivalent to ruby Array.map! # the previous node should be data (as Array or Hash) # the next node should be a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "Select function requires a block." end previous_node = [] if previous_node==:null if previous_node.is_a?(Array) previous_node.map! do |item| eval(block.clone,item) end tree[index] = previous_node tree[previous_index] = nil if !previous_index.nil? elsif previous_node.is_a?(Hash) tree[index] = eval(block,previous_node) tree[previous_index] = nil if !previous_index.nil? else raise "Select function not applicable to #{previous_node.class}: #{previous_node}" end break when :extension # the previous node should be a data (as Hash) # the next node optionally is a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "Extension function requires a block." end if previous_node.is_a?(Hash) FHIR.logger.debug "Evaling Extension Block...." exts = data['extension'] if exts.is_a?(Array) url = nil begin url = block.tree.first.gsub(/\'|\"/,'') rescue raise "Extension function requires a single URL as String." end ext = exts.select{|x|x['url']==url}.first tree[index] = ext tree[previous_index] = nil if !previous_index.nil? else raise "Extension function not applicable to #{exts.class}: #{exts}" end else raise "Extension not applicable to #{previous_node.class}: #{previous_node}" end break when :children # if there is a previous node, it should be data (as Hash) # otherwise, use the context as data if previous_node.is_a?(Hash) tree[index] = previous_node.values tree[previous_index] = nil if !previous_index.nil? substitutions+=1 elsif data.is_a?(Hash) tree[index] = data.values substitutions+=1 else raise "Children not applicable to #{previous_node.class}: #{previous_node}" end break when :first # the previous node should be an Array of length > 1 if previous_node.is_a?(Array) tree[index] = previous_node.first tree[previous_index] = nil if !previous_index.nil? else raise "First function is not applicable to #{previous_node.class}: #{previous_node}" end when :last # the previous node should be an Array of length > 1 if previous_node.is_a?(Array) tree[index] = previous_node.last tree[previous_index] = nil if !previous_index.nil? else raise "Last function is not applicable to #{previous_node.class}: #{previous_node}" end when :tail # the previous node should be an Array of length > 1 if previous_node.is_a?(Array) tree[index] = previous_node.last(previous_node.length-1) tree[previous_index] = nil if !previous_index.nil? else raise "Tail function is not applicable to #{previous_node.class}: #{previous_node}" end end FHIR.logger.debug "F===> #{tree}" end previous_index = index previous_node = tree[index] end FHIR.logger.debug "---------------------------------------------------" tree.compact! end tree.each_with_index do |node,index| tree[index] = node[1..-2] if node.is_a?(String) && node.start_with?("'") && node.end_with?("'") end FHIR.logger.debug "DATA: #{tree}" # evaluate all the functions at this level functions = [:all,:not,:empty,:exists,:startsWith,:substring,:contains,:in,:distinct,:toInteger,:count] size = -1 while(tree.length!=size) FHIR.logger.debug "FUNC: #{tree}" previous_node = data previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) case node when :all if previous_node.is_a?(Array) result = true previous_node.each{|item| result = (result && convertToBoolean(item))} tree[index] = result tree[previous_index] = nil if !previous_index.nil? else tree[index] = convertToBoolean(previous_node) tree[previous_index] = nil if !previous_index.nil? end when :not tree[index] = !convertToBoolean(previous_node) tree[previous_index] = nil if !previous_index.nil? when :count tree[index] = 0 tree[index] = 1 if !previous_node.nil? tree[index] = previous_node.length if previous_node.is_a?(Array) tree[previous_index] = nil if !previous_index.nil? when :empty tree[index] = (previous_node==:null || previous_node.empty? rescue previous_node.nil?) tree[previous_index] = nil if !previous_index.nil? when :exists tree[index] = !previous_node.nil? && previous_node!=:null tree[previous_index] = nil if !previous_index.nil? when :distinct tree[index] = (previous_node.uniq rescue previous_node) tree[previous_index] = nil if !previous_index.nil? when :startsWith # the previous node should be a data (as String) # the next node should be a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "StartsWith function requires a block." end if previous_node.is_a?(String) FHIR.logger.debug "Evaling StartsWith Block...." prefix = eval(block,data) tree[index] = previous_node.start_with?(prefix) rescue false tree[previous_index] = nil if !previous_index.nil? else raise "StartsWith function not applicable to #{previous_node.class}: #{previous_node}" end break when :substring # the previous node should be a data (as String) # the next node should be a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "Substring function requires a block." end if previous_node.is_a?(String) args = block.tree.first start = 0 length = previous_node.length if args.is_a?(String) && args.include?(',') args = args.split(',') start = args.first.to_i length = args.last.to_i-1 else FHIR.logger.debug "Evaling Substring Block...." start = eval(block,data) length = previous_node.length - start end tree[index] = previous_node[start..(start+length)] tree[previous_index] = nil if !previous_index.nil? else raise "Substring function not applicable to #{previous_node.class}: #{previous_node}" end break when :contains # the previous node should be a data (as String) # the next node should be a block or subexpression (as FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) tree[index+1] = nil else raise "Contains function requires a block." end if previous_node.is_a?(String) FHIR.logger.debug "Evaling Contains Block...." substring = eval(block,data) tree[index] = previous_node.include?(substring) rescue false tree[previous_index] = nil if !previous_index.nil? else raise "Contains function not applicable to #{previous_node.class}: #{previous_node}" end break when :in # the previous node should be a data (as String, Number, or Boolean) # the next node should an Array (possibly as a block or subexpression/FluentPath::Expression) block = tree[index+1] if block.is_a?(FluentPath::Expression) FHIR.logger.debug "Evaling In Block...." tree[index+1] = eval(block,data) end array = tree[index+1] if array.is_a?(Array) tree[index+1] = nil else raise "In function requires an array." end if previous_node.is_a?(String) || previous_node==true || previous_node==false || previous_node.is_a?(Numeric) tree[index] = array.include?(previous_node) rescue false tree[previous_index] = nil if !previous_index.nil? else raise "In function not applicable to #{previous_node.class}: #{previous_node}" end break when :toInteger # the previous node should be a data (as String, Integer, Boolean) if previous_node.is_a?(String) tree[index] = previous_node.to_i rescue 0 elsif previous_node.is_a?(Numeric) tree[index] = previous_node.to_i else tree[index] = 0 tree[index] = 1 if convertToBoolean(previous_node) end tree[previous_index] = nil if !previous_index.nil? break else raise "Function not implemented: #{node}" end end previous_index = index previous_node = node end tree.compact! end # evaluate all mult/div functions = [:"/",:"*"] size = -1 while(tree.length!=size) FHIR.logger.debug "MATH: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) tree[index+1] = eval(tree[index+1],data) if tree[index+1].is_a?(FluentPath::Expression) left = previous_node right = tree[index+1] case node when :"/" tree[index] = (left/right) when :"*" tree[index] = (left*right) end tree[previous_index] = nil tree[index+1] = nil break end previous_index = index previous_node = node end tree.compact! end FHIR.logger.debug "MATH: #{tree}" # evaluate all add/sub functions = [:"+",:"-"] size = -1 while(tree.length!=size) FHIR.logger.debug "MATH: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) tree[index+1] = eval(tree[index+1],data) if tree[index+1].is_a?(FluentPath::Expression) left = previous_node right = tree[index+1] case node when :"+" tree[index] = (left+right) when :"-" tree[index] = (left-right) end tree[previous_index] = nil tree[index+1] = nil break end previous_index = index previous_node = node end tree.compact! end FHIR.logger.debug "MATH: #{tree}" # evaluate all equality tests functions = [:"=",:"!=",:"<=",:">=",:"<",:">"] size = -1 while(tree.length!=size) FHIR.logger.debug "EQ: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) tree[index+1] = eval(tree[index+1],data) if tree[index+1].is_a?(FluentPath::Expression) left = previous_node right = tree[index+1] case node when :"=" tree[index] = (left==right) when :"!=" tree[index] = (left!=right) when :"<=" tree[index] = (left<=right) when :">=" tree[index] = (left>=right) when :"<" tree[index] = (left<right) when :">" tree[index] = (left>right) else raise "Equality operator not implemented: #{node}" end tree[previous_index] = nil tree[index+1] = nil break end previous_index = index previous_node = node end tree.compact! end FHIR.logger.debug "EQ: #{tree}" # evaluate all logical tests functions = [:and,:or,:xor] size = -1 while(tree.length!=size) FHIR.logger.debug "LOGIC: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) tree[index+1] = eval(tree[index+1],data) if tree[index+1].is_a?(FluentPath::Expression) left = convertToBoolean(previous_node) right = convertToBoolean(tree[index+1]) case node when :and tree[index] = (left&&right) when :or tree[index] = (left||right) when :xor tree[index] = (left^right) else raise "Logical operator not implemented: #{node}" end tree[previous_index] = nil tree[index+1] = nil break end previous_index = index previous_node = node end tree.compact! end FHIR.logger.debug "LOGIC: #{tree}" functions = [:implies] size = -1 while(tree.length!=size) FHIR.logger.debug "IMPLIES: #{tree}" previous_node = nil previous_index = nil size = tree.length tree.each_with_index do |node,index| if node.is_a?(Symbol) && functions.include?(node) previous_node = eval(previous_node,data) if previous_node.is_a?(FluentPath::Expression) tree[index+1] = eval(tree[index+1],data) if tree[index+1].is_a?(FluentPath::Expression) case node when :implies tree[index] = false exists = !previous_node.nil? && previous_node!=:null implication = convertToBoolean(tree[index+1]) tree[index] = true if (exists && (implication || tree[index+1]==false)) else raise "Logical operator not implemented: #{node}" end tree[previous_index] = nil tree[index+1] = nil break end previous_index = index previous_node = node end tree.compact! end FHIR.logger.debug "IMPLIES: #{tree}" # check for symbols tree.each do |node| raise "Unhandled reserved symbol: #{node}" if node.is_a?(Symbol) end FHIR.logger.debug "OUT: #{tree}" tree.map! do |out| while out.is_a?(FluentPath::Expression) out = eval(out,data) end out end FHIR.logger.debug "RETURN: #{tree.first}" tree.first end |
.evaluate(expression, hash, parent = nil) ⇒ Object
This is the entry point to using the FluentPath class
7 8 9 10 11 12 13 |
# File 'lib/fluentpath/evaluate.rb', line 7 def self.evaluate(expression,hash,parent=nil) @@context = hash @@parent = parent tree = FluentPath.parse(expression) FHIR.logger.debug "TREE: #{tree}" eval(tree,hash) end |
.get(key, hash) ⇒ Object
Get a value from a hash, with some special handling of self references
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fluentpath/evaluate.rb', line 17 def self.get(key,hash) return @@context if ['$context','$resource'].include?(key) return @@parent if key=='$parent' return 'http://unitsofmeasure.org' if key=='%ucum' return 'http://snomed.info/sct' if key=='%sct' return 'http://loinc.org' if key=='%loinc' return key.gsub!(/\A\'|\'\Z/,'') if key.start_with?("'") && key.end_with?("'") key.gsub!(/\A"|"\Z/,'') # remove quotes around path if they exist if hash.is_a?(Array) response = [] hash.each do |e| if e.is_a?(Hash) item = e[key] if item.is_a?(Array) item.each{|i| response << i } else response << item end end end return response end return :null if !hash.is_a?(Hash) return hash if hash['resourceType']==key val = hash[key] if val.nil? # this block is a dangerous hack to get fields of multiple data types # e.g. 'value' instead of 'valueQuantity', or 'onset' instead of 'onsetDateTime' or 'onsetPeriod' nkey = hash.keys.select{|x|x.start_with?(key)}.first if !nkey.nil? tail = nkey.gsub(key,'') val = hash[nkey] if (tail[0]==tail[0].capitalize) end end val = :null if val.nil? val = "'#{val}'" if val.is_a?(String) && !(val.start_with?("'") && val.end_with?("'")) val end |
.parse(expression) ⇒ Object
5 6 7 |
# File 'lib/fluentpath/parse.rb', line 5 def self.parse(expression) build_tree( tokenize(expression) ) end |
.reassemble_strings(tokens) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fluentpath/parse.rb', line 47 def self.reassemble_strings(tokens) tokens.each_with_index do |token,index| if token.is_a?(String) e_index = nil if token.start_with?('"') && !token.end_with?('"') e_index = tokens[index..-1].index{|t| t.end_with?('"')} elsif token.start_with?("'") && !token.end_with?("'") e_index = tokens[index..-1].index{|t| t.end_with?("'")} end if e_index i = index+1 while(i <= index+e_index) tokens[index] += tokens[i] tokens[i] = '' i+=1 end end end end end |
.tokenize(expression) ⇒ Object
This method tokenizes the expression into a flat array of tokens
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fluentpath/parse.rb', line 10 def self.tokenize(expression) raw_tokens = expression.gsub('()','').split(/(\(|\)|\s|>=|<=|>|<|=|!=|\+|-|\/|\*)/) # recreate strings if they were split size = nil while(raw_tokens.include?("'") && size!=raw_tokens.length) index = raw_tokens.index("'") e_index = raw_tokens[(index+1)..raw_tokens.length].index("'") raw_tokens[index] = raw_tokens[index..(index+e_index+1)].join for i in (index+1)..(index+e_index+1) raw_tokens[i] = nil end raw_tokens.compact! size = raw_tokens.length end # reassemble strings that might have been inadvertently split that start and end with double-quotes reassemble_strings(raw_tokens) tokens = [] raw_tokens.each do |token| # split a path unless it is quoted if token.include?('.') && !(token.start_with?("'") && token.end_with?("'")) token.split('.').each{|t|tokens << t} # split arrays and replace with array elsif token.include?('|') array = [] token.split('|').each{|t|array << t.gsub('\'','')} tokens << array else tokens << token end end # we may need to reassemble quoted strings again reassemble_strings(tokens) tokens.delete_if { |token| (token.length==0 || (token.is_a?(String) && token.match(/\S/).nil?) ) } FHIR.logger.debug "TOKENS: #{tokens}" tokens end |