Esempio n. 1
0
            public static string EcActionsCounterChangeBlock(IEnumerable <ECState> states)
            {
                string rules   = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S1 + ": 1;\n";
                string rformat = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S2 + " & " + Smv.AlgStepsCounterVar +
                                 "=0 & ({0}): ";
                const string modFormatSuffix    = "({1}) mod {2};\n";
                const string normalFormatSuffix = "{1};\n";

                string rule1 = "";
                string rule2 = "";

                foreach (ECState state in states)
                {
                    string add = "(";
                    add   += Smv.EccStateVar + "=" + Smv.EccState(state.Name);
                    add   += " & ";
                    add   += Smv.EcActionsCounterVar + " {0} " + (state.ActionsCount > 0 ? state.ActionsCount : 1);
                    add   += ") | ";
                    rule1 += String.Format(add, "<");
                    rule2 += String.Format(add, "=");
                }
                rules += String.Format(rformat + modFormatSuffix, rule1.TrimEnd(Smv.OrTrimChars), Smv.EcActionsCounterVar + " + 1", states.Max(state => state.ActionsCount) + 1);
                rules += String.Format(rformat + normalFormatSuffix, rule2.TrimEnd(Smv.OrTrimChars), " 0 ");

                return(String.Format(Smv.NextCaseBlock, Smv.EcActionsCounterVar, rules));
            }
Esempio n. 2
0
            public static string EccStatesDeclaration(IEnumerable <ECState> states)
            {
                string eccStates = "";

                foreach (var ecState in states)
                {
                    eccStates += Smv.EccState(ecState.Name) + Smv.ModuleParameters.Splitter;
                }
                return(String.Format("VAR {0} : {{{1}}};\n", Smv.EccStateVar,
                                     eccStates.TrimEnd(Smv.ModuleParameters.Splitter.ToCharArray())));
            }
Esempio n. 3
0
            public static string BasicModuleDefines(IEnumerable <ECState> states, IEnumerable <Event> events, IEnumerable <ECTransition> transitions, bool showUnconditionalTransitions)
            {
                string ecTran = "";

                foreach (ECState state in states)
                {
                    IEnumerable <ECTransition> stateTrans = transitions.Where(t => t.Source == state.Name);
                    if (!stateTrans.Any())
                    {
                        continue;
                    }

                    ecTran += "(";
                    ecTran += Smv.EccStateVar + "=" + Smv.EccState(state.Name);

                    string transitionRules = "";
                    foreach (var transition in stateTrans)
                    {
                        transitionRules += "(";
                        if (transition.Condition == null || transition.Condition == "1")
                        {
                            if (!showUnconditionalTransitions)
                            {
                                transitionRules = null;
                                break;
                            }
                            else
                            {
                                transitionRules += "1";
                            }
                        }
                        else
                        {
                            transitionRules += _translateEventNames(Smv.ClearConditionExpr(transition.Condition), events);
                        }

                        transitionRules += ") | ";
                    }
                    if (transitionRules != null)
                    {
                        ecTran += "  & (" + transitionRules.TrimEnd(Smv.OrTrimChars) + ")";
                    }
                    ecTran += ") | ";
                }

                string existsEnabledECTran  = String.Format("DEFINE {0}:= {1};\n", Smv.ExistsEnabledEcTran, ecTran.Trim(Smv.OrTrimChars));
                string absentsEnabledECTran = "\n";

                //string alphabeta = "--alpha/beta\nDEFINE alpha_beta := ( (alpha & S_smv=s0_osm & !ExistsInputEvent | S_smv=s1_osm & (!ExistsEnabledECTran)) );\n";
                return(FbSmvCommon.DefineExistsInputEvent(events) + existsEnabledECTran + absentsEnabledECTran); // + alphabeta;
            }
Esempio n. 4
0
            private static void _addCounterRules(ref string rule1, ref string rule2, ECState state, int algsCount, ECAction action)
            {
                string add = "(";

                add += Smv.EccStateVar + "=" + Smv.EccState(state.Name);
                add += " & ";
                add += Smv.EcActionsCounterVar + " = " + (action.Number);
                //(state.ActionsCount > 0 ? state.ActionsCount : 1);
                add   += " & ";
                add   += Smv.AlgStepsCounterVar + " {0} " + (algsCount > 0 ? algsCount : 1);
                add   += ") | ";
                rule1 += String.Format(add, "<");
                rule2 += String.Format(add, "=");
            }
