public static org.hl7.fhir.dstu2016may.model.StructureDefinition convertStructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition src) throws FHIRException { if (src == null || src.isEmpty()) return null; org.hl7.fhir.dstu2016may.model.StructureDefinition tgt = new org.hl7.fhir.dstu2016may.model.StructureDefinition(); copyDomainResource(src, tgt); if (src.hasUrl()) tgt.setUrl(src.getUrl()); for (org.hl7.fhir.dstu3.model.Identifier t : src.getIdentifier()) tgt.addIdentifier(convertIdentifier(t)); if (src.hasVersion()) tgt.setVersion(src.getVersion()); tgt.setName(src.getName()); if (src.hasTitle()) tgt.setDisplay(src.getTitle()); tgt.setStatus(convertConformanceResourceStatus(src.getStatus())); if (src.hasExperimental()) tgt.setExperimental(src.getExperimental()); if (src.hasPublisher()) tgt.setPublisher(src.getPublisher()); for (org.hl7.fhir.dstu3.model.ContactDetail t : src.getContact()) tgt.addContact(convertStructureDefinitionContactComponent(t)); if (src.hasDate()) tgt.setDate(src.getDate()); if (src.hasDescription()) tgt.setDescription(src.getDescription()); for (org.hl7.fhir.dstu3.model.UsageContext t : src.getUseContext()) if (t.hasValueCodeableConcept()) tgt.addUseContext(convertCodeableConcept(t.getValueCodeableConcept())); for (org.hl7.fhir.dstu3.model.CodeableConcept t : src.getJurisdiction()) tgt.addUseContext(convertCodeableConcept(t));
public org.hl7.fhir.dstu3.model.StructureDefinition convertStructureDefinition(org.hl7.fhir.instance.model.StructureDefinition src) throws FHIRException { if (src == null || src.isEmpty()) return null; org.hl7.fhir.dstu3.model.StructureDefinition tgt = new org.hl7.fhir.dstu3.model.StructureDefinition(); copyDomainResource(src, tgt); tgt.setUrl(src.getUrl()); for (org.hl7.fhir.instance.model.Identifier t : src.getIdentifier()) tgt.addIdentifier(convertIdentifier(t)); tgt.setVersion(src.getVersion()); tgt.setName(src.getName()); tgt.setTitle(src.getDisplay()); tgt.setStatus(convertConformanceResourceStatus(src.getStatus())); if (src.hasExperimental()) tgt.setExperimental(src.getExperimental()); tgt.setPublisher(src.getPublisher()); for (org.hl7.fhir.instance.model.StructureDefinition.StructureDefinitionContactComponent t : src.getContact()) tgt.addContact(convertStructureDefinitionContactComponent(t)); if (src.hasDate()) tgt.setDate(src.getDate()); tgt.setDescription(src.getDescription()); for (org.hl7.fhir.instance.model.CodeableConcept t : src.getUseContext()) if (isJurisdiction(t)) tgt.addJurisdiction(convertCodeableConcept(t)); else tgt.addUseContext(convertCodeableConceptToUsageContext(t)); tgt.setPurpose(src.getRequirements()); tgt.setCopyright(src.getCopyright()); for (org.hl7.fhir.instance.model.Coding t : src.getCode()) tgt.addKeyword(convertCoding(t)); tgt.setFhirVersion(src.getFhirVersion());
public void addAncestorProfiles(StructureDefinition sd) { if (sd.getDerivation().equals(StructureDefinition.TypeDerivationRule.CONSTRAINT)) { StructureDefinition parentSd = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition()); if (parentSd != null && !profiles.containsKey(parentSd)) { ProfileUsage pu = new ProfileUsage(parentSd); pu.setChecked(); // We're going to check the child, so no need to check the parent profiles.put(parentSd, pu); } } }
private ElementDefinition resolveType(String type) { if (logical != null) for (BundleEntryComponent be : logical.getEntry()) { if (be.hasResource() && be.getResource() instanceof StructureDefinition) { StructureDefinition sd = (StructureDefinition) be.getResource(); if (sd.getId().equals(type)) return sd.getSnapshot().getElement().get(0); } } String url = "http://hl7.org/fhir/StructureDefinition/" + type; long t = System.nanoTime(); StructureDefinition sd = context.fetchResource(StructureDefinition.class, url); sdTime = sdTime + (System.nanoTime() - t); if (sd == null || !sd.hasSnapshot()) return null; else return sd.getSnapshot().getElement().get(0); }
public org.hl7.fhir.instance.model.StructureDefinition convertStructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition src) throws FHIRException { if (src == null || src.isEmpty()) return null; org.hl7.fhir.instance.model.StructureDefinition tgt = new org.hl7.fhir.instance.model.StructureDefinition(); copyDomainResource(src, tgt); tgt.setUrl(src.getUrl()); for (org.hl7.fhir.dstu3.model.Identifier t : src.getIdentifier()) tgt.addIdentifier(convertIdentifier(t)); tgt.setVersion(src.getVersion()); tgt.setName(src.getName()); tgt.setDisplay(src.getTitle()); tgt.setStatus(convertConformanceResourceStatus(src.getStatus())); if (src.hasExperimental()) tgt.setExperimental(src.getExperimental()); tgt.setPublisher(src.getPublisher()); for (org.hl7.fhir.dstu3.model.ContactDetail t : src.getContact()) tgt.addContact(convertStructureDefinitionContactComponent(t)); if (src.hasDate()) tgt.setDate(src.getDate()); tgt.setDescription(src.getDescription()); for (org.hl7.fhir.dstu3.model.UsageContext t : src.getUseContext()) if (t.hasValueCodeableConcept()) tgt.addUseContext(convertCodeableConcept(t.getValueCodeableConcept())); for (org.hl7.fhir.dstu3.model.CodeableConcept t : src.getJurisdiction()) tgt.addUseContext(convertCodeableConcept(t)); tgt.setRequirements(src.getPurpose()); tgt.setCopyright(src.getCopyright()); for (org.hl7.fhir.dstu3.model.Coding t : src.getKeyword()) tgt.addCode(convertCoding(t)); tgt.setFhirVersion(src.getFhirVersion());
StructureDefinition profile = new StructureDefinition(); profiles.add(profile); profile.setDerivation(TypeDerivationRule.CONSTRAINT); profile.setType(type); profile.setBaseDefinition(prop.getBaseProperty().getStructure().getUrl()); profile.setName("Profile for "+profile.getType()+" for "+sliceName); profile.setUrl(map.getUrl().replace("StructureMap", "StructureDefinition")+"-"+profile.getType()+suffix); ctxt.setUserData("profile", profile.getUrl()); // then we can easily assign this profile url for validation later when we actually transform profile.setId(map.getId()+"-"+profile.getType()+suffix); profile.setStatus(map.getStatus()); profile.setExperimental(map.getExperimental()); profile.setDescription("Generated automatically from the mapping by the Java Reference Implementation"); for (ContactDetail c : map.getContact()) { ContactDetail p = profile.addContact(); p.setName(c.getName()); for (ContactPoint cc : c.getTelecom()) p.addTelecom(cc); profile.setDate(map.getDate()); profile.setCopyright(map.getCopyright()); profile.setFhirVersion(Constants.VERSION); profile.setKind(prop.getBaseProperty().getStructure().getKind()); profile.setAbstract(false); ElementDefinition ed = profile.getDifferential().addElement(); ed.setPath(profile.getType()); prop.profileProperty = new Property(worker, ed, profile); return prop;
@Override public Base makeProperty(int hash, String name) throws FHIRException { switch (hash) { case 116079: return getUrlElement(); case -1618432855: return addIdentifier(); case 351608024: return getVersionElement(); case 3373707: return getNameElement(); case 110371416: return getTitleElement(); case -892481550: return getStatusElement(); case -404562712: return getExperimentalElement(); case 3076014: return getDateElement(); case 1447404028: return getPublisherElement(); case 951526432: return addContact(); case -1724546052: return getDescriptionElement(); case -669707736: return addUseContext(); case -507075711: return addJurisdiction(); case -220463842: return getPurposeElement(); case 1522889671: return getCopyrightElement(); case -814408215: return addKeyword(); case 461006061: return getFhirVersionElement(); case 837556430: return addMapping(); case 3292052: return getKindElement(); case 1732898850: return getAbstractElement(); case -102839927: return getContextTypeElement(); case 951530927: return addContextElement(); case -802505007: return addContextInvariantElement(); case 3575610: return getTypeElement(); case 1139771140: return getBaseDefinitionElement(); case -1353885513: return getDerivationElement(); case 284874180: return getSnapshot();
switch (hash) { case 116079: // url this.url = castToUri(value); // UriType return value; case -1618432855: // identifier this.getIdentifier().add(castToIdentifier(value)); // Identifier return value; case 351608024: // version this.version = castToString(value); // StringType return value; case 3373707: // name this.name = castToString(value); // StringType return value; case 110371416: // title this.title = castToString(value); // StringType return value; case -892481550: // status value = new PublicationStatusEnumFactory().fromType(castToCode(value)); this.status = (Enumeration) value; // Enumeration<PublicationStatus> return value; case -404562712: // experimental this.experimental = castToBoolean(value); // BooleanType return value; case 3076014: // date this.date = castToDateTime(value); // DateTimeType return value; case 1447404028: // publisher this.publisher = castToString(value); // StringType return value;
for (ElementDefinition ec : profile.getDifferential().getElement()) checkExtensions(profile, errors, "differential", ec); rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "missing Snapshot at "+profile.getName()+"."+profile.getName()); for (ElementDefinition ec : profile.getSnapshot().getElement()) checkExtensions(profile, errors, "snapshot", ec); if (rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "A snapshot is required")) { for (ElementDefinition ed : profile.getSnapshot().getElement()) { checkExtensions(profile, errors, "snapshot", ed); for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) { if (forBuild) { if (!inExemptList(inv.getKey())) { if (rule(errors, IssueType.BUSINESSRULE, profile.getId()+"::"+ed.getPath()+"::"+inv.getKey(), inv.hasExpression(), "The invariant has no FHIR Path expression ("+inv.getXpath()+")")) { try { new FHIRPathEngine(context).check(null, profile.getType(), ed.getPath(), inv.getExpression()); // , inv.hasXpath() && inv.getXpath().startsWith("@value") } catch (Exception e) {
@Override public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) { ArrayList<StructureDefinition> retVal = new ArrayList<StructureDefinition>(); Set<String> urls = new HashSet<String>(); for (IValidationSupport nextSupport : myChain) { for (StructureDefinition next : nextSupport.fetchAllStructureDefinitions(theContext)) { if (isBlank(next.getUrl()) || urls.add(next.getUrl())) { retVal.add(next); } } } return retVal; }
public void checkAllInvariants(){ for (StructureDefinition sd : context.allStructures()) { if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION) { for (ElementDefinition ed : sd.getSnapshot().getElement()) { for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) { if (inv.hasExpression()) { try { ExpressionNode n = (ExpressionNode) inv.getUserData("validator.expression.cache"); if (n == null) { n = fpe.parse(inv.getExpression()); inv.setUserData("validator.expression.cache", n); } fpe.check(null, sd.getKind() == StructureDefinitionKind.RESOURCE ? sd.getType() : "DomainResource", ed.getPath(), n); } catch (Exception e) { System.out.println("Error processing structure ["+sd.getId()+"] path "+ed.getPath()+":"+inv.getKey()+" (\""+inv.getExpression()+"\"): "+e.getMessage()); } } } } } } }
if (snapshotStack.contains(derived.getUrl())) throw new DefinitionException("Circular snapshot references detected; cannot generate snapshot (stack = "+snapshotStack.toString()+")"); snapshotStack.add(derived.getUrl()); derived.setSnapshot(new StructureDefinitionSnapshotComponent()); if (derived.hasDifferential() && !derived.getDifferential().getElementFirstRep().getPath().contains(".") && !derived.getDifferential().getElementFirstRep().getType().isEmpty()) throw new Error("type on first differential element!"); for (ElementDefinition e : derived.getDifferential().getElement()) e.clearUserData(GENERATED_IN_SNAPSHOT); processPaths("", derived.getSnapshot(), base.getSnapshot(), derived.getDifferential(), baseCursor, diffCursor, base.getSnapshot().getElement().size()-1, derived.getDifferential().hasElement() ? derived.getDifferential().getElement().size()-1 : -1, url, derived.getId(), null, null, false, base.getUrl(), null, false); if (!derived.getSnapshot().getElementFirstRep().getType().isEmpty()) throw new Error("type on first snapshot element for "+derived.getSnapshot().getElementFirstRep().getPath()+" in "+derived.getUrl()+" from "+base.getUrl()); updateMaps(base, derived); setIds(derived, false); for (ElementDefinition e : derived.getDifferential().getElement()) { if (!e.hasUserData(GENERATED_IN_SNAPSHOT)) { System.out.println("Error in snapshot generation: Snapshot for "+derived.getUrl()+" does not contain differential element with id: " + e.getId()); System.out.println("Differential: "); for (ElementDefinition ed : derived.getDifferential().getElement()) System.out.println(" "+ed.getPath()+" : "+typeSummary(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+" id = "+ed.getId()); System.out.println("Snapshot: "); for (ElementDefinition ed : derived.getSnapshot().getElement()) System.out.println(" "+ed.getPath()+" : "+typeSummary(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+" id = "+ed.getId()); throw new DefinitionException("Snapshot for "+derived.getUrl()+" does not contain differential element with id: " + e.getId());
public void setIds(StructureDefinition sd, boolean checkFirst) throws DefinitionException { if (!checkFirst || !sd.hasDifferential() || hasMissingIds(sd.getDifferential().getElement())) { if (!sd.hasDifferential()) sd.setDifferential(new StructureDefinitionDifferentialComponent()); generateIds(sd.getDifferential().getElement(), sd.getName()); } if (!checkFirst || !sd.hasSnapshot() || hasMissingIds(sd.getSnapshot().getElement())) { if (!sd.hasSnapshot()) sd.setSnapshot(new StructureDefinitionSnapshotComponent()); generateIds(sd.getSnapshot().getElement(), sd.getName()); } }
public void populateLogicalSnapshot(StructureDefinition sd) throws FHIRException { sd.getSnapshot().getElement().add(sd.getDifferential().getElementFirstRep().copy()); if (sd.hasBaseDefinition()) { StructureDefinition base = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition()); if (base == null) throw new FHIRException("Unable to find base definition for logical model: "+sd.getBaseDefinition()+" from "+sd.getUrl()); copyElements(sd, base.getSnapshot().getElement()); } copyElements(sd, sd.getDifferential().getElement()); }
if (dt == null) throw new DefinitionException(cpath+" has children for type "+typeCode(outcome.getType())+" in profile "+profileName+", but can't find type"); contextName = dt.getUrl(); int start = diffCursor; while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath+".")) diffCursor++; processPaths(indent+" ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start, dt.getSnapshot().getElement().size()-1, diffCursor-1, url, profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false); StructureDefinition sd = context.fetchResource(StructureDefinition.class, p); if (sd != null) { if (!sd.hasSnapshot()) { StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition()); if (sdb == null) throw new DefinitionException("no base for "+sd.getBaseDefinition()); generateSnapshot(sdb, sd, sd.getUrl(), sd.getName()); template = sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath()); template.setSliceName(null); if (dt == null) throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") for type "+typeCode(outcome.getType())+" in profile "+profileName+", but can't find type"); contextName = dt.getUrl(); processPaths(indent+" ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start-1, dt.getSnapshot().getElement().size()-1, diffCursor - 1, url, profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false); contextName = dt.getUrl(); int start = diffCursor; while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
} else { if (def.getIsModifier()) rule(errors, IssueType.STRUCTURE, element.line(), element.col(), path + "[url='" + url + "']", ex.getSnapshot().getElement().get(0).getIsModifier(), "Extension modifier mismatch: the extension element is labelled as a modifier, but the underlying extension is not"); else rule(errors, IssueType.STRUCTURE, element.line(), element.col(), path + "[url='" + url + "']", !ex.getSnapshot().getElement().get(0).getIsModifier(), "Extension modifier mismatch: the extension element is not labelled as a modifier, but the underlying extension is"); checkExtensionContext(errors, element, /* path+"[url='"+url+"']", */ ex, stack, ex.getUrl()); rule(errors, IssueType.STRUCTURE, element.line(), element.col(), path + "[url='" + url + "']", ex.getSnapshot().getElement().get(0).getIsModifier(), "The Extension '" + url + "' must be used as a modifierExtension"); else rule(errors, IssueType.STRUCTURE, element.line(), element.col(), path + "[url='" + url + "']", !ex.getSnapshot().getElement().get(0).getIsModifier(), "The Extension '" + url + "' must not be used as an extension (it's a modifierExtension)"); validateElement(appContext, errors, ex, ex.getSnapshot().getElement().get(0), null, null, resource, element, "Extension", stack, false);
private boolean checkExtensionContext(List<ValidationMessage> errors, Element element, StructureDefinition definition, NodeStack stack, String extensionParent) { String extUrl = definition.getUrl(); CommaSeparatedStringBuilder p = new CommaSeparatedStringBuilder(); for (String lp : stack.getLogicalPaths()) p.append(lp); if (definition.getContextType() == ExtensionContext.DATATYPE) { boolean ok = false; CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); for (StringType ct : definition.getContext()) { b.append(ct.getValue()); if (ct.getValue().equals("*") || stack.getLogicalPaths().contains(ct.getValue() + ".extension")) } else if (definition.getContextType() == ExtensionContext.EXTENSION) { boolean ok = false; for (StringType ct : definition.getContext()) if (ct.getValue().equals("*") || ct.getValue().equals(extensionParent)) ok = true; return rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), ok, "The extension " + extUrl + " is not allowed to be used with the extension '" + extensionParent + "'"); } else if (definition.getContextType() == ExtensionContext.RESOURCE) { boolean ok = false; for (StringType ct : definition.getContext()) { String c = ct.getValue(); b.append(c);
res.setName(name); res.setCardinality(cardinality); res.setProfileLink(profile.getUserString("path")); res.setResType(profile.getType()); StructureDefinition base = context.fetchResource(StructureDefinition.class, res.getResType()); if (base != null) res.setResLink(base.getUserString("path")); res.setId(profile.getId()); res.setProfile(profile.getDerivation() == TypeDerivationRule.CONSTRAINT); StringBuilder b = new StringBuilder(); b.append(res.getResType()); boolean first = true; boolean open = false; if (profile.getDerivation() == TypeDerivationRule.CONSTRAINT) { res.setDescription(profile.getName()); for (ElementDefinition ed : profile.getSnapshot().getElement()) { if (isKeyProperty(ed.getBase().getPath()) && ed.hasFixed()) { if (first) { b.append("]"); } else res.setDescription("Base FHIR "+profile.getName()); res.setType(b.toString()); return res ;
public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException { if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT) throw new DefinitionException("not the right kind of structure to generate schematrons for"); if (!structure.hasSnapshot()) throw new DefinitionException("needs a snapshot"); StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition()); SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName()); ElementDefinition ed = structure.getSnapshot().getElement().get(0); generateForChildren(sch, "f:"+ed.getPath(), ed, structure, base); sch.dump(); }
public boolean IsLogicalAndHasPrimitiveValue(String name) { // if (canBePrimitive!= null) // return canBePrimitive; canBePrimitive = false; if (structure.getKind() != StructureDefinitionKind.LOGICAL) return false; if (!hasType(name)) return false; StructureDefinition sd = context.fetchResource(StructureDefinition.class, structure.getUrl().substring(0, structure.getUrl().lastIndexOf("/")+1)+getType(name)); if (sd == null) sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+getType(name)); if (sd != null && sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) return true; if (sd == null || sd.getKind() != StructureDefinitionKind.LOGICAL) return false; for (ElementDefinition ed : sd.getSnapshot().getElement()) { if (ed.getPath().equals(sd.getId()+".value") && ed.getType().size() == 1 && isPrimitive(ed.getType().get(0).getCode())) { canBePrimitive = true; return true; } } return false; }