Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules validation #167

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public ArchetypeValidator(MetaModels models) {
validationsPhase3 = new ArrayList<>();
validationsPhase3.add(new AnnotationsValidation());
validationsPhase3.add(new FlatFormValidation());
validationsPhase3.add(new RulesValidation());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ public enum ErrorType implements MessageCode {
VDSEV(I18n.register("archetype slot 'exclude' constraint validity. The 'exclude' constraint in an archetype slot must conform to the slot constraint validity rules.")),
VACSO(I18n.register("single-valued attribute child object occurrences validity: the occurrences of a child object of a single-valued attribute cannot have an upper limit greater than 1.")),
VACMCU(I18n.register("cardinality/occurrences upper bound validity: where a cardinality with a finite upper bound is stated on an attribute, for all immediate child objects for which an occurrences constraint is stated, the occurrences must either have an open upper bound (i.e. n..*) which is interpreted as the maximum value allowed within the cardinality, or else a finite upper bound which is ⇐ the cardinality upper bound.")),
WOUC(I18n.register("code in terminology not used in archetype definition"));



WOUC(I18n.register("code in terminology not used in archetype definition")),
VRRLPAR(I18n.register("path in rules does not exist")),
RULES_VARIABLE_NOT_DEFINED(I18n.register("Variable not defined"));

private final String description;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.nedap.archie.archetypevalidator.validations;

import com.google.common.collect.Lists;
import com.nedap.archie.aom.Archetype;
import com.nedap.archie.aom.ArchetypeModelObject;
import com.nedap.archie.aom.CAttribute;
import com.nedap.archie.aom.CObject;
import com.nedap.archie.archetypevalidator.ArchetypeValidationBase;
import com.nedap.archie.archetypevalidator.ErrorType;
import com.nedap.archie.paths.PathSegment;
import com.nedap.archie.paths.PathUtil;
import com.nedap.archie.query.APathQuery;
import com.nedap.archie.rules.Assertion;
import com.nedap.archie.rules.BinaryOperator;
import com.nedap.archie.rules.Expression;
import com.nedap.archie.rules.ExpressionVariable;
import com.nedap.archie.rules.ForAllStatement;
import com.nedap.archie.rules.Function;
import com.nedap.archie.rules.ModelReference;
import com.nedap.archie.rules.RuleStatement;
import com.nedap.archie.rules.UnaryOperator;
import org.openehr.utils.message.I18n;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RulesValidation extends ArchetypeValidationBase {


private Map<String, String> variableToPathMap;

@Override
public void validate() {
variableToPathMap = new LinkedHashMap<>();
if(archetype.getRules() != null && archetype.getRules().getRules() != null) {
for(RuleStatement statement:archetype.getRules().getRules()) {
if(statement instanceof Assertion) {
Assertion toValidate = (Assertion) statement;
validate(toValidate);
} else if (statement instanceof ExpressionVariable) {
ExpressionVariable variable = (ExpressionVariable) statement;
validate(variable.getExpression());

}
}
}
}

private void validate(Assertion toValidate) {
Expression expression = toValidate.getExpression();
validate(expression);
}

private void validate(Expression expression) {
if(expression instanceof BinaryOperator) {
validate((BinaryOperator) expression);
} else if (expression instanceof ModelReference) {
validate((ModelReference) expression);
} else if (expression instanceof UnaryOperator) {
validate((UnaryOperator) expression);
} else if (expression instanceof Function) {
validate((Function) expression);
} else if (expression instanceof ForAllStatement) {
validate((ForAllStatement) expression);
}
}

private void validate(BinaryOperator operator) {
validate(operator.getLeftOperand());
validate(operator.getRightOperand());
}

private void validate(UnaryOperator operator) {
validate(operator.getOperand());
}

private void validate(ModelReference reference) {
if(!validatePath(getPath(reference))) {
this.addWarning(ErrorType.VRRLPAR, reference.toString());
}
}

private void validate(Function function) {
for(Expression argument:function.getArguments()) {
validate(argument);
}
}

private void validate(ForAllStatement statement) {
if(statement.getPathExpression() instanceof ModelReference) {
String path = getPath((ModelReference) statement.getPathExpression());
variableToPathMap.put(statement.getVariableName(), path);
validate(statement.getAssertion());
variableToPathMap.remove(statement.getVariableName());
} else {
//cannot validate yet
}

}

private String getPath(ModelReference pathExpression) {
if(pathExpression.getVariableReferencePrefix() != null && !pathExpression.getVariableReferencePrefix().isEmpty()) {
if(variableToPathMap.containsKey(pathExpression.getVariableReferencePrefix())) {
return variableToPathMap.get(pathExpression.getVariableReferencePrefix()) + pathExpression.getPath();
} else {
addWarning(ErrorType.RULES_VARIABLE_NOT_DEFINED, I18n.t("Variable {0} used, but not defined", pathExpression.getVariableReferencePrefix()));
}
}
return pathExpression.getPath();
}

private boolean validatePath(String path) {
Archetype operationalTemplate = repository.getOperationalTemplate(archetype.getArchetypeId().toString());
List<PathSegment> pathSegments = new APathQuery(path).getPathSegments();
int i = pathSegments.size() -1;
List<ArchetypeModelObject> archetypeModelObjects = null;
String subPath = null;
for(; i >= 0; i--) {
subPath = joinPathUntil(pathSegments, i);
archetypeModelObjects = operationalTemplate.itemsAtPath(subPath);
if(!archetypeModelObjects.isEmpty()) {
break;
}
}
if(archetypeModelObjects.isEmpty()) {
return false;
} else {
String restOfPath = i >= pathSegments.size() -1 ? "" : PathUtil.getPath(pathSegments.subList(i+1, pathSegments.size()));
if(restOfPath.isEmpty()) {
return true;
}
for(ArchetypeModelObject object: archetypeModelObjects) {
List<String> typeNames = getTypeNames(object);
for(String typeName:typeNames) {
if (this.combinedModels.hasReferenceModelPath(typeName, restOfPath)) {
return true;
}
}
}
}
return false;

}

private List<String> getTypeNames(ArchetypeModelObject object) {
if(object instanceof CObject) {
return Lists.newArrayList(((CObject) object).getRmTypeName());
} else if (object instanceof CAttribute) {
CAttribute attribute = (CAttribute) object;
ArrayList<String> result= new ArrayList<>();
for(CObject child:attribute.getChildren()) {
result.addAll(getTypeNames(child));
}
return result;
}
return Collections.emptyList();
}

private String joinPathUntil(List<PathSegment> pathSegments, int i) {
return PathUtil.getPath(pathSegments.subList(0, i+1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.nedap.archie.archetypevalidator;

import com.nedap.archie.flattener.FullArchetypeRepository;
import com.nedap.archie.rminfo.ArchieRMInfoLookup;
import com.nedap.archie.rminfo.MetaModels;
import com.nedap.archie.rminfo.ReferenceModels;
import com.nedap.archie.testutil.TestUtil;
import org.junit.Test;
import org.openehr.referencemodels.BuiltinReferenceModels;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertTrue;

public class RuleStatementValidationTest {

private static final Logger logger = LoggerFactory.getLogger(CKMArchetypeValidatorTest.class);

private static Set<String> archetypesWithKnownErrors = new HashSet<>();
static {

}


@Test
public void rulesExamplesValidTest() {

FullArchetypeRepository repository = TestUtil.parseRuleExamples();
ReferenceModels models = new ReferenceModels();
models.registerModel(ArchieRMInfoLookup.getInstance());
logger.info("archetypes parsed: " + repository.getAllArchetypes().size());
repository.compile(models);

runTest(repository);

}

private void runTest(FullArchetypeRepository repository) {
List<ValidationResult> allValidationResults = repository.getAllValidationResults();
List<ValidationResult> resultWithErrors = allValidationResults.stream()
.filter(r -> !r.passes())
.filter(r -> !archetypesWithKnownErrors.contains(r.getArchetypeId()))
.collect(Collectors.toList());

StringBuilder error = new StringBuilder();
for(ValidationResult result:resultWithErrors) {
error.append(result);
error.append("\n\n");
}

assertTrue(error.toString(), resultWithErrors.isEmpty());
}
}
24 changes: 24 additions & 0 deletions tools/src/test/java/com/nedap/archie/testutil/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,28 @@ public static FullArchetypeRepository parseCKM() {
return result;
}

public static FullArchetypeRepository parseRuleExamples() {
InMemoryFullArchetypeRepository result = new InMemoryFullArchetypeRepository();
Reflections reflections = new Reflections("com/nedap/archie/rules/evaluation", new ResourcesScanner());
List<String> adlFiles = new ArrayList(reflections.getResources(Pattern.compile(".*\\.adls")));
for(String file:adlFiles) {
Archetype archetype;
ANTLRParserErrors errors;
try (InputStream stream = TestUtil.class.getResourceAsStream("/" + file)) {
ADLParser parser = new ADLParser();
parser.setLogEnabled(false);
archetype = parser.parse(stream);
errors = parser.getErrors();
if (errors.hasNoErrors()) {
result.addArchetype(archetype);
} else {
logger.warn("error parsing archetype: {}", errors);
}
} catch (Exception e) {
logger.warn("exception parsing archetype {}", file, e);
}
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archetype (adl_version=2.0.5; rm_release=1.0.2; generated)
openEHR-EHR-OBSERVATION.matches.v1.0.0
openEHR-EHR-OBSERVATION.and.v1.0.0

language
original_language = <[ISO_639-1::en]>
Expand Down Expand Up @@ -71,12 +71,56 @@ terminology
term_definitions = <
["en"] = <
["id1"] = <
text = <"Blood Pressure">
description = <"The local measurement of arterial blood pressure which is a surrogate for arterial. pressure in the systemic circulation. Most commonly, use of the term 'blood pressure' refers to measurement of brachial artery pressure in the upper arm.">
>
text = <"Blood Pressure">
description = <"The local measurement of arterial blood pressure which is a surrogate for arterial. pressure in the systemic circulation. Most commonly, use of the term 'blood pressure' refers to measurement of brachial artery pressure in the upper arm.">
>
["id3"] = <
text = <"Event">
description = <"Event">
>
["id4"] = <
text = <"Data">
description = <"Data">
>
["id5"] = <
text = <"element 1">
description = <"element 1">
>
["id6"] = <
text = <"element 2">
description = <"element 2">
>
["id7"] = <
text = <"element 3">
description = <"element 3">
>
["at1"] = <
text = <"Option 1">
description = <"Option 1">
>
["at2"] = <
text = <"Option 2">
description = <"Option 2">
>
["at3"] = <
text = <"Option 3">
description = <"Option 3">
>
["at5"] = <
text = <"Option 5">
description = <"Option 5">
>
["at6"] = <
text = <"Option 6">
description = <"Option 6">
>
["at7"] = <
text = <"Option 7">
description = <"Option 7">
>
["ac1"] = <
text = <"Value set 1">
description = <"Value set 1">
>
>
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archetype (adl_version=2.0.5; rm_release=1.0.2; generated)
openEHR-EHR-OBSERVATION.boolean_relops.v1.0.0
openEHR-EHR-OBSERVATION.boolean_operand_relops.v1.0.0

language
original_language = <[ISO_639-1::en]>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archetype (adl_version=2.0.5; rm_release=1.0.2; generated)
openEHR-EHR-OBSERVATION.multiplicity.v1.0.0
openEHR-EHR-OBSERVATION.calculated_path_values.v1.0.0

language
original_language = <[ISO_639-1::en]>
Expand Down Expand Up @@ -79,9 +79,29 @@ terminology
term_definitions = <
["en"] = <
["id1"] = <
text = <"Blood Pressure">
description = <"The local measurement of arterial blood pressure which is a surrogate for arterial. pressure in the systemic circulation. Most commonly, use of the term 'blood pressure' refers to measurement of brachial artery pressure in the upper arm.">
>
text = <"Blood Pressure">
description = <"The local measurement of arterial blood pressure which is a surrogate for arterial. pressure in the systemic circulation. Most commonly, use of the term 'blood pressure' refers to measurement of brachial artery pressure in the upper arm.">
>
["id3"] = <
text = <"Event">
description = <"Event">
>
["id4"] = <
text = <"Data">
description = <"Data">
>
["id5"] = <
text = <"element 1">
description = <"element 1">
>
["id6"] = <
text = <"element 2">
description = <"element 2">
>
["id7"] = <
text = <"element 3">
description = <"element 3">
>
>
>

Loading