001package org.hl7.fhir.r4.model; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r4 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023 024import java.util.ArrayList; 025import java.util.List; 026 027import org.hl7.fhir.utilities.Utilities; 028 029public class ExpressionNode { 030 031 public enum Kind { 032 Name, Function, Constant, Group, Unary 033 } 034 public static class SourceLocation { 035 private int line; 036 private int column; 037 public SourceLocation(int line, int column) { 038 super(); 039 this.line = line; 040 this.column = column; 041 } 042 public int getLine() { 043 return line; 044 } 045 public int getColumn() { 046 return column; 047 } 048 public void setLine(int line) { 049 this.line = line; 050 } 051 public void setColumn(int column) { 052 this.column = column; 053 } 054 055 public String toString() { 056 return Integer.toString(line)+", "+Integer.toString(column); 057 } 058 } 059 public enum Function { 060 Custom, 061 062 Empty, Not, Exists, SubsetOf, SupersetOf, IsDistinct, Distinct, Count, Where, Select, All, Repeat, Aggregate, Item /*implicit from name[]*/, As, Is, Single, 063 First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length, 064 Children, Descendants, MemberOf, Trace, Check, Today, Now, Resolve, Extension, AllFalse, AnyFalse, AllTrue, AnyTrue, 065 HasValue, AliasAs, Alias, HtmlChecks, OfType, Type, 066 ConvertsToBoolean, ConvertsToInteger, ConvertsToString, ConvertsToDecimal, ConvertsToQuantity, ConvertsToDateTime, ConvertsToTime, ToBoolean, ToInteger, ToString, ToDecimal, ToQuantity, ToDateTime, ToTime, ConformsTo; 067 068 public static Function fromCode(String name) { 069 if (name.equals("empty")) return Function.Empty; 070 if (name.equals("not")) return Function.Not; 071 if (name.equals("exists")) return Function.Exists; 072 if (name.equals("subsetOf")) return Function.SubsetOf; 073 if (name.equals("supersetOf")) return Function.SupersetOf; 074 if (name.equals("isDistinct")) return Function.IsDistinct; 075 if (name.equals("distinct")) return Function.Distinct; 076 if (name.equals("count")) return Function.Count; 077 if (name.equals("where")) return Function.Where; 078 if (name.equals("select")) return Function.Select; 079 if (name.equals("all")) return Function.All; 080 if (name.equals("repeat")) return Function.Repeat; 081 if (name.equals("aggregate")) return Function.Aggregate; 082 if (name.equals("item")) return Function.Item; 083 if (name.equals("as")) return Function.As; 084 if (name.equals("is")) return Function.Is; 085 if (name.equals("single")) return Function.Single; 086 if (name.equals("first")) return Function.First; 087 if (name.equals("last")) return Function.Last; 088 if (name.equals("tail")) return Function.Tail; 089 if (name.equals("skip")) return Function.Skip; 090 if (name.equals("take")) return Function.Take; 091 if (name.equals("union")) return Function.Union; 092 if (name.equals("combine")) return Function.Combine; 093 if (name.equals("intersect")) return Function.Intersect; 094 if (name.equals("exclude")) return Function.Exclude; 095 if (name.equals("iif")) return Function.Iif; 096 if (name.equals("lower")) return Function.Lower; 097 if (name.equals("upper")) return Function.Upper; 098 if (name.equals("toChars")) return Function.ToChars; 099 if (name.equals("substring")) return Function.Substring; 100 if (name.equals("startsWith")) return Function.StartsWith; 101 if (name.equals("endsWith")) return Function.EndsWith; 102 if (name.equals("matches")) return Function.Matches; 103 if (name.equals("replaceMatches")) return Function.ReplaceMatches; 104 if (name.equals("contains")) return Function.Contains; 105 if (name.equals("replace")) return Function.Replace; 106 if (name.equals("length")) return Function.Length; 107 if (name.equals("children")) return Function.Children; 108 if (name.equals("descendants")) return Function.Descendants; 109 if (name.equals("memberOf")) return Function.MemberOf; 110 if (name.equals("trace")) return Function.Trace; 111 if (name.equals("check")) return Function.Check; 112 if (name.equals("today")) return Function.Today; 113 if (name.equals("now")) return Function.Now; 114 if (name.equals("resolve")) return Function.Resolve; 115 if (name.equals("extension")) return Function.Extension; 116 if (name.equals("allFalse")) return Function.AllFalse; 117 if (name.equals("anyFalse")) return Function.AnyFalse; 118 if (name.equals("allTrue")) return Function.AllTrue; 119 if (name.equals("anyTrue")) return Function.AnyTrue; 120 if (name.equals("hasValue")) return Function.HasValue; 121 if (name.equals("alias")) return Function.Alias; 122 if (name.equals("aliasAs")) return Function.AliasAs; 123 if (name.equals("htmlChecks")) return Function.HtmlChecks; 124 if (name.equals("ofType")) return Function.OfType; 125 if (name.equals("type")) return Function.Type; 126 if (name.equals("toInteger")) return Function.ToInteger; 127 if (name.equals("toDecimal")) return Function.ToDecimal; 128 if (name.equals("toString")) return Function.ToString; 129 if (name.equals("toQuantity")) return Function.ToQuantity; 130 if (name.equals("toBoolean")) return Function.ToBoolean; 131 if (name.equals("toDateTime")) return Function.ToDateTime; 132 if (name.equals("toTime")) return Function.ToTime; 133 if (name.equals("convertsToInteger")) return Function.ConvertsToInteger; 134 if (name.equals("convertsToDecimal")) return Function.ConvertsToDecimal; 135 if (name.equals("convertsToString")) return Function.ConvertsToString; 136 if (name.equals("convertsToQuantity")) return Function.ConvertsToQuantity; 137 if (name.equals("convertsToBoolean")) return Function.ConvertsToBoolean; 138 if (name.equals("convertsToDateTime")) return Function.ConvertsToDateTime; 139 if (name.equals("convertsToTime")) return Function.ConvertsToTime; 140 if (name.equals("conformsTo")) return Function.ConformsTo; 141 return null; 142 } 143 public String toCode() { 144 switch (this) { 145 case Empty : return "empty"; 146 case Not : return "not"; 147 case Exists : return "exists"; 148 case SubsetOf : return "subsetOf"; 149 case SupersetOf : return "supersetOf"; 150 case IsDistinct : return "isDistinct"; 151 case Distinct : return "distinct"; 152 case Count : return "count"; 153 case Where : return "where"; 154 case Select : return "select"; 155 case All : return "all"; 156 case Repeat : return "repeat"; 157 case Aggregate : return "aggregate"; 158 case Item : return "item"; 159 case As : return "as"; 160 case Is : return "is"; 161 case Single : return "single"; 162 case First : return "first"; 163 case Last : return "last"; 164 case Tail : return "tail"; 165 case Skip : return "skip"; 166 case Take : return "take"; 167 case Union : return "union"; 168 case Combine : return "combine"; 169 case Intersect : return "intersect"; 170 case Exclude : return "exclude"; 171 case Iif : return "iif"; 172 case ToChars : return "toChars"; 173 case Lower : return "lower"; 174 case Upper : return "upper"; 175 case Substring : return "substring"; 176 case StartsWith : return "startsWith"; 177 case EndsWith : return "endsWith"; 178 case Matches : return "matches"; 179 case ReplaceMatches : return "replaceMatches"; 180 case Contains : return "contains"; 181 case Replace : return "replace"; 182 case Length : return "length"; 183 case Children : return "children"; 184 case Descendants : return "descendants"; 185 case MemberOf : return "memberOf"; 186 case Trace : return "trace"; 187 case Check : return "check"; 188 case Today : return "today"; 189 case Now : return "now"; 190 case Resolve : return "resolve"; 191 case Extension : return "extension"; 192 case AllFalse : return "allFalse"; 193 case AnyFalse : return "anyFalse"; 194 case AllTrue : return "allTrue"; 195 case AnyTrue : return "anyTrue"; 196 case HasValue : return "hasValue"; 197 case Alias : return "alias"; 198 case AliasAs : return "aliasAs"; 199 case HtmlChecks : return "htmlChecks"; 200 case OfType : return "ofType"; 201 case Type : return "type"; 202 case ToInteger : return "toInteger"; 203 case ToDecimal : return "toDecimal"; 204 case ToString : return "toString"; 205 case ToBoolean : return "toBoolean"; 206 case ToQuantity : return "toQuantity"; 207 case ToDateTime : return "toDateTime"; 208 case ToTime : return "toTime"; 209 case ConvertsToInteger : return "convertsToInteger"; 210 case ConvertsToDecimal : return "convertsToDecimal"; 211 case ConvertsToString : return "convertsToString"; 212 case ConvertsToBoolean : return "convertsToBoolean"; 213 case ConvertsToQuantity : return "convertsToQuantity"; 214 case ConvertsToDateTime : return "convertsToDateTime"; 215 case ConvertsToTime : return "isTime"; 216 case ConformsTo : return "conformsTo"; 217 default: return "??"; 218 } 219 } 220 } 221 222 public enum Operation { 223 Equals, Equivalent, NotEquals, NotEquivalent, LessThan, Greater, LessOrEqual, GreaterOrEqual, Is, As, Union, Or, And, Xor, Implies, 224 Times, DivideBy, Plus, Minus, Concatenate, Div, Mod, In, Contains, MemberOf; 225 226 public static Operation fromCode(String name) { 227 if (Utilities.noString(name)) 228 return null; 229 if (name.equals("=")) 230 return Operation.Equals; 231 if (name.equals("~")) 232 return Operation.Equivalent; 233 if (name.equals("!=")) 234 return Operation.NotEquals; 235 if (name.equals("!~")) 236 return Operation.NotEquivalent; 237 if (name.equals(">")) 238 return Operation.Greater; 239 if (name.equals("<")) 240 return Operation.LessThan; 241 if (name.equals(">=")) 242 return Operation.GreaterOrEqual; 243 if (name.equals("<=")) 244 return Operation.LessOrEqual; 245 if (name.equals("|")) 246 return Operation.Union; 247 if (name.equals("or")) 248 return Operation.Or; 249 if (name.equals("and")) 250 return Operation.And; 251 if (name.equals("xor")) 252 return Operation.Xor; 253 if (name.equals("is")) 254 return Operation.Is; 255 if (name.equals("as")) 256 return Operation.As; 257 if (name.equals("*")) 258 return Operation.Times; 259 if (name.equals("/")) 260 return Operation.DivideBy; 261 if (name.equals("+")) 262 return Operation.Plus; 263 if (name.equals("-")) 264 return Operation.Minus; 265 if (name.equals("&")) 266 return Operation.Concatenate; 267 if (name.equals("implies")) 268 return Operation.Implies; 269 if (name.equals("div")) 270 return Operation.Div; 271 if (name.equals("mod")) 272 return Operation.Mod; 273 if (name.equals("in")) 274 return Operation.In; 275 if (name.equals("contains")) 276 return Operation.Contains; 277 if (name.equals("memberOf")) 278 return Operation.MemberOf; 279 return null; 280 281 } 282 public String toCode() { 283 switch (this) { 284 case Equals : return "="; 285 case Equivalent : return "~"; 286 case NotEquals : return "!="; 287 case NotEquivalent : return "!~"; 288 case Greater : return ">"; 289 case LessThan : return "<"; 290 case GreaterOrEqual : return ">="; 291 case LessOrEqual : return "<="; 292 case Union : return "|"; 293 case Or : return "or"; 294 case And : return "and"; 295 case Xor : return "xor"; 296 case Times : return "*"; 297 case DivideBy : return "/"; 298 case Plus : return "+"; 299 case Minus : return "-"; 300 case Concatenate : return "&"; 301 case Implies : return "implies"; 302 case Is : return "is"; 303 case As : return "as"; 304 case Div : return "div"; 305 case Mod : return "mod"; 306 case In : return "in"; 307 case Contains : return "contains"; 308 case MemberOf : return "memberOf"; 309 default: return "??"; 310 } 311 } 312 } 313 314 public enum CollectionStatus { 315 SINGLETON, ORDERED, UNORDERED; 316 } 317 318 //the expression will have one of either name or constant 319 private String uniqueId; 320 private Kind kind; 321 private String name; 322 private Base constant; 323 private Function function; 324 private List<ExpressionNode> parameters; // will be created if there is a function 325 private ExpressionNode inner; 326 private ExpressionNode group; 327 private Operation operation; 328 private boolean proximal; // a proximal operation is the first in the sequence of operations. This is significant when evaluating the outcomes 329 private ExpressionNode opNext; 330 private SourceLocation start; 331 private SourceLocation end; 332 private SourceLocation opStart; 333 private SourceLocation opEnd; 334 private TypeDetails types; 335 private TypeDetails opTypes; 336 337 338 public ExpressionNode(int uniqueId) { 339 super(); 340 this.uniqueId = Integer.toString(uniqueId); 341 } 342 343 public String toString() { 344 StringBuilder b = new StringBuilder(); 345 switch (kind) { 346 case Name: 347 b.append(name); 348 break; 349 case Function: 350 if (function == Function.Item) 351 b.append("["); 352 else { 353 b.append(name); 354 b.append("("); 355 } 356 boolean first = true; 357 for (ExpressionNode n : parameters) { 358 if (first) 359 first = false; 360 else 361 b.append(", "); 362 b.append(n.toString()); 363 } 364 if (function == Function.Item) 365 b.append("]"); 366 else { 367 b.append(")"); 368 } 369 break; 370 case Constant: 371 if (constant instanceof StringType) 372 b.append("'"+Utilities.escapeJson(constant.primitiveValue())+"'"); 373 else if (constant instanceof Quantity) { 374 Quantity q = (Quantity) constant; 375 b.append(Utilities.escapeJson(q.getValue().toPlainString())); 376 b.append(" '"); 377 b.append(Utilities.escapeJson(q.getUnit())); 378 b.append("'"); 379 } else if (constant.primitiveValue() != null) 380 b.append(Utilities.escapeJson(constant.primitiveValue())); 381 else 382 b.append(Utilities.escapeJson(constant.toString())); 383 break; 384 case Group: 385 b.append("("); 386 b.append(group.toString()); 387 b.append(")"); 388 } 389 if (inner != null) { 390 b.append("."); 391 b.append(inner.toString()); 392 } 393 if (operation != null) { 394 b.append(" "); 395 b.append(operation.toCode()); 396 b.append(" "); 397 b.append(opNext.toString()); 398 } 399 400 return b.toString(); 401 } 402 403 public String getName() { 404 return name; 405 } 406 public void setName(String name) { 407 this.name = name; 408 } 409 public Base getConstant() { 410 return constant; 411 } 412 public void setConstant(Base constant) { 413 this.constant = constant; 414 } 415 416 public Function getFunction() { 417 return function; 418 } 419 public void setFunction(Function function) { 420 this.function = function; 421 if (parameters == null) 422 parameters = new ArrayList<ExpressionNode>(); 423 } 424 425 public boolean isProximal() { 426 return proximal; 427 } 428 public void setProximal(boolean proximal) { 429 this.proximal = proximal; 430 } 431 public Operation getOperation() { 432 return operation; 433 } 434 public void setOperation(Operation operation) { 435 this.operation = operation; 436 } 437 public ExpressionNode getInner() { 438 return inner; 439 } 440 public void setInner(ExpressionNode value) { 441 this.inner = value; 442 } 443 public ExpressionNode getOpNext() { 444 return opNext; 445 } 446 public void setOpNext(ExpressionNode value) { 447 this.opNext = value; 448 } 449 public List<ExpressionNode> getParameters() { 450 return parameters; 451 } 452 public boolean checkName() { 453 if (!name.startsWith("$")) 454 return true; 455 else 456 return Utilities.existsInList(name, "$this", "$total"); 457 } 458 459 public Kind getKind() { 460 return kind; 461 } 462 463 public void setKind(Kind kind) { 464 this.kind = kind; 465 } 466 467 public ExpressionNode getGroup() { 468 return group; 469 } 470 471 public void setGroup(ExpressionNode group) { 472 this.group = group; 473 } 474 475 public SourceLocation getStart() { 476 return start; 477 } 478 479 public void setStart(SourceLocation start) { 480 this.start = start; 481 } 482 483 public SourceLocation getEnd() { 484 return end; 485 } 486 487 public void setEnd(SourceLocation end) { 488 this.end = end; 489 } 490 491 public SourceLocation getOpStart() { 492 return opStart; 493 } 494 495 public void setOpStart(SourceLocation opStart) { 496 this.opStart = opStart; 497 } 498 499 public SourceLocation getOpEnd() { 500 return opEnd; 501 } 502 503 public void setOpEnd(SourceLocation opEnd) { 504 this.opEnd = opEnd; 505 } 506 507 public String getUniqueId() { 508 return uniqueId; 509 } 510 511 512 public int parameterCount() { 513 if (parameters == null) 514 return 0; 515 else 516 return parameters.size(); 517 } 518 519 public String Canonical() { 520 StringBuilder b = new StringBuilder(); 521 write(b); 522 return b.toString(); 523 } 524 525 public String summary() { 526 switch (kind) { 527 case Name: return uniqueId+": "+name; 528 case Function: return uniqueId+": "+function.toString()+"()"; 529 case Constant: return uniqueId+": "+constant; 530 case Group: return uniqueId+": (Group)"; 531 } 532 return "??"; 533 } 534 535 private void write(StringBuilder b) { 536 537 switch (kind) { 538 case Name: 539 b.append(name); 540 break; 541 case Constant: 542 b.append(constant); 543 break; 544 case Function: 545 b.append(function.toCode()); 546 b.append('('); 547 boolean f = true; 548 for (ExpressionNode n : parameters) { 549 if (f) 550 f = false; 551 else 552 b.append(", "); 553 n.write(b); 554 } 555 b.append(')'); 556 557 break; 558 case Group: 559 b.append('('); 560 group.write(b); 561 b.append(')'); 562 } 563 564 if (inner != null) { 565 b.append('.'); 566 inner.write(b); 567 } 568 if (operation != null) { 569 b.append(' '); 570 b.append(operation.toCode()); 571 b.append(' '); 572 opNext.write(b); 573 } 574 } 575 576 public String check() { 577 578 switch (kind) { 579 case Name: 580 if (Utilities.noString(name)) 581 return "No Name provided @ "+location(); 582 break; 583 584 case Function: 585 if (function == null) 586 return "No Function id provided @ "+location(); 587 for (ExpressionNode n : parameters) { 588 String msg = n.check(); 589 if (msg != null) 590 return msg; 591 } 592 593 break; 594 595 case Unary: 596 break; 597 case Constant: 598 if (constant == null) 599 return "No Constant provided @ "+location(); 600 break; 601 602 case Group: 603 if (group == null) 604 return "No Group provided @ "+location(); 605 else { 606 String msg = group.check(); 607 if (msg != null) 608 return msg; 609 } 610 } 611 if (inner != null) { 612 String msg = inner.check(); 613 if (msg != null) 614 return msg; 615 } 616 if (operation == null) { 617 618 if (opNext != null) 619 return "Next provided when it shouldn't be @ "+location(); 620 } 621 else { 622 if (opNext == null) 623 return "No Next provided @ "+location(); 624 else 625 opNext.check(); 626 } 627 return null; 628 629 } 630 631 private String location() { 632 return Integer.toString(start.line)+", "+Integer.toString(start.column); 633 } 634 635 public TypeDetails getTypes() { 636 return types; 637 } 638 639 public void setTypes(TypeDetails types) { 640 this.types = types; 641 } 642 643 public TypeDetails getOpTypes() { 644 return opTypes; 645 } 646 647 public void setOpTypes(TypeDetails opTypes) { 648 this.opTypes = opTypes; 649 } 650 651}