Ejemplo n.º 1
0
        private static void WriteStatusAndCommuncationNext(SMVModel nuSMV)
        {
            string op = "";

            //write instances
            foreach (var module in nuSMV.Modules)
            {
                Instance instance = module.Instance;
                //write parent instances...
                if (instance is ParentInstance)
                {
                    op += TVariables.WriteStatusNextWithInstance(module, instance);
                    op += TVariables.WriteParentDivisionVariableNext(module, instance as ParentInstance);
                }
                else if (instance is ChildInstance)
                {
                    //Print status rules of each child instance
                    op += TVariables.WriteChildStatusNext(module, instance as ChildInstance);
                    op += TVariables.WriteChildDivisionVariablesNext(instance as ChildInstance);
                }

                foreach (var variable in module.Variables)
                {
                    if (variable.Behaviour == VariableBehaviour.COMMUNICATION)
                    {
                        op += TVariables.CommunicationVariableNext(module, instance, variable);
                    }
                }
            }
            Writer.WriteLine(op);
        }
Ejemplo n.º 2
0
        public static void Translate(KpCore.KpModel kpModel, Experiment kpx, string outFileName)
        {
            KPsystem    kpSystem    = kpModel.KPsystem;
            KpMetaModel kpMetaModel = new KpMetaModel(kpSystem);

            try
            {
                //Translate KP model to SMV model
                SMVModel nuSMV = BNuSMV.buildModel(kpSystem);

                //SMVModels are loaded, in first run write variable to XML files, in 2nd run these values can be read.
                foreach (var module in nuSMV.Modules)
                {
                    BVariables.writeBounds2XML(module);
                }

                //Generate SMV file
                if (nuSMV != null)
                {
                    PrintNuSMV(nuSMV, kpMetaModel, kpx, outFileName);
                }
                else
                {
                    throw new Exception("NuSMV translation failed.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred during NuSMV translation.");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }
Ejemplo n.º 3
0
        private static void PrintNuSMV(SMVModel nuSMV, KpMetaModel kpMetaModel, Experiment kpx, string outFileName)
        {
            //instantiate output file, otherwise to console
            Writer.FileName = outFileName;
            Writer.CleanFile(outFileName);
            bool firstModule = true;

            foreach (var module in nuSMV.Modules)
            {
                if (!firstModule)
                {
                    TUtil.AddDashedComment(module.Type.ToUpper());
                }
                WriteModule(module);
                WriteVariables(module);
                WriteINVAR(module);
                WriteInit(module);
                if (!module.HasDivisionRule && module.HasDissolutionRule)
                {
                    WriteStatusNext(module);
                }

                WriteNext(module);

                firstModule = false;
            }
            WriteMain(nuSMV);
            WriteStatusAndCommuncationNext(nuSMV);
            WriteProperties(kpx, kpMetaModel);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get list of all connected instances to @param kpInstance as the list of SMV instances.
        /// </summary>
        /// <param name="nuSMV"></param>
        /// <param name="kPsystem"></param>
        /// <param name="kpInstance"></param>
        /// <returns></returns>
        public static List <Instance> getInstanceConnections(SMVModel nuSMV, KPsystem kPsystem, MInstance kpInstance)
        {
            List <Instance> smvInstances = new List <Instance>();

            foreach (var connected in kpInstance.Connections)
            {
                MType    targetType   = findTargetTypeOfInstance(kPsystem, connected);
                Module   targetModule = nuSMV.getModule(targetType, connected);
                Instance smvInstance  = targetModule.Instance;
                if (smvInstance != null)
                {
                    // if connection is not exist then add it
                    if (!smvInstances.Exists(item => item.Name == smvInstance.Name))
                    {
                        smvInstances.Add(smvInstance);
                    }
                }
                else
                {
                    throw new Exception("Corresponding SMV instance of " + connected.Name + " not found!");
                }
            }

            return(smvInstances);
        }
Ejemplo n.º 5
0
        private static Case buildSynchNext(SMVModel nuSMV)
        {
            Case     newCase  = new Case();
            CaseLine caseLine = new CaseLine();

            foreach (var module in nuSMV.Modules)
            {
                string next  = SMVKeys.NEXT + "(" + module.Instance.Name + "." + CustomVariables.TURN + ")";
                string ready = TurnStates.READY;
                caseLine.Rule.AppendBoolExpression(new BoolExp(next, NuSMV.RelationalOperator.EQUAL, ready), BinaryOperator.AND);
                if (module.HasDivisionRule)
                {
                    foreach (var childIntstance in module.ChildInstances)
                    {
                        next  = SMVKeys.NEXT + "(" + childIntstance.Name + "." + CustomVariables.TURN + ")";
                        ready = TurnStates.READY;
                        caseLine.Rule.AppendBoolExpression(new BoolExp(next, NuSMV.RelationalOperator.EQUAL, ready), BinaryOperator.AND);
                    }
                }
            }
            caseLine.Result = new Expression(SynchStates.EXCHANGE);
            newCase.CaseLines.Add(caseLine);
            newCase.CaseLines.Add(BRulesStandardVar.trueCaseLine(SynchStates.BUSY));
            return(newCase);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// For each MInstance (KP instance) generate a SMV instance.
        /// </summary>
        /// <param name="nuSMV"></param>
        /// <param name="module"></param>
        /// <param name="type"></param>
        /// <param name="kpInstance"></param>
        public static void generateSMVInstances(SMVModel nuSMV, Module module, MType type, MInstance kpInstance)
        {
            Instance smvInstance = null;

            //If module has division rule then being child or parent becomes matter, otherwise they are default SMV instances
            if (module.HasDivisionRule)
            {
                //Define module behaviour based on whether the instance is a parent or a child
                //Status parameter, parents start in ACTIVE; child instances start in NONEXIST state
                ParameterVar statusParam = new ParameterVar();

                //A Child instance
                if (kpInstance is KPChildInstance)
                {
                    // generate SMV Child Instance
                    smvInstance = new ChildInstance();
                    smvInstance.DivisionType = DIVISIONTYPE.CHILD;

                    //Start Status parameter in NONEXISTS state
                    statusParam.Name      = CustomVariables.STATUS;
                    statusParam.Behaviour = VariableBehaviour.CUSTOM;
                    statusParam.Init      = StatusStates.NONEXIST;
                    smvInstance.Parameters.Add(statusParam);
                    //add this instance to its parent children
                    crossReferChild2ParentInstance(nuSMV, type, kpInstance, smvInstance as ChildInstance);
                }
                // A Parent instance
                else
                {
                    // generate SMV Parent Instance
                    smvInstance = new ParentInstance();
                    smvInstance.DivisionType = DIVISIONTYPE.PARENT;

                    //Start Status parameter in Active state
                    statusParam.Name      = CustomVariables.STATUS;
                    statusParam.Behaviour = VariableBehaviour.CUSTOM;
                    statusParam.Init      = StatusStates.ACTIVE;
                    smvInstance.Parameters.Add(statusParam);
                }
                //set parameter to module as well.
                module.Parameters.Add(statusParam);
            }
            //Default instance
            else
            {
                smvInstance = new Instance();
                smvInstance.DivisionType = DIVISIONTYPE.NODIVISION;
            }
            smvInstance.Name = kpInstance.Name;
            //join custom parameters, e.g., status, with model parameters
            smvInstance.Parameters.UnionWith(getInstanceParameters(module, type, kpInstance));
            //cross reference module and its instance
            smvInstance.Module = module;
            module.Instance    = smvInstance;
        }
Ejemplo n.º 7
0
        private static void assignLastCasesToNext(SMVModel nuSMV)
        {
            foreach (var module in nuSMV.Modules)
            {
                BRulesStandardVar.assignLastCasesToNext(module);

                if (module.HasDivisionRule)
                {
                    BRulesStandardVar.assignLastCasesToDivisionVars(module);
                }
            }
        }
Ejemplo n.º 8
0
        private static Case buildPStepNext(SMVModel nuSMV)
        {
            Case     newCase  = new Case();
            CaseLine caseLine = new CaseLine();
            //_sync = _EXCH
            ICondition synchIsExch = new BoolExp(CustomVariables.SYNCH, NuSMV.RelationalOperator.EQUAL, SynchStates.EXCHANGE);

            caseLine.Rule.Condition = synchIsExch;
            caseLine.Result         = new Expression(Truth.TRUE);
            newCase.CaseLines.Add(caseLine);
            newCase.CaseLines.Add(BRulesStandardVar.trueCaseLine(Truth.FALSE));
            return(newCase);
        }
Ejemplo n.º 9
0
        private static void buildMain(SMVModel nuSMV)
        {
            MainModule main = new MainModule();
            //build next case for synch variable
            Case synchNext = buildSynchNext(nuSMV);

            main.Synch.Next.CaseStatement = synchNext;
            //build next case for PStep variable
            Case pStepNext = buildPStepNext(nuSMV);

            main.PStep.Next.CaseStatement = pStepNext;

            nuSMV.MainModule = main;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Cross reference child instance to its parent, and vice versa
        /// </summary>
        /// <param name="type"></param>
        /// <param name="kpInstance"></param>
        private static void crossReferChild2ParentInstance(SMVModel nuSMV, MType type, MInstance kpInstance, ChildInstance childSMVInstance)
        {
            //Access its parent KP instance
            MInstance      parentKPInstance  = (kpInstance as KPChildInstance).ParentKPInstance;
            ParentInstance parentSMVInstance = getSMVInstance(nuSMV, parentKPInstance);

            if (childSMVInstance.ParentInstance == null)
            {
                childSMVInstance.ParentInstance = parentSMVInstance;
            }
            else
            {
                throw new Exception("Error: Cross reference error with child and parent SMV instance...");
            }
            if (!parentSMVInstance.ChildInstances.Contains(childSMVInstance))
            {
                parentSMVInstance.ChildInstances.Add(childSMVInstance);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Return SMV counterpart of given KP instance
        /// </summary>
        /// <param name="nuSMV"></param>
        /// <param name="parentKPInstance"></param>
        /// <returns></returns>
        private static ParentInstance getSMVInstance(SMVModel nuSMV, MInstance parentKPInstance)
        {
            ParentInstance parentSMVInstance = null;

            foreach (Module module in nuSMV.Modules)
            {
                if (module.Instance.Name.Equals(parentKPInstance.Name))
                {
                    if (module.Instance is ParentInstance)
                    {
                        parentSMVInstance = (ParentInstance)module.Instance;
                        break;
                    }
                }
            }
            if (parentSMVInstance == null)
            {
                throw new Exception("Error: KPInstance counterpart not found in any SMV modules!");
            }
            return(parentSMVInstance);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Build variables comes from KP model.
        /// </summary>
        /// <param name="nuSMV"></param>
        /// <param name="module"></param>
        /// <param name="kpType"></param>
        public static void buildStandardVariables(SMVModel nuSMV, NuSMV.Module module, KPsystem kpSystem, KpCore.MType kpType)
        {
            ExecutionStrategy eS = kpType.ExecutionStrategy;
            int strategyIndex    = 0;

            //First get variables from current type
            while (eS != null)
            {
                foreach (var rule in eS.Rules)
                {
                    //check if it is has guards, then create its variables.
                    if (rule.IsGuarded)
                    {
                        buildGuardVariable(kpSystem, kpType, module, strategyIndex, rule);
                    }

                    if (rule.Type == RuleType.MULTISET_REWRITING)
                    {
                        buildReWritingVariables(kpSystem, kpType, module, strategyIndex, rule);
                    }
                    else if (rule.Type == RuleType.REWRITE_COMMUNICATION)
                    {
                        buildCommunicationVariables(nuSMV, module, kpSystem, kpType, strategyIndex, rule);
                    }
                    else if (rule.Type == RuleType.MEMBRANE_DIVISION)
                    {
                        buildDivisionVariables(module, kpSystem, kpType, strategyIndex, rule);
                    }
                    else if (rule.Type == RuleType.MEMBRANE_DISSOLUTION)
                    {
                        buildDissolutionVariables(kpSystem, kpType, module, strategyIndex, rule);
                    }
                }
                strategyIndex++;
                eS = eS.Next;
            }
        }
Ejemplo n.º 13
0
        private static void WriteMain(SMVModel nuSMV)
        {
            TUtil.AddDashedComment("MAIN");
            string op = SMVKeys.MODULE + " " + SMVKeys.MAIN + "\n";

            op += SMVKeys.VAR + "\n";
            //write synch,and PStep vars at first.
            op += TVariables.VariableDefinition(nuSMV.MainModule.Synch);
            op += TVariables.VariableDefinition(nuSMV.MainModule.PStep);

            //write instances
            foreach (var module in nuSMV.Modules)
            {
                //parent instance
                op += TVariables.Instances(module.Instance);
                //if exists, then child instances
                if (module.HasDivisionRule)
                {
                    foreach (var childInstance in module.ChildInstances)
                    {
                        op += TVariables.Instances(childInstance);
                    }
                }
            }
            op += SMVKeys.ASSIGN + "\n";

            //write init of synch and PStep vars
            op += TVariables.InitVariable(nuSMV.MainModule.Synch);
            op += TVariables.InitVariable(nuSMV.MainModule.PStep);

            //write next of synch and PStep vars
            op += TVariables.VariableNext(nuSMV.MainModule.Synch);
            op += TVariables.VariableNext(nuSMV.MainModule.PStep);

            Writer.WriteLine(op);
        }
Ejemplo n.º 14
0
        public static SMVModel buildModel(KPsystem param_kpSystem)
        {
            kpSystem = param_kpSystem;
            if (kpSystem != null)
            {
                //set an id to each rules
                prepareKpSystem(kpSystem);
                nuSMV = new SMVModel();
                try
                {
                    //If there are division rules, generate child instances as kPInstance
                    foreach (var kpType in kpSystem.Types)
                    {
                        if (kpTypeHasDivisionRule(kpType))
                        {
                            List <KPChildInstance> kpChildInstances = BDivisionInstances.generateKPChildInstances(kpType);
                            if (kpChildInstances != null)
                            {
                                kpType.Instances.AddRange(kpChildInstances);
                            }
                        }
                    }

                    //Generate a module for each kPInstance, including child instances(if division rule exist)
                    foreach (var kpType in kpSystem.Types)
                    {
                        foreach (MInstance kpInstance in kpType.Instances)
                        {
                            Module module = new Module();
                            prepareSMVModule(module, kpType, kpInstance);
                            nuSMV.Modules.Add(module);
                        }
                    }
                    foreach (var kpType in kpSystem.Types)
                    {
                        foreach (MInstance kpInstance in kpType.Instances)
                        {
                            buildInstanceConnections(kpType, kpInstance);
                        }
                    }
                    foreach (var kpType in kpSystem.Types)
                    {
                        foreach (MInstance kpInstance in kpType.Instances)
                        {
                            Module module = nuSMV.getModule(kpType, kpInstance);
                            BVariables.generateCustomVariables(kpSystem, kpType, module);
                        }
                    }
                    foreach (var kpType in kpSystem.Types)
                    {
                        foreach (MInstance kpInstance in kpType.Instances)
                        {
                            buildModule(kpType, kpInstance);
                        }
                    }

                    if (nuSMV != null)
                    {
                        foreach (var kpType in kpSystem.Types)
                        {
                            foreach (MInstance kpInstance in kpType.Instances)
                            {
                                Module module = nuSMV.getModule(kpType, kpInstance);
                                if (module.HasDivisionRule)
                                {
                                    BDivisionInstances.generateDivisionRules(kpType, kpInstance, module);
                                }
                            }
                        }
                    }
                    //assign cases for synch variable of main module.
                    buildMain(nuSMV);
                    //finalize model, add caseLast caselines, do modifications, sorting etc.
                    assignLastCasesToNext(nuSMV);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(nuSMV);
        }
Ejemplo n.º 15
0
        private static void buildCommunicationVariables(SMVModel nuSMV, NuSMV.Module module, KPsystem kpSystem, KpCore.MType type, int strategyIndex, KpCore.Rule rule)
        {
            RewriteCommunicationRule rcr = (RewriteCommunicationRule)rule;
            string         varName       = "";
            VariableOrigin origin        = VariableOrigin.Original;
            bool           isLeft        = true;

            //regular left hand-side rules
            foreach (var leftHRule in rcr.Lhs)
            {
                varName = leftHRule.Key;
                isLeft  = true;
                buildReWritingVariable(kpSystem, type, module, strategyIndex, rule, varName, origin, isLeft);
            }
            //regular right hand-side rules
            foreach (var rigthHRule in rcr.Rhs)
            {
                varName = rigthHRule.Key;
                origin  = VariableOrigin.Original;
                isLeft  = false;
                //first generate original one, then its copy
                if (!module.isVariableExist(varName))
                {
                    buildReWritingVariable(kpSystem, type, module, strategyIndex, rule, varName, origin, isLeft);
                }
                string copyVarName = varName + SMVPreFix.COPY;
                origin = VariableOrigin.Copy;
                buildReWritingVariable(kpSystem, type, module, strategyIndex, rule, copyVarName, origin, isLeft);
            }
            //Targeted rules
            foreach (var target in rcr.TargetRhs.Values)
            {
                TargetedMultiset   targetMultiSet       = (TargetedMultiset)target;
                InstanceIdentifier targetTypeIdentifier = (InstanceIdentifier)targetMultiSet.Target;
                MType targetType = null;
                foreach (var tempType in kpSystem.Types)
                {
                    if (tempType.Name == targetTypeIdentifier.Value)
                    {
                        targetType = tempType;
                    }
                }
                //for each connected instance of the target type, create a copy variable foreach object in the multiset
                foreach (var connectedInstance in module.Instance.ConnectedTo)
                {
                    if (connectedInstance.Module.Type == targetType.Name)
                    {
                        Module   targetModule = connectedInstance.Module;
                        Multiset ms           = targetMultiSet.Multiset;
                        foreach (var obj in ms.Objects)
                        {
                            varName = obj;
                            Variable targetVariable = new Variable(varName);

                            string   currentCpVarName = SMVPreFix.getConnectedCopyCommVarName(varName, targetModule);
                            Variable currentCpVar     = new Variable(currentCpVarName);

                            //create original variable inside target module
                            if (!targetModule.isVariableExist(varName))
                            {
                                setBoundIntType(kpSystem, targetType, targetModule, targetVariable);
                                targetVariable.Behaviour = VariableBehaviour.COMMUNICATION;
                                targetVariable.Origin    = VariableOrigin.OriginalCommVar;
                                targetVariable.Init      = setOrUpdateInit(targetModule, targetVariable);
                                targetModule.Variables.Add(targetVariable);
                            }
                            else
                            {
                                //if variable is already in target module, then make sure, it is set as communication var.
                                targetVariable           = (Variable)targetModule.Variables.First(item => item.Name.Equals(varName));
                                targetVariable.Behaviour = VariableBehaviour.COMMUNICATION;
                                targetVariable.Origin    = VariableOrigin.OriginalCommVar;
                                targetVariable.Init      = setOrUpdateInit(targetModule, targetVariable);
                            }
                            //create a varName_InstanceName_TargetModule, variable (as copy) inside current module.
                            if (!module.isVariableExist(currentCpVarName))
                            {
                                Variable orginalVariable = (Variable)targetModule.getVariable(varName);
                                currentCpVar.Type      = orginalVariable.Type;
                                currentCpVar.Behaviour = VariableBehaviour.REWRITING;
                                currentCpVar.Origin    = VariableOrigin.CopyOfCommVar;
                                currentCpVar.Init      = "0";
                                module.Variables.Add(currentCpVar);
                            }
                            else
                            {
                                //if variable exists then update the values.
                                currentCpVar = (Variable)module.Variables.First(item => item.Name.Equals(currentCpVarName));
                            }
                            //add result of rule to caseline
                            BRulesComVar.addCaseLineToCurrentCopyCommVar(targetVariable, currentCpVar, rule, module, targetModule, strategyIndex);
                        }
                    }
                }
            }
        }
Ejemplo n.º 16
0
 public static void buildVariables(KPsystem kpSystem, SMVModel nuSMV, NuSMV.Module module, KpCore.MType kpType)
 {
     // variables comes from KP model.
     buildStandardVariables(nuSMV, module, kpSystem, kpType);
 }