public static ReadableGuide importGuide(Guide guide, String language) { Logger.getLogger(GuideImporter.class) .debug("Importing guide: " + guide.getId() + ", lang=" + language); Map<String, ArchetypeElementInstantiationRuleLine> gtCodeElementMap = new HashMap<String, ArchetypeElementInstantiationRuleLine>(); ArchetypeElementInstantiationRuleLine dummyAEIRL = new ArchetypeElementInstantiationRuleLine(new ArchetypeInstantiationRuleLine()); dummyAEIRL.setGTCode("currentDateTime"); gtCodeElementMap.put("currentDateTime", dummyAEIRL); GuideDefinition guideDefinition = guide.getDefinition(); TermDefinition termDefinition = null; if (guide.getOntology().getTermDefinitions() != null && guide.getOntology().getTermDefinitions().get(language) != null) { termDefinition = guide.getOntology().getTermDefinitions().get(language); } else { termDefinition = new TermDefinition(); } ReadableGuide readableGuide = new ReadableGuide(termDefinition); if (guideDefinition != null) { List<ArchetypeBinding> ab = guideDefinition.getArchetypeBindings(); if (ab != null) { for (ArchetypeBinding archetypeBinding : ab) { ArchetypeInstantiationRuleLine airl = new ArchetypeInstantiationRuleLine(); ArchetypeReference ar = new ArchetypeReference( archetypeBinding.getDomain(), archetypeBinding.getArchetypeId(), archetypeBinding.getTemplateId(), archetypeBinding.getFunction()); airl.setArchetypeReference(ar); readableGuide.getDefinitionRuleLines().add(airl); if (archetypeBinding.getElements() != null) { for (ElementBinding elementBinding : archetypeBinding.getElements().values()) { ArchetypeElementInstantiationRuleLine aeirl = new ArchetypeElementInstantiationRuleLine(airl); aeirl.setGTCode(elementBinding.getId()); String elementId = archetypeBinding.getArchetypeId() + elementBinding.getPath(); ArchetypeElementVO archetypeElementVO = ArchetypeElements.getArchetypeElement( archetypeBinding.getTemplateId(), elementId); log.debug("elementId: " + elementId + ", archetypeElementVO: " + archetypeElementVO); aeirl.setArchetypeElementVO(archetypeElementVO); gtCodeElementMap.put(elementBinding.getId(), aeirl); } } if (archetypeBinding.getPredicateStatements() != null) { for (ExpressionItem expressionItem : archetypeBinding.getPredicateStatements()) { if (expressionItem instanceof BinaryExpression) { BinaryExpression binaryExpression = (BinaryExpression) expressionItem; if (binaryExpression.getLeft() instanceof Variable && binaryExpression.getRight() instanceof ConstantExpression) { Variable variable = (Variable) binaryExpression.getLeft(); ConstantExpression constantExpression2 = (ConstantExpression) binaryExpression.getRight(); WithElementPredicateAttributeDefinitionRuleLine wepdrl = new WithElementPredicateAttributeDefinitionRuleLine(); airl.addChildRuleLine(wepdrl); String path = variable.getPath(); String dvStr = constantExpression2.getValue(); ArchetypeElementVO archetypeElementVO = ArchetypeElements.getArchetypeElement( archetypeBinding.getTemplateId(), archetypeBinding.getArchetypeId() + path); wepdrl .getArchetypeElementRuleLineDefinitionElement() .setValue(archetypeElementVO); String rmType = archetypeElementVO.getRMType(); if (OpenEHRDataValues.DV_TEXT.equals(rmType) && (OperatorKind.IS_A.equals(binaryExpression.getOperator()) || OperatorKind.IS_NOT_A.equals(binaryExpression.getOperator()))) { rmType = OpenEHRDataValues.DV_CODED_TEXT; } DataValue dv = parseDataValue(rmType, dvStr); wepdrl.getDataValueRuleLineElement().setValue(dv); wepdrl .getComparisonOperatorRuleLineElement() .setValue(binaryExpression.getOperator()); } else if (binaryExpression.getLeft() instanceof Variable && binaryExpression.getRight() instanceof ExpressionItem) { Variable variable = (Variable) binaryExpression.getLeft(); ExpressionItem expressionItemAux = binaryExpression.getRight(); WithElementPredicateExpressionDefinitionRuleLine wepdrl = new WithElementPredicateExpressionDefinitionRuleLine(); airl.addChildRuleLine(wepdrl); String path = variable.getPath(); ArchetypeElementVO archetypeElementVO = ArchetypeElements.getArchetypeElement( archetypeBinding.getTemplateId(), archetypeBinding.getArchetypeId() + path); wepdrl .getArchetypeElementRuleLineDefinitionElement() .setValue(archetypeElementVO); wepdrl.getExpressionRuleLineElement().setValue(expressionItemAux); wepdrl .getComparisonOperatorRuleLineElement() .setValue(binaryExpression.getOperator()); } } } } } } if (guideDefinition.getPreConditionExpressions() != null) { for (ExpressionItem expressionItem : guideDefinition.getPreConditionExpressions()) { processExpressionItem( readableGuide.getPreconditionRuleLines(), null, expressionItem, gtCodeElementMap); } } Map<String, Rule> rulesMap = guideDefinition.getRules(); if (rulesMap != null) { List<Rule> rules = new ArrayList<Rule>(rulesMap.values()); Collections.sort(rules, new RulePriorityComparator()); for (Rule rule : rules) { ReadableRule rr = new ReadableRule(readableGuide.getTermDefinition(), rule.getId()); readableGuide.getReadableRules().put(rule.getId(), rr); if (rule.getWhenStatements() != null) { for (ExpressionItem expressionItem : rule.getWhenStatements()) { processExpressionItem( rr.getConditionRuleLines(), null, expressionItem, gtCodeElementMap); } } if (rule.getThenStatements() != null) { for (ExpressionItem expressionItem : rule.getThenStatements()) { processExpressionItem( rr.getActionRuleLines(), null, expressionItem, gtCodeElementMap); } } } } } setTermDefinitionsOnRuleLines(readableGuide.getDefinitionRuleLines(), termDefinition); setTermDefinitionsOnRuleLines(readableGuide.getPreconditionRuleLines(), termDefinition); for (ReadableRule readableRule : readableGuide.getReadableRules().values()) { setTermDefinitionsOnRuleLines(readableRule.getConditionRuleLines(), termDefinition); setTermDefinitionsOnRuleLines(readableRule.getActionRuleLines(), termDefinition); } return readableGuide; }
protected static void processComparisonExpression( Collection<RuleLine> ruleLines, RuleLine parentRuleLine, BinaryExpression binaryExpression, Map<String, ArchetypeElementInstantiationRuleLine> gtCodeELementMap) { GTCodeRuleLineElement gtCodeRuleLineElement = null; String attribute = null; String gtCode = null; if (binaryExpression.getLeft() instanceof Variable) { Variable var = (Variable) binaryExpression.getLeft(); gtCode = var.getCode(); log.debug("gtCode: " + gtCode); gtCodeRuleLineElement = gtCodeELementMap.get(gtCode).getGTCodeRuleLineElement(); attribute = var.getAttribute(); } if (gtCodeRuleLineElement != null) { if (attribute == null) { if (binaryExpression.getRight() instanceof ConstantExpression) { ConstantExpression constantExpression = (ConstantExpression) binaryExpression.getRight(); String dvStr = constantExpression.getValue(); DataValue dv = null; if (!dvStr.equals("null")) { log.debug("codeRuleLineElement: " + gtCodeRuleLineElement.getValue()); String rmType = null; if (!OpenEHRConst.CURRENT_DATE_TIME_ID.equals(gtCode)) { ArchetypeElementVO archetypeElementVO = gtCodeELementMap.get(gtCodeRuleLineElement.getValue()).getArchetypeElement(); rmType = archetypeElementVO.getRMType(); if (OpenEHRDataValues.DV_TEXT.equals(rmType) && (OperatorKind.IS_A.equals(binaryExpression.getOperator()) || OperatorKind.IS_NOT_A.equals(binaryExpression.getOperator()))) { rmType = OpenEHRDataValues.DV_CODED_TEXT; } } else { rmType = OpenEHRDataValues.DV_DATE_TIME; } dv = parseDataValue(rmType, dvStr); } if (dv != null) { ElementComparisonWithDVConditionRuleLine eccrl = new ElementComparisonWithDVConditionRuleLine(); eccrl.getArchetypeElementRuleLineElement().setValue(gtCodeRuleLineElement); eccrl.getDataValueRuleLineElement().setValue(dv); eccrl.getComparisonOperatorRuleLineElement().setValue(binaryExpression.getOperator()); addRuleLine(eccrl, ruleLines, parentRuleLine); } else { ElementInitializedConditionRuleLine eicrl = new ElementInitializedConditionRuleLine(); eicrl.getArchetypeElementRuleLineElement().setValue(gtCodeRuleLineElement); eicrl.getExistenceOperatorRuleLineElement().setOperator(binaryExpression.getOperator()); addRuleLine(eicrl, ruleLines, parentRuleLine); } } else if (binaryExpression.getRight() instanceof Variable) { Variable varRight = (Variable) binaryExpression.getRight(); String gtCodeAux = varRight.getCode(); ElementComparisonWithElementConditionRuleLine eccrl = new ElementComparisonWithElementConditionRuleLine(); eccrl.getArchetypeElementRuleLineElement().setValue(gtCodeRuleLineElement); eccrl .getSecondArchetypeElementRuleLineElement() .setValue(gtCodeELementMap.get(gtCodeAux).getGTCodeRuleLineElement()); eccrl.getComparisonOperatorRuleLineElement().setValue(binaryExpression.getOperator()); addRuleLine(eccrl, ruleLines, parentRuleLine); } else { log.error( "Unknown expression '" + binaryExpression.getRight().getClass().getName() + "'"); } } else { if (attribute.equals(OpenEHRConst.NULL_FLAVOR_ATTRIBUTE)) { ElementComparisonWithNullValueConditionRuleLine ecwnvc = new ElementComparisonWithNullValueConditionRuleLine(); ecwnvc.getArchetypeElementRuleLineElement().setValue(gtCodeRuleLineElement); ConstantExpression constantExpression = (ConstantExpression) binaryExpression.getRight(); String dvStr = constantExpression.getValue(); DataValue dv = parseDataValue(OpenEHRDataValues.DV_CODED_TEXT, dvStr); if (dv instanceof DvCodedText) { ecwnvc.getNullValueRuleLineElement().setValue((DvCodedText) dv); } ecwnvc .getEqualityComparisonOperatorRuleLineElement() .setValue(binaryExpression.getOperator()); addRuleLine(ecwnvc, ruleLines, parentRuleLine); } else { // Expression ElementAttributeComparisonConditionRuleLine eaccrl = new ElementAttributeComparisonConditionRuleLine(); eaccrl.getArchetypeElementAttributeRuleLineElement().setAttributeFunction(attribute); ArchetypeElementRuleLineElement aerle = new ArchetypeElementRuleLineElement(eaccrl); aerle.setValue(gtCodeRuleLineElement); eaccrl.getArchetypeElementAttributeRuleLineElement().setValue(aerle); eaccrl.getExpressionRuleLineElement().setValue(binaryExpression.getRight()); eaccrl.getComparisonOperatorRuleLineElement().setValue(binaryExpression.getOperator()); addRuleLine(eaccrl, ruleLines, parentRuleLine); } } } else { log.error("Unknown expression '" + binaryExpression.getLeft().getClass().getName() + "'"); } }