Skip to content

Commit

Permalink
Merge pull request #57 from FirelyTeam/prepare-for-different-dataretr…
Browse files Browse the repository at this point in the history
…iever-role

More last-minute cleanup of the public surface
  • Loading branch information
ewoutkramer authored Sep 7, 2023
2 parents 04603b7 + 0c9cbca commit 04e83d5
Show file tree
Hide file tree
Showing 230 changed files with 1,377 additions and 1,244 deletions.
25 changes: 13 additions & 12 deletions Cql/CodeGeneration.NET/AssemblyCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* available at https://raw.githubusercontent.com/FirelyTeam/cql-sdk/main/LICENSE
*/

using Hl7.Cql.CodeGeneration.NET;
using Hl7.Cql.Abstractions;
using Hl7.Cql.Compiler;
using Hl7.Cql.Elm;
using Hl7.Cql.Graph;
using Hl7.Cql.Runtime;
Expand All @@ -24,7 +25,7 @@
using System.Reflection;
using System.Text;

namespace Hl7.Cql.Compiler
namespace Hl7.Cql.CodeGeneration.NET
{
internal class AssemblyCompiler
{
Expand All @@ -51,14 +52,14 @@ internal IDictionary<string, AssemblyData> Compile(IEnumerable<Library> elmPacka
var references = new[]
{
// Core engine references
typeof(Hl7.Cql.CqlDeclarationAttribute).Assembly, // Cql.Abstractions
typeof(Hl7.Cql.Comparers.CqlComparers).Assembly, // Cql.Comparers
typeof(Hl7.Cql.Conversion.IUnitConverter).Assembly, // Cql.Conversion
typeof(Hl7.Cql.Operators.ICqlOperators).Assembly, // Cql.Operators
typeof(Hl7.Cql.Primitives.CqlPrimitiveType).Assembly, // Cql.Primitives
typeof(Hl7.Cql.Runtime.CqlContext).Assembly, // Cql.Runtime
typeof(Hl7.Cql.ValueSets.IValueSetDictionary).Assembly, // Cql.ValueSets
typeof(Hl7.Cql.Iso8601.DateIso8601).Assembly, // Iso8601
typeof(CqlDeclarationAttribute).Assembly, // Cql.Abstractions
typeof(Comparers.CqlComparers).Assembly, // Cql.Comparers
typeof(Conversion.IUnitConverter).Assembly, // Cql.Conversion
typeof(Operators.ICqlOperators).Assembly, // Cql.Operators
typeof(Primitives.CqlPrimitiveType).Assembly, // Cql.Primitives
typeof(CqlContext).Assembly, // Cql.Runtime
typeof(IValueSetDictionary).Assembly, // Cql.ValueSets
typeof(Iso8601.DateIso8601).Assembly, // Iso8601
}
.Concat(TypeResolver.ModelAssemblies)
.Distinct()
Expand All @@ -85,7 +86,7 @@ internal IDictionary<string, AssemblyData> Compile(IEnumerable<Library> elmPacka
var all = new DefinitionDictionary<LambdaExpression>();
foreach (var package in elmPackages)
{
var builder = new ExpressionBuilder(Binding, TypeManager, package, builderLogger);
var builder = new ExpressionBuilder(Binding, TypeManager, package, builderLogger, new(false));
var expressions = builder.Build();
all.Merge(expressions);
}
Expand Down Expand Up @@ -240,7 +241,7 @@ private void CompileNode(Stream sourceCodeStream,
string version = string.Empty;
if (parts.Length > 1)
version = parts[1];
asmInfo.AppendLine(CultureInfo.InvariantCulture, $"[assembly: Hl7.Cql.CqlLibraryAttribute(\"{name}\", \"{version}\")]");
asmInfo.AppendLine(CultureInfo.InvariantCulture, $"[assembly: Hl7.Cql.Abstractions.CqlLibraryAttribute(\"{name}\", \"{version}\")]");
var asmInfoTree = SyntaxFactory.ParseSyntaxTree(asmInfo.ToString());

var compilation = CSharpCompilation.Create($"{node.NodeId}")
Expand Down
2 changes: 1 addition & 1 deletion Cql/CodeGeneration.NET/AssemblyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Collections.Generic;
using System.Reflection;

namespace Hl7.Cql.Compiler
namespace Hl7.Cql.CodeGeneration.NET
{
/// <summary>
/// Stores information about a dynamically generated assembly.
Expand Down
6 changes: 4 additions & 2 deletions Cql/CodeGeneration.NET/CSharpSourceCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

using AgileObjects.ReadableExpressions;
using Hl7.Cql.Abstractions;
using Hl7.Cql.CodeGeneration.NET.Visitors;
using Hl7.Cql.Graph;
using Hl7.Cql.Primitives;
Expand Down Expand Up @@ -140,6 +141,7 @@ public void Write(DefinitionDictionary<LambdaExpression> definitions,
var writer = new StreamWriter(tupleTypeStream);
WriteUsings(writer);
var indentLevel = 0;
writer.WriteLine();
writer.WriteLine(indentLevel, $"namespace {tupleType.Namespace}");
writer.WriteLine(indentLevel, "{");
indentLevel += 1;
Expand Down Expand Up @@ -178,11 +180,12 @@ public void Write(DefinitionDictionary<LambdaExpression> definitions,
using var writer = new StreamWriter(stream, Encoding.UTF8, 1024, leaveOpen: true);
int indentLevel = 0;
WriteUsings(writer);
writer.WriteLine();

if (!string.IsNullOrWhiteSpace(Namespace))
{
writer.WriteLine(indentLevel, $"namespace {Namespace}");
writer.WriteLine(indentLevel, "{");
writer.WriteLine();
indentLevel += 1;
}
// Namespace
Expand Down Expand Up @@ -481,7 +484,6 @@ private void WriteUsings(TextWriter writer)

private int WriteTupleType(TextWriter writer, int indentLevel, Type tupleType)
{
writer.WriteLine();
writer.WriteLine(indentLevel, $"[System.CodeDom.Compiler.GeneratedCode(\"{Tool}\", \"{Version}\")]");
writer.WriteLine(indentLevel, $"public class {tupleType.Name}: {PrettyTypeName(tupleType.BaseType!)}");
writer.WriteLine(indentLevel, "{");
Expand Down
8 changes: 4 additions & 4 deletions Cql/CoreTests/ExpressionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hl7.Cql.Compiler;
using Hl7.Cql.Firely;
using Hl7.Cql.Fhir;
using Hl7.Cql.Elm;
using System.IO;
using Hl7.Cql;
using Hl7.Cql.Model;
using Hl7.Cql.Conversion;
using Hl7.Fhir.Introspection;
using Hl7.Fhir.Model;
using Hl7.Cql.Abstractions;

namespace CoreTests
{
[TestClass]
public class ExpressionBuilderTests
{
private static readonly TypeResolver TypeResolver = new FirelyTypeResolver(Hl7.Fhir.Model.ModelInfo.ModelInspector);
private static readonly TypeConverter TypeConverter = FirelyTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector);
private static readonly TypeResolver TypeResolver = new FhirTypeResolver(Hl7.Fhir.Model.ModelInfo.ModelInspector);
private static readonly TypeConverter TypeConverter = FhirTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector);

private ILogger<ExpressionBuilder> CreateLogger() => LoggerFactory
.Create(logging => logging.AddDebug())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using FluentAssertions;
using Hl7.Cql.Firely;
using Hl7.Cql.Fhir;
using Hl7.Cql.Primitives;
using Hl7.Cql.ValueSets;
using Hl7.Fhir.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace CoreTests.Firely
namespace CoreTests.Fhir
{
[TestClass]
public class DataRetrieverTests
public class DataSourceTests
{
[TestMethod]
public void FiltersOnResourceType()
{
var dr = buildRetriever();
var dr = buildDataSource();

var results = dr.RetrieveByCodes<Patient>();
results.Should().HaveCount(2);
Expand All @@ -25,7 +25,7 @@ public void FiltersOnResourceType()
[TestMethod]
public void FiltersOnDefaultProp()
{
var dr = buildRetriever();
var dr = buildDataSource();

var results = dr.RetrieveByCodes<Observation>(new[] { new CqlCode("x", "http://nu.nl", null, null) });
results.Should().HaveCount(1);
Expand All @@ -45,8 +45,8 @@ public void FiltersOnDefaultProp()
[TestMethod]
public void FiltersOnSpecificProp()
{
var dr = buildRetriever();
var model = new FirelyTypeResolver(ModelInfo.ModelInspector);
var dr = buildDataSource();
var model = new FhirTypeResolver(ModelInfo.ModelInspector);
var genderProp = model.GetProperty(model.ResolveType("{http://hl7.org/fhir}Patient"), "gender");
genderProp.Should().NotBeNull();

Expand All @@ -59,7 +59,7 @@ public void FiltersOnSpecificProp()
dr.RetrieveByCodes<Patient>(new[] { new CqlCode("male", "http://hl7.org/fhir/administrative-gender", null, null) }, activeProp).ToList());
}

private BundleDataRetriever buildRetriever()
private BundleDataSource buildDataSource()
{
var resources = new Resource[]
{
Expand All @@ -72,7 +72,7 @@ private BundleDataRetriever buildRetriever()
var bundle = new Bundle();
foreach (var r in resources) { bundle.AddResourceEntry(r, $"http://someresource/{r.GetHashCode()}"); }

return new BundleDataRetriever(bundle, new HashValueSetDictionary()); // we're not calling IsInValueSet, so we can pass an empty dict
return new BundleDataSource(bundle, new HashValueSetDictionary()); // we're not calling IsInValueSet, so we can pass an empty dict
}
}
}
4 changes: 2 additions & 2 deletions Cql/CoreTests/FhirTypeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hl7.Cql.Conversion;
using Hl7.Cql.Firely;
using Hl7.Cql.Fhir;
using Hl7.Cql.Iso8601;
using Hl7.Cql.Primitives;
using Hl7.Fhir.Model;
Expand All @@ -12,7 +12,7 @@ namespace CoreTests
[TestClass]
public class FhirTypeConverterTests
{
internal static readonly TypeConverter FhirTypeConverter = FirelyTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector);
internal static readonly TypeConverter FhirTypeConverter = Hl7.Cql.Fhir.FhirTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector);

[TestMethod]
public void ConvertParameters_Integer()
Expand Down
8 changes: 4 additions & 4 deletions Cql/CoreTests/HL7Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hl7.Cql.Compiler;
using Hl7.Cql.Firely;
using Hl7.Cql.Fhir;
using Hl7.Cql.Graph;
using Hl7.Cql.Runtime;
using Hl7.Fhir.Model;
Expand Down Expand Up @@ -44,8 +44,8 @@ public LambdasFacade()
public static void ClassInitialize(TestContext context)
{
var hl7TestDirectory = new DirectoryInfo(@"Input\ELM\HL7");
var resolver = new FirelyTypeResolver(ModelInfo.ModelInspector);
var binding = new CqlOperatorsBinding(resolver, FirelyTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector));
var resolver = new FhirTypeResolver(ModelInfo.ModelInspector);
var binding = new CqlOperatorsBinding(resolver, FhirTypeConverter.Create(Hl7.Fhir.Model.ModelInfo.ModelInspector));
var typeManager = new TypeManager(resolver);

var fhirHelpersPackage = Hl7.Cql.Elm.Library.LoadFromJson(new FileInfo(@"Input\ELM\Libs\FHIRHelpers-4.0.1.json"));
Expand Down Expand Up @@ -77,7 +77,7 @@ public static void ClassInitialize(TestContext context)
});

var allDelegates = LambdasByTestName.Lambdas.CompileAll();
Context = FirelyCqlContext.Create(delegates: allDelegates);
Context = FhirCqlContext.WithDataSource(delegates: allDelegates);
}

internal static CqlContext Context;
Expand Down
16 changes: 10 additions & 6 deletions Cql/CoreTests/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public void Model_FhirR4()
[TestMethod]
public void Age()
{
var dataRetriever = new UnitTestDataRetriever(new[] {
var dataSource = new UnitTestDataSource(new[] {
new UnitTestPatient {
birthDate = new DateIso8601(1983, 4, 4)
}
});

var ctx = new CqlContext(CqlOperators.Create(new UnitTestTypeResolver(),
dataRetriever: dataRetriever,
dataSource: dataSource,
now: new DateTimeIso8601(2023, 3, 28, null, null, null, null, null, null)));
var age = ctx.Operators.Age("a");
Assert.AreEqual(age, 39);
Expand All @@ -40,13 +40,13 @@ public void Age()
[TestMethod]
public void AgeAt()
{
var dataRetriever = new UnitTestDataRetriever(new[] {
var dataSource = new UnitTestDataSource(new[] {
new UnitTestPatient {
birthDate = new DateIso8601(1983, 4, 4)
}
});
var ctx = new CqlContext(CqlOperators.Create(new UnitTestTypeResolver(),
dataRetriever: dataRetriever,
dataSource: dataSource,
now: new DateTimeIso8601(2023, 3, 28, null, null, null, null, null, null)));
var age = ctx.Operators.AgeAt(new CqlDate(2013, 3, 28), "a");
Assert.AreEqual(age, 29);
Expand All @@ -57,15 +57,19 @@ private class UnitTestPatient
public DateIso8601 birthDate { get; set; }
}

private class UnitTestDataRetriever : IDataRetriever
private class UnitTestDataSource : IDataSource
{
public UnitTestDataRetriever(IEnumerable<object> data)
public UnitTestDataSource(IEnumerable<object> data)
{
Data = data?.ToList() ?? new List<object>();
}

public IList<object> Data { get; }

#if VNEXT
public event EventHandler DataChanged;
#endif

public IEnumerable<T> RetrieveByCodes<T>(IEnumerable<CqlCode> codes = null, PropertyInfo _ = null) where T : class =>
Data.OfType<T>();

Expand Down
Loading

0 comments on commit 04e83d5

Please sign in to comment.