Skip to content

Commit

Permalink
Merge pull request #1423 from FirelyTeam/feature/add-includeDesignati…
Browse files Browse the repository at this point in the history
…ons-in-vs-expander

feature: add includeDesignations parameter in ValueSet expander
  • Loading branch information
marcovisserFurore authored Jul 21, 2020
2 parents b4937b3 + 71bbea2 commit b1753ed
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
60 changes: 60 additions & 0 deletions src/Hl7.Fhir.Specification.Tests/Source/TerminologyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Hl7.Fhir.Specification.Terminology;
using Hl7.Fhir.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -89,6 +90,65 @@ public void ExpansionOfComposeImport()
Assert.Equal(304, testVs.Expansion.Total);
}

[Fact]
public void TestIncludeDesignation()
{
var testVs = _resolver.ResolveByCanonicalUri("http://hl7.org/fhir/ValueSet/animal-genderstatus").DeepCopy() as ValueSet;
Assert.False(testVs.HasExpansion);
var expander = new ValueSetExpander(new ValueSetExpanderSettings { ValueSetSource = _resolver });

//Import codes from codesystem
expander.Expand(testVs);
Assert.DoesNotContain(testVs.Expansion.Contains, c => c.Designation.Any());

expander.Settings.IncludeDesignations = true;
expander.Expand(testVs);

Assert.Contains(testVs.Expansion.Parameter, p => p.Name == "includeDesignations" && (p.Value as FhirBoolean).Value == true);
Assert.Contains(testVs.Expansion.Contains, c => c.Designation.Any(d => d.Language == "nl" && d.Value == "gesteriliseerd"));

//compose codes
testVs = new ValueSet
{
Compose = new ValueSet.ComposeComponent
{
Include = new List<ValueSet.ConceptSetComponent>
{
new ValueSet.ConceptSetComponent
{
System = "http://hl7.org/fhir/v3/NullFlavor",
Concept = new List<ValueSet.ConceptReferenceComponent>
{

new ValueSet.ConceptReferenceComponent
{
Code = "UNK",
Display = "unknown",
Designation = new List<ValueSet.DesignationComponent>
{
new ValueSet.DesignationComponent
{
Language = "nl",
Value = "onbekend"
}
}
}
}
}
}
}
};

expander.Settings.IncludeDesignations = false;
expander.Expand(testVs);
Assert.DoesNotContain(testVs.Expansion.Contains, c => c.Designation.Any());
expander.Settings.IncludeDesignations = true;
expander.Expand(testVs);

Assert.Contains(testVs.Expansion.Parameter, p => p.Name == "includeDesignations" && (p.Value as FhirBoolean).Value == true);
Assert.Contains(testVs.Expansion.Contains, c => c.Designation.Any(d => d.Language == "nl" && d.Value == "onbekend"));
}

[Fact]
public void TestPropertyRetrieval()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void Expand(ValueSet source)
{
// Note we are expanding the valueset in-place, so it's up to the caller to decide whether
// to clone the valueset, depending on store and performance requirements.

source.Expansion = ValueSet.ExpansionComponent.Create();
setExpansionParameters(source);

try
{
Expand All @@ -49,6 +49,21 @@ public void Expand(ValueSet source)
source.Expansion = null;
throw e;
}

}

private void setExpansionParameters(ValueSet vs)
{
vs.Expansion.Parameter = new List<ValueSet.ParameterComponent>();
if(Settings.IncludeDesignations)
{
vs.Expansion.Parameter.Add(new ValueSet.ParameterComponent
{
Name = "includeDesignations",
Value = new FhirBoolean(true)
});
}
//TODO add more parameters to the valuset here when we implement them.
}


Expand Down Expand Up @@ -79,10 +94,9 @@ private void handleCompose(ValueSet source)

// handleImport(source);
handleInclude(source);
handleExclude(source);
handleExclude(source);
}


private List<ValueSet.ContainsComponent> collectConcepts(ValueSet.ConceptSetComponent conceptSet)
{
List<ValueSet.ContainsComponent> result = new List<ValueSet.ContainsComponent>();
Expand All @@ -101,7 +115,15 @@ private void handleCompose(ValueSet source)
{
// We'd probably really have to look this code up in the original ValueSet (by system) to know something about 'abstract'
// and what would we do with a hierarchy if we encountered that in the include?
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display);
if(Settings.IncludeDesignations)
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display, concept.Designation);
}
else
{
result.Add(conceptSet.System, conceptSet.Version, concept.Code, concept.Display);
}

}
}
else
Expand Down Expand Up @@ -200,7 +222,7 @@ private void handleExclude(ValueSet source)
if (importedCs == null) throw new ValueSetUnknownException($"Cannot resolve canonical reference '{uri}' to CodeSystem");

