package org.openmrs.module.logicws.test; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import org.openmrs.Cohort; import org.openmrs.Obs; import org.openmrs.api.context.Context; import org.openmrs.cohort.CohortDefinition; import org.openmrs.cohort.CohortDefinitionItemHolder; import org.openmrs.logic.LogicCriteria; import org.openmrs.logic.LogicException; import org.openmrs.logic.LogicService; import org.openmrs.logic.result.Result; import org.openmrs.test.BaseModuleContextSensitiveTest; /** * Test Logic Service API. * * @author Kevin Peters */ public class LogicWsTest extends BaseModuleContextSensitiveTest { /* * * @Override protected void onSetUpInTransaction() throws Exception { * initializeInMemoryDatabase(); * * executeDataSet("org/openmrs/module/logicws/test/include/LogicServiceIssue.xml"); * * authenticate(); } * */ @Override protected void onSetUpBeforeTransaction() throws Exception { super.onSetUpBeforeTransaction(); authenticate(); } @Override public Boolean useInMemoryDatabase() { return false; } /** * Public constructor */ public LogicWsTest() { } /** * Tests whether the logic service returns tokens * * @throws Exception */ public void testGetTokens() throws Exception { Set tokens = Context.getLogicService().getTokens(); System.out.println("Tokens: " + tokens); } /* * public void testAddTokenTags() throws Exception { * Context.getLogicService().addTokenTag("AGE", "COMMON"); * Context.getLogicService().addTokenTag("GENDER", "COMMON"); * Context.getLogicService().addTokenTag("DEATH DATE", "COMMON"); * * System.out.println(Context.getLogicService().getTokensByTag("COMMON")); * * System.out.println(Context.getLogicService().getTagsByToken("GENDER")); * //Context.getLogicService(). } */ public void testGetCohorts() { for (CohortDefinitionItemHolder c : Context.getCohortService() .getAllCohortDefinitions()) { System.out.println(""); } } public void testGetTags() throws Exception { Set tags = Context.getLogicService().findTags(""); System.out.println("Tags: " + tags); } public void testAggregate() { CohortDefinition cohortDefinition = Context.getCohortService() .getCohortDefinition( "5:org.openmrs.cohort.StaticCohortDefinition"); Cohort patients = Context.getCohortService().evaluate(cohortDefinition, null); LogicService ls = Context.getLogicService(); List tokens = new ArrayList(); //tokens.add(LogicCriteria.parse("{GENDER}")); tokens.add(LogicCriteria.parse("LAST {AGE}")); //tokens.add(LogicCriteria.parse("{WEIGHT (KG)}")); Map> resultsForTokens = null; try { resultsForTokens = ls.eval(patients, tokens); } catch (LogicException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(resultsForTokens); } public void testGetStackedData() { CohortDefinition cohortDefinition = Context.getCohortService() .getCohortDefinition( "5:org.openmrs.cohort.StaticCohortDefinition"); Cohort patients = Context.getCohortService().evaluate(cohortDefinition, null); LogicService ls = Context.getLogicService(); List tokens = new ArrayList(); tokens.add(LogicCriteria.parse("{WEIGHT (KG)}")); tokens.add(LogicCriteria.parse("{AGE}")); tokens.add(LogicCriteria.parse("{HEIGHT (CM)}")); Map> resultsForTokens = null; try { resultsForTokens = ls.eval(patients, tokens); } catch (LogicException e) { // TODO Auto-generated catch block e.printStackTrace(); } String save = ""; for (Map.Entry> resByCrit : resultsForTokens .entrySet()) { for (Map.Entry resByPat : resByCrit.getValue() .entrySet()) { if (resByPat.getValue().size() > 0) { for (int i = 0; i < resByPat.getValue().size(); i++) { System.out.println(""); // always print the patient id first System.out.println("" + resByPat.getKey() + ""); System.out.println("" + resByCrit.getKey().getRootToken() + ""); System.out.println("" + resByPat.getValue().get(i).toString() + ""); System.out.println("" + resByPat.getValue().get(i).getResultDate() + ""); Obs obs = null; try { obs = (Obs) resByPat.getValue().get(i).getResultObject(); } catch(ClassCastException e) { } System.out.println("" + obs.getLocation() + ""); System.out.println("" + obs.getEncounter().getEncounterDatetime() + ""); System.out.println("" + obs.getEncounter().getEncounterType() + ""); // assertFalse(save.equals(resByPat.getValue().get(i).toString())); save = resByPat.getValue().get(i).toString(); System.out.println(""); } } else { System.out.println(""); // always print the patient id first System.out.println("" + resByPat.getKey() + ""); System.out.println("" + resByCrit.getKey().getRootToken() + ""); System.out.println("" + resByPat.getValue().toString() + ""); System.out.println("" + resByPat.getValue().getResultDate() + ""); Obs obs = null; try { obs = (Obs) resByPat.getValue().getResultObject(); } catch(ClassCastException e) { } if (obs != null) { System.out.println("" + obs.getLocation() + ""); System.out.println("" + obs.getEncounter().getEncounterDatetime() + ""); System.out.println("" + obs.getEncounter().getEncounterType() + ""); System.out.println(""); } System.out.println(""); } } } } public void testCauseOfDeath() { CohortDefinition cohortDefinition = Context.getCohortService() .getCohortDefinition( "5:org.openmrs.cohort.StaticCohortDefinition"); Cohort patients = Context.getCohortService().evaluate(cohortDefinition, null); LogicService ls = Context.getLogicService(); List tokens = new ArrayList(); tokens.add(LogicCriteria.parse("{CAUSE OF DEATH}")); //tokens.add(LogicCriteria.parse("{AGE}")); //tokens.add(LogicCriteria.parse("{HEIGHT (CM)}")); Map> resultsForTokens = null; try { resultsForTokens = ls.eval(patients, tokens); } catch (LogicException e) { // TODO Auto-generated catch block e.printStackTrace(); } String save = ""; for (Map.Entry> resByCrit : resultsForTokens .entrySet()) { for (Map.Entry resByPat : resByCrit.getValue() .entrySet()) { if (resByPat.getValue().size() > 0) { for (int i = 0; i < resByPat.getValue().size(); i++) { System.out.println(""); // always print the patient id first System.out.println("" + resByPat.getKey() + ""); System.out.println("" + resByCrit.getKey().getRootToken() + ""); System.out.println("" + resByPat.getValue().get(i).toString() + ""); System.out.println("" + resByPat.getValue().get(i).getResultDate() + ""); Obs obs = null; try { obs = (Obs) resByPat.getValue().get(i).getResultObject(); } catch(ClassCastException e) { } System.out.println("" + obs.getLocation() + ""); System.out.println("" + obs.getEncounter().getEncounterDatetime() + ""); System.out.println("" + obs.getEncounter().getEncounterType() + ""); // assertFalse(save.equals(resByPat.getValue().get(i).toString())); save = resByPat.getValue().get(i).toString(); System.out.println(""); } } else { System.out.println(""); // always print the patient id first System.out.println("" + resByPat.getKey() + ""); System.out.println("" + resByCrit.getKey().getRootToken() + ""); System.out.println("" + resByPat.getValue().toString() + ""); System.out.println("" + resByPat.getValue().getResultDate() + ""); Obs obs = null; try { obs = (Obs) resByPat.getValue().getResultObject(); } catch(ClassCastException e) { } if (obs != null) { System.out.println("" + obs.getLocation() + ""); System.out.println("" + obs.getEncounter().getEncounterDatetime() + ""); System.out.println("" + obs.getEncounter().getEncounterType() + ""); System.out.println(""); } System.out.println(""); } } } } }