public void Test(string extension)
        {
            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                foreach (string aasxPath in aasxPaths)
                {
                    /*
                     * The chain is as follows:
                     * - First load from AASX (package A)
                     * - Convert package 1 to `extension` format and save as path 1
                     * - Load from the path 1 in `extension` format (package B)
                     * - Save package B in `extension` format to path 2
                     *
                     * We expect the content of the two files (path 1 and path 2, respectively) to be equal.
                     */
                    using (var packageA = new AdminShellPackageEnv(aasxPath))
                    {
                        string path1 = System.IO.Path.Combine(tmpDir.Path, $"first{extension}");
                        string path2 = System.IO.Path.Combine(tmpDir.Path, $"second{extension}");

                        packageA.SaveAs(path1, writeFreshly: true);

                        using (var packageB = new AdminShellPackageEnv(path1))
                        {
                            packageB.SaveAs(path2, writeFreshly: true);
                            AssertFilesEqual(path1, path2, aasxPath);
                        }
                    }
                }
            }
        }
Exemple #2
0
        /*
         * TODO (mristin, 2020-10-05): This test has been temporary disabled so that we can merge in the branch
         * MIHO/EnhanceDocumentShelf. The test should be fixed in a future pull request and we will then re-enable it
         * again.
         *
         * Please do not forget to remove the Resharper directive at the top of this class.
         *
         * [Test]
         *
         * dead-csharp ignore this comment
         */
        public void TestLoadSaveXmlValidate()
        {
            var validator = AasSchemaValidation.NewXmlValidator();

            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                string tmpDirPath = tmpDir.Path;

                foreach (string aasxPath in aasxPaths)
                {
                    using (var package = new AdminShellPackageEnv(aasxPath))
                    {
                        /*
                         * TODO (mristin, 2020-09-17): Remove autofix once XSD and Aasx library in sync
                         *
                         * Package has been loaded, now we need to do an automatic check & fix.
                         *
                         * This is necessary as Aasx library is still not conform with the XSD AASX schema and breaks
                         * certain constraints (*e.g.*, the cardinality of langString = 1..*).
                         */
                        var recs = package.AasEnv.ValidateAll();
                        if (recs != null)
                        {
                            package.AasEnv.AutoFix(recs);
                        }

                        // Save as XML
                        string name    = Path.GetFileName(aasxPath);
                        string outPath = System.IO.Path.Combine(tmpDirPath, $"{name}.converted.xml");
                        package.SaveAs(outPath, writeFreshly: true);

                        using (var fileStream = System.IO.File.OpenRead(outPath))
                        {
                            var records = new AasValidationRecordList();
                            validator.Validate(records, fileStream);
                            if (records.Count != 0)
                            {
                                var parts = new List <string>
                                {
                                    $"Failed to validate XML file exported from {aasxPath} to {outPath}:"
                                };
                                parts.AddRange(records.Select((r) => r.Message));
                                throw new AssertionException(string.Join(Environment.NewLine, parts));
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private void ButtonTabPanels_Click(object sender, RoutedEventArgs e)
        {
            if (sender == ButtonCreateDoc)
            {
                // show the edit panel
                OuterTabControl.SelectedItem = TabPanelEdit;
                ButtonAddUpdateDoc.Content   = "Add";

                //// TODO (MIHO, 2020-09-29): if the V1.1 template works and is adopted, the old
                //// V1.0 shall be removed completely (over complicated) */
                //// make a template description for the content (remeber it)
                var desc = theOptions.FormVdi2770;
                if (desc == null)
                {
                    desc = DocumentShelfOptions.CreateVdi2770TemplateDesc(theOptions);
                }

                // latest version (from resources)
                if (this.CheckBoxLatestVersion.IsChecked == true)
                {
                    desc = DocumentShelfOptions.CreateVdi2770v11TemplateDesc();
                }

                this.currentFormDescription = desc;
                formInUpdateMode            = false;
                updateSourceElements        = null;

                // take over existing data
                this.currentFormInst = new FormInstanceSubmodelElementCollection(null, currentFormDescription);
                this.currentFormInst.PresetInstancesBasedOnSource(updateSourceElements);
                this.currentFormInst.outerEventStack = theEventStack;

                // bring it to the panel
                var elementsCntl = new FormListOfDifferentControl();
                elementsCntl.ShowHeader  = false;
                elementsCntl.DataContext = this.currentFormInst;
                ScrollViewerForm.Content = elementsCntl;
            }

            if (sender == ButtonAddUpdateDoc)
            {
                // add
                if (this.currentFormInst != null && this.currentFormDescription != null &&
                    thePackage != null &&
                    theOptions != null && theOptions.SemIdDocument != null &&
                    theSubmodel != null)
                {
                    // on this level of the hierarchy, shall a new SMEC be created or shall
                    // the existing source of elements be used?
                    AdminShell.SubmodelElementWrapperCollection currentElements = null;
                    if (formInUpdateMode && updateSourceElements != null)
                    {
                        currentElements = updateSourceElements;
                    }
                    else
                    {
                        currentElements = new AdminShell.SubmodelElementWrapperCollection();
                    }

                    // create a sequence of SMEs
                    try
                    {
                        this.currentFormInst.AddOrUpdateDifferentElementsToCollection(
                            currentElements, thePackage, addFilesToPackage: true);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "when adding Document");
                    }

                    // the InstSubmodel, which started the process, should have a "fresh" SMEC available
                    // make it unique in the Documentens Submodel
                    var newSmc = this.currentFormInst?.sme as AdminShell.SubmodelElementCollection;

                    // if not update, put them into the Document's Submodel
                    if (!formInUpdateMode && currentElements != null && newSmc != null)
                    {
                        // make newSmc unique in the cotext of the Submodel
                        FormInstanceHelper.MakeIdShortUnique(theSubmodel.submodelElements, newSmc);

                        // add the elements
                        newSmc.value = currentElements;

                        // add the whole SMC
                        theSubmodel.Add(newSmc);
                    }

#if __may_be_not__
                    // save directly to ensure consistency
                    try
                    {
                        if (thePackage.Filename != null)
                        {
                            thePackage.SaveAs(thePackage.Filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (theLogger != null)
                        {
                            theLogger.Log(
                                $"Saving package {thePackage.Filename} failed for adding Document " +
                                $"and gave: {ex.Message}");
                        }
                    }
#endif
                }
                else
                {
                    Log.Error("Preconditions for adding Document not met.");
                }

                // change back
                OuterTabControl.SelectedItem = TabPanelList;

                // re-display
                ParseSubmodelToListItems(
                    this.theSubmodel, this.theOptions, theViewModel.TheSelectedDocClass,
                    theViewModel.TheSelectedLanguage, theViewModel.TheSelectedListType);

                // re-display also in Explorer
                var evt = new AasxPluginResultEventRedrawAllElements();
                if (theEventStack != null)
                {
                    theEventStack.PushEvent(evt);
                }
            }

            if (sender == ButtonCancel)
            {
                OuterTabControl.SelectedItem = TabPanelList;
            }

            if (sender == ButtonFixCDs)
            {
                // check if CDs are present
                var theDefs = new AasxPredefinedConcepts.DefinitionsVDI2770.SetOfDefsVDI2770(
                    new AasxPredefinedConcepts.DefinitionsVDI2770());
                var theCds = theDefs.GetAllReferables().Where(
                    (rf) => { return(rf is AdminShell.ConceptDescription); }).ToList();

                // v11
                if (CheckBoxLatestVersion.IsChecked == true)
                {
                    theCds = AasxPredefinedConcepts.VDI2770v11.Static.GetAllReferables().Where(
                        (rf) => { return(rf is AdminShell.ConceptDescription); }).ToList();
                }

                if (theCds.Count < 1)
                {
                    Log.Error(
                        "Not able to find appropriate ConceptDescriptions in pre-definitions. " +
                        "Aborting.");
                    return;
                }

                // check for Environment
                var env = this.thePackage?.AasEnv;
                if (env == null)
                {
                    Log.Error(
                        "Not able to access AAS environment for set of Submodel's ConceptDescriptions. Aborting.");
                    return;
                }

                // be safe?
                if (MessageBoxResult.Yes != MessageBox.Show(
                        "Add missing ConceptDescriptions to the AAS?", "Question",
                        MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }

                // ok, check
                int nr = 0;
                foreach (var x in theCds)
                {
                    var cd = x as AdminShell.ConceptDescription;
                    if (cd == null || cd.identification == null)
                    {
                        continue;
                    }
                    var cdFound = env.FindConceptDescription(cd.identification);
                    if (cdFound != null)
                    {
                        continue;
                    }
                    // ok, add
                    var newCd = new AdminShell.ConceptDescription(cd);
                    env.ConceptDescriptions.Add(newCd);
                    nr++;
                }

                // ok
                Log.Info("In total, {0} ConceptDescriptions were added to the AAS environment.", nr);
            }

            if (sender == ButtonCreateEntity)
            {
                // show the edit panel
                OuterTabControl.SelectedItem = TabPanelEntity;
            }

            if (sender == ButtonCancelEntity)
            {
                OuterTabControl.SelectedItem = TabPanelList;
            }

            if (sender == ButtonAddEntity &&
                this.theSubmodel != null &&
                TextBoxEntityIdShort.Text.Trim().HasContent())
            {
                // add entity
                this.theSubmodel.SmeForWrite.CreateSMEForCD <AdminShell.Entity>(
                    AasxPredefinedConcepts.VDI2770v11.Static.CD_DocumentedEntity,
                    idShort: "" + TextBoxEntityIdShort.Text.Trim(),
                    addSme: true);

                // switch back
                OuterTabControl.SelectedItem = TabPanelList;

                // re-display also in Explorer
                var evt = new AasxPluginResultEventRedrawAllElements();
                if (theEventStack != null)
                {
                    theEventStack.PushEvent(evt);
                }
            }
        }
        private void ButtonTabPanels_Click(object sender, RoutedEventArgs e)
        {
            if (sender == ButtonCreateDoc)
            {
                // show the edit panel
                OuterTabControl.SelectedItem = TabPanelEdit;
                ButtonAddUpdateDoc.Content   = "Add";

                // make a template description for the content (remeber it)
                var desc = theOptions.FormVdi2770;
                if (desc == null)
                {
                    desc = DocumentShelfOptions.CreateVdi2770TemplateDesc(theOptions);
                }

                this.currentFormDescription = desc;
                formInUpdateMode            = false;
                updateSourceElements        = null;

                // take over existing data
                this.currentFormInst = new FormInstanceSubmodelElementCollection(null, currentFormDescription);
                this.currentFormInst.PresetInstancesBasedOnSource(updateSourceElements);

                // bring it to the panel
                var elementsCntl = new FormListOfDifferentControl();
                elementsCntl.ShowHeader  = false;
                elementsCntl.DataContext = this.currentFormInst;
                ScrollViewerForm.Content = elementsCntl;
            }

            if (sender == ButtonAddUpdateDoc)
            {
                // add
                if (this.currentFormInst != null && this.currentFormDescription != null &&
                    thePackage != null &&
                    theOptions != null && theOptions.SemIdDocument != null &&
                    theSubmodel != null)
                {
                    // on this level of the hierarchy, shall a new SMEC be created or shall
                    // the existing source of elements be used?
                    AdminShell.SubmodelElementWrapperCollection currentElements = null;
                    if (formInUpdateMode && updateSourceElements != null)
                    {
                        currentElements = updateSourceElements;
                    }
                    else
                    {
                        currentElements = new AdminShell.SubmodelElementWrapperCollection();
                    }

                    // create a sequence of SMEs
                    try
                    {
                        this.currentFormInst.AddOrUpdateDifferentElementsToCollection(
                            currentElements, thePackage, addFilesToPackage: true);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "when adding Document");
                    }

                    // the InstSubmodel, which started the process, should have a "fresh" SMEC available
                    // make it unique in the Documentens Submodel
                    var newSmc = this.currentFormInst?.sme as AdminShell.SubmodelElementCollection;

                    // if not update, put them into the Document's Submodel
                    if (!formInUpdateMode && currentElements != null && newSmc != null)
                    {
                        // make newSmc unique in the cotext of the Submodel
                        FormInstanceHelper.MakeIdShortUnique(theSubmodel.submodelElements, newSmc);

                        // add the elements
                        newSmc.value = currentElements;

                        // add the whole SMC
                        theSubmodel.Add(newSmc);
                    }

#if __may_be_not__
                    // save directly to ensure consistency
                    try
                    {
                        if (thePackage.Filename != null)
                        {
                            thePackage.SaveAs(thePackage.Filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (theLogger != null)
                        {
                            theLogger.Log(
                                $"Saving package {thePackage.Filename} failed for adding Document " +
                                $"and gave: {ex.Message}");
                        }
                    }
#endif
                }
                else
                {
                    Log.Error("Preconditions for adding Document not met.");
                }

                // change back
                OuterTabControl.SelectedItem = TabPanelList;

                // re-display
                ParseSubmodelToListItems(
                    this.theSubmodel, this.theOptions, theViewModel.TheSelectedDocClass,
                    theViewModel.TheSelectedLanguage, theViewModel.TheSelectedListType);

                // re-display also in Explorer
                var evt = new AasxPluginResultEventRedrawAllElements();
                if (theEventStack != null)
                {
                    theEventStack.PushEvent(evt);
                }
            }

            if (sender == ButtonCancel)
            {
                OuterTabControl.SelectedItem = TabPanelList;
            }
        }
Exemple #5
0
        private void ButtonTabPanels_Click(object sender, RoutedEventArgs e)
        {
            if (sender == ButtonUpdate)
            {
                // add
                if (this.currentFormInst != null &&
                    thePackage != null &&
                    theOptions != null &&
                    theSubmodel != null)
                {
                    // on this level of the hierarchy, shall a new SMEC be created or shall
                    // the existing source of elements be used?
                    AdminShell.SubmodelElementWrapperCollection currentElements = null;
                    if (formInUpdateMode && updateSourceElements != null)
                    {
                        currentElements = updateSourceElements;
                    }
                    else
                    {
                    }

                    // create a sequence of SMEs
                    try
                    {
                        this.currentFormInst.AddOrUpdateDifferentElementsToCollection(
                            currentElements, thePackage, addFilesToPackage: true, editSource: true);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "when adding Document");
                    }

#if __may_be_not__
                    // save directly to ensure consistency
                    try
                    {
                        if (thePackage.Filename != null)
                        {
                            thePackage.SaveAs(thePackage.Filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (theLogger != null)
                        {
                            theLogger.Log(
                                $"Saving package {thePackage.Filename} failed for adding Document " +
                                $"and gave: {ex.Message}");
                        }
                    }
#endif
                }
                else
                {
                    Log.Error("Preconditions for adding entities from GenericForm not met.");
                }

                // re-display
                DisplaySubmodel();

                // re-display also in Explorer
                var evt = new AasxPluginResultEventRedrawAllElements();
                if (theEventStack != null)
                {
                    theEventStack.PushEvent(evt);
                }
            }

            if (sender == ButtonFixCDs)
            {
                // check if CDs are present
                if (currentFormRecord == null || currentFormRecord.ConceptDescriptions == null ||
                    currentFormRecord.ConceptDescriptions.Count < 1)
                {
                    Log.Error(
                        "Not able to find appropriate ConceptDescriptions in the GeneralForm option records. " +
                        "Aborting.");
                    return;
                }

                // check for Environment
                var env = this.thePackage?.AasEnv;
                if (env == null)
                {
                    Log.Error(
                        "Not able to access AAS environment for set of Submodel's ConceptDescriptions. Aborting.");
                    return;
                }

                // be safe?
                if (MessageBoxResult.Yes != MessageBox.Show(
                        "Add missing ConceptDescriptions to the AAS?", "Question",
                        MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }

                // ok, check
                int nr = 0;
                foreach (var cd in currentFormRecord.ConceptDescriptions)
                {
                    if (cd == null || cd.identification == null)
                    {
                        continue;
                    }
                    var cdFound = env.FindConceptDescription(cd.identification);
                    if (cdFound != null)
                    {
                        continue;
                    }
                    // ok, add
                    var newCd = new AdminShell.ConceptDescription(cd);
                    env.ConceptDescriptions.Add(newCd);
                    nr++;
                }

                // ok
                Log.Info("In total, {0} ConceptDescriptions were added to the AAS environment.", nr);
            }
        }
Exemple #6
0
        public void TestLoadSaveXmlValidate()
        {
            // Load the schema

            var xmlSchemaSet = new Xml.Schema.XmlSchemaSet();

            xmlSchemaSet.XmlResolver = new Xml.XmlUrlResolver();

            string schemaPath = Path.Combine(
                TestContext.CurrentContext.TestDirectory,
                "Resources\\schemas\\xml\\AAS.xsd");

            xmlSchemaSet.Add(null, schemaPath);

            var schemaMessages = new List <string>();

            xmlSchemaSet.ValidationEventHandler +=
                (object sender, Xml.Schema.ValidationEventArgs e) => { schemaMessages.Add(e.Message); };
            xmlSchemaSet.Compile();

            if (schemaMessages.Count > 0)
            {
                var parts = new List <string> {
                    $"Failed to compile the schema: {schemaPath}"
                };
                parts.AddRange(schemaMessages);

                throw new InvalidOperationException(string.Join(Environment.NewLine, parts));
            }

            // Load-Save-Validate

            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                string tmpDirPath = tmpDir.Path;

                foreach (string aasxPath in aasxPaths)
                {
                    using (var package = new AdminShellPackageEnv(aasxPath))
                    {
                        string name    = Path.GetFileName(aasxPath);
                        string outPath = System.IO.Path.Combine(tmpDirPath, $"{name}.converted.xml");

                        package.SaveAs(outPath, writeFreshly: true);

                        var settings = new Xml.XmlReaderSettings();
                        settings.ValidationType = Xml.ValidationType.Schema;
                        settings.Schemas        = xmlSchemaSet;

                        var messages = new List <string>();
                        settings.ValidationEventHandler +=
                            (object sender, Xml.Schema.ValidationEventArgs e) =>
                        {
                            messages.Add(e.Message);
                        };

                        using (var reader = Xml.XmlReader.Create(outPath, settings))
                        {
                            while (reader.Read())
                            {
                                // Invoke callbacks
                            }
                            ;

                            if (messages.Count > 0)
                            {
                                var parts = new List <string>
                                {
                                    $"Failed to validate XML file exported from {aasxPath} to {outPath}:"
                                };
                                parts.AddRange(messages);

                                throw new AssertionException(string.Join(Environment.NewLine, parts));
                            }
                        }
                    }
                }
            }
        }