var result = new List<ValueSet.ContainsComponent>();
result.AddRange(importedCs.Concept.Select(c => c.ToContainsComponent(importedCs)));
result.AddRange(importedCs.Concept.Select(c => c.ToContainsComponent(importedCs, Settings)));

return result;
}
Expand All @@ -209,14 +231,15 @@ private void handleExclude(ValueSet source)

public static class ContainsSetExtensions
{
public static ValueSet.ContainsComponent Add(this List<ValueSet.ContainsComponent> dest, string system, string version, string code, string display, IEnumerable<ValueSet.ContainsComponent> children = null)
public static ValueSet.ContainsComponent Add(this List<ValueSet.ContainsComponent> dest, string system, string version, string code, string display, List<ValueSet.DesignationComponent> designations = null, IEnumerable<ValueSet.ContainsComponent> children = null)
{
var newContains = new ValueSet.ContainsComponent();

newContains.System = system;
newContains.Code = code;
newContains.Display = display;
newContains.Version = version;
newContains.Designation = designations;

if (children != null)
newContains.Contains = new List<ValueSet.ContainsComponent>(children);
Expand Down Expand Up @@ -245,14 +268,16 @@ public static void Remove(this List<ValueSet.ContainsComponent> dest, List<Value
dest.Remove(sourceConcept.System, sourceConcept.Code);
}

internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.ConceptDefinitionComponent source, CodeSystem system)
internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.ConceptDefinitionComponent source, CodeSystem system, ValueSetExpanderSettings settings)
{
var newContains = new ValueSet.ContainsComponent();

newContains.System = system.Url;
newContains.Version = system.Version;
newContains.Code = source.Code;
newContains.Display = source.Display;
newContains.Display = source.Display;
if(settings.IncludeDesignations)
newContains.Designation = source.Designation.ToValueSetDesignations();

var abstractProperty = source.ListConceptProperties(system, CodeSystem.CONCEPTPROPERTY_NOT_SELECTABLE).SingleOrDefault();
if (abstractProperty?.Value is FhirBoolean isAbstract)
Expand All @@ -264,9 +289,27 @@ internal static ValueSet.ContainsComponent ToContainsComponent(this CodeSystem.C

if (source.Concept.Any())
newContains.Contains.AddRange(
source.Concept.Select(c => c.ToContainsComponent(system)));
source.Concept.Select(c => c.ToContainsComponent(system, settings)));

return newContains;
}

private static List<ValueSet.DesignationComponent> ToValueSetDesignations(this List<CodeSystem.DesignationComponent> csDesignations)
{
var vsDesignations = new List<ValueSet.DesignationComponent>();
csDesignations.ForEach(d => vsDesignations.Add(d.ToValueSetDesignation()));
return vsDesignations;
}

private static ValueSet.DesignationComponent ToValueSetDesignation(this CodeSystem.DesignationComponent csDesignation)
{
return new ValueSet.DesignationComponent
{
Language = csDesignation.Language,
Use = csDesignation.Use,
Value = csDesignation.Value
};
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class ValueSetExpanderSettings
/// </summary>
public int MaxExpansionSize { get; set; } = DefaultMaxExpansionSize;

/// <summary>
/// Controls whether concept designations are to be included or excluded in value set expansions
/// </summary>
public bool IncludeDesignations { get; set; }

/// <summary>Default constructor. Creates a new <see cref="ValueSetExpanderSettings"/> instance with default property values.</summary>
public ValueSetExpanderSettings() { }

Expand All @@ -51,6 +56,7 @@ public void CopyTo(ValueSetExpanderSettings other)

other.MaxExpansionSize = MaxExpansionSize;
other.ValueSetSource = ValueSetSource;
other.IncludeDesignations = IncludeDesignations;
}

/// <summary>Creates a new <see cref="ValueSetExpanderSettings"/> object that is a copy of the current instance.</summary>
Expand Down

0 comments on commit b1753ed

Please sign in to comment.