Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
Plugged in logging ... cleaned up evaluators ... created base class f…
Browse files Browse the repository at this point in the history
…or temporal types - remove dup code
  • Loading branch information
Chris Schuler authored and Chris Schuler committed Jun 12, 2017
1 parent 1971e6a commit ccd7ea6
Show file tree
Hide file tree
Showing 136 changed files with 3,193 additions and 2,527 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ subprojects {
testCompile group: 'uk.co.datumedge', name: 'hamcrest-json', version: '0.2'
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
runtime files('src/main/resources')
runtime files('src/main/resources/log4j.properties')
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opencds.cqf.cql.elm.execution;

import org.opencds.cqf.cql.execution.Context;
import org.opencds.cqf.cql.execution.TraceExecution;
import org.opencds.cqf.cql.runtime.Quantity;
import java.math.BigDecimal;

Expand All @@ -16,29 +17,33 @@

/**
* Created by Bryn on 5/24/2016.
* Edited by Chris Schuler on 6/14/2016
*/
public class AbsEvaluator extends org.cqframework.cql.elm.execution.Abs {
@Override
public Object evaluate(Context context) {
Object value = getOperand().evaluate(context);

if (value == null) {
public static Object abs(Object operand) {
if (operand == null) {
return null;
}

if (value instanceof Integer) {
return Math.abs((Integer)value);
if (operand instanceof Integer) {
return Math.abs((Integer)operand);
}

else if (value instanceof BigDecimal) {
return ((BigDecimal)value).abs();
else if (operand instanceof BigDecimal) {
return ((BigDecimal)operand).abs();
}

else if (value instanceof Quantity) {
return (((Quantity)value).getValue()).abs();
else if (operand instanceof Quantity) {
return new Quantity().withValue((((Quantity)operand).getValue()).abs()).withUnit(((Quantity)operand).getUnit());
}

throw new IllegalArgumentException(String.format("Cannot %s with argument of type '%s'.",this.getClass().getSimpleName(), value.getClass().getName()));
throw new IllegalArgumentException(String.format("Cannot evaluate the Abs operator with an argument of type '%s'.", operand.getClass().getName()));
}

@Override
public Object evaluate(Context context) {
Object operand = getOperand().evaluate(context);

return context.logTrace(this.getClass(), abs(operand), operand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.opencds.cqf.cql.execution.Context;
import org.opencds.cqf.cql.runtime.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

Expand Down Expand Up @@ -46,8 +44,6 @@ This example results in the value DateTime(2016) even though the date/time value
*/
public class AddEvaluator extends org.cqframework.cql.elm.execution.Add {

final Logger logger = LoggerFactory.getLogger(AddEvaluator.class);

private static final int YEAR_RANGE_MAX = 9999;

public static Object add(Object left, Object right) {
Expand Down Expand Up @@ -151,37 +147,11 @@ else if (left instanceof String && right instanceof String) {
throw new IllegalArgumentException(String.format("Cannot AddEvaluator arguments of type '%s' and '%s'.", left.getClass().getName(), right.getClass().getName()));
}

public void logIt(Object left, Object right) {
if (left == null && right != null) {
logger.debug("Left operand: null, Right operand: {}", right.toString());
}
else if (left != null && right == null) {
logger.debug("Left operand: {}, Right operand: null");
}
else if (left == null && right == null) {
logger.debug("Both operands are null");
}
else {
logger.debug("Left operand: {}, Right operand: {}", left.toString(), right.toString());
}
}

@Override
public Object evaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);

if (left == null || right == null) {
if (context.isTraceLoggingEnabled()) {
logIt(left, right);
}
return null;
}

if (context.isTraceLoggingEnabled()) {
logIt(left, right);
}

return add(left, right);
return context.logTrace(this.getClass(), add(left, right), left, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,102 +29,108 @@ after precision of(left Time, right Time) Boolean
*/

/**
* Created by Chris Schuler on 6/7/2016
*/
* Created by Chris Schuler on 6/7/2016
*/
public class AfterEvaluator extends org.cqframework.cql.elm.execution.After {

@Override
public Object evaluate(Context context) {
Object testLeft = getOperand().get(0).evaluate(context);
Object testRight = getOperand().get(1).evaluate(context);

if (testLeft == null || testRight == null) { return null; }

// (Interval, Interval)
if (testLeft instanceof Interval && testRight instanceof Interval) {
return GreaterEvaluator.greater(((Interval)testLeft).getStart(), ((Interval)testRight).getEnd());
}

// (Interval, Point)
else if (testLeft instanceof Interval && !(testRight instanceof Interval)) {
return GreaterEvaluator.greater(((Interval)testLeft).getStart(), testRight);
}
public static Object after(Object left, Object right, String precision) {
if (left == null || right == null) {
return null;
}

// (Point, Interval)
else if (!(testLeft instanceof Interval) && testRight instanceof Interval) {
return GreaterEvaluator.greater(testLeft, ((Interval)testRight).getEnd());
}
// (Interval, Interval)
if (left instanceof Interval && right instanceof Interval) {
return GreaterEvaluator.greater(((Interval)left).getStart(), ((Interval)right).getEnd());
}

// (DateTime, DateTime)
else if (testLeft instanceof DateTime && testRight instanceof DateTime) {
DateTime leftDT = (DateTime)testLeft;
DateTime rightDT = (DateTime)testRight;
String precision = getPrecision() == null ? null : getPrecision().value();
// (Interval, Point)
else if (left instanceof Interval) {
return GreaterEvaluator.greater(((Interval)left).getStart(), right);
}

if (precision == null) {
throw new IllegalArgumentException("Precision must be specified.");
}
// (Point, Interval)
else if (right instanceof Interval) {
return GreaterEvaluator.greater(left, ((Interval)right).getEnd());
}

int idx = DateTime.getFieldIndex(precision);
// (DateTime, DateTime)
else if (left instanceof DateTime && right instanceof DateTime) {
DateTime leftDT = (DateTime)left;
DateTime rightDT = (DateTime)right;

if (precision == null) {
throw new IllegalArgumentException("Precision must be specified.");
}

int idx = DateTime.getFieldIndex(precision);

if (idx != -1) {
// check level of precision
if (idx + 1 > leftDT.getPartial().size() || idx + 1 > rightDT.getPartial().size()) {

// Uncertainty
if (Uncertainty.isUncertain(leftDT, precision)) {
return GreaterEvaluator.greater(Uncertainty.getHighLowList(leftDT, precision).get(0), rightDT);
}

else if (Uncertainty.isUncertain(rightDT, precision)) {
return GreaterEvaluator.greater(leftDT, Uncertainty.getHighLowList(rightDT, precision).get(1));
}
return null;
}
return leftDT.getPartial().getValue(idx) > rightDT.getPartial().getValue(idx);
}

else {
throw new IllegalArgumentException(String.format("Invalid duration precision: %s", precision));
}
}

if (idx != -1) {
// check level of precision
if (idx + 1 > leftDT.getPartial().size() || idx + 1 > rightDT.getPartial().size()) {
// (Time, Time)
else if (left instanceof Time && right instanceof Time) {
Time leftT = (Time)left;
Time rightT = (Time)right;

// Uncertainty
if (Uncertainty.isUncertain(leftDT, precision)) {
return GreaterEvaluator.greater(Uncertainty.getHighLowList(leftDT, precision).get(0), rightDT);
}
if (precision == null) {
throw new IllegalArgumentException("Precision must be specified.");
}

else if (Uncertainty.isUncertain(rightDT, precision)) {
return GreaterEvaluator.greater(leftDT, Uncertainty.getHighLowList(rightDT, precision).get(1));
}
return null;
}
return leftDT.getPartial().getValue(idx) > rightDT.getPartial().getValue(idx);
}
int idx = Time.getFieldIndex(precision);

else {
throw new IllegalArgumentException(String.format("Invalid duration precision: %s", precision));
}
}
if (idx != -1) {
// check level of precision
if (idx + 1 > leftT.getPartial().size() || idx + 1 > rightT.getPartial().size()) {

// (Time, Time)
else if (testLeft instanceof Time && testRight instanceof Time) {
Time leftT = (Time)testLeft;
Time rightT = (Time)testRight;
String precision = getPrecision() == null ? null : getPrecision().value();
// Uncertainty
if (Uncertainty.isUncertain(leftT, precision)) {
return GreaterEvaluator.greater(Uncertainty.getHighLowList(leftT, precision).get(0), rightT);
}

if (precision == null) {
throw new IllegalArgumentException("Precision must be specified.");
}
else if (Uncertainty.isUncertain(rightT, precision)) {
return GreaterEvaluator.greater(leftT, Uncertainty.getHighLowList(rightT, precision).get(1));
}

int idx = Time.getFieldIndex(precision);
return null;
}

if (idx != -1) {
// check level of precision
if (idx + 1 > leftT.getPartial().size() || idx + 1 > rightT.getPartial().size()) {
return leftT.getPartial().getValue(idx) > rightT.getPartial().getValue(idx);
}

// Uncertainty
if (Uncertainty.isUncertain(leftT, precision)) {
return GreaterEvaluator.greater(Uncertainty.getHighLowList(leftT, precision).get(0), rightT);
}
else {
throw new IllegalArgumentException(String.format("Invalid duration precision: %s", precision));
}
}

else if (Uncertainty.isUncertain(rightT, precision)) {
return GreaterEvaluator.greater(leftT, Uncertainty.getHighLowList(rightT, precision).get(1));
}
throw new IllegalArgumentException(String.format("Cannot After arguments of type '%s' and '%s'.", left.getClass().getName(), right.getClass().getName()));
}

return null;
}
@Override
public Object evaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);

return leftT.getPartial().getValue(idx) > rightT.getPartial().getValue(idx);
}
String precision = getPrecision() == null ? null : getPrecision().value();

else {
throw new IllegalArgumentException(String.format("Invalid duration precision: %s", precision));
}
return context.logTrace(this.getClass(), after(left, right, precision), left, right);
}

throw new IllegalArgumentException(String.format("Cannot After arguments of type '%s' and '%s'.", testLeft.getClass().getName(), testRight.getClass().getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,30 @@
* Created by Bryn on 5/25/2016.
*/
public class AllTrueEvaluator extends org.cqframework.cql.elm.execution.AllTrue {
@Override
public Object evaluate(Context context) {

Object src = getSource().evaluate(context);

public static Object allTrue(Object src) {
if (src == null) {
return true;
}

if(src instanceof Iterable) {
Iterable<Object> element = (Iterable<Object>)src;
Iterator<Object> elemsItr = element.iterator();
Iterable<Object> element = (Iterable<Object>)src;
Iterator<Object> elemsItr = element.iterator();

if (!elemsItr.hasNext()) { // empty list
return true;
}

while (elemsItr.hasNext()) {
Object exp = elemsItr.next();
Object exp = elemsItr.next();

if (exp == null) { // skip null
continue;
}
Boolean boolVal = (Boolean) exp;
if (exp == null) { // skip null
continue;
}
Boolean boolVal = (Boolean) exp;

if (Boolean.FALSE == boolVal) return false;
}
if (Boolean.FALSE == boolVal) return false;
}
}

else {
Expand All @@ -51,4 +48,12 @@ public Object evaluate(Context context) {

return true;
}

@Override
public Object evaluate(Context context) {

Object src = getSource().evaluate(context);

return context.logTrace(this.getClass(), allTrue(src), src);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,33 @@
* Created by Bryn on 5/25/2016.
*/
public class AndEvaluator extends org.cqframework.cql.elm.execution.And {

public static Object and(Boolean left, Boolean right) {
if (left == null || right == null) {
if ((left != null && !left) || (right != null && !right)) {
return false;
}

return null;
}

return (left && right);
}

@Override
public Object evaluate(Context context) {
Boolean left = (Boolean) getOperand().get(0).evaluate(context);
Boolean right = (Boolean) getOperand().get(1).evaluate(context);

// need this dup code to avoid null pointer ...
if (left == null || right == null) {
if ((left != null && left == false) || (right != null && right == false)) {
if ((left != null && !left) || (right != null && !right)) {
return false;
}

return null;
}

return (left && right);
return context.logTrace(this.getClass(), (left && right), left, right);
}
}
Loading

0 comments on commit ccd7ea6

Please sign in to comment.