private ExeXml.Pol ClonePol(ExeXml.Pol pol)
 {
     ExeXml.Pol clone = new ExeXml.Pol {
         name = pol.name, on = pol.on, order = pol.order
     };
     foreach (ExeXml.Fun fun in pol.funs.Values)
     {
         clone.funs.Add(Guid.NewGuid().ToString(), CloneFun(fun));
     }
     return(clone);
 }
        private bool FindFun(string funID, Dictionary <string, ExeXml.Pol> pols, Description description,
                             out ExeXml.Fun foundFun, out ExeXml.Pol parentPol)
        {
            foreach (ExeXml.Pol pol in pols.Values)
            {
                if (pol.funs.ContainsKey(funID))
                {
                    foundFun = pol.funs[funID]; parentPol = pol; return(true);
                }
            }

            infoStore.communicator.ReportError(new Communicator.ErrorInfo()
            {
                isWarning = false,
                message   = $"{description.Get()}: unknown function-id {funID}"
            });
            foundFun = null; parentPol = null; return(false);
        }
        private bool FindPol(ExeXml.AddOn addOn, string polName, Dictionary <string, ExeXml.Pol> pols,
                             out ExeXml.Pol foundPol, out string foundPolID)
        {
            foreach (var p in pols)
            {
                if (p.Value.name.ToLower() == polName.ToLower())
                {
                    foundPol = p.Value; foundPolID = p.Key; return(true);
                }
            }

            infoStore.communicator.ReportError(new Communicator.ErrorInfo()
            {
                isWarning = false,
                message   = $"{addOn.cao.shortName}: policy {polName} not found"
            });
            foundPol = null; foundPolID = null; return(false);
        }
 internal Description(Description parDesc, string _queryName)
 {
     pol = parDesc.pol; fun = parDesc.fun; funID = parDesc.funID; par = parDesc.par; parID = parDesc.parID; queryName = _queryName;
 }
 internal Description(Description funDesc, ExeXml.Par _par, string _parID = null)
 {
     pol = funDesc.pol; fun = funDesc.fun; funID = funDesc.funID; par = _par; parID = _parID;
 }
        internal bool isPlaceholder = false; // is relevant for e.g. FunDefVar and FunDefUprate (see FunBase.GetPlaceholderGroupPar)

        internal Description(ExeXml.Pol _pol, ExeXml.Fun _fun, string _funID = null)
        {
            pol = _pol; fun = _fun; funID = _funID;
        }
        private void HandleExtensionSwitches(List <ExeXml.AddOn> addOns)
        {
            // first overwrite the default switch settings in the country file, if the user changed them (via config-settings sent by the run-tool)
            TakeExtensionSwitches(infoStore.runConfig.extensionSwitches);

            // then overwrite with any instructions from extensions, the means the priority of switches to be on or off is as follows:
            // highest: add-ons (via AddOn_ExtensionSwitch), next: user settings (via run-tool), lowest: default settings (via set-switches-dialog)
            TakeAddOnExtensionSwitches(addOns);

            // take care of a "feature" that is mainly there for compatibility with the version before March 2020, where the switches of functions within extension-policies were visible instead of set to switch
            // namly that such functions can be permanantly off instead of switchable
            List <ExeXml.Fun> permanentOffFunctions = TakeCareOfPermanentOffFunctions();

            // lists for taking into account that pol/fun/par can be switched by more than one extension and that 'on' always wins
            List <string> onIds = new List <string>();
            Dictionary <string, ExeXml.Fun> parsToRemove = new Dictionary <string, ExeXml.Fun>();

            foreach (var extension in infoStore.country.extensions.Values)
            {
                foreach (var extPol in extension.polIds)
                {
                    string polId     = extPol.Key;
                    bool   isBasePol = extPol.Value == true; // true: a base-policy, that is switched off if the extension is on

                    if (!infoStore.country.cao.pols.ContainsKey(polId))
                    {
                        continue;
                    }
                    ExeXml.Pol pol = infoStore.country.cao.pols[polId];

                    bool?switchOn = null;
                    if (extension.on == true)
                    {
                        switchOn = isBasePol ? false : true;                       // extension-switch is on: added-as-on ✔: on; added-as-off ✘: off
                    }
                    else if (!isBasePol)
                    {
                        switchOn = false;                  // extension-switch is off or n/a: added-as-on ✔: off; added-as-off ✘: do not touch
                    }
                    if (switchOn == true)
                    {
                        pol.on = true;
                    }
                    if (switchOn == false && !onIds.Contains(polId))
                    {
                        pol.on = false;
                    }

                    // add the policy and all its functions and parameters, which do not have an own setting, to the switched-on (i.e. winning) elements
                    if (switchOn == true)
                    {
                        onIds.AddUnique(polId);
                        foreach (var f in pol.funs)
                        {
                            if (!extension.funIds.ContainsKey(f.Key))
                            {
                                string funId = f.Key; ExeXml.Fun fun = f.Value;
                                fun.on = true; onIds.AddUnique(funId);
                                foreach (var parId in fun.pars.Keys)
                                {
                                    if (!extension.parIds.ContainsKey(parId))
                                    {
                                        if (parsToRemove.ContainsKey(parId))
                                        {
                                            parsToRemove.Remove(parId);
                                        }
                                        onIds.AddUnique(parId);
                                    }
                                }
                            }
                        }
                    }
                }
                foreach (var extFun in extension.funIds)
                {
                    string funId     = extFun.Key;
                    bool   isBaseFun = extFun.Value == true; // true: a base-function, that is switched off if the extension is on

                    ExeXml.Fun fun = null;
                    foreach (var pol in infoStore.country.cao.pols)
                    {
                        var funs = from f in pol.Value.funs where f.Key == funId select f; if (!funs.Any())
                        {
                            continue;
                        }
                        fun = funs.First().Value;
                        if (fun.pars.Count == 0)
                        {
                            fun = null;                      // this takes care of functions which are set to n/a including all their parameters for this system, i.e. ignores them
                        }
                        break;
                    }
                    if (fun == null)
                    {
                        continue;
                    }

                    // see explanation above, however note: if parent-policy is switched off, all included functions are off as well
                    // if parent-policy is switched on, included functions can still be switched off
                    bool?switchOn = null;
                    if (extension.on == true)
                    {
                        switchOn = isBaseFun ? false : true;                       // extension-switch is on: added-as-on ✔: on; added-as-off ✘: off
                    }
                    else if (!isBaseFun)
                    {
                        switchOn = false;                  // extension-switch is off or n/a: added-as-on ✔: off; added-as-off ✘: do not touch
                    }
                    if (switchOn == true)
                    {
                        fun.on = true;
                    }
                    if (switchOn == false && !onIds.Contains(funId))
                    {
                        fun.on = false;
                    }

                    // add the function and all its parameters, which do not have an own setting, to the switched-on (i.e. winning) elements
                    if (switchOn == true)
                    {
                        onIds.AddUnique(funId);
                        foreach (var parId in fun.pars.Keys)
                        {
                            if (!extension.parIds.ContainsKey(parId))
                            {
                                if (parsToRemove.ContainsKey(parId))
                                {
                                    parsToRemove.Remove(parId);
                                }
                                onIds.AddUnique(parId);
                            }
                        }
                    }
                }
                foreach (var extPar in extension.parIds)
                {
                    string parId = extPar.Key; bool isBasePar = extPar.Value == true;

                    // other than for pol/fun we do not need the par, but its id and the parent-fun, because we need to delete the parameter in case
                    ExeXml.Fun parentFun = null;
                    foreach (var pol in infoStore.country.cao.pols)
                    {
                        foreach (var fun in pol.Value.funs)
                        {
                            if ((from p in fun.Value.pars where p.Key == parId select p).Any())
                            {
                                parentFun = fun.Value; break;
                            }
                        }
                        if (parentFun != null)
                        {
                            break;
                        }
                    }
                    if (parentFun == null)
                    {
                        continue;
                    }

                    bool?keepPar = null;
                    if (extension.on == true)
                    {
                        keepPar = isBasePar ? false : true;                       // extension-switch is on: added-as-on ✔: keep par, added-as-off ✘: remove par
                    }
                    else if (!isBasePar)
                    {
                        keepPar = false;                  // extension-switch is off or n/a: added-as-on ✔: remove par, added-as-off ✘: do not touch
                    }
                    if (keepPar == true && parsToRemove.ContainsKey(parId))
                    {
                        parsToRemove.Remove(parId);
                    }
                    if (keepPar == false && !onIds.Contains(parId))
                    {
                        parsToRemove.TryAdd(parId, parentFun);
                    }
                    if (keepPar == true)
                    {
                        onIds.AddUnique(parId);
                    }
                }
            }
            foreach (var ptr in parsToRemove)
            {
                ptr.Value.pars.Remove(ptr.Key);                               // remove the respective parameters only after taking all extensions into account
            }
            foreach (ExeXml.Fun pof in permanentOffFunctions)
            {
                pof.on = false;
            }
        }
