Ejemplo n.º 1
0
  /*
   * List of expressions in GDL 1. guide definition pre-conditions 2.
   * archetype binding predicates 3. rule when statements 4. rule then
   * statements
   */
  private void bindExpressions(Guide guide) throws Exception {
    List<String> preConditions = guide.getDefinition().getPreConditions();

    guide.getDefinition().setPreConditionExpressions(parseExpressions(preConditions));

    GuideDefinition definition = guide.getDefinition();
    if (definition.getArchetypeBindings() != null) {
      Collection<ArchetypeBinding> bindings = definition.getArchetypeBindings();
      for (ArchetypeBinding binding : bindings) {
        binding.setPredicateStatements(parseExpressions(binding.getPredicates()));
      }
      if (definition.getRules() != null) {
        Collection<Rule> rules = definition.getRules().values();
        for (Rule rule : rules) {
          rule.setWhenStatements(parseExpressions(rule.getWhen()));
          rule.setThenStatements(toAssignments(parseExpressions(rule.getThen())));
        }
      }
    }
  }
Ejemplo n.º 2
0
  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;
  }