private void ExportTable_EnumerateSubmodel( ExportTableAasEntitiesList list, AdminShell.AdministrationShellEnv env, bool broadSearch, int depth, AdminShell.Submodel sm, AdminShell.SubmodelElement sme) { // check if (list == null || env == null || sm == null) { return; } // // Submodel or SME ?? // AdminShell.IEnumerateChildren coll = null; if (sme == null) { // yield SM list.Add(new ExportTableAasEntitiesItem(depth, sm: sm)); // use collection coll = sm; } else { // simple check for SME collection if (sme is AdminShell.IEnumerateChildren) { coll = (sme as AdminShell.IEnumerateChildren); } } // pass 1: process value if (coll != null) { foreach (var ci in coll.EnumerateChildren()) { // gather data for this entity var sme2 = ci.submodelElement; var cd = env.FindConceptDescription(sme2?.semanticId?.Keys); list.Add(new ExportTableAasEntitiesItem(depth, sm, sme2, cd)); // go directly deeper? if (!broadSearch && ci.submodelElement != null && ci.submodelElement is AdminShell.IEnumerateChildren) { ExportTable_EnumerateSubmodel( list, env, broadSearch: false, depth: 1 + depth, sm: sm, sme: ci.submodelElement); } } } // pass 2: go for recursion AFTER? if (broadSearch) { if (coll != null) { foreach (var ci in coll.EnumerateChildren()) { if (ci.submodelElement != null && ci.submodelElement is AdminShell.IEnumerateChildren) { ExportTable_EnumerateSubmodel( list, env, broadSearch: true, depth: 1 + depth, sm: sm, sme: ci.submodelElement); } } } } }
public static void Export(AdminShell.AdministrationShellEnv env, AdminShell.Submodel sm, string fn) { // access if (fn == null || env == null || sm == null || sm.idShort == null || sm.submodelElements == null) { return; } // make text file // ReSharper disable once ConvertToUsingDeclaration using (var snippets = new System.IO.StreamWriter(fn)) { // Phase (1) which ConceptDescriptions need to be exported // Phase (2) export CDs as JSON // Phase (3) export SubModel as JSON without SMEs // Phase (4) export list of IDs used // Phase (5) generate look ups // Phase (1) which ConceptDescriptions need to be exported snippets.WriteLine("Phase (1) Check, which ConceptDescriptions need to be exported:"); snippets.WriteLine("==============================================================="); var usedCds = new Dictionary <string, AdminShell.ConceptDescription>(); foreach (var sme in sm.submodelElements?.FindDeep <AdminShell.SubmodelElement>()) { // for SME, try to lookup CD if (sme.semanticId == null) { continue; } var cd = env.FindConceptDescription(sme.semanticId.GetAsExactlyOneKey()); if (cd == null) { continue; } // name? var ids = cd.idShort; if ((ids == null || ids == "") && cd.GetIEC61360() != null) { ids = cd.GetIEC61360().shortName?.GetDefaultStr(); } if (ids == null || ids == "") { continue; } ids = "CD_" + ids; // only one in dictionary! if (usedCds.ContainsKey(ids)) { snippets.WriteLine($"Information: duplicate evaluated CD idShort {ids} .. skipping!"); continue; } snippets.WriteLine($"Identified {ids} to be exported"); // add usedCds.Add(ids, cd); } snippets.WriteLine(); // Phase (2) export SubModel as JSON without SMEs string message = "Phase (2) export SubModel as JSON without SMEs. " + "Paste the following into appropriate JSON (resource) file:"; snippets.WriteLine(message); snippets.WriteLine(new String('=', message.Length)); var keySM = "SM_" + sm.idShort; if (true) { // ok, for Serialization we just want the plain element with no BLOBs.. var settings = new JsonSerializerSettings(); settings.ContractResolver = new AdminShellConverters.AdaptiveFilterContractResolver( deep: false, complete: false); var jsonStr = JsonConvert.SerializeObject(sm, Formatting.Indented, settings); // export snippets.WriteLine($"\"{keySM}\" : {jsonStr},"); } snippets.WriteLine(); // Phase (3) export CDs message = "Phase (3) export CDs as JSON. Paste the following into appropriate JSON (resource) file:"; snippets.WriteLine(message); snippets.WriteLine(new String('=', message.Length)); foreach (var k in usedCds.Keys) { // ok, for Serialization we just want the plain element with no BLOBs.. var settings = new JsonSerializerSettings(); settings.ContractResolver = new AdminShellConverters.AdaptiveFilterContractResolver( deep: false, complete: false); var jsonStr = JsonConvert.SerializeObject(usedCds[k], Formatting.Indented, settings); // export snippets.WriteLine($"\"{k}\" : {jsonStr},"); } snippets.WriteLine(); // Phase (4) export list of IDs used message = "Phase (4) export list of IDs used. " + "Paste the following into appropriate C# file and reformat it:"; snippets.WriteLine(message); snippets.WriteLine(new String('=', message.Length)); snippets.WriteLine(keySM); foreach (var k in usedCds.Keys) { snippets.WriteLine($"\t\t{k},"); } snippets.WriteLine(); // Phase (5) generate look ups message = "Phase (5) generate look ups. Paste the following into appropriate C# file and reformat it:"; snippets.WriteLine(message); snippets.WriteLine(new String('=', message.Length)); snippets.WriteLine($"this.{keySM} = bs.RetrieveReferable<AdminShell.Submodel>(\"{keySM}\");"); foreach (var k in usedCds.Keys) { snippets.WriteLine($"this.{k} = bs.RetrieveReferable<AdminShell.ConceptDescription>(\"{k}\");"); } snippets.WriteLine(); } }
protected ContextResult CreateBodyCD( ImportCellMatchContextBase context, AdminShell.AdministrationShellEnv env) { // access if (context?.Sme == null || context?.CD == null || env == null || _options == null) { return(null); } // first test, if the CD already exists var test = env.FindConceptDescription(context.Sme.semanticId); if (test != null) { return new ContextResult() { Elem = test } } ; // a semanticId is required to link the Sme and the CD together if (context.Sme.semanticId == null || context.Sme.semanticId.Count < 1) { // generate a new one for SME + CD // this modifies the SME! var id = new AdminShell.Identification( AdminShell.Identification.IRI, AdminShellUtil.GenerateIdAccordingTemplate(_options.TemplateIdConceptDescription)); context.Sme.semanticId = new AdminShell.SemanticId( new AdminShell.Key(AdminShell.Key.ConceptDescription, true, id.idType, id.id)); } // create, add var cd = new AdminShell.ConceptDescription(context?.CD); env.ConceptDescriptions.Add(cd); var res = new ContextResult() { Elem = cd }; // link CD to SME var sid = context.Sme.semanticId.GetAsExactlyOneKey(); if (sid == null) { // should not happen, see above return(null); } cd.identification = new AdminShell.Identification(sid.idType, sid.value); // some further attributes if (!cd.idShort.HasContent()) { cd.idShort = context.Sme.idShort; } // ok return(res); }
private static void RecurseExportAsTemplate( AdminShell.SubmodelElementWrapperCollection smwc, FormDescListOfElement tels, AdminShell.AdministrationShellEnv env = null, AdminShell.ListOfConceptDescriptions cds = null) { // access if (smwc == null || tels == null) { return; } // over all elems foreach (var smw in smwc) { if (smw != null && smw.submodelElement != null) { FormDescSubmodelElement tsme = null; if (smw.submodelElement is AdminShell.Property p) { tsme = new FormDescProperty( "" + p.idShort, FormMultiplicity.One, p.semanticId?.GetAsExactlyOneKey(), "" + p.idShort, valueType: p.valueType); } if (smw.submodelElement is AdminShell.MultiLanguageProperty mlp) { tsme = new FormDescMultiLangProp( "" + mlp.idShort, FormMultiplicity.One, mlp.semanticId?.GetAsExactlyOneKey(), "" + mlp.idShort); } if (smw.submodelElement is AdminShell.File fl) { tsme = new FormDescFile( "" + fl.idShort, FormMultiplicity.One, fl.semanticId?.GetAsExactlyOneKey(), "" + fl.idShort); } if (smw.submodelElement is AdminShell.ReferenceElement rf) { tsme = new FormDescReferenceElement( "" + rf.idShort, FormMultiplicity.One, rf.semanticId?.GetAsExactlyOneKey(), "" + rf.idShort); } if (smw.submodelElement is AdminShell.SubmodelElementCollection smec) { tsme = new FormDescSubmodelElementCollection( "" + smec.idShort, FormMultiplicity.One, smec.semanticId?.GetAsExactlyOneKey(), "" + smec.idShort); } if (tsme != null) { // take over directly tsme.PresetCategory = smw.submodelElement.category; // Qualifers var qs = smw.submodelElement.qualifiers; var q = qs?.FindType("FormTitle"); if (q != null) { tsme.FormTitle = "" + q.value; } q = qs?.FindType("FormInfo"); if (q != null) { tsme.FormInfo = "" + q.value; } q = qs?.FindType("EditIdShort"); if (q != null) { tsme.FormEditIdShort = q.value.Trim().ToLower() == "true"; } q = qs?.FindType("EditDescription"); if (q != null) { tsme.FormEditDescription = q.value.Trim().ToLower() == "true"; } q = qs?.FindType("Multiplicity"); if (q != null) { foreach (var m in (FormMultiplicity[])Enum.GetValues(typeof(FormMultiplicity))) { if (("" + q.value) == Enum.GetName(typeof(FormMultiplicity), m)) { tsme.Multiplicity = m; } } } q = qs?.FindType("PresetValue"); if (q != null && tsme is FormDescProperty) { (tsme as FormDescProperty).presetValue = "" + q.value; } q = qs?.FindType("PresetMimeType"); if (q != null && tsme is FormDescFile) { (tsme as FormDescFile).presetMimeType = "" + q.value; } q = qs?.FindType("FormChoices"); if (q != null && q.value.HasContent() && tsme is FormDescProperty fdprop) { var choices = q.value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (choices != null && choices.Length > 0) { fdprop.comboBoxChoices = choices; } } // adopt presetIdShort if (tsme.Multiplicity == FormMultiplicity.ZeroToMany || tsme.Multiplicity == FormMultiplicity.OneToMany) { tsme.PresetIdShort += "{0:00}"; } // Ignore this element q = qs?.FindType("FormIgnore"); if (q != null) { continue; } } if (tsme != null) { tels.Add(tsme); } // in any case, check for CD if (env != null && cds != null && smw?.submodelElement?.semanticId?.Keys != null) { var masterCd = env.FindConceptDescription(smw?.submodelElement?.semanticId?.Keys); if (masterCd != null && masterCd.identification != null) { // already in cds? var copyCd = cds.Find(masterCd.identification); if (copyCd == null) { // add clone cds.Add(new AdminShell.ConceptDescription(masterCd)); } } } // recurse if (smw.submodelElement is AdminShell.SubmodelElementCollection) { RecurseExportAsTemplate( (smw.submodelElement as AdminShell.SubmodelElementCollection).value, (tsme as FormDescSubmodelElementCollection).value, env, cds); } } } }
private void ExportTable_EnumerateSubmodel( List <ExportTableAasEntitiesList> list, AdminShell.AdministrationShellEnv env, bool broadSearch, bool actInHierarchy, int depth, AdminShell.Submodel sm, AdminShell.SubmodelElement sme) { // check if (list == null || env == null || sm == null) { return; } // // Submodel or SME ?? // AdminShell.IEnumerateChildren coll = null; if (sme == null) { // yield SM // MIHO 21-11-24: IMHO this makes no sense //// list.Add(new ExportTableAasEntitiesItem(depth, sm: sm, parentSm: sm)); // use collection coll = sm; } else { // simple check for SME collection if (sme is AdminShell.IEnumerateChildren) { coll = (sme as AdminShell.IEnumerateChildren); } } // prepare listItem ExportTableAasEntitiesList listItem = null; if (!actInHierarchy) { // add everything in one list if (list.Count < 1) { list.Add(new ExportTableAasEntitiesList()); } listItem = list[0]; } else { // create a new list for each recursion listItem = new ExportTableAasEntitiesList(); list.Add(listItem); } // pass 1: process value if (coll != null) { foreach (var ci in coll.EnumerateChildren()) { // gather data for this entity var sme2 = ci.submodelElement; var cd = env.FindConceptDescription(sme2?.semanticId?.Keys); // add listItem.Add(new ExportTableAasEntitiesItem(depth, sm, sme2, cd, parent: coll as AdminShell.Referable)); // go directly deeper? if (!broadSearch && ci.submodelElement != null && ci.submodelElement is AdminShell.IEnumerateChildren) { ExportTable_EnumerateSubmodel( list, env, broadSearch: false, actInHierarchy, depth: 1 + depth, sm: sm, sme: ci.submodelElement); } } } // pass 2: go for recursion AFTER? if (broadSearch) { if (coll != null) { foreach (var ci in coll.EnumerateChildren()) { if (ci.submodelElement != null && ci.submodelElement is AdminShell.IEnumerateChildren) { ExportTable_EnumerateSubmodel( list, env, broadSearch: true, actInHierarchy, depth: 1 + depth, sm: sm, sme: ci.submodelElement); } } } } }