Beispiel #1
0
        /// <summary>
        /// Starts the real conversion process.
        /// It iterates over the GSDML properties and translates it to AML.
        /// Then it recursively starts a new Handle call with the translated property.
        /// </summary>
        /// <typeparam name="TA">The type of the current AML head object.</typeparam>
        /// <param name="currentAmlHead">The current AML head object.</param>
        /// <param name="currentGsdHead">The current GSD head object as a XmlElement.</param>
        private static void Handle <TA>(XmlNode currentGsdHead, TA currentAmlHead)
        {
            Logger?.Log(LogLevel.Debug, $"Started the Handle function with these heads. GSD: {currentGsdHead.Name} AML: {currentAmlHead.GetType().Name}");

            // Iterate over the properties of the current GSD parent.
            foreach (XmlNode gsdChildNode in currentGsdHead.ChildNodes)
            {
                Logger?.Log(LogLevel.Debug, $"Current iterated GSD child node {gsdChildNode.Name}.");

                // Try to get a translation rule of the gsdTranslationElements list.
                var translationRule = TranslationRules.FirstOrDefault(node => node.Name.Equals(gsdChildNode.Name));
                // If the rule does not exist, it cannot be translated. It continues with the next property.
                if (translationRule == null)
                {
                    Logger?.Log(LogLevel.Debug, $"Translation rule for {gsdChildNode.Name} was not found. Skip the node.");
                    continue;
                }
                Logger?.Log(LogLevel.Debug, $"Translation rule was found for {gsdChildNode.Name}. Now we are trying to translate it.");

                // Translate the gsdChildNode to AML.
                var newAmlHead = Translate(ref currentAmlHead, translationRule);
                Logger?.Log(LogLevel.Info, $"Translated successfully {gsdChildNode.Name} to {translationRule["Replacement"]?.FirstChild.Name}.");

                // If the new AML head is an array, it calls for every element in the array the Handle function.
                if (newAmlHead.GetType().IsArray)
                {
                    Logger?.Log(LogLevel.Debug, "The translated AML head is an array. Therefore for every element it calls the Handle function.");
                    foreach (var amlHeadElement in newAmlHead)
                    {
                        Handle(gsdChildNode, amlHeadElement);
                    }
                }
                else
                {
                    // Call the Handle function with the current gsdChildNode and the new AML head.
                    Logger?.Log(LogLevel.Debug, "The translated AML is not an array. Therefore the Handle function will be directly called.");
                    Handle(gsdChildNode, newAmlHead);
                }
            }
            Logger?.Log(LogLevel.Info, $"The Handle function ended for these heads. AML: {currentAmlHead.GetType().Name} GSD: {currentGsdHead.Name}");
        }
Beispiel #2
0
        /// <summary>
        /// This function handles a new Rule call in the translation table.
        /// It takes the inner text of the rule node and translate it with the corresponding rule.
        /// </summary>
        /// <param name="childNode">The rule node which contains the information which rule should be applied.</param>
        /// <param name="replacementInstance">The instance in which the rule instance will be set/added.</param>
        private static void HandleRuleCall(XmlNode childNode, dynamic replacementInstance)
        {
            // Get the translation rule
            var translationRule = TranslationRules.FirstOrDefault(node => node.Name.Equals(childNode.InnerText));

            if (translationRule == null)
            {
                Logger?.Log(LogLevel.Debug, $"Translation rule for {childNode.Name} was not found. Skip the node.");
                return;
            }

            var lastNode = Util.IterateThroughGsdDocument(translationRule.Name);

            if (lastNode == null)
            {
                Logger?.Log(LogLevel.Warning, $"Failed to iterate thorugh a rule path. {translationRule.Name}");
                // throw new InvalidDataException("Failed to handle a rule call in translation table.");
            }

            var preLastnode = (XmlElement)lastNode.ParentNode;

            var(ruleReplacement, _, refList) = Util.GetInformationFromRule(translationRule);
            var nodeList = preLastnode?.GetElementsByTagName(lastNode.Name);

            if (nodeList == null)
            {
                Logger?.Log(LogLevel.Error, $"Could not create a list out of {lastNode.Name}.");
                throw new InvalidDataException("Could not create a list out of a rule call in translation table.");
            }

            foreach (XmlNode node in nodeList)
            {
                var ruleReferences = Util.ParseReferences(refList, node);
                var(_, ruleReplacementPropertyType, isRuleReplacementPropertyArray) = Util.GetProperty(ruleReplacement.Name);
                var ruleReplacementInstance = Util.CreateInstance(ruleReplacementPropertyType, isRuleReplacementPropertyArray);
                SetAttributes(ruleReplacement, ruleReplacementInstance, ruleReferences);
                AddSubInstancesToInstance(ruleReplacement, ruleReplacementInstance, isRuleReplacementPropertyArray, ruleReferences);
                replacementInstance.Add(ruleReplacementInstance);
            }
        }