Esempio n. 5
0
            public static string AlgStepsCounterChangeBlock(IEnumerable <ECState> states, IEnumerable <ECAction> actions, IEnumerable <TranslatedAlg> algorithms)
            {
                //if (!algorithms.Any()) return "";
                string       rules              = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S1 + ": 1;\n";
                string       rformat            = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S2 + " & ({0}):";
                const string modFormatSuffix    = "({1}) mod {2};\n";
                const string normalFormatSuffix = "{1};\n";
                string       rule1              = "";
                string       rule2              = "";

                foreach (ECState state in states)
                {
                    string stateName = state.Name;
                    if (state.ActionsCount == 0)
                    {
                        string add = "(";
                        add   += Smv.EccStateVar + "=" + Smv.EccState(state.Name);
                        add   += " & ";
                        add   += Smv.EcActionsCounterVar + " = 1";
                        add   += " & ";
                        add   += Smv.AlgStepsCounterVar + " {0} 1";
                        add   += ") | ";
                        rule1 += String.Format(add, "<");
                        rule2 += String.Format(add, "=");
                    }
                    else
                    {
                        foreach (ECAction action in actions.Where(act => act.ECState == stateName))
                        {
                            int algsCount = 0;
                            if (action.Algorithm != null)
                            {
                                var actionAlg = algorithms.FirstOrDefault(alg => alg.Name == action.Algorithm);
                                if (actionAlg != null)
                                {
                                    algsCount = actionAlg.Lines.Count;
                                }
                            }
                            _addCounterRules(ref rule1, ref rule2, state, algsCount, action);
                        }
                    }
                }
                int maxAlgStepsCount = algorithms.Any() ? algorithms.Max(alg => alg.Lines.Max(line => line.NI)) : 1;

                rules += String.Format(rformat + modFormatSuffix, rule1.TrimEnd(Smv.OrTrimChars), Smv.AlgStepsCounterVar + " + 1", maxAlgStepsCount + 1);
                rules += String.Format(rformat + normalFormatSuffix, rule2.TrimEnd(Smv.OrTrimChars), " 0 ");

                return(String.Format(Smv.NextCaseBlock, Smv.AlgStepsCounterVar, rules));
            }
