internal static string WriteStatusNextWithInstance(NuSMV.Module module, Instance instance) { string op = ""; Variable status = module.Status; op = "next (" + instance.Name + "." + module.Status.Name + ") := case\n"; foreach (var caseLine in status.Next.CaseStatement.CaseLines) { // CaseLine newcaseLine = new CaseLine(); if (!(caseLine.Rule is CommunicationRule)) { string temp = ""; temp += TVariables.insertInstanceName(caseLine.Rule.Condition, instance); temp += " : "; // for last true case if (caseLine.Rule.Condition is TruthValue) { temp += instance.Name + "." + caseLine.Result; } else { temp += caseLine.Result; } temp += ";\n"; op += temp; } } op += "esac;\n"; return(op); }
internal static string WriteParentDivisionVariableNext(NuSMV.Module module, ParentInstance parentInstance) { string op = ""; foreach (var variable in module.Variables) { if (variable.Behaviour == VariableBehaviour.DIVISION) { op += "next (" + parentInstance.Name + "." + variable.Name + ") := case\n"; foreach (var caseLine in (variable as Variable).Next.CaseStatement.CaseLines) { string temp = "\t"; temp += TVariables.insertInstanceName(caseLine.Rule.Condition, parentInstance); temp += " : "; temp += insertInstanceName2Expression(caseLine.Result, parentInstance); //if (caseLine.Result is InstancedExp) // temp += parentInstance.Name + "." + caseLine.Result; //else if (caseLine.Result is Expression) // temp += caseLine.Result; temp += ";\n"; op += temp; } op += "esac;\n"; } } return(op); }
internal static string CommunicationVariableNext(NuSMV.Module module, Instance instance, IVar var) { if (var is Variable) { string op = "case\n"; Variable variable = (Variable)var; Next next = variable.Next; foreach (var caseLine in next.CaseStatement.CaseLines) { if ((caseLine.Rule is CommunicationRule)) { op += caseLine.ToString().Replace(SMVKeys.SELF, instance.Name); } } foreach (var caseLine in next.CaseStatement.CaseLines) { // CaseLine newcaseLine = new CaseLine(); if (!(caseLine.Rule is CommunicationRule)) { string temp = ""; temp += insertInstanceName(caseLine.Rule.Condition, instance); temp += " : "; if (caseLine.Result is InstancedExp) { if (!caseLine.Result.ToString().Contains(instance.Name)) { temp += instance.Name + "." + caseLine.Result; } else { temp += caseLine.Result; } } else if (caseLine.Result is Expression) { temp += caseLine.Result; } temp += ";\n"; op += temp; } } op += "esac;\n"; return("next (" + instance.Name + "." + var.Name + ") := " + op); } else { return("Couldn't generate next for " + var.Name + " at " + MethodBase.GetCurrentMethod().Name + ";\n");; } }
private static void buildDivisionVariables(NuSMV.Module module, KPsystem kpSystem, KpCore.MType type, int strategyIndex, KpCore.Rule rule) { DivisionRule divisionRule = (DivisionRule)rule; foreach (var leftHRule in divisionRule.Lhs) { Variable variable = new Variable(leftHRule.Key); if (!module.Variables.Contains(variable)) { variable.Type = new BoundInt(0, setMax(kpSystem, type, module, variable)); variable.Behaviour = VariableBehaviour.REWRITING; variable.Init = setOrUpdateInit(module, variable); module.Variables.Add(variable); } else { //if variable exists then update the upperbound value. variable = (Variable)module.Variables.First(item => item.Name.Equals(leftHRule.Key)); } //add result of rule to caseline BRulesStandardVar.addCaseLineToStandardVariable(variable, rule, module, strategyIndex); } foreach (InstanceBlueprint compartment in divisionRule.Rhs) { MType compType = compartment.Type; if (type.Name.Equals(compType.Name)) { Multiset ms = compartment.Multiset; foreach (var obj in ms.Objects) { Variable variable = new Variable(obj); if (!module.Variables.Contains(variable)) { variable.Type = new BoundInt(0, setMax(kpSystem, compType, module, variable)); variable.Behaviour = VariableBehaviour.DIVISION; variable.Init = setOrUpdateInit(module, variable); module.Variables.Add(variable); } else { variable = (Variable)module.Variables.First(item => item.Name.Equals(obj)); variable.Behaviour = VariableBehaviour.DIVISION; } } } } // add rule to status variable BRulesCustomVar.addRuleToStatusVariable(rule, module, strategyIndex); }
/// <summary> /// Print status rules of each child instance /// </summary> internal static string WriteChildStatusNext(NuSMV.Module module, ChildInstance childInstance) { string op = ""; Variable status = childInstance.DivisionStatus; op = "next (" + childInstance.Name + "." + status.Name + ") := case\n"; bool firstRule = true;// first rule related with parent, so, its rule shouldn't be named with child instances foreach (var caseLine in status.Next.CaseStatement.CaseLines) { string temp = ""; if (!firstRule) { // add child name as prefix temp += TVariables.insertInstanceName(caseLine.Rule.Condition, childInstance); } else { temp += caseLine.Rule.Condition; firstRule = false; } temp += " : "; // for last true case if (caseLine.Rule.Condition is TruthValue) { temp += childInstance.Name + "." + caseLine.Result; } else { temp += caseLine.Result; } temp += ";\n"; op += temp; } op += "esac;\n"; return(op); }
/// <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; } }
internal static void addRuleToStatusVariable(KpCore.Rule rule, NuSMV.Module module, int strategyIndex) { // ((main._status = _willDIVIDE) & (_sync = _BUSY)) : _willDIVIDE; ((main._status = _willDIVIDE) & (_sync = // _EXCH)) : _DIVIDED; if (module.HasDivisionRule) { CaseLine willDivideCaseLine = new CaseLine(); //((main._status = _willDIVIDE) & (_sync = _BUSY)) : _willDIVIDE; ICondition willDivide = BRulesStandardVar.getStatusCondition(module, StatusStates.WILLDIVIDE); ICondition syncBusy = new BoolExp(CustomVariables.SYNCH, NuSMV.RelationalOperator.EQUAL, SynchStates.BUSY); willDivideCaseLine.Rule.Condition = new CompoundBoolExpression(willDivide, BinaryOperator.AND, syncBusy); willDivideCaseLine.Result = new Expression(StatusStates.WILLDIVIDE); if (!BRulesComVar.ruleExist(module.Status.Next, willDivideCaseLine)) { module.Status.Next.addCaseLine(willDivideCaseLine); } // ((main._status = _willDIVIDE) & (_sync = _EXCH)) : _DIVIDED; CaseLine dividedCaseLine = new CaseLine(); ICondition syncExch = new BoolExp(CustomVariables.SYNCH, NuSMV.RelationalOperator.EQUAL, SynchStates.EXCHANGE); dividedCaseLine.Rule.Condition = new CompoundBoolExpression(willDivide, BinaryOperator.AND, syncExch); dividedCaseLine.Result = new Expression(StatusStates.DIVIDED); if (!BRulesComVar.ruleExist(module.Status.Next, dividedCaseLine)) { module.Status.Next.addCaseLine(dividedCaseLine); } } // ((main._status = _willDISSOLVE) & (_sync = _BUSY)) : _willDISSOLVE; ((main._status = _willDISSOLVE) & (_sync = // _EXCH)) : _DISSOLVED; if (module.HasDissolutionRule) { CaseLine caseLine2 = new CaseLine(); //status = willDissolve & (_sync = _BUSY) : willDissolve; ICondition willSolve = BRulesStandardVar.getStatusCondition(module, StatusStates.WILLDISSOLVE); ICondition syncBusy = new BoolExp(CustomVariables.SYNCH, NuSMV.RelationalOperator.EQUAL, SynchStates.BUSY); caseLine2.Rule.Condition = new CompoundBoolExpression(willSolve, BinaryOperator.AND, syncBusy); caseLine2.Result = new Expression(StatusStates.WILLDISSOLVE); if (!BRulesComVar.ruleExist(module.Status.Next, caseLine2)) { module.Status.Next.addCaseLine(caseLine2); } //((c1._status = _willDISSOLVE) & (_sync = _EXCH)) : _DISSOLVED; CaseLine caseLine3 = new CaseLine(); ICondition syncExch = new BoolExp(CustomVariables.SYNCH, NuSMV.RelationalOperator.EQUAL, SynchStates.EXCHANGE); caseLine3.Rule.Condition = new CompoundBoolExpression(willSolve, BinaryOperator.AND, syncExch); caseLine3.Result = new Expression(StatusStates.DISSOLVED); if (!BRulesComVar.ruleExist(module.Status.Next, caseLine3)) { module.Status.Next.addCaseLine(caseLine3); } } CaseLine caseLine = new CaseLine(); caseLine.Result = getResultOfStatusRuleFromKPRule(module, rule); caseLine.Rule = BRulesStandardVar.extractStandardRuleFromKPRule(rule, module, strategyIndex); caseLine.Rule.ID = rule.Id; //if module has division rule then, dissolve process happens inside instances. // ICondition statusCondition = BRulesStandardVar.getStatusCondition(module); No need status=Active condition anymore ICondition sequence = BRulesStandardVar.getSequenceCondition(module, strategyIndex, rule.Id); //CompoundBoolExpression compound = new CompoundBoolExpression(statusCondition, BinaryOperator.AND, sequence); caseLine.Rule.AddBoolExpression(sequence, BinaryOperator.AND); //if the case line has not added yet if (!BRulesComVar.ruleExist(module.Status.Next, caseLine)) { module.Status.Next.addCaseLine(caseLine); } }
internal static void addCaseLineToCurrentCopyCommVar(Variable orjVariable, Variable copyCommVar, KpCore.Rule rule, Module module, Module targetModule, int strategyIndex) { //for each variable generate a case line CaseLine caseLine = new CaseLine(); OperExp result = new OperExp(); int resultValue = 0; RewriteCommunicationRule rcr = (RewriteCommunicationRule)rule; foreach (var target in rcr.TargetRhs.Values) { TargetedMultiset targetMultiSet = (TargetedMultiset)target; InstanceIdentifier targetType = (InstanceIdentifier)targetMultiSet.Target; if (targetModule.Type == targetType.Value) { Multiset ms = targetMultiSet.Multiset; foreach (var obj in ms.Objects) { if (obj.Equals(orjVariable.Name)) { resultValue += ms[obj]; } } } } result.Exp = copyCommVar.Name; if (resultValue != 0) { result.Oper.Value = MathOper.ADD; result.Result = new Expression(resultValue.ToString()); } caseLine.Result = result; caseLine.Rule = BRulesStandardVar.extractStandardRuleFromKPRule(rule, module, strategyIndex); ICondition sequenceCondition = BRulesStandardVar.getSequenceCondition(module, strategyIndex, rule.Id); ICondition targetBounds = getBoundCondition(copyCommVar, caseLine.Result); CompoundBoolExpression sequenceAndBound = new CompoundBoolExpression(sequenceCondition, BinaryOperator.AND, targetBounds); ICondition statusCondition = BRulesStandardVar.getTurnCondition(module, strategyIndex); CompoundBoolExpression statusAndSequence = new CompoundBoolExpression(statusCondition, BinaryOperator.AND, sequenceAndBound); // _conn = to_c2 if (module.connectionToModuleExist(targetModule)) { BoolExp connEqInstance = new BoolExp(module.getConnectionToModule(targetModule).Name, NuSMV.RelationalOperator.EQUAL, SMVPreFix.getConnectedTo(targetModule.Instance)); caseLine.Rule.AddBoolExpression(connEqInstance, BinaryOperator.AND); } caseLine.Rule.AddBoolExpression(statusAndSequence, BinaryOperator.AND); caseLine.Rule.ID = rule.Id; if (copyCommVar.Next != null) { //if the case line has not added yet if (!ruleExist(copyCommVar.Next, caseLine)) { copyCommVar.Next.addCaseLine(caseLine); } } }
/// <summary> /// if variable has NOT been set by XML file then set max automatically /// </summary> /// <param name="kpSystem"></param> /// <param name="myType"></param> /// <param name="module"></param> /// <param name="variable"></param> /// <returns></returns> private static int setMax(KPsystem kpSystem, MType myType, NuSMV.Module module, IVar variable) { int maxVal = 0; foreach (var type in kpSystem.Types) { ExecutionStrategy eS = type.ExecutionStrategy; while (eS != null) { //Inside this type if (myType.Name.Equals(type.Name)) { foreach (var rule in eS.Rules) { //Normal variable if (rule.Type == RuleType.MULTISET_REWRITING) { RewritingRule rwr = (RewritingRule)rule; foreach (var leftHRule in rwr.Lhs) { if (variable.Name.Equals(leftHRule.Key)) { maxVal = Math.Max(maxVal, leftHRule.Value); } } foreach (var rigthHRule in rwr.Rhs) { if (variable.Name.Equals(rigthHRule.Key)) { maxVal = Math.Max(maxVal, rigthHRule.Value); } } } // Communication Var else if (rule.Type == RuleType.REWRITE_COMMUNICATION) { RewriteCommunicationRule rcr = (RewriteCommunicationRule)rule; foreach (var leftHRule in rcr.Lhs) { if (variable.Name.Equals(leftHRule.Key)) { maxVal = Math.Max(maxVal, leftHRule.Value); } } } else if (rule.Type == RuleType.MEMBRANE_DIVISION) { DivisionRule divisionRule = (DivisionRule)rule; foreach (var leftHRule in divisionRule.Lhs) { if (variable.Name.Equals(leftHRule.Key)) { maxVal = Math.Max(maxVal, leftHRule.Value); } } foreach (InstanceBlueprint compartment in divisionRule.Rhs) { MType compType = compartment.Type; if (myType.Name.Equals(compType.Name)) { Multiset ms = compartment.Multiset; foreach (var obj in ms.Objects) { if (variable.Name.Equals(obj)) { maxVal = Math.Max(maxVal, ms[obj]); } } } } } else if (rule.Type == RuleType.MEMBRANE_DISSOLUTION) { DissolutionRule dissolutionRule = (DissolutionRule)rule; foreach (var leftHRule in dissolutionRule.Lhs) { if (variable.Name.Equals(leftHRule.Key)) { maxVal = Math.Max(maxVal, leftHRule.Value); } } } } } //Inside communication rule of other types else { foreach (var rule in eS.Rules) { if (rule.Type == RuleType.REWRITE_COMMUNICATION) { RewriteCommunicationRule rcr = (RewriteCommunicationRule)rule; foreach (var target in rcr.TargetRhs.Values) { TargetedMultiset tg = (TargetedMultiset)target; InstanceIdentifier identifier = (InstanceIdentifier)tg.Target; if (myType.Name.Equals(identifier.Value)) { Multiset ms = tg.Multiset; foreach (var rightVar in ms.Objects) { if (variable.Name.Equals(rightVar)) { maxVal = Math.Max(maxVal, ms[rightVar]); } } } } } else if (rule.Type == RuleType.MEMBRANE_DIVISION) { DivisionRule divisionRule = (DivisionRule)rule; foreach (InstanceBlueprint compartment in divisionRule.Rhs) { MType compType = compartment.Type; if (myType.Name.Equals(compType.Name)) { Multiset ms = compartment.Multiset; foreach (var obj in ms.Objects) { if (variable.Name.Equals(obj)) { maxVal = Math.Max(maxVal, ms[obj]); } } } } } } } eS = eS.Next; } //also compare parameter values foreach (var instance in type.Instances) { foreach (var param in instance.Multiset.Objects) { if (variable.Name.Equals(param)) { maxVal = Math.Max(maxVal, instance.Multiset[param]); } } } } return(maxVal); }
private static void buildDissolutionVariables(KPsystem kpSystem, KpCore.MType type, NuSMV.Module module, int strategyIndex, KpCore.Rule rule) { //Preserve variable value and update status value. DissolutionRule dissolutionRule = (DissolutionRule)rule; foreach (var leftHRule in dissolutionRule.Lhs) { Variable variable = new Variable(leftHRule.Key); if (!module.Variables.Contains(variable)) { variable.Type = new BoundInt(0, setMax(kpSystem, type, module, variable)); variable.Behaviour = VariableBehaviour.REWRITING; variable.Init = setOrUpdateInit(module, variable); module.Variables.Add(variable); } else { //if variable exists then update the upperbound value. variable = (Variable)module.Variables.First(item => item.Name.Equals(leftHRule.Key)); } //add result of rule to caseline BRulesStandardVar.addCaseLineToStandardVariable(variable, rule, module, strategyIndex); } // add rule to status variable BRulesCustomVar.addRuleToStatusVariable(rule, module, strategyIndex); }
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); } } } } }
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); } }
public static void buildVariables(KPsystem kpSystem, SMVModel nuSMV, NuSMV.Module module, KpCore.MType kpType) { // variables comes from KP model. buildStandardVariables(nuSMV, module, kpSystem, kpType); }
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); } }