private static void extractVarFromBasicGuards(KPsystem kpSystem, MType kpType, Module module, BasicGuard basicGuard) { Multiset ms = basicGuard.Multiset; NuSMV.RelationalOperator oper = SMVUtil.getRelationalOperator(basicGuard.Operator); Variable variable = null; foreach (var varName in ms.Objects) { if (!module.isVariableExist(varName)) { variable = new Variable(varName); int upperBound = ms[varName]; //if the guard requires a number greater, or greaterEqual, then it upperbound should be at least one number greater then, //the condition if (oper.Operator == NuSMV.RelationalOperator.GEQ || oper.Operator == NuSMV.RelationalOperator.GT) { upperBound = upperBound + 1; } variable.Type = new BoundInt(0, upperBound); setBoundIntType(kpSystem, kpType, module, variable); variable.Behaviour = VariableBehaviour.REWRITING; variable.Init = setOrUpdateInit(module, variable); module.Variables.Add(variable); } } }
private static void buildReWritingVariables(KPsystem kpSystem, KpCore.MType kpType, NuSMV.Module module, int strategyIndex, KpCore.Rule kpRule) { RewritingRule rwr = (RewritingRule)kpRule; string varName = ""; VariableOrigin origin = VariableOrigin.Original; bool isLeft = true; foreach (var leftHRule in rwr.Lhs) { varName = leftHRule.Key; origin = VariableOrigin.Original; isLeft = true; buildReWritingVariable(kpSystem, kpType, module, strategyIndex, kpRule, varName, origin, isLeft); } foreach (var rigthHRule in rwr.Rhs) { varName = rigthHRule.Key; origin = VariableOrigin.Original; isLeft = false; //first generate original one, then its copy if (!module.isVariableExist(varName)) { buildReWritingVariable(kpSystem, kpType, module, strategyIndex, kpRule, varName, origin, isLeft); } string copyVarName = varName + SMVPreFix.COPY; origin = VariableOrigin.Copy; buildReWritingVariable(kpSystem, kpType, module, strategyIndex, kpRule, copyVarName, origin, isLeft); } }
private static void assignStatusAndSyncToAVariable(Module module, IVar variable) { if (variable is Variable) { CaseLine caseLine = new CaseLine(); Variable var = (Variable)variable; String exchangeState = SynchStates.EXCHANGE; //For Copy variables inside module if (var.Origin == VariableOrigin.Copy || var.Origin == VariableOrigin.CopyOfCommVar) { Expression result = new Expression("0"); caseLine.Rule.Condition = getStatusAndSynchCondition(module, exchangeState); caseLine.Result = result; var.Next.CaseStatement.CaseLines.Add(caseLine); } //For Original variables inside module else if (var.Origin == VariableOrigin.Original && var.Behaviour != VariableBehaviour.COMMUNICATION) { //if it has copy var, then add var + var_cp if (module.isVariableExist(SMVPreFix.getCopyName(var))) { OperExp result = new OperExp(); result.Exp = variable.Name; result.Oper.Value = MathOper.ADD; //if variable is a division variable, then it has go to main part, like comm vars, so it will be instanced. if (var.Behaviour == VariableBehaviour.DIVISION) { result.Result = new InstancedExp(variable.Name + SMVPreFix.COPY); } else { result.Result = new Expression(variable.Name + SMVPreFix.COPY); } ICondition boundCondition = getBoundCondition(var, result); caseLine.Rule.Condition = new CompoundBoolExpression(getStatusAndSynchCondition(module, exchangeState), BinaryOperator.AND, boundCondition); caseLine.Result = result; var.Next.CaseStatement.CaseLines.Add(caseLine); } } //for communication variables, goes into main module. else if (var.Origin == VariableOrigin.OriginalCommVar && var.Behaviour == VariableBehaviour.COMMUNICATION) { CaseLine commCaseLine = assignStatusAndSyncToACommVariable(module, var); var.Next.CaseStatement.CaseLines.Add(commCaseLine); } } }
private static void buildReWritingVariable(KPsystem kpSystem, KpCore.MType kpType, NuSMV.Module module, int strategyIndex, KpCore.Rule kpRule, string newVarName, VariableOrigin origin, bool isLeft) { Variable newVar = null; //if variable does not exist then create it, if (!module.isVariableExist(newVarName)) { if (origin == VariableOrigin.Original) { newVar = new Variable(newVarName); setBoundIntType(kpSystem, kpType, module, newVar); newVar.Behaviour = VariableBehaviour.REWRITING; newVar.Init = setOrUpdateInit(module, newVar); newVar.Origin = VariableOrigin.Original; if (isLeft) { //if it is on left then add it, but do not add rules to first not-copy variable on right. BRulesStandardVar.addCaseLineToStandardVariable(newVar, kpRule, module, strategyIndex); } } else if (origin == VariableOrigin.Copy) { newVar = new Variable(newVarName); Variable orginalVariable = (Variable)module.getVariable(newVarName.Replace(SMVPreFix.COPY, "")); newVar.Type = orginalVariable.Type; newVar.Behaviour = VariableBehaviour.REWRITING; newVar.Init = "0"; newVar.Origin = VariableOrigin.Copy; //add result of rule to caseline BRulesStandardVar.addCaseLineToStandardVariable(newVar, kpRule, module, strategyIndex); } if (newVar != null) { module.Variables.Add(newVar); } else { throw new Exception("Cannot create variable : " + newVarName); } } else { //bring variable to add new rules. newVar = (Variable)module.Variables.First(item => item.Name.Equals(newVarName)); BRulesStandardVar.addCaseLineToStandardVariable(newVar, kpRule, module, strategyIndex); } }
private static CaseLine assignStatusAndSyncToACommVariable(Module module, Variable variable) { CaseLine commCaseLine = new CaseLine(); OperExp result = new OperExp(); result.Exp = new InstancedExp(module.Instance.Name, variable.Name).ToString(); result.Oper.Value = MathOper.ADD; //if it has copy var, then add var + var_cp string res = ""; if (module.isVariableExist(SMVPreFix.getCopyName(variable))) { res += new InstancedExp(module.Instance.Name, SMVPreFix.getCopyName(variable)); } foreach (var connectedInstance in module.Instance.ConnectedTo) { if (connectedInstance.Module.isVariableExist(SMVPreFix.getConnectedCopyCommVarName(variable, module))) { if (!string.IsNullOrWhiteSpace(res)) { res += " + "; } res += new InstancedExp(connectedInstance.Name, SMVPreFix.getConnectedCopyCommVarName(variable, module)); } } result.Result = new Expression(res); //(c1.status = _ACTIVE) CompoundBoolExpression statusAndSync = getInstancedStatusAndSyncCondition(module); ICondition boundCondition = getBoundCondition(variable, result); ICondition compound = new CompoundBoolExpression(statusAndSync, BinaryOperator.AND, boundCondition); CommunicationRule rule = new CommunicationRule(); rule.Condition = compound; //commCaseLine.Rule.Condition = compound; commCaseLine.Rule = rule; commCaseLine.Result = result; return(commCaseLine); }
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); } } } } }