Esempio n. 6
0
            private static string _getVarChangingRules(Variable variable, IEnumerable <AlgorithmLine> currrentVarLines, IEnumerable <ECAction> actions, Settings settings)
            {
                string rules = "";

                foreach (AlgorithmLine line in currrentVarLines)
                {
                    foreach (ECAction action in _findActionsByAlgorithmName(actions, line.AlgorithmName))
                    {
                        string rule = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S2;
                        rule += " & ";
                        rule += Smv.EccStateVar + "=" + Smv.EccState(action.ECState);
                        rule += " & ";
                        rule += Smv.EcActionsCounterVar + "=" + action.Number;
                        rule += " & ";
                        rule += Smv.AlgStepsCounterVar + "=" + line.NI;
                        if (line.Condition != "1")
                        {
                            rule += " & ";
                            rule += "(" + Smv.ClearConditionExpr(line.Condition) + ")";
                        }
                        //string val = line.Value;
                        string val = Smv.ClearConditionExpr(line.Value);
                        if (String.Compare(val, "false", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            val = Smv.False;
                        }
                        if (String.Compare(val, "true", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            val = Smv.True;
                        }

                        if (variable.SmvType.GetType() != typeof(Smv.DataTypes.BoolSmvType) && settings.ModularArithmetics)
                        {
                            //string rangeStr = variable.SmvType.Split()
                            Smv.DataTypes.RangeSmvType varType = (Smv.DataTypes.RangeSmvType)variable.SmvType;
                            rule += " : (" + _modulo_range(val, varType.RangeBegin, varType.RangeEnd) + ");\n";;
                        }
                        else
                        {
                            rule += " : (" + val + ");\n";
                        }
                        rules += rule;
                    }
                }
                return(rules);
            }
Esempio n. 7
0
            public static string SetOutputVarBuffers(IEnumerable <Variable> variables, IEnumerable <Event> events, IEnumerable <ECAction> actions, IEnumerable <WithConnection> withConnections, ShowMessageDelegate showMessage)
            {
                string outVarsChangeString = "";

                foreach (Variable variable in variables.Where(v => v.Direction == Direction.Output))
                {
                    string rule = Smv.OsmStateVar + "=" + Smv.Osm.S2 + " & " + Smv.AlgStepsCounterVar + "=0" +
                                  " & ({0}) : {1};\n";
                    string outCond = "";

                    List <ECAction> ac = new List <ECAction>();
                    foreach (WithConnection connection in withConnections.Where(conn => conn.Var == variable.Name))
                    {
                        var outputEventsActions = actions.Where(act => act.Output == connection.Event);
                        if (outputEventsActions.Count() == 0)
                        {
                            showMessage(String.Format("Warning! Event {0} associated with output {1} never fires.", connection.Event, variable.Name));
                        }
                        foreach (ECAction action in outputEventsActions)
                        {
                            if (!ac.Contains(action))
                            {
                                ac.Add(action);
                            }
                        }
                    }

                    foreach (ECAction action in ac)
                    {
                        outCond += "(" + Smv.EccStateVar + "=" + Smv.EccState(action.ECState) + " & " +
                                   Smv.EcActionsCounterVar + "=" + action.Number + ") | ";
                    }

                    if (outCond != "")
                    {
                        rule = String.Format(rule, outCond.TrimEnd(Smv.OrTrimChars), variable.Name);
                        outVarsChangeString += String.Format(Smv.NextCaseBlock, Smv.ModuleParameters.Variable(variable.Name), rule);
                    }
                    else
                    {
                        showMessage(String.Format("Warning! No active events associated with output {0}.", variable.Name));
                    }
                }
                return(outVarsChangeString);
            }
Esempio n. 8
0
            public static string OutputEventsSettingRules(IEnumerable <Event> events, IEnumerable <ECAction> actions, bool useProcesses)
            {
                string eventChangeString = "";

                foreach (Event ev in events.Where(ev => ev.Direction == Direction.Output))
                {
                    string       rule           = "\t" + Smv.OsmStateVar + "=" + Smv.Osm.S2 + " & " + Smv.AlgStepsCounterVar + "=0" + " & ({0})";
                    const string setValue       = " : {0};\n";
                    string       outCond        = "";
                    bool         eventSignalSet = false;
                    foreach (ECAction action in actions.Where(act => act.Output == ev.Name))
                    {
                        outCond       += "(" + Smv.EccStateVar + "=" + Smv.EccState(action.ECState) + " & " + Smv.EcActionsCounterVar + "=" + action.Number + ") | ";
                        eventSignalSet = true;
                    }
                    if (eventSignalSet)
                    {
                        rule = String.Format(rule, outCond.TrimEnd(Smv.OrTrimChars));
                        if (useProcesses)
                        {
                            eventChangeString += String.Format(Smv.NextCaseBlock, Smv.ModuleParameters.Event(ev.Name), rule + String.Format(setValue, Smv.True));
                        }
                        else
                        {
                            eventChangeString += String.Format(Smv.DefineBlock, Smv.ModuleParameters.Event(ev.Name) + "_set", rule);
                        }
                    }
                    else
                    {
                        if (useProcesses)
                        {
                            eventChangeString += String.Format(Smv.NextCaseBlock, Smv.ModuleParameters.Event(ev.Name), "");
                        }
                        else
                        {
                            eventChangeString += String.Format(Smv.DefineBlock, Smv.ModuleParameters.Event(ev.Name) + "_set", Smv.False);
                        }
                    }
                }
                return(eventChangeString);
            }
Esempio n. 9
0
            public static string EcStateChangeBlock(IEnumerable <ECTransition> transitions, IEnumerable <Event> events)
            {
                string ecTransitionsSmv = "";

                foreach (var transition in transitions)
                {
                    ecTransitionsSmv += "\t";
                    ecTransitionsSmv += Smv.EccStateVar + "=" + Smv.EccState(transition.Source);
                    ecTransitionsSmv += " & ";
                    ecTransitionsSmv += Smv.OsmStateVar + "=" + Smv.Osm.S1;

                    if (transition.Condition != null && transition.Condition != "1")
                    {
                        ecTransitionsSmv += " & " + _translateEventNames(Smv.ClearConditionExpr(transition.Condition), events);
                    }
                    ecTransitionsSmv += " : ";
                    ecTransitionsSmv += Smv.EccState(transition.Destination);
                    ecTransitionsSmv += ";\n";
                }
                return(String.Format("\n" + Smv.NextCaseBlock + "\n", Smv.EccStateVar, ecTransitionsSmv));
            }
Esempio n. 10
0
            public string TranslateBasicFB(FBType fbType, bool eventSignalResetSolve = true, bool showUnconditionalTransitions = false)
            {
                string smvModule = "";

                ExecutionModel executionModel  = _executionModels.FirstOrDefault(em => em.FBTypeName == fbType.Name);
                var            events          = _storage.Events.Where(ev => ev.FBType == fbType.Name);
                var            variables       = _storage.Variables.Where(ev => ev.FBType == fbType.Name);
                var            states          = _storage.EcStates.Where(ev => ev.FBType == fbType.Name);
                var            algorithms      = _storage.Algorithms.Where(alg => alg.FBType == fbType.Name && alg.Language == AlgorithmLanguages.ST);
                var            smvAlgs         = _translateAlgorithms(algorithms);
                var            actions         = _storage.EcActions.Where(act => act.FBType == fbType.Name);
                var            withConnections = _storage.WithConnections.Where(conn => conn.FBType == fbType.Name);
                var            transitions     = _storage.EcTransitions.Where(tr => tr.FBType == fbType.Name);

                smvModule += BasicFbSmv.ModuleHeader(events, variables, fbType.Name);

                smvModule += BasicFbSmv.OsmStatesDeclaration();
                smvModule += BasicFbSmv.EccStatesDeclaration(states) + "\n";
                smvModule += BasicFbSmv.EcActionsCounterDeclaration(states);
                smvModule += BasicFbSmv.AlgStepsCounterDeclaration(smvAlgs);

                smvModule += Smv.Assign;
                smvModule += String.Format(Smv.VarInitializationBlock, Smv.EccStateVar, Smv.EccState(states.First(s => true).Name));
                smvModule += String.Format(Smv.VarInitializationBlock, Smv.OsmStateVar, Smv.Osm.S0);
                smvModule += BasicFbSmv.ModuleVariablesInitBlock(variables) + "\n";
                smvModule += String.Format(Smv.VarInitializationBlock, Smv.EcActionsCounterVar, "0");
                smvModule += String.Format(Smv.VarInitializationBlock, Smv.AlgStepsCounterVar, "0");

                smvModule += BasicFbSmv.EcStateChangeBlock(transitions, events);
                smvModule += Smv.OsmStateChangeBlock + "\n";
                smvModule += BasicFbSmv.EcActionsCounterChangeBlock(states) + "\n";
                smvModule += BasicFbSmv.AlgStepsCounterChangeBlock(states, actions, smvAlgs) + "\n";

                smvModule += BasicFbSmv.InputVariablesSampleBasic(variables, withConnections) + "\n";
                smvModule += BasicFbSmv.OutputVariablesChangingRules(variables, actions, _storage.AlgorithmLines.Where(line => line.FBType == fbType.Name), _settings) + "\n";
                smvModule += BasicFbSmv.SetOutputVarBuffers(variables, events, actions, withConnections, _showMessage) + "\n";
                smvModule += BasicFbSmv.SetServiceSignals(_settings.UseProcesses) + "\n";

                smvModule += BasicFbSmv.EventInputsResetRules(events, executionModel, eventSignalResetSolve, _settings.UseProcesses) + "\n";
                smvModule += BasicFbSmv.OutputEventsSettingRules(events, actions, _settings.UseProcesses) + "\n";

                smvModule += BasicFbSmv.BasicModuleDefines(states, events, transitions, showUnconditionalTransitions) + "\n";

                smvModule += FbSmvCommon.ModuleFooter(_settings) + "\n";
                return(smvModule);
            }