/// <summary> /// This is very inneficient. Consider alternatives ... /// </summary> /// <param name="instance"></param> /// <param name="type"></param> /// <param name="kp"></param> /// <returns></returns> public override bool IsApplicable(MInstance instance, MType type, KPsystem kp) { if (!base.IsApplicable(instance, type, kp)) { return(false); } foreach (IInstanceIdentifier identifier in TargetRhs.Keys) { if (identifier is InstanceIdentifier) { InstanceIdentifier ii = identifier as InstanceIdentifier; if (ii.Indicator == InstanceIndicator.TYPE) { MType mt = kp[ii.Value]; bool atLeastOneConnection = false; foreach (MInstance connection in instance.Connections) { if (mt.Instances.Contains(connection)) { atLeastOneConnection = true; break; } } if (!atLeastOneConnection) { return(false); } } } } return(true); }
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); }
/// <summary> /// Deep copy /// </summary> /// <param name="?"></param> /// <returns></returns> public KPsystem Clone() { KPsystem clone = new KPsystem(); PItem.CopyProperties(this, clone); Dictionary <MInstance, MInstance> instanceCloneRegistry = new Dictionary <MInstance, MInstance>(); foreach (KeyValuePair <string, MType> kv in types) { MType mtype = kv.Value; MType typeClone = new MType(mtype.Name); PItem.CopyProperties(mtype, typeClone); foreach (MInstance instance in mtype.Instances) { MInstance instanceClone = instance.Clone(); instanceCloneRegistry.Add(instance, instanceClone); typeClone.Instances.Add(instanceClone); } clone.AddType(typeClone); } foreach (KeyValuePair <MInstance, MInstance> kv in instanceCloneRegistry) { MInstance tar = kv.Value; foreach (MInstance connection in kv.Key.Connections) { tar.Connections.Add(instanceCloneRegistry[connection]); } } return(clone); }
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); } }
public MType TryGetType(string typeName) { MType x = null; types.TryGetValue(typeName, out x); return(x); }
public MType GetTypeForId(int id) { MType t = null; mtypeIds.TryGetValue(id, out t); return(t); }
public InstanceBlueprint(MType mt, Multiset ms) { if (mt == null) { throw new ArgumentNullException("mt", "MType cannot be null"); } mtype = mt; multiset = ms; }
public MType EnsureType(string typeName) { MType t = null; types.TryGetValue(typeName, out t); if (t == null) { t = new MType(typeName); types.Add(typeName, t); } return(t); }
/// <summary> /// Deep copy /// </summary> /// <returns></returns> public MType Clone() { MType clone = new MType(this.Name); PItem.CopyProperties(this, clone); foreach (MInstance instance in instances) { clone.Instances.Add(instance.Clone()); } return(clone); }
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 MType Append(MType mtype) { foreach (MInstance instance in mtype.Instances) { instances.Add(instance); } if (strategy.IsEmpty()) { strategy = mtype.ExecutionStrategy; } else { strategy.Next = mtype.ExecutionStrategy; } return(this); }
public void AddType(MType t) { types.Add(t.Name, t); }
/// <summary> /// Tests whether this Rule is applicable to an instance of the specified type belonging to the kP system kp. /// </summary> /// <param name="instance"></param> /// <param name="type"></param> /// <param name="kp"></param> /// <returns></returns> public virtual bool IsApplicable(MInstance instance, MType type, KPsystem kp) { return(IsGuarded ? Guard.IsSatisfiedBy(instance.Multiset) : true); }
public override bool IsApplicable(MInstance instance, MType type, KPsystem kp) { return(base.IsApplicable(instance, type, kp) && instance.Multiset >= lhs); }
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); } } } } }
public static void buildVariables(KPsystem kpSystem, SMVModel nuSMV, NuSMV.Module module, KpCore.MType kpType) { // variables comes from KP model. buildStandardVariables(nuSMV, module, kpSystem, kpType); }
public TypeTargetedMultiset(MType mtype, Multiset multiset) : base(new InstanceIdentifier(InstanceIndicator.TYPE, mtype.Name), multiset) { MType = mtype; }
public InstanceBlueprint(MType mt) : this(mt, new Multiset()) { }
public TypeTargetedMultiset(MType mtype) : this(mtype, new Multiset()) { }
/// <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; } }
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); }
public void RemoveType(MType t) { types.Remove(t.Name); }
public TypedInstance(MType mtype, MInstance minstance) { Type = mtype; Instance = minstance; }