static void InitUserProfiles() { Console.WriteLine($"Fetch profiles in target location..."); var src = _dirSource = new DirectorySource(_targetPath, includeSubDirs); _cachedDirSource = new CachedResolver(src); var userProfiles = _userProfiles = src.ListResourceUris().ToList(); Console.WriteLine($"Found {userProfiles.Count} profiles."); Console.WriteLine($"Determine mappings..."); foreach (var profile in userProfiles) { var sd = _cachedDirSource.FindStructureDefinition(profile, false); if (EnsureSnapshot(sd)) { var key = ModelInfo.CanonicalUriForFhirCoreType(sd.Type); if (!_mappings.TryGetValue(key, out string existing)) { Console.WriteLine($"Map references of type '{sd.Type}' to user profile '{sd.Url}'"); _mappings.Add(key, sd.Url); } else { Console.WriteLine($"Warning! Ignore duplicate user profile '{sd.Url}' for reference target type '{sd.Type}'"); } } } }
private static StructureDefinition patientWithSpecificOrganization(IEnumerable <ElementDefinition.AggregationMode> aggregation, string prefix) { var result = createTestSD($"http://validationtest.org/fhir/StructureDefinition/PatientWith{prefix}Organization", $"Patient with {prefix} managing organization", $"Patient for which the managingOrganization reference is limited to {prefix} references", FHIRDefinedType.Patient); var cons = result.Differential.Element; cons.Add(new ElementDefinition("Patient").OfType(FHIRDefinedType.Patient)); cons.Add(new ElementDefinition("Patient.managingOrganization") .OfType(FHIRDefinedType.Reference, ModelInfo.CanonicalUriForFhirCoreType(FHIRDefinedType.Organization), aggregation)); return(result); }
public void TestLoadResourceFromZipStream() { // Harvest summaries and load artifact straight from core ZIP archive // Use XmlNavigatorStream to navigate resources stored inside a zip file // ZipDeflateStream does not support seeking (forward-only stream) // Therefore this only works for the XmlNavigatorStream, as the ctor does NOT (need to) call Reset() // JsonNavigatorStream cannot support zip streams; ctor needs to call Reset after scanning resourceType ArtifactSummary corePatientSummary; var corePatientUrl = ModelInfo.CanonicalUriForFhirCoreType(FHIRDefinedType.Patient); string zipEntryName = "profiles-resources.xml"; // Generate summaries from core ZIP resource definitions (extract in memory) using (var archive = ZipFile.Open(ZipSource.SpecificationZipFileName, ZipArchiveMode.Read)) { var entry = archive.Entries.FirstOrDefault(e => e.Name == zipEntryName); Assert.IsNotNull(entry); using (var entryStream = entry.Open()) using (var navStream = new XmlNavigatorStream(entryStream)) { var summaries = ArtifactSummaryGenerator.Default.Generate(navStream); Assert.IsNotNull(summaries); corePatientSummary = summaries.FindConformanceResources(corePatientUrl).FirstOrDefault(); } } Assert.IsNotNull(corePatientSummary); Assert.AreEqual(ResourceType.StructureDefinition, corePatientSummary.ResourceType); Assert.AreEqual(corePatientUrl, corePatientSummary.GetConformanceCanonicalUrl()); // Load core Patient resource from ZIP (extract in memory) using (var archive = ZipFile.Open(ZipSource.SpecificationZipFileName, ZipArchiveMode.Read)) { var entry = archive.Entries.FirstOrDefault(e => e.Name == zipEntryName); using (var entryStream = entry.Open()) using (var navStream = new XmlNavigatorStream(entryStream)) { var nav = navStream.Current; if (nav != null) { // Parse target resource from navigator var parser = new BaseFhirParser(); var corePatient = parser.Parse <StructureDefinition>(nav); Assert.IsNotNull(corePatient); Assert.AreEqual(corePatientUrl, corePatient.Url); } } } }
/// <summary>Returns the explicit primary type profile, if specified, or otherwise the core profile url for the specified type code.</summary> public static string TypeProfile(this ElementDefinition.TypeRefComponent elemType) { string profile = null; if (elemType != null) { profile = elemType.Profile.FirstOrDefault(); if (profile == null && elemType.Code.HasValue) { profile = ModelInfo.CanonicalUriForFhirCoreType(elemType.Code.Value); } } return(profile); }
public static string GetDeclaredProfiles(this ElementDefinition.TypeRefComponent typeRef) { if (typeRef.Profile.Any()) { return(typeRef.Profile.First()); // Take the first, this will disappear in STU3 anyway } else if (typeRef.Code.HasValue) { return(ModelInfo.CanonicalUriForFhirCoreType(typeRef.Code.Value)); } else { return(null); } }
public void TestZipSourceMask() { var zipFile = Path.Combine(Directory.GetCurrentDirectory(), "specification.zip"); Assert.IsTrue(File.Exists(zipFile), "Error! specification.zip is not available."); var za = new ZipSource(zipFile); za.Mask = "profiles-types.xml"; var artifacts = za.ListArtifactNames().ToArray(); Assert.AreEqual(1, artifacts.Length); Assert.AreEqual("profiles-types.xml", artifacts[0]); var resourceIds = za.ListResourceUris(ResourceType.StructureDefinition).ToArray(); Assert.IsNotNull(resourceIds); Assert.IsTrue(resourceIds.Length > 0); Assert.IsTrue(resourceIds.All(url => url.StartsWith("http://hl7.org/fhir/StructureDefinition/"))); // + total number of known FHIR core types // - total number of known (concrete) resources // - 1 for abstract type Resource // - 1 for abstract type DomainResource // + 1 xhtml (not present as FhirCsType) // ======================================= // total number of known FHIR (complex & primitive) datatypes var coreDataTypes = ModelInfo.FhirCsTypeToString.Where(kvp => !ModelInfo.IsKnownResource(kvp.Key) && kvp.Value != "Resource" && kvp.Value != "DomainResource" ) .Select(kvp => kvp.Value).Concat(new[] { "xhtml" }); var numCoreDataTypes = coreDataTypes.Count(); Assert.AreEqual(resourceIds.Length, numCoreDataTypes); // Assert.IsTrue(resourceIds.All(url => ModelInfo.CanonicalUriForFhirCoreType)); var coreTypeUris = coreDataTypes.Select(typeName => ModelInfo.CanonicalUriForFhirCoreType(typeName)).ToArray(); // Boths arrays should contains same urls, possibly in different order Assert.AreEqual(coreTypeUris.Length, resourceIds.Length); Assert.IsTrue(coreTypeUris.All(url => resourceIds.Contains(url))); Assert.IsTrue(resourceIds.All(url => coreTypeUris.Contains(url))); }
public void TestLoadResourceFromZipSource() { // ZipSource extracts core ZIP archive to (temp) folder, then delegates to DirectorySource // i.e. artifact summaries are harvested from files on disk var source = ZipSource.CreateValidationSource(); var summaries = source.ListSummaries(); var patientUrl = ModelInfo.CanonicalUriForFhirCoreType(FHIRDefinedType.Patient); var patientSummary = summaries.FindConformanceResources(patientUrl).FirstOrDefault(); Assert.IsNotNull(patientSummary); Assert.AreEqual(ResourceType.StructureDefinition, patientSummary.ResourceType); Assert.AreEqual(patientUrl, patientSummary.GetConformanceCanonicalUrl()); Assert.IsNotNull(patientSummary.Origin); var patientStructure = source.LoadBySummary <StructureDefinition>(patientSummary); Assert.IsNotNull(patientStructure); }
public ProfilePreprocessor(Func <string, StructureDefinition> profileResolver, Action <StructureDefinition> snapshotGenerator, IElementNavigator instance, string declaredTypeProfile, IEnumerable <StructureDefinition> additionalProfiles, IEnumerable <string> additionalCanonicals) { _profileResolver = profileResolver; _snapshotGenerator = snapshotGenerator; _path = instance.Location; _profiles = new ProfileAssertion(_path, _profileResolver); if (instance.Type != null) { _profiles.SetInstanceType(ModelInfo.CanonicalUriForFhirCoreType(instance.Type)); } if (declaredTypeProfile != null) { _profiles.SetDeclaredType(declaredTypeProfile); } // This is only for resources, but I don't bother checking, since this will return empty anyway _profiles.AddStatedProfile(instance.Children("meta").Children("profile").Select(p => p.Value).Cast <string>()); //Almost identically, extensions can declare adherance to a profile using the 'url' attribute if (declaredTypeProfile == ModelInfo.CanonicalUriForFhirCoreType(FHIRDefinedType.Extension)) { var urlDeclaration = instance.Children("url").FirstOrDefault()?.Value as string; if (urlDeclaration != null && urlDeclaration.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { _profiles.AddStatedProfile(urlDeclaration); } } if (additionalProfiles != null) { _profiles.AddStatedProfile(additionalProfiles); } if (additionalCanonicals != null) { _profiles.AddStatedProfile(additionalCanonicals); } }
public void ResourceWithStatedProfiles() { var assertion = new ProfileAssertion("Observation", resolve); assertion.SetDeclaredType(FHIRDefinedType.Observation); Assert.True(assertion.Validate().Success); assertion.AddStatedProfile(ModelInfo.CanonicalUriForFhirCoreType(FHIRDefinedType.Observation)); assertion.AddStatedProfile("http://validationtest.org/fhir/StructureDefinition/WeightHeightObservation"); assertion.AddStatedProfile("http://hl7.org/fhir/StructureDefinition/devicemetricobservation"); var report = assertion.Validate(); Assert.True(report.Success); Assert.Equal(2, assertion.MinimalProfiles.Count()); Assert.Equal(assertion.MinimalProfiles, assertion.StatedProfiles.Skip(1)); assertion.SetDeclaredType(FHIRDefinedType.Procedure); report = assertion.Validate(); Assert.False(report.Success); Assert.Contains("is incompatible with the declared type", report.ToString()); }
public static StructureDefinition FindStructureDefinitionForCoreType(this IResourceResolver resolver, string typename) { var url = ModelInfo.CanonicalUriForFhirCoreType(typename); return(resolver.FindStructureDefinition(url)); }
public void SetInstanceType(FHIRDefinedType instanceType) { SetInstanceType(ModelInfo.CanonicalUriForFhirCoreType(instanceType)); }
public void SetDeclaredType(FHIRDefinedType declaredType) { SetDeclaredType(ModelInfo.CanonicalUriForFhirCoreType(declaredType)); }