use of org.hl7.fhir.r5.model.Expression in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method buildHumanNameExpression.
private void buildHumanNameExpression(ElementDefinition ed, StringBuilder expression, String discriminator, HumanName name) throws DefinitionException {
if (name.hasExtension())
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN__EXTENSIONS_ARE_NOT_ALLOWED__FOR_DISCRIMINATOR_FOR_SLICE_, discriminator, ed.getId()));
boolean first = true;
expression.append(discriminator + ".where(");
if (name.hasUse()) {
first = false;
expression.append("use = '" + name.getUse().toCode() + "'");
}
if (name.hasText()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("text = '" + name.getText() + "'");
}
if (name.hasFamily()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("family = '" + name.getFamily() + "'");
}
if (name.hasGiven()) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), name.fhirType(), "given"));
}
if (name.hasPrefix()) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), name.fhirType(), "prefix"));
}
if (name.hasSuffix()) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), name.fhirType(), "suffix"));
}
if (name.hasPeriod()) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), name.fhirType(), "period"));
}
if (first) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_NO_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), name.fhirType()));
}
expression.append(").exists()");
}
use of org.hl7.fhir.r5.model.Expression in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method sliceMatches.
/**
* @param element - the candidate that might be in the slice
* @param path - for reporting any errors. the XPath for the element
* @param slicer - the definition of how slicing is determined
* @param ed - the slice for which to test membership
* @param errors
* @param stack
* @param srcProfile
* @return
* @throws DefinitionException
* @throws DefinitionException
* @throws IOException
* @throws FHIRException
*/
private boolean sliceMatches(ValidatorHostContext hostContext, Element element, String path, ElementDefinition slicer, ElementDefinition ed, StructureDefinition profile, List<ValidationMessage> errors, List<ValidationMessage> sliceInfo, NodeStack stack, StructureDefinition srcProfile) throws DefinitionException, FHIRException {
if (!slicer.getSlicing().hasDiscriminator())
// cannot validate in this case
return false;
ExpressionNode n = (ExpressionNode) ed.getUserData("slice.expression.cache");
if (n == null) {
long t = System.nanoTime();
// GG: this approach is flawed because it treats discriminators individually rather than collectively
StringBuilder expression = new StringBuilder("true");
boolean anyFound = false;
Set<String> discriminators = new HashSet<>();
for (ElementDefinitionSlicingDiscriminatorComponent s : slicer.getSlicing().getDiscriminator()) {
String discriminator = s.getPath();
discriminators.add(discriminator);
List<ElementDefinition> criteriaElements = getCriteriaForDiscriminator(path, ed, discriminator, profile, s.getType() == DiscriminatorType.PROFILE, srcProfile);
boolean found = false;
for (ElementDefinition criteriaElement : criteriaElements) {
found = true;
if (s.getType() == DiscriminatorType.TYPE) {
String type = null;
if (!criteriaElement.getPath().contains("[") && discriminator.contains("[")) {
discriminator = discriminator.substring(0, discriminator.indexOf('['));
String lastNode = tail(discriminator);
type = tail(criteriaElement.getPath()).substring(lastNode.length());
type = type.substring(0, 1).toLowerCase() + type.substring(1);
} else if (!criteriaElement.hasType() || criteriaElement.getType().size() == 1) {
if (discriminator.contains("["))
discriminator = discriminator.substring(0, discriminator.indexOf('['));
if (criteriaElement.hasType()) {
type = criteriaElement.getType().get(0).getWorkingCode();
} else if (!criteriaElement.getPath().contains(".")) {
type = criteriaElement.getPath();
} else {
throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_NO_TYPES, discriminator, ed.getId(), profile.getUrl()));
}
} else if (criteriaElement.getType().size() > 1) {
throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_MULTIPLE_TYPES_, discriminator, ed.getId(), profile.getUrl(), criteriaElement.typeSummary()));
} else
throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_NO_TYPES, discriminator, ed.getId(), profile.getUrl()));
if (discriminator.isEmpty())
expression.append(" and $this is " + type);
else
expression.append(" and " + discriminator + " is " + type);
} else if (s.getType() == DiscriminatorType.PROFILE) {
if (criteriaElement.getType().size() == 0) {
throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_A_TYPE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
}
if (criteriaElement.getType().size() != 1) {
throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_ONLY_ONE_TYPE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
}
List<CanonicalType> list = discriminator.endsWith(".resolve()") || discriminator.equals("resolve()") ? criteriaElement.getType().get(0).getTargetProfile() : criteriaElement.getType().get(0).getProfile();
if (list.size() == 0) {
throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_A_TYPE_WITH_A_PROFILE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
} else if (list.size() > 1) {
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(" or ");
for (CanonicalType c : list) {
b.append(discriminator + ".conformsTo('" + c.getValue() + "')");
}
expression.append(" and (" + b + ")");
} else {
expression.append(" and " + discriminator + ".conformsTo('" + list.get(0).getValue() + "')");
}
} else if (s.getType() == DiscriminatorType.EXISTS) {
if (criteriaElement.hasMin() && criteriaElement.getMin() >= 1)
expression.append(" and (" + discriminator + ".exists())");
else if (criteriaElement.hasMax() && criteriaElement.getMax().equals("0"))
expression.append(" and (" + discriminator + ".exists().not())");
else
throw new FHIRException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_ELEMENT_EXISTENCE_BUT_SLICE__NEITHER_SETS_MIN1_OR_MAX0, discriminator, ed.getId()));
} else if (criteriaElement.hasFixed()) {
buildFixedExpression(ed, expression, discriminator, criteriaElement);
} else if (criteriaElement.hasPattern()) {
buildPattternExpression(ed, expression, discriminator, criteriaElement);
} else if (criteriaElement.hasBinding() && criteriaElement.getBinding().hasStrength() && criteriaElement.getBinding().getStrength().equals(BindingStrength.REQUIRED) && criteriaElement.getBinding().hasValueSet()) {
expression.append(" and (" + discriminator + " memberOf '" + criteriaElement.getBinding().getValueSet() + "')");
} else {
found = false;
}
if (found)
break;
}
if (found)
anyFound = true;
}
if (!anyFound) {
if (slicer.getSlicing().getDiscriminator().size() > 1)
throw new DefinitionException(context.formatMessage(I18nConstants.COULD_NOT_MATCH_ANY_DISCRIMINATORS__FOR_SLICE__IN_PROFILE___NONE_OF_THE_DISCRIMINATOR__HAVE_FIXED_VALUE_BINDING_OR_EXISTENCE_ASSERTIONS, discriminators, ed.getId(), profile.getUrl(), discriminators));
else
throw new DefinitionException(context.formatMessage(I18nConstants.COULD_NOT_MATCH_DISCRIMINATOR__FOR_SLICE__IN_PROFILE___THE_DISCRIMINATOR__DOES_NOT_HAVE_FIXED_VALUE_BINDING_OR_EXISTENCE_ASSERTIONS, discriminators, ed.getId(), profile.getUrl(), discriminators));
}
try {
n = fpe.parse(fixExpr(expression.toString(), null));
} catch (FHIRLexerException e) {
if (STACK_TRACE)
e.printStackTrace();
throw new FHIRException(context.formatMessage(I18nConstants.PROBLEM_PROCESSING_EXPRESSION__IN_PROFILE__PATH__, expression, profile.getUrl(), path, e.getMessage()));
}
timeTracker.fpe(t);
ed.setUserData("slice.expression.cache", n);
}
ValidatorHostContext shc = hostContext.forSlicing();
boolean pass = evaluateSlicingExpression(shc, element, path, profile, n);
if (!pass) {
slicingHint(sliceInfo, IssueType.STRUCTURE, element.line(), element.col(), path, false, isProfile(slicer), (context.formatMessage(I18nConstants.DOES_NOT_MATCH_SLICE_, ed.getSliceName())), "discriminator = " + Utilities.escapeXml(n.toString()), null);
for (String url : shc.getSliceRecords().keySet()) {
slicingHint(sliceInfo, IssueType.STRUCTURE, element.line(), element.col(), path, false, isProfile(slicer), context.formatMessage(I18nConstants.DETAILS_FOR__MATCHING_AGAINST_PROFILE_, stack.getLiteralPath(), url), context.formatMessage(I18nConstants.PROFILE__DOES_NOT_MATCH_FOR__BECAUSE_OF_THE_FOLLOWING_PROFILE_ISSUES__, url, stack.getLiteralPath(), errorSummaryForSlicingAsHtml(shc.getSliceRecords().get(url))), errorSummaryForSlicingAsText(shc.getSliceRecords().get(url)));
}
}
return pass;
}
use of org.hl7.fhir.r5.model.Expression in project org.hl7.fhir.core by hapifhir.
the class ValidationEngine method evaluateFhirPath.
public String evaluateFhirPath(String source, String expression) throws FHIRException, IOException {
Content cnt = igLoader.loadContent(source, "validate", false);
FHIRPathEngine fpe = this.getValidator(null).getFHIRPathEngine();
Element e = Manager.parseSingle(context, new ByteArrayInputStream(cnt.focus), cnt.cntType);
ExpressionNode exp = fpe.parse(expression);
return fpe.evaluateToString(new ValidatorHostContext(context, e), e, e, e, exp);
}
use of org.hl7.fhir.r5.model.Expression in project org.hl7.fhir.core by hapifhir.
the class Params method loadCliContext.
/**
* TODO Don't do this all in one for loop. Use the above methods.
*/
public static CliContext loadCliContext(String[] args) throws Exception {
CliContext cliContext = new CliContext();
// load the parameters - so order doesn't matter
for (int i = 0; i < args.length; i++) {
if (args[i].equals(VERSION)) {
cliContext.setSv(VersionUtilities.getCurrentPackageVersion(args[++i]));
} else if (args[i].equals(OUTPUT)) {
if (i + 1 == args.length)
throw new Error("Specified -output without indicating output file");
else
cliContext.setOutput(args[++i]);
} else if (args[i].equals(HTML_OUTPUT)) {
if (i + 1 == args.length)
throw new Error("Specified -html-output without indicating output file");
else
cliContext.setHtmlOutput(args[++i]);
} else if (args[i].equals(PROXY)) {
// ignore next parameter
i++;
} else if (args[i].equals(PROXY_AUTH)) {
i++;
} else if (args[i].equals(PROFILE)) {
String p = null;
if (i + 1 == args.length) {
throw new Error("Specified -profile without indicating profile source");
} else {
p = args[++i];
cliContext.addProfile(p);
}
} else if (args[i].equals(BUNDLE)) {
String p = null;
String r = null;
if (i + 1 == args.length) {
throw new Error("Specified -profile without indicating bundle rule ");
} else {
r = args[++i];
}
if (i + 1 == args.length) {
throw new Error("Specified -profile without indicating profile source");
} else {
p = args[++i];
}
cliContext.getBundleValidationRules().add(new BundleValidationRule(r, p));
} else if (args[i].equals(QUESTIONNAIRE)) {
if (i + 1 == args.length)
throw new Error("Specified -questionnaire without indicating questionnaire mode");
else {
String q = args[++i];
cliContext.setQuestionnaireMode(QuestionnaireMode.fromCode(q));
}
} else if (args[i].equals(LEVEL)) {
if (i + 1 == args.length)
throw new Error("Specified -level without indicating level mode");
else {
String q = args[++i];
cliContext.setLevel(ValidationLevel.fromCode(q));
}
} else if (args[i].equals(NATIVE)) {
cliContext.setDoNative(true);
} else if (args[i].equals(ASSUME_VALID_REST_REF)) {
cliContext.setAssumeValidRestReferences(true);
} else if (args[i].equals(DEBUG)) {
cliContext.setDoDebug(true);
} else if (args[i].equals(SCT)) {
cliContext.setSnomedCT(args[++i]);
} else if (args[i].equals(RECURSE)) {
cliContext.setRecursive(true);
} else if (args[i].equals(SHOW_MESSAGES_FROM_REFERENCES)) {
cliContext.setShowMessagesFromReferences(true);
} else if (args[i].equals(LOCALE)) {
if (i + 1 == args.length) {
throw new Error("Specified -locale without indicating locale");
} else {
cliContext.setLocale(new Locale(args[++i]));
}
} else if (args[i].equals(EXTENSIONS)) {
cliContext.getExtensions().add(args[++i]);
} else if (args[i].equals(NO_INTERNAL_CACHING)) {
cliContext.setNoInternalCaching(true);
} else if (args[i].equals(NO_EXTENSIBLE_BINDING_WARNINGS)) {
cliContext.setNoExtensibleBindingMessages(true);
} else if (args[i].equals(NO_UNICODE_BIDI_CONTROL_CHARS)) {
cliContext.setNoUnicodeBiDiControlChars(true);
} else if (args[i].equals(NO_INVARIANTS)) {
cliContext.setNoInvariants(true);
} else if (args[i].equals(WANT_INVARIANTS_IN_MESSAGES)) {
cliContext.setWantInvariantsInMessages(true);
} else if (args[i].equals(HINT_ABOUT_NON_MUST_SUPPORT)) {
cliContext.setHintAboutNonMustSupport(true);
} else if (args[i].equals(TO_VERSION)) {
cliContext.setTargetVer(args[++i]);
cliContext.setMode(EngineMode.VERSION);
} else if (args[i].equals(DO_NATIVE)) {
cliContext.setCanDoNative(true);
} else if (args[i].equals(NO_NATIVE)) {
cliContext.setCanDoNative(false);
} else if (args[i].equals(TRANSFORM)) {
cliContext.setMap(args[++i]);
cliContext.setMode(EngineMode.TRANSFORM);
} else if (args[i].equals(COMPILE)) {
cliContext.setMap(args[++i]);
cliContext.setMode(EngineMode.COMPILE);
} else if (args[i].equals(NARRATIVE)) {
cliContext.setMode(EngineMode.NARRATIVE);
} else if (args[i].equals(SPREADSHEET)) {
cliContext.setMode(EngineMode.SPREADSHEET);
} else if (args[i].equals(SNAPSHOT)) {
cliContext.setMode(EngineMode.SNAPSHOT);
} else if (args[i].equals(SECURITY_CHECKS)) {
cliContext.setSecurityChecks(true);
} else if (args[i].equals(CRUMB_TRAIL)) {
cliContext.setCrumbTrails(true);
} else if (args[i].equals(VERBOSE)) {
cliContext.setCrumbTrails(true);
} else if (args[i].equals(ALLOW_EXAMPLE_URLS)) {
String bl = args[++i];
if ("true".equals(bl)) {
cliContext.setAllowExampleUrls(true);
} else if ("false".equals(bl)) {
cliContext.setAllowExampleUrls(false);
} else {
throw new Error("Value for " + ALLOW_EXAMPLE_URLS + " not understood: " + bl);
}
} else if (args[i].equals(SHOW_TIMES)) {
cliContext.setShowTimes(true);
} else if (args[i].equals(OUTPUT_STYLE)) {
cliContext.setOutputStyle(args[++i]);
} else if (args[i].equals(SCAN)) {
cliContext.setMode(EngineMode.SCAN);
} else if (args[i].equals(TERMINOLOGY)) {
if (i + 1 == args.length)
throw new Error("Specified -tx without indicating terminology server");
else
cliContext.setTxServer("n/a".equals(args[++i]) ? null : args[i]);
} else if (args[i].equals(TERMINOLOGY_LOG)) {
if (i + 1 == args.length)
throw new Error("Specified -txLog without indicating file");
else
cliContext.setTxLog(args[++i]);
} else if (args[i].equals(TERMINOLOGY_CACHE)) {
if (i + 1 == args.length)
throw new Error("Specified -txCache without indicating file");
else
cliContext.setTxCache(args[++i]);
} else if (args[i].equals(LOG)) {
if (i + 1 == args.length)
throw new Error("Specified -log without indicating file");
else
cliContext.setMapLog(args[++i]);
} else if (args[i].equals(LANGUAGE)) {
if (i + 1 == args.length)
throw new Error("Specified -language without indicating language");
else
cliContext.setLang(args[++i]);
} else if (args[i].equals(IMPLEMENTATION_GUIDE) || args[i].equals(DEFINITION)) {
if (i + 1 == args.length)
throw new Error("Specified " + args[i] + " without indicating ig file");
else {
String s = args[++i];
String version = Common.getVersionFromIGName(null, s);
if (version == null) {
cliContext.addIg(s);
} else {
cliContext.setSv(version);
}
}
} else if (args[i].equals(MAP)) {
if (cliContext.getMap() == null) {
if (i + 1 == args.length)
throw new Error("Specified -map without indicating map file");
else
cliContext.setMap(args[++i]);
} else {
throw new Exception("Can only nominate a single -map parameter");
}
} else if (args[i].startsWith(X)) {
i++;
} else if (args[i].equals(CONVERT)) {
cliContext.setMode(EngineMode.CONVERT);
} else if (args[i].equals(FHIRPATH)) {
cliContext.setMode(EngineMode.FHIRPATH);
if (cliContext.getFhirpath() == null)
if (i + 1 == args.length)
throw new Error("Specified -fhirpath without indicating a FHIRPath expression");
else
cliContext.setFhirpath(args[++i]);
else
throw new Exception("Can only nominate a single -fhirpath parameter");
} else {
cliContext.addSource(args[i]);
}
}
return cliContext;
}
use of org.hl7.fhir.r5.model.Expression in project org.hl7.fhir.core by hapifhir.
the class RdfParser method composeOperationOutcomeOperationOutcomeIssueComponent.
protected void composeOperationOutcomeOperationOutcomeIssueComponent(Complex parent, String parentType, String name, OperationOutcome.OperationOutcomeIssueComponent element, int index) {
if (element == null)
return;
Complex t;
if (Utilities.noString(parentType))
t = parent;
else {
t = parent.predicate("fhir:" + parentType + '.' + name);
}
composeBackboneElement(t, "issue", name, element, index);
if (element.hasSeverityElement())
composeEnum(t, "OperationOutcome", "severity", element.getSeverityElement(), -1);
if (element.hasCodeElement())
composeEnum(t, "OperationOutcome", "code", element.getCodeElement(), -1);
if (element.hasDetails())
composeCodeableConcept(t, "OperationOutcome", "details", element.getDetails(), -1);
if (element.hasDiagnosticsElement())
composeString(t, "OperationOutcome", "diagnostics", element.getDiagnosticsElement(), -1);
for (int i = 0; i < element.getLocation().size(); i++) composeString(t, "OperationOutcome", "location", element.getLocation().get(i), i);
for (int i = 0; i < element.getExpression().size(); i++) composeString(t, "OperationOutcome", "expression", element.getExpression().get(i), i);
}
Aggregations