/** @see {@link LogicService#getRule(String)} */ @Test @Verifies(value = "should return ReferenceRule", method = "getRule(String)") public void getRule_shouldReturnReferenceRule() throws Exception { Rule rule = Context.getLogicService().getRule("%%person.birthdate"); Assert.assertNotNull(rule); Assert.assertTrue(rule.getClass().isAssignableFrom(ReferenceRule.class)); }
/** TODO: should we still support this kind of notation? */ public void shouldReferenceRule() { LogicService logicService = Context.getLogicService(); Cohort patients = new Cohort(); Map<Integer, Result> result = null; patients.addMember(2); try { result = logicService.eval(patients, "%%obs.CD4 COUNT"); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("%%obs.CD4 COUNT").first()); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("%%person.BIRTHDATE")); Assert.assertNotNull(result); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 1980); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); result = logicService.eval( patients, new LogicCriteriaImpl("%%person.BIRTHDATE").before(cal.getTime())); Assert.assertNotNull(result); } catch (LogicException e) { log.error("Error generated", e); } }
/** @see {@link LogicService#getRule(String)} */ @Test(expected = LogicException.class) @Verifies( value = "should fail when no Rule is associated with the input token", method = "getRule(String)") public void getRule_shouldFailWhenNoRuleIsAssociatedWithTheInputToken() throws Exception { Rule rule = Context.getLogicService().getRule("UNKNOWN RULE"); Assert.assertNull(rule); }
/** @see {@link LogicService#findToken(String)} */ @Test @Verifies( value = "should return all registered token matching the input fully", method = "getTokens(String)") public void getTokens_shouldReturnAllRegisteredTokenMatchingTheInputFully() throws Exception { LogicService logicService = Context.getLogicService(); List<String> tokens = logicService.getTokens("AGE"); Assert.assertEquals(1, tokens.size()); }
/** @see {@link LogicService#getRule(String)} */ @Test @Verifies( value = "should return Rule associated with the input token", method = "getRule(String)") public void getRule_shouldReturnRuleAssociatedWithTheInputToken() throws Exception { Rule ageRule = Context.getLogicService().getRule("AGE"); Assert.assertNotNull(ageRule); Assert.assertTrue(AgeRule.class.isAssignableFrom(ageRule.getClass())); }
/** @see {@link LogicService#removeRule(String)} */ @Test(expected = LogicException.class) @Verifies(value = "should remove rule", method = "removeRule(String)") public void removeRule_shouldRemoveRule() throws Exception { LogicService logicService = Context.getLogicService(); Rule ageRule = logicService.getRule("AGE"); Assert.assertNotNull(ageRule); logicService.removeRule("AGE"); Rule afterDeleteAgeRule = logicService.getRule("AGE"); Assert.assertNull(afterDeleteAgeRule); }
/** @see {@link LogicService#addTokenTag(String,String)} */ @Test @Verifies(value = "should add tag for a token", method = "addTokenTag(String,String)") public void addTokenTag_shouldAddTagForAToken() throws Exception { LogicService logicService = Context.getLogicService(); int initialTags = logicService.getTokenTags("HIV POSITIVE").size(); logicService.addTokenTag("HIV POSITIVE", "a tag"); int finalTags = logicService.getTokenTags("HIV POSITIVE").size(); Assert.assertEquals(initialTags + 1, finalTags); }
/** @see {@link LogicService#findToken(String)} */ @Test @Verifies( value = "should return all registered token matching the input partially", method = "getTokens(String)") public void getTokens_shouldReturnAllRegisteredTokenMatchingTheInputPartially() throws Exception { LogicService logicService = Context.getLogicService(); Context.getService(TokenService.class) .registerToken("One Another", new ClassRuleProvider(), "one"); Context.getService(TokenService.class) .registerToken("Two Another", new ClassRuleProvider(), "two"); List<String> tokens = logicService.getTokens("ANOTHER"); Assert.assertEquals(2, tokens.size()); }
/** @see {@link LogicService#getTokensByTag(String)} */ @Test @Verifies( value = "should return set of token associated with a tag", method = "getTokensWithTag(String)") public void getTokensWithTag_shouldReturnSetOfTokenAssociatedWithATag() throws Exception { LogicService logicService = Context.getLogicService(); String[] setOfTags = {"birth", "date", "born"}; Context.getService(TokenService.class) .registerToken("ANOTHER AGE RULE", new ClassRuleProvider(), AgeRule.class.getName()); for (String tag : setOfTags) logicService.addTokenTag("ANOTHER AGE RULE", tag); Collection<String> tokens = logicService.getTokensWithTag("birth"); Assert.assertEquals(1, tokens.size()); }
/** TODO make this test use assert statements instead of printing to stdout */ @Test public void shouldHIVPositiveRule() { LogicService logicService = Context.getLogicService(); Cohort patients = new Cohort(); Map<Integer, Result> result = null; patients.addMember(2); try { result = logicService.eval( patients, new LogicCriteriaImpl("PROBLEM ADDED") .contains("HUMAN IMMUNODEFICIENCY VIRUS") .first()); Assert.assertNotNull(result); result = logicService.eval( patients, new LogicCriteriaImpl("PROBLEM ADDED").contains("HIV INFECTED").first()); Assert.assertNotNull(result); result = logicService.eval( patients, new LogicCriteriaImpl("PROBLEM ADDED") .contains("ASYMPTOMATIC HIV INFECTION") .first()); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("HIV VIRAL LOAD").first()); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("HIV VIRAL LOAD, QUALITATIVE").first()); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("CD4 COUNT").lt(200).first()); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("HIV POSITIVE")); Assert.assertNotNull(result); } catch (LogicException e) { log.error("Error generated", e); } }
/** @see {@link LogicService#findTags(String)} */ @Test @Verifies( value = "should return set of tags matching input tag partially", method = "getTags(String)") public void getTags_shouldReturnSetOfTagsMatchingInputTagPartially() throws Exception { LogicService logicService = Context.getLogicService(); TokenService tokenService = Context.getService(TokenService.class); String[] similarTags = {"tags01", "tags02", "tags03", "tags04", "tags05"}; Rule rule = logicService.getRule("AGE"); Assert.assertNotNull(rule); tokenService.registerToken("ANOTHER AGE", new ClassRuleProvider(), AgeRule.class.getName()); for (String tag : similarTags) logicService.addTokenTag("ANOTHER AGE", tag); List<String> tags = logicService.getTags("tags"); Assert.assertEquals(similarTags.length, tags.size()); }
/** @see {@link LogicService#removeTokenTag(String,String)} */ @Test @Verifies(value = "should remove tag from a token", method = "removeTokenTag(String,String)") public void removeTokenTag_shouldRemoveTagFromAToken() throws Exception { LogicService logicService = Context.getLogicService(); String[] setOfTags = {"birth", "date", "born"}; Context.getService(TokenService.class) .registerToken("ANOTHER AGE RULE", new ClassRuleProvider(), AgeRule.class.getName()); for (String tag : setOfTags) logicService.addTokenTag("ANOTHER AGE RULE", tag); Collection<String> retrievedTags = logicService.getTokenTags("ANOTHER AGE RULE"); Assert.assertEquals(setOfTags.length, retrievedTags.size()); logicService.removeTokenTag("ANOTHER AGE RULE", "date"); retrievedTags = logicService.getTokenTags("ANOTHER AGE RULE"); Assert.assertEquals(setOfTags.length - 1, retrievedTags.size()); }
/** @see {@link LogicService#getTagsByToken(String)} */ @Test @Verifies( value = "should return set of tags for a certain token", method = "getTokenTags(String)") public void getTokenTags_shouldReturnSetOfTagsForACertainToken() throws Exception { LogicService logicService = Context.getLogicService(); String[] setOfTags = {"birth", "date", "born"}; Context.getService(TokenService.class) .registerToken("ANOTHER AGE RULE", new ClassRuleProvider(), AgeRule.class.getName()); for (String tag : setOfTags) logicService.addTokenTag("ANOTHER AGE RULE", tag); Set<String> retrievedTags = logicService.getTokenTags("ANOTHER AGE RULE"); Assert.assertEquals(setOfTags.length, retrievedTags.size()); logicService.addTokenTag("ANOTHER AGE RULE", "tag"); retrievedTags = logicService.getTokenTags("ANOTHER AGE RULE"); Assert.assertEquals(setOfTags.length + 1, retrievedTags.size()); }
/** TODO make this test use assert statements instead of printing to stdout */ @Test public void shouldDemographicsRule() { LogicService logicService = Context.getLogicService(); Cohort patients = new Cohort(); Map<Integer, Result> result; patients.addMember(2); try { result = logicService.eval(patients, new LogicCriteriaImpl("BIRTHDATE")); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("AGE")); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("AGE").gt(10)); Assert.assertNotNull(result); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2000); result = logicService.eval(patients, new LogicCriteriaImpl("BIRTHDATE").after(cal.getTime())); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("BIRTHDATE").before(cal.getTime())); Assert.assertNotNull(result); // test the index date functionality Calendar index = Calendar.getInstance(); index.set(1970, 0, 1); result = logicService.eval(patients, new LogicCriteriaImpl("BIRTHDATE")); Assert.assertNotNull(result); result = logicService.eval(patients, new LogicCriteriaImpl("BIRTHDATE").within(Duration.years(5))); Assert.assertNotNull(result); } catch (LogicException e) { log.error("testDemographicsRule: Error generated", e); } }
/** @see {@link LogicService#getTokens()} */ @Test @Verifies(value = "should return all registered token", method = "getAllTokens()") public void getAllTokens_shouldReturnAllRegisteredToken() throws Exception { Collection<String> tokens = Context.getLogicService().getAllTokens(); Assert.assertEquals(19, tokens.size()); }
/** @see {@link LogicService#findToken(String)} */ @Test @Verifies(value = "should not fail when input is null", method = "getTokens(String)") public void getTokens_shouldNotFailWhenInputIsNull() throws Exception { Collection<String> tokens = Context.getLogicService().getTokens(null); Assert.assertNotNull(tokens); }
/** 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); } }
public class getLastObs implements Rule { private Log log = LogFactory.getLog(this.getClass()); private LogicService logicService = Context.getLogicService(); /** * * * * @see org.openmrs.logic.rule.Rule#getParameterList() */ public Set<RuleParameterInfo> getParameterList() { return null; } /** * * * * @see org.openmrs.logic.rule.Rule#getDependencies() */ public String[] getDependencies() { return new String[] {}; } /** * * * * @see org.openmrs.logic.rule.Rule#getTTL() */ public int getTTL() { return 0; // 60 * 30; // 30 minutes } /** * * * * @see org.openmrs.logic.rule.Rule#getDatatype(String) */ public Datatype getDefaultDatatype() { return Datatype.CODED; } /** * 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(); } }