Search in sources :

Example 16 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method checkAddTypeUnion.

private void checkAddTypeUnion(String path, List<TypeRefComponent> results, TypeRefComponent nw) throws DefinitionException, IOException, FHIRFormatError {
    boolean pfound = false;
    boolean tfound = false;
    nw = nw.copy();
    if (nw.hasAggregation())
        throw new DefinitionException("Aggregation not supported: " + path);
    for (TypeRefComponent ex : results) {
        if (Utilities.equals(ex.getCode(), nw.getCode())) {
            if (!ex.hasProfile() && !nw.hasProfile())
                pfound = true;
            else if (!ex.hasProfile()) {
                pfound = true;
            } else if (!nw.hasProfile()) {
                pfound = true;
                ex.setProfile(null);
            } else {
                // both have profiles. Is one derived from the other?
                StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getProfile());
                StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getProfile());
                if (sdex != null && sdnw != null) {
                    if (sdex == sdnw) {
                        pfound = true;
                    } else if (derivesFrom(sdex, sdnw)) {
                        ex.setProfile(nw.getProfile());
                        pfound = true;
                    } else if (derivesFrom(sdnw, sdex)) {
                        pfound = true;
                    } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
                        ProfileComparison comp = compareProfiles(sdex, sdnw);
                        if (comp.getSuperset() != null) {
                            pfound = true;
                            ex.setProfile("#" + comp.id);
                        }
                    }
                }
            }
            if (!ex.hasTargetProfile() && !nw.hasTargetProfile())
                tfound = true;
            else if (!ex.hasTargetProfile()) {
                tfound = true;
            } else if (!nw.hasTargetProfile()) {
                tfound = true;
                ex.setTargetProfile(null);
            } else {
                // both have profiles. Is one derived from the other?
                StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getTargetProfile());
                StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getTargetProfile());
                if (sdex != null && sdnw != null) {
                    if (sdex == sdnw) {
                        tfound = true;
                    } else if (derivesFrom(sdex, sdnw)) {
                        ex.setTargetProfile(nw.getTargetProfile());
                        tfound = true;
                    } else if (derivesFrom(sdnw, sdex)) {
                        tfound = true;
                    } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
                        ProfileComparison comp = compareProfiles(sdex, sdnw);
                        if (comp.getSuperset() != null) {
                            tfound = true;
                            ex.setTargetProfile("#" + comp.id);
                        }
                    }
                }
            }
        }
    }
    if (!tfound || !pfound)
        results.add(nw);
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 17 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class ProfileComparer method compareElements.

/**
 * left and right refer to the same element. Are they compatible?
 * @param outcome
 * @param outcome
 * @param path
 * @param left
 * @param right
 * @- if there's a problem that needs fixing in this code
 * @throws DefinitionException
 * @throws IOException
 * @throws FHIRFormatError
 */
