Example #1
0
        void BuildReferences(SDInfo fi,
                             List <SDInfo> references,
                             bool recurseFlag)
        {
            const String fcn = "BuildReferences";

            foreach (Extension e in fi.StructDef.Extension.ToArray())
            {
                if (e.Url.ToLower().Trim() == Global.FragmentUrl)
                {
                    FhirUrl extUrl = (FhirUrl)e.Value;
                    if (this.SDFragments.TryGetValue(extUrl.Value.Trim(), out SDInfo reference) == false)
                    {
                        this.ConversionError(this.GetType().Name,
                                             fcn,
                                             $"Can't find fragment {extUrl}");
                    }
                    else if (references.Contains(reference) == false)
                    {
                        if (recurseFlag)
                        {
                            this.BuildReferences(reference, references, recurseFlag);
                        }
                        references.Add(reference);
                    }
                }
            }
        }
Example #2
0
        void DefineInterfaces(SDInfo fi)
        {
            StringBuilder interfaces = new StringBuilder();

            SDInfo[] refFrags = fi.DirectReferencedFragments.ToArray();
            if (refFrags.Length > 0)
            {
                if (refFrags.Length == 1)
                {
                    interfaces
                    .AppendLine($": {CSMisc.InterfaceName(refFrags[0])}")
                    ;
                }
                else
                {
                    interfaces
                    .AppendLine($": {CSMisc.InterfaceName(refFrags[0])},")
                    ;
                    for (Int32 i = 1; i < refFrags.Length - 1; i++)
                    {
                        SDInfo refFrag = refFrags[i];
                        interfaces
                        .AppendLine($"        {CSMisc.InterfaceName(refFrag)},")
                        ;
                    }
                    interfaces
                    .AppendLine($"        {CSMisc.InterfaceName(refFrags[refFrags.Length - 1])}")
                    ;
                }
            }

            fi.InterfaceEditor.TryAddUserMacro("Interfaces", interfaces.ToString());
            fi.InterfaceEditor.Blocks.Find("*Header").Reload();
        }
Example #3
0
        public void AddFragment(String filePath)
        {
            const String fcn = "AddFragment";

            DomainResource domainResource;

            switch (Path.GetExtension(filePath).ToUpper(CultureInfo.InvariantCulture))
            {
            case ".XML":
            {
                FhirXmlParser parser = new FhirXmlParser();
                domainResource = parser.Parse <DomainResource>(File.ReadAllText(filePath));
                break;
            }

            case ".JSON":
            {
                FhirJsonParser parser = new FhirJsonParser();
                domainResource = parser.Parse <DomainResource>(File.ReadAllText(filePath));
                break;
            }

            default:
                throw new Exception($"Unknown extension for serialized fhir resource '{filePath}'");
            }

            switch (domainResource)
            {
            case CodeSystem cs:
            {
                CSInfo ci = new CSInfo(cs);
                this.CodeSystems.Add(cs.Url, ci);
            }
            break;

            case ValueSet vs:
            {
                VSInfo vi = new VSInfo(vs);
                this.ValueSets.Add(vs.Url, vi);
            }
            break;

            case StructureDefinition sd:
            {
                SDInfo fi = new SDInfo(this, sd);
                this.SDFragments.Add(sd.Url.Trim(), fi);
            }
            break;

            default:
                this.ConversionError(this.GetType().Name,
                                     fcn,
                                     $"Unimplemented fragment resource type {domainResource.GetType().Name} file {filePath}");
                return;
            }
        }
