private static IExp resultOfConsumerRule(KpCore.Rule kpRule, Variable variable) { OperExp result = new OperExp(); int value = 0;//value of the variable ConsumerRule cr = null; if (kpRule.Type == RuleType.MEMBRANE_DISSOLUTION) { cr = (DissolutionRule)kpRule; } else if (kpRule.Type == RuleType.MEMBRANE_DIVISION) { cr = (DivisionRule)kpRule; } result.Oper.Value = MathOper.SUB; foreach (var leftHRule in cr.Lhs) { if (leftHRule.Key.Equals(variable.Name)) { value += leftHRule.Value; } } result.Exp = variable.Name; result.Result = new Expression(value.ToString()); return(result); }
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); } } }
private static string insertInstanceName2Expression(IExp expression, Instance instance) { string temp = ""; //_c1.e + self.e_cp if (expression is OperExp) { OperExp operExp = (OperExp)expression; // c1.e temp += instance.Name + "." + operExp.Exp; // if right of operation requires instance name //c1.e + c1.e_cp if (operExp.Result is InstancedExp) { temp += " " + operExp.Oper.Value + " " + instance.Name + "." + operExp.Result; } else { temp += " " + operExp.Oper.Value + " " + operExp.Result; } } else if (expression is InstancedExp) { temp += instance.Name + "." + expression; } else if (expression is Expression) { temp += expression; } return(temp); }
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 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 IExp resultOfReWritingRule(KpCore.Rule kpRule, Variable variable) { OperExp result = new OperExp(); int value = 0;//value of the variable RewritingRule rwr = null; if (kpRule.Type == RuleType.MULTISET_REWRITING) { rwr = (RewritingRule)kpRule; } else if (kpRule.Type == RuleType.REWRITE_COMMUNICATION) { rwr = (RewriteCommunicationRule)kpRule; } string orijinalVarName = variable.Name.Replace(SMVPreFix.COPY, ""); if (variable.Origin == VariableOrigin.Original || variable.Origin == VariableOrigin.OriginalCommVar) { result.Oper.Value = MathOper.SUB; foreach (var leftHRule in rwr.Lhs) { if (leftHRule.Key.Equals(orijinalVarName)) { value += leftHRule.Value; } } } else if (variable.Origin == VariableOrigin.Copy || variable.Origin == VariableOrigin.CopyOfCommVar) { result.Oper.Value = MathOper.ADD; foreach (var rigthHRule in rwr.Rhs) { if (rigthHRule.Key.Equals(orijinalVarName)) { value += rigthHRule.Value; } } } result.Exp = variable.Name; result.Result = new Expression(value.ToString()); return(result); }
public static ICondition insertInstanceName(ICondition left, Instance instance) { // ICondition condition = null; if (left is BoolExp) { BoolExp condition = new BoolExp(); BoolExp boolExp = (BoolExp)left; if (boolExp.Left.Exp.Contains(SMVKeys.SELF)) { if (boolExp.Left is OperExp) { OperExp tempOper = new OperExp(); tempOper.Exp = (boolExp.Left as OperExp).Exp; tempOper.Oper.Value = (boolExp.Left as OperExp).Oper.Value; tempOper.Result = (boolExp.Left as OperExp).Result; condition.Left = new Expression(); condition.Left.Exp = tempOper.ToString().Replace(SMVKeys.SELF, instance.Name); } else { condition.Left = new Expression(); condition.Left.Exp = condition.ToString().Replace(SMVKeys.SELF, instance.Name); } condition.RelationalOperator = boolExp.RelationalOperator; condition.Right = new Expression(); condition.Right.Exp = boolExp.Right.Exp; } else if (!boolExp.Left.Exp.Contains(instance.Name)) { // if it is a shared variable (such as main's synch variable), then don't call with compartment name if (boolExp.Left.Exp == CustomVariables.SYNCH) { // actually do nothing. condition.Left = new Expression(); condition.Left.Exp = boolExp.Left.Exp; } else if (boolExp.Left is OperExp) { condition.Left = new Expression(); condition.Left.Exp = insertInstanceName2Expression(boolExp.Left, instance); } else { condition.Left = new Expression(); condition.Left.Exp = instance.Name + "." + boolExp.Left.Exp; } condition.RelationalOperator = boolExp.RelationalOperator; condition.Right = new Expression(); condition.Right.Exp = boolExp.Right.Exp; } else { condition = boolExp; } return(condition); } else if (left is BoolVariable) { BoolVariable boolGuard = (BoolVariable)left; BoolVariable condition = new BoolVariable(); if (boolGuard.BoolGuard.Exp == SMVKeys.SELF) { (condition as BoolVariable).BoolGuard.Exp = boolGuard.BoolGuard.Exp.Replace(SMVKeys.SELF, instance.Name); } else { (condition as BoolVariable).BoolGuard.Exp = instance.Name + "." + boolGuard.BoolGuard.Exp; } return(condition);; } else if (left is NegatedBoolExpression) { NegatedBoolExpression negated = (NegatedBoolExpression)left; NegatedBoolExpression condition = new NegatedBoolExpression(); BoolExp boolExp = (BoolExp)negated.BoolExpression; if (boolExp.Left.Exp == SMVKeys.SELF) { ((condition as NegatedBoolExpression).BoolExpression as BoolExp).Left.Exp = boolExp.Left.Exp.Replace(SMVKeys.SELF, instance.Name); } else { ((condition as NegatedBoolExpression).BoolExpression as BoolExp).Left.Exp = instance.Name + "." + boolExp.Left.Exp; } return(condition); } else if (left is TruthValue) { return(left); } else { if (left != null) { CompoundBoolExpression comp = (CompoundBoolExpression)left; CompoundBoolExpression compoundBoolExpr = new CompoundBoolExpression(); compoundBoolExpr.LeftCondition = insertInstanceName(comp.LeftCondition, instance); compoundBoolExpr.BinaryOperator = comp.BinaryOperator; compoundBoolExpr.RightCondition = insertInstanceName(comp.RightCondition, instance); // condition = compoundBoolExpr; return(compoundBoolExpr); } else { return(null); } } }