private boolean compareElements(ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException, IOException, FHIRFormatError {
    // preconditions:
    assert (path != null);
    assert (left != null);
    assert (right != null);
    assert (left.path().equals(right.path()));
    // we ignore slicing right now - we're going to clone the root one anyway, and then think about clones
    // simple stuff
    ElementDefinition subset = new ElementDefinition();
    subset.setPath(left.path());
    // not allowed to be different:
    // can't be bothered even testing this one
    subset.getRepresentation().addAll(left.current().getRepresentation());
    if (!outcome.ruleCompares(subset, left.current().getDefaultValue(), right.current().getDefaultValue(), path + ".defaultValue[x]", BOTH_NULL))
        return false;
    subset.setDefaultValue(left.current().getDefaultValue());
    if (!outcome.ruleEqual(path, subset, left.current().getMeaningWhenMissing(), right.current().getMeaningWhenMissing(), "meaningWhenMissing Must be the same", true))
        return false;
    subset.setMeaningWhenMissing(left.current().getMeaningWhenMissing());
    if (!outcome.ruleEqual(subset, left.current().getIsModifier(), right.current().getIsModifier(), path, "isModifier"))
        return false;
    subset.setIsModifier(left.current().getIsModifier());
    if (!outcome.ruleEqual(subset, left.current().getIsSummary(), right.current().getIsSummary(), path, "isSummary"))
        return false;
    subset.setIsSummary(left.current().getIsSummary());
    // descriptive properties from ElementDefinition - merge them:
    subset.setLabel(mergeText(subset, outcome, path, "label", left.current().getLabel(), right.current().getLabel()));
    subset.setShort(mergeText(subset, outcome, path, "short", left.current().getShort(), right.current().getShort()));
    subset.setDefinition(mergeText(subset, outcome, path, "definition", left.current().getDefinition(), right.current().getDefinition()));
    subset.setComment(mergeText(subset, outcome, path, "comments", left.current().getComment(), right.current().getComment()));
    subset.setRequirements(mergeText(subset, outcome, path, "requirements", left.current().getRequirements(), right.current().getRequirements()));
    subset.getCode().addAll(mergeCodings(left.current().getCode(), right.current().getCode()));
    subset.getAlias().addAll(mergeStrings(left.current().getAlias(), right.current().getAlias()));
    subset.getMapping().addAll(mergeMappings(left.current().getMapping(), right.current().getMapping()));
    // left will win for example
    subset.setExample(left.current().hasExample() ? left.current().getExample() : right.current().getExample());
    subset.setMustSupport(left.current().getMustSupport() || right.current().getMustSupport());
    ElementDefinition superset = subset.copy();
    // compare and intersect
    superset.setMin(unionMin(left.current().getMin(), right.current().getMin()));
    superset.setMax(unionMax(left.current().getMax(), right.current().getMax()));
    subset.setMin(intersectMin(left.current().getMin(), right.current().getMin()));
    subset.setMax(intersectMax(left.current().getMax(), right.current().getMax()));
    outcome.rule(subset, subset.getMax().equals("*") || Integer.parseInt(subset.getMax()) >= subset.getMin(), path, "Cardinality Mismatch: " + card(left) + "/" + card(right));
    superset.getType().addAll(unionTypes(path, left.current().getType(), right.current().getType()));
    subset.getType().addAll(intersectTypes(subset, outcome, path, left.current().getType(), right.current().getType()));
    outcome.rule(subset, !subset.getType().isEmpty() || (!left.current().hasType() && !right.current().hasType()), path, "Type Mismatch:\r\n  " + typeCode(left) + "\r\n  " + typeCode(right));
    // <fixed[x]><!-- ?? 0..1 * Value must be exactly this --></fixed[x]>
    // <pattern[x]><!-- ?? 0..1 * Value must have at least these property values --></pattern[x]>
    superset.setMaxLengthElement(unionMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
    subset.setMaxLengthElement(intersectMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
    if (left.current().hasBinding() || right.current().hasBinding()) {
        compareBindings(outcome, subset, superset, path, left.current(), right.current());
    }
    // note these are backwards
    superset.getConstraint().addAll(intersectConstraints(path, left.current().getConstraint(), right.current().getConstraint()));
    subset.getConstraint().addAll(unionConstraints(subset, outcome, path, left.current().getConstraint(), right.current().getConstraint()));
    // now process the slices
    if (left.current().hasSlicing() || right.current().hasSlicing()) {
        if (isExtension(left.path()))
            return compareExtensions(outcome, path, superset, subset, left, right);
        else
            throw new DefinitionException("Slicing is not handled yet");
    // todo: name
    }
    // add the children
    outcome.subset.getSnapshot().getElement().add(subset);
    outcome.superset.getSnapshot().getElement().add(superset);
    return compareChildren(subset, outcome, path, left, right);
}
Also used : ElementDefinition(org.hl7.fhir.dstu3.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 18 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

public Element parse(JsonObject object) throws FHIRFormatError, DefinitionException {
    JsonElement rt = object.get("resourceType");
    if (rt == null) {
        logError(line(object), col(object), "$", IssueType.INVALID, "Unable to find resourceType property", IssueSeverity.FATAL);
        return null;
    } else {
        String name = rt.getAsString();
        String path = "/" + name;
        StructureDefinition sd = getDefinition(line(object), col(object), name);
        if (sd == null)
            return null;
        Element result = new Element(name, new Property(context, sd.getSnapshot().getElement().get(0), sd));
        checkObject(object, path);
        result.markLocation(line(object), col(object));
        result.setType(name);
        parseChildren(path, object, result, true);
        result.numberChildren();
        return result;
    }
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) JsonElement(com.google.gson.JsonElement) JsonElement(com.google.gson.JsonElement) SpecialElement(org.hl7.fhir.dstu3.elementmodel.Element.SpecialElement)

Example 19 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parse.

@Override
public Element parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException {
    // if we're parsing at this point, then we're going to use the custom parser
    map = new HashMap<JsonElement, LocationData>();
    String source = TextFile.streamToString(stream);
    if (policy == ValidationPolicy.EVERYTHING) {
        JsonObject obj = null;
        try {
            obj = JsonTrackingParser.parse(source, map);
        } catch (Exception e) {
            logError(-1, -1, "(document)", IssueType.INVALID, "Error parsing JSON: " + e.getMessage(), IssueSeverity.FATAL);
            return null;
        }
        assert (map.containsKey(obj));
        return parse(obj);
    } else {
        JsonObject obj = (JsonObject) new com.google.gson.JsonParser().parse(source);
        // assert (map.containsKey(obj));
        return parse(obj);
    }
}
Also used : LocationData(org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 20 with DefinitionException

use of org.hl7.fhir.exceptions.DefinitionException in project org.hl7.fhir.core by hapifhir.

the class JsonParser method parseChildren.

private void parseChildren(String path, JsonObject object, Element context, boolean hasResourceType) throws DefinitionException, FHIRFormatError {
    reapComments(object, context);
    List<Property> properties = context.getProperty().getChildProperties(context.getName(), null);
    Set<String> processed = new HashSet<String>();
    if (hasResourceType)
        processed.add("resourceType");
    processed.add("fhir_comments");
    // first pass: process the properties
    for (Property property : properties) {
        if (property.isChoice()) {
            for (TypeRefComponent type : property.getDefinition().getType()) {
                String eName = property.getName().substring(0, property.getName().length() - 3) + Utilities.capitalize(type.getCode());
                if (!isPrimitive(type.getCode()) && object.has(eName)) {
                    parseChildComplex(path, object, context, processed, property, eName);
                    break;
                } else if (isPrimitive(type.getCode()) && (object.has(eName) || object.has("_" + eName))) {
                    parseChildPrimitive(object, context, processed, property, path, eName);
                    break;
                }
            }
        } else if (property.isPrimitive(property.getType(null))) {
            parseChildPrimitive(object, context, processed, property, path, property.getName());
        } else if (object.has(property.getName())) {
            parseChildComplex(path, object, context, processed, property, property.getName());
        }
    }
    // second pass: check for things not processed
    if (policy != ValidationPolicy.NONE) {
        for (Entry<String, JsonElement> e : object.entrySet()) {
            if (!processed.contains(e.getKey())) {
                logError(line(e.getValue()), col(e.getValue()), path, IssueType.STRUCTURE, "Unrecognised property '@" + e.getKey() + "'", IssueSeverity.ERROR);
            }
        }
    }
}
Also used : TypeRefComponent(org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)186 ArrayList (java.util.ArrayList)99 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)91 FHIRException (org.hl7.fhir.exceptions.FHIRException)50 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)32 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)29 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)29 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)28 IOException (java.io.IOException)27 HashMap (java.util.HashMap)24 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)23 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)20 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)17 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)17 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)16 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)16 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)14 StructureDefinition (org.hl7.fhir.dstu2016may.model.StructureDefinition)13 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)12