Example #4
0
        void BuildFragments()
        {
            List <SDInfo> unprocessed = new List <SDInfo>();

            unprocessed.AddRange(this.SDFragments.Values);

            bool Buildable(SDInfo fi)
            {
                foreach (SDInfo refFrag in fi.AllReferencedFragments)
                {
                    if (unprocessed.Contains(refFrag) == true)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            IEnumerable <SDInfo> OrderedFragments()
            {
                bool buildFlag = true;

                while (unprocessed.Count > 0)
                {
                    if (buildFlag == false)
                    {
                        throw new Exception("Unbuildable fragments. Circular reference?");
                    }
                    buildFlag = false;
                    Int32 index = 0;
                    while (index < unprocessed.Count)
                    {
                        SDInfo fi = unprocessed[index];
                        if (Buildable(fi))
                        {
                            buildFlag = true;
                            unprocessed.Remove(fi);
                            yield return(fi);
                        }
                        else
                        {
                            index += 1;
                        }
                    }
                }
            }

            SDInfo[] fiArr = OrderedFragments().ToArray();
            for (Int32 i = 0; i < fiArr.Length; i++)
            {
                SDInfo fi = fiArr[i];
                Trace.WriteLine($"Starting building fragment {fi.StructDef.Url.LastUriPart()}");
                this.BuildFragment(fi);
                Trace.WriteLine($"Completed building fragment {fi.StructDef.Url.LastUriPart()}");
            }
        }
        protected void MergeFragment(SDInfo fi)
        {
            const String fcn = "MergeFragment";

            this.CSBuilder.ConversionInfo(this.GetType().Name,
                                          fcn,
                                          $"Integrating fragment {fi.StructDef.Url.LastUriPart()}");

            if (fi != this.fragBase)
            {
                if (fi.ClassEditor != null)
                {
                    CodeBlockMerger cbm = new CodeBlockMerger(this.fragBase.ClassEditor);
                    foreach (CodeBlockNested codeBlock in fi.ClassEditor.Blocks.AllNamedBlocks)
                    {
                        cbm.Merge(codeBlock);
                    }
                }
            }
        }
        protected void VisitFragments(VisitFragment vi,
                                      SDInfo fragBase)
        {
            HashSet <SDInfo> visitedFrags = new HashSet <SDInfo>();

            void Visit(VisitFragment vi,
                       SDInfo fi,
                       Int32 level)
            {
                if (visitedFrags.Contains(fi))
                {
                    return;
                }
                vi(fi, level);
                visitedFrags.Add(fi);
                foreach (SDInfo refFrag in fragBase.AllReferencedFragments)
                {
                    Visit(vi, refFrag, level + 1);
                }
            }

            Visit(vi, fragBase, 0);
        }
Example #7
0
 public DefineComposition(Builder csBuilder,
                          SDInfo fragBase) : base(csBuilder, fragBase)
 {
 }
 public DefineBase(Builder csBuilder,
                   SDInfo fragBase)
 {
     this.CSBuilder = csBuilder;
     this.fragBase  = fragBase;
 }
Example #9
0
 public DefineExtension(Builder csBuilder,
                        SDInfo fragBase) : base(csBuilder, fragBase)
 {
 }
Example #10
0
 public static String ClassName(SDInfo fi) => $"{MachineName(fi.StructDef.Name)}";
Example #11
0
 public static String InterfaceName(SDInfo fi) => $"I{MachineName(fi.StructDef.Name)}";
Example #12
0
 public static String LocalClassNameSpace(SDInfo fi) => $"BreastRadLib.{CSMisc.ClassName(fi)}Local;";
 public DefineServiceRequest(Builder csBuilder,
                             SDInfo fragBase) : base(csBuilder, fragBase)
 {
 }
Example #14
0
 public DefineDiagnosticReport(Builder csBuilder,
                               SDInfo fragBase) : base(csBuilder, fragBase)
 {
 }
Example #15
0
        void BuildFragment(SDInfo fi)
        {
            const String fcn = "BuildFragment";

            String fhirType = fi.BaseDefinitionName;

            this.ConversionInfo(this.GetType().Name,
                                fcn,
                                $"Processing fragment {fi.StructDef.Name}");

            if (String.Compare(CSMisc.ClassName(fi), this.BreakOnClass, StringComparison.OrdinalIgnoreCase) == 0)
            {
                Debugger.Break();
            }

            if (
                (fi.IsFragment() == false) &&
                (fi.StructDef.BaseDefinition != Global.ExtensionUrl)
                )
            {
                String url = fi.StructDef.Url.Trim().ToLower();
                this.resourceFactoryProfileBlock
                .AppendCode($"case \"{url}\":")
                .AppendCode($"    return new {CSMisc.ClassName(fi)}(doc, ({fhirType}) resource);")
                ;
            }

            this.DefineInterfaces(fi);
            DefineBase csDef = null;

            switch (fi.StructDef.BaseDefinition)
            {
            case Global.ExtensionUrl:
                csDef = new DefineExtension(this, fi);
                break;

            case Global.ResourceUrl:
                break;

            case Global.ObservationUrl:
                csDef = new DefineObservation(this, fi);
                break;

            case Global.DiagnosticReportUrl:
                csDef = new DefineDiagnosticReport(this, fi);
                break;

            case Global.CompositionUrl:
                csDef = new DefineComposition(this, fi);
                break;

            case Global.ServiceRequestUrl:
                csDef = new DefineServiceRequest(this, fi);
                break;

            default:
                this.ConversionError(this.GetType().Name,
                                     fcn,
                                     $"No builder defined for class {fi.StructDef.BaseDefinition}");
                break;
            }

            if (csDef != null)
            {
                csDef.Clear();
                csDef.DefBase();
                csDef.Build();
            }
        }
Example #16
0
 void AddMacros(CodeEditor ce,
                SDInfo fi)
 {
     ce.TryAddUserMacro("FhirBase", fi.StructDef.BaseDefinition.LastUriPart());
 }