/** * Limits results by a given encounter id * * @see org.openmrs.logic.Rule#eval(org.openmrs.logic.LogicContext, org.openmrs.Patient, * java.util.Map) */ public Result eval(LogicContext context, Integer patientId, Map<String, Object> parameters) throws LogicException { if (parameters == null) { return Result.emptyResult(); } String conceptName = (String) parameters.get("param1"); if (conceptName == null) { return Result.emptyResult(); } Result ruleResult = null; Integer encounterId = (Integer) parameters.get("encounterId"); if (encounterId == null) { return Result.emptyResult(); } LogicCriteria conceptCriteria = new LogicCriteriaImpl(conceptName); LogicCriteria fullCriteria = null; LogicCriteria encounterCriteria = new LogicCriteriaImpl("encounterId").equalTo(encounterId.intValue()); fullCriteria = conceptCriteria.and(encounterCriteria); ruleResult = context.read(patientId, context.getLogicDataSource("obs"), fullCriteria.last()); if (ruleResult != null && ruleResult.size() > 0) { return ruleResult.get(0); } return Result.emptyResult(); }
/** @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof Result)) { return false; } Result r = (Result) obj; if (EmptyResult.class.isAssignableFrom(r.getClass()) && this.isEmpty()) { return true; } if (EmptyResult.class.isAssignableFrom(this.getClass()) && r.isEmpty()) { return true; } if (isSingleResult() && r.isSingleResult()) { if (datatype == null) { return false; } // both are single results switch (datatype) { case BOOLEAN: return (valueBoolean.equals(r.valueBoolean)); case CODED: return (valueCoded.equals(r.valueCoded)); case DATETIME: return (valueDatetime.equals(r.valueDatetime)); case NUMERIC: return (valueNumeric.equals(r.valueNumeric)); case TEXT: return (valueText.equals(r.valueText)); default: return false; } } if (isSingleResult() || r.isSingleResult()) { // we already know they're not both single results, so if one is // single, it's not a match return false; } if (this.size() != r.size()) { return false; } // at this point, we have two results that are lists, so members must // match exactly for (int i = 0; i < this.size(); i++) { if (!this.get(i).equals(r.get(i))) { return false; } } return true; }
/** TODO make this test use assert statements instead of printing to stdout */ @Test public void shouldObservationRule() { LogicService logicService = Context.getLogicService(); Cohort patients = new Cohort(); Map<Integer, Result> result = null; patients.addMember(2); try { Result r = logicService.eval(2, new LogicCriteriaImpl("CD4 COUNT")); Assert.assertNotNull(r); Assert.assertEquals(0, r.size()); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT")); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").lt(170)); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").gt(185)); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").equalTo(190)); Assert.assertNotNull(result); Calendar cal = Calendar.getInstance(); cal.set(2006, 3, 11); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").before(cal.getTime())); Assert.assertNotNull(result); result = logicService.eval( patients, new LogicCriteriaImpl("CD4 COUNT").lt(190).before(cal.getTime())); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").after(cal.getTime())); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").last()); Assert.assertNotNull(result); result = logicService.eval( patients, new LogicCriteriaImpl("CD4 COUNT").last().before(cal.getTime())); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").not().lt(150)); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").not().not().lt(150)); Assert.assertNotNull(result); patients.addMember(2); patients.addMember(3); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").lt(200)); Assert.assertNotNull(result); patients.addMember(39); result = logicService.eval( patients, new LogicCriteriaImpl("PROBLEM ADDED").contains("HUMAN IMMUNODEFICIENCY VIRUS")); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT")); Assert.assertNotNull(result); result = logicService.eval( patients, new LogicCriteriaImpl("CD4 COUNT").within(Duration.months(1))); Assert.assertNotNull(result); } catch (LogicException e) { log.error("testObservationRule: Error generated", e); } }