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 void writeTargetedMultiset(TargetedMultiset tm) { owt.Write("{"); owt.Write("\"instanceIdentifier\":"); writeInstanceIdentifier(tm.Target); owt.Write(", \"multiset\":"); writeMultiset(tm.Multiset); owt.Write("}"); }
public object VisitTargetedMultiset(KpLinguaParser.TargetedMultisetContext context) { var targetedMultiset = default(TargetedMultiset); var instanceIdentifier = new InstanceIdentifier(context.typeReference().Accept(this) as string); var multisetAtomContext = context.multisetAtom(); if (multisetAtomContext != null) { targetedMultiset = new TargetedMultiset(instanceIdentifier, multisetAtomContext.Accept(this) as Multiset); } else { targetedMultiset = new TargetedMultiset(instanceIdentifier, context.nonEmptyMultiset().Accept(this) as Multiset); } return(targetedMultiset); }
private TargetedMultiset readTargetedMultiset(XPathNavigator parentNode, TargetedMultiset x = null) { if (parentNode == null) { return(x); } InstanceIdentifier ins = readInstanceIdentifier(parentNode); if (x == null) { x = new TargetedMultiset(ins); } else { x.Target = ins; } readMultiset(parentNode, x.Multiset); return(x); }
/// <summary> /// if current type, has communication rules which sends to target type /// </summary> /// <param name="currentType">current KP type</param> /// <param name="targetType">target KP Type</param> /// <returns></returns> private static bool communicationRuleIncludesTargetType(MType currentType, MType targetType) { bool communicationRuleToTargetExist = false; ExecutionStrategy kpEs = currentType.ExecutionStrategy; while (kpEs != null) { foreach (var rule in kpEs.Rules) { if (rule.Type == RuleType.REWRITE_COMMUNICATION) { RewriteCommunicationRule rcr = (RewriteCommunicationRule)rule; foreach (var target in rcr.TargetRhs.Values) { TargetedMultiset targetMultiSet = (TargetedMultiset)target; InstanceIdentifier targetTypeIdent = (InstanceIdentifier)targetMultiSet.Target; if (targetType.Name == targetTypeIdent.Value) { communicationRuleToTargetExist = true; break; } } } if (communicationRuleToTargetExist) { break; } } if (communicationRuleToTargetExist) { break; } kpEs = kpEs.Next; } return(communicationRuleToTargetExist); }
private Rule readRule(XPathNavigator nav) { ConsumerRule rule = null; XPathNavigator rhsNav = nav.SelectSingleNode("rhs"); if (rhsNav != null) { Multiset m = readMultiset(rhsNav); XPathNodeIterator div; if (m.IsEmpty()) { //then we can have structure changing rules div = rhsNav.SelectChildren("instance", String.Empty); if (div.Count > 0) { DivisionRule r = new DivisionRule(); while (div.MoveNext()) { string mtype = div.Current.GetAttribute("mtype", String.Empty); InstanceBlueprint ib = null; if (String.IsNullOrEmpty(mtype)) { ib = new InstanceBlueprint(currentType); } else { ib = new InstanceBlueprint(kp[mtype]); } readMultiset(div.Current, ib.Multiset); r.Rhs.Add(ib); } rule = r; } else { XPathNavigator v; v = rhsNav.SelectSingleNode("linkCreate"); if (v == null) { v = rhsNav.SelectSingleNode("linkDestroy"); if (v == null) { v = rhsNav.SelectSingleNode("dissolve"); if (v != null) { rule = new DissolutionRule(); } } else { rule = LinkRule.LinkDestroy(readInstanceIdentifier(v)); } } else { rule = LinkRule.LinkCreate(readInstanceIdentifier(v)); } } } if (rule == null) { div = rhsNav.SelectChildren("target", String.Empty); if (div.Count > 0) { RewriteCommunicationRule rcr = new RewriteCommunicationRule(); rcr.Rhs.Add(m); while (div.MoveNext()) { TargetedMultiset tm = readTargetedMultiset(div.Current); TargetedMultiset otm = null; rcr.TargetRhs.TryGetValue(tm.Target, out otm); if (otm == null) { rcr.TargetRhs.Add(tm.Target, tm); } else { otm.Multiset.Add(tm.Multiset); } } rule = rcr; } else if (m != null) { rule = new RewritingRule(); (rule as RewritingRule).Rhs.Add(m); } } } if (rule == null) { rule = new ConsumerRule(); } readPItem(nav, rule); rule.Guard = readGuard(nav.SelectSingleNode("guard")); readMultiset(nav.SelectSingleNode("lhs"), rule.Lhs); return(rule); }
/// <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 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); } } } } }