Ejemplo n.º 8
0
        private void CheckInfo(string switchFunId, Dictionary <string, Tuple <string, string> > changes,
                               out Dictionary <ExeXml.Fun, bool> switchInfo)
        {
            switchInfo = new Dictionary <ExeXml.Fun, bool>(); // the final switch is set in context with the run-cond, here only gather info
            foreach (var change in changes)
            {
                string group = change.Key, polFunIdent = change.Value.Item1, onOff = change.Value.Item2; bool on = true;
                if (polFunIdent == null || onOff == null)
                {
                    infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                    {
                        isWarning = false,
                        message   = $"Change param ({switchFunId}): insufficient definition of group {group}"
                    });
                    continue;
                }

                switch (onOff)
                {
                case DefPar.Value.ON: on = true; break;

                case DefPar.Value.OFF: on = false; break;

                default: infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                    {
                        isWarning = false,
                        message   = $"Change switch ({switchFunId}): invalid change {onOff}"
                    }); continue;
                }

                bool found = false;
                foreach (var p in infoStore.country.cao.pols)
                {
                    ExeXml.Pol pol = p.Value; string polId = p.Key;
                    if (polId.ToLower() == polFunIdent.ToLower() || pol.name.ToLower() == polFunIdent.ToLower()) // policy identified by id or name
                    {
                        if (pol.on != on)                                                                        // otherwise nothing to do: change from on to on or off to off
                        {
                            foreach (ExeXml.Fun fun in pol.funs.Values)
                            {
                                if (!fun.on)
                                {
                                    continue;    // a switched off function inside the policy is off, whatever the switch is set to
                                }
                                fun.on = pol.on; // the fun (currently on) must take the switch of the pol, because the pol is switched on (see below)
                                switchInfo.TryAdd(fun, on);
                            }
                            pol.on = true; // the policy must be on, otherwise the function-switches have no effect
                        }
                        found = true; break;
                    }

                    if (p.Value.funs.ContainsKey(polFunIdent)) // function (must be) identfied by id
                    {
                        if (p.Value.funs[polFunIdent].on != on)
                        {
                            switchInfo.Add(p.Value.funs[polFunIdent], on);
                        }
                        found = true; break;
                    }
                }
                if (!found)
                {
                    infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                    {
                        isWarning = false,
                        message   = $"Change switch ({switchFunId}): identifier {polFunIdent} not found"
                    });
                }
            }
        }