public static void UpdateMappings(
            Document document,
            ComponentOccurrence targetOcc,
            List <ParameterMappingInfo> erasedMappings,
            List <ParameterMappingInfo> newMappings,
            string ruleName,
            bool fireDepRules,
            bool dontRunAuto,
            bool updateWhenDone)
        {
            try
            {
                IiLogicAutomation ruleApi = iLogicUtilities.GetiLogicAutomation();

                iLogicRule rule = ruleApi.GetRule(document, ruleName);

                if (rule == null)
                {
                    rule = ruleApi.AddRule(document, ruleName, string.Empty);
                }

                //First we will remove erased mappings
                foreach (ParameterMappingInfo mapping in erasedMappings)
                {
                    string mappingStr = CreateMappingString(mapping);

                    if (rule.Text.Contains(mappingStr))
                    {
                        rule.Text = rule.Text.Replace(mappingStr, string.Empty);
                    }
                }

                //Clean up rule, we will remove all empty sections here
                if (erasedMappings.Count > 0)
                {
                    RemoveEmptySections(rule);
                }

                //Then add new mappings
                List <string> newMappingsStr = new List <string>();

                foreach (ParameterMappingInfo mapping in newMappings)
                {
                    string mappingStr = CreateMappingString(mapping);

                    if (!rule.Text.Contains(mappingStr))
                    {
                        newMappingsStr.Add(mappingStr);
                    }
                }

                string updateStr = "iLogicVb.UpdateWhenDone = " + (updateWhenDone ? "True" : "False");

                if (!rule.Text.Contains(updateStr))
                {
                    newMappingsStr.Add(updateStr);
                }

                if (newMappingsStr.Count > 0)
                {
                    string startTag = "\n'<LinkParameters:Section:Start> This code section was generated by LinkParameters Add-In (Do NOT modify)\n";

                    string endTag = "'<LinkParameters:Section:End>" + " [Date: " + System.DateTime.Now.ToString() + "]\n";

                    rule.Text += startTag;

                    foreach (string line in newMappingsStr)
                    {
                        rule.Text += line + "\n";
                    }

                    rule.Text += endTag;
                }

                rule.FireDependentImmediately = fireDepRules;
                rule.AutomaticOnParamChange   = !dontRunAuto;

                if (!dontRunAuto)
                {
                    ruleApi.RunRuleDirect(rule);
                }
            }
            catch
            {
            }
        }