Beispiel #1
0
        private static ICondition extractGuardConditionFromKpGuard(IGuard guard)
        {
            ICondition condition = null;

            if (guard is BasicGuard)
            {
                BasicGuard basicGuard = (BasicGuard)guard;
                condition = getBoolExpression(basicGuard);
            }
            else if (guard is NegatedGuard)
            {
                NegatedGuard negatedGuard = (NegatedGuard)guard;
                Console.Error.WriteLine("KP NegatedGuard to SMV translation has not been implemented yet!");
            }
            else if (guard is CompoundGuard)
            {
                CompoundGuard          compoundGuard    = (CompoundGuard)guard;
                CompoundBoolExpression compoundBoolExpr = new CompoundBoolExpression();
                compoundBoolExpr.LeftCondition  = extractGuardConditionFromKpGuard(compoundGuard.Lhs);
                compoundBoolExpr.BinaryOperator = SMVUtil.getBinaryOperator(compoundGuard.Operator);
                compoundBoolExpr.RightCondition = extractGuardConditionFromKpGuard(compoundGuard.Rhs);
                condition = compoundBoolExpr;
            }
            return(condition);
        }
        private static ICondition extractConditionFromKpGuard(Module sourceModule, IGuard guard)
        {
            ICondition condition = null;

            if (guard is BasicGuard)
            {
                BasicGuard basicGuard = (BasicGuard)guard;
                condition = getBoolExpression(sourceModule, basicGuard);
            }
            else if (guard is NegatedGuard)
            {
                NegatedGuard negatedGuard = (NegatedGuard)guard;
                Console.Error.WriteLine("NegatedGuard is not applicable.");
                // ICondition negatedBooleanExpression = getNegatedGuard(negatedGuard);
            }
            else if (guard is CompoundGuard)
            {
                CompoundGuard          compoundGuard    = (CompoundGuard)guard;
                CompoundBoolExpression compoundBoolExpr = null;
                compoundBoolExpr.LeftCondition  = extractConditionFromKpGuard(sourceModule, compoundGuard.Lhs);
                compoundBoolExpr.BinaryOperator = SMVUtil.getBinaryOperator(compoundGuard.Operator);
                compoundBoolExpr.RightCondition = extractConditionFromKpGuard(sourceModule, compoundGuard.Rhs);
                condition = compoundBoolExpr;
            }
            return(condition);
        }
Beispiel #3
0
        private void Guard(List <int> list, IGuard guard, bool negated = false)
        {
            if (guard is BasicGuard)
            {
                BasicGuard basicGuard = (BasicGuard)guard;
                int        rel        = 0;
                switch (basicGuard.Operator)
                {
                case RelationalOperator.LT: rel = 0; break;

                case RelationalOperator.LEQ: rel = 1; break;

                case RelationalOperator.EQUAL: rel = 2; break;

                case RelationalOperator.NOT_EQUAL: rel = 3; break;

                case RelationalOperator.GT: rel = 4; break;

                case RelationalOperator.GEQ: rel = 5; break;
                }
                if (negated)
                {
                    rel += 8;
                }
                list.Add(rel);
                Multiset multiset = basicGuard.Multiset;
                string   o        = multiset.Objects.ToList()[0];
                list.Add(objectsId[o]);
                list.Add(multiset[o]);
            }
            else
            if (guard is NegatedGuard)
            {
                NegatedGuard negatedGuard = (NegatedGuard)guard;
                Guard(list, negatedGuard.Operand, true);
            }
            else
            if (guard is CompoundGuard)
            {
                CompoundGuard compoundGuard = (CompoundGuard)guard;
                int           op;
                if (compoundGuard.Operator == BinaryGuardOperator.AND)
                {
                    op = 0;
                }
                else
                {
                    op = 1;
                }
                Guard(list, compoundGuard.Lhs, negated);
                list.Add(op);
                Guard(list, compoundGuard.Rhs, negated);
            }
        }
Beispiel #4
0
        public void writeGuard(IGuard g)
        {
            if (g == null)
            {
                return;
            }
            owt.Write("{");
            if (g is BasicGuard)
            {
                BasicGuard bg = g as BasicGuard;
                owt.Write("\"operator\":");
                switch (bg.Operator)
                {
                case RelationalOperator.EQUAL: owt.Write("\"eq\""); break;

                case RelationalOperator.GEQ: owt.Write("\"geq\""); break;

                case RelationalOperator.GT: owt.Write("\"gt\""); break;

                case RelationalOperator.LT: owt.Write("\"lt\""); break;

                case RelationalOperator.LEQ: owt.Write("\"leq\""); break;

                case RelationalOperator.NOT_EQUAL: owt.Write("\"neq\""); break;
                }
                owt.Write(", \"multiset\":");
                writeMultiset(bg.Multiset);
            }
            else if (g is NegatedGuard)
            {
                NegatedGuard ng = g as NegatedGuard;
                owt.Write("\"operator\":\"not\"");
                owt.Write(", \"operatnd\":");
                writeGuard(ng.Operand);
            }
            else if (g is CompoundGuard)
            {
                CompoundGuard cg = g as CompoundGuard;
                owt.Write("\"operator\":");
                switch (cg.Operator)
                {
                case BinaryGuardOperator.AND: owt.Write("\"and\""); break;

                case BinaryGuardOperator.OR: owt.Write("\"or\""); break;
                }
                owt.Write(", \"lhs\":");
                writeGuard(cg.Lhs);
                owt.Write(", \"rhs\":");
                writeGuard(cg.Rhs);
            }
            owt.Write("}");
        }
Beispiel #5
0
        /// <summary>
        /// Extracts boolean expression from a KP guard. In addition it adds module name as an identifier.
        /// </summary>
        /// <param name="sourceModule">module of guard</param>
        /// <param name="basicGuard"></param>
        /// <returns></returns>
        public static ICondition getBoolExpression(BasicGuard basicGuard)
        {
            Multiset ms = basicGuard.Multiset;

            NuSMV.RelationalOperator oper = SMVUtil.getRelationalOperator(basicGuard.Operator);
            BoolExp booleanExpression     = new BoolExp();

            foreach (var obj in ms.Objects)
            {
                booleanExpression.Left = new Expression(obj);
                booleanExpression.RelationalOperator = oper;
                booleanExpression.Right = new Expression(ms[obj].ToString());
            }
            return(booleanExpression);
        }
Beispiel #6
0
 private static void extractVarsFromGuards(KPsystem kpSystem, MType kpType, Module module, IGuard guard)
 {
     if (guard is BasicGuard)
     {
         BasicGuard basicGuard = (BasicGuard)guard;
         extractVarFromBasicGuards(kpSystem, kpType, module, basicGuard);
     }
     else if (guard is NegatedGuard)
     {
         NegatedGuard negatedGuard = (NegatedGuard)guard;
         Console.Error.WriteLine("This part has not implemented yet.");
         // ICondition negatedBooleanExpression = getNegatedGuard(negatedGuard);
     }
     else if (guard is CompoundGuard)
     {
         CompoundGuard compoundGuard = (CompoundGuard)guard;
         extractVarsFromGuards(kpSystem, kpType, module, compoundGuard.Lhs);
         extractVarsFromGuards(kpSystem, kpType, module, compoundGuard.Rhs);
     }
 }
        public object VisitGuardOperand(KpLinguaParser.GuardOperandContext context)
        {
            var guard = default(IGuard);

            var multisetContext           = context.nonEmptyMultiset();
            var guardContext              = context.guard();
            var relationalOperatorContext = context.RelationalOperator();

            if (multisetContext != null)
            {
                var multiset           = multisetContext.Accept(this) as Multiset;
                var relationalOperator = relationalOperatorContext != null?GetRelationalOperator(relationalOperatorContext) : RelationalOperator.EQUAL;

                guard = new BasicGuard(multiset, relationalOperator);
            }
            else if (guardContext != null)
            {
                guard = guardContext.Accept(this) as IGuard;
            }

            return(guard);
        }
        /// <summary>
        /// Extracts boolean expression from a KP guard. In addition it adds module name as an identifier.
        /// </summary>
        /// <param name="sourceModule">module of guard</param>
        /// <param name="basicGuard"></param>
        /// <returns></returns>
        private static ICondition getBoolExpression(Module sourceModule, BasicGuard basicGuard)
        {
            Multiset ms = basicGuard.Multiset;

            NuSMV.RelationalOperator oper = SMVUtil.getRelationalOperator(basicGuard.Operator);
            BoolExp booleanExpression     = new BoolExp();

            foreach (var obj in ms.Objects)
            {
                if (sourceModule != null)
                {
                    booleanExpression.Left = new InstancedExp(sourceModule.Type, obj);
                }
                else
                {
                    booleanExpression.Left = new InstancedExp(obj);
                }
                booleanExpression.RelationalOperator = oper;
                booleanExpression.Right = new Expression(ms[obj].ToString());
            }
            return(booleanExpression);
        }
        public static string Guard(IGuard guard)
        {
            StringBuilder buf = new StringBuilder();

            if (guard is BasicGuard)
            {
                BasicGuard g = guard as BasicGuard;
                switch (g.Operator)
                {
                case RelationalOperator.EQUAL: {
                    buf.Append("=");
                } break;

                case RelationalOperator.NOT_EQUAL: {
                    buf.Append("!");
                } break;

                case RelationalOperator.GEQ: {
                    buf.Append(">=");
                } break;

                case RelationalOperator.LEQ: {
                    buf.Append("<=");
                } break;

                case RelationalOperator.LT: {
                    buf.Append("<");
                } break;

                case RelationalOperator.GT: {
                    buf.Append(">");
                } break;
                }

                buf.Append(Multiset(g.Multiset));
            }
            else if (guard is NegatedGuard)
            {
                NegatedGuard g = guard as NegatedGuard;
                buf.AppendFormat("!({0})", Guard(g.Operand));
            }
            else if (guard is CompoundGuard)
            {
                CompoundGuard g = guard as CompoundGuard;

                bool useP = false;
                if (g.Lhs is CompoundGuard)
                {
                    CompoundGuard cgLhs = g.Lhs as CompoundGuard;
                    if (cgLhs.Operator != g.Operator)
                    {
                        useP = true;
                    }
                }

                if (useP)
                {
                    buf.Append("(").Append(Guard(g.Lhs)).Append(")");
                }
                else
                {
                    buf.Append(Guard(g.Lhs));
                }

                if (g.Operator == BinaryGuardOperator.AND)
                {
                    buf.Append(" & ");
                }
                else
                {
                    buf.Append(" | ");
                }

                useP = false;
                if (g.Rhs is CompoundGuard)
                {
                    CompoundGuard cgRhs = g.Rhs as CompoundGuard;
                    if (cgRhs.Operator != g.Operator)
                    {
                        useP = true;
                    }
                }

                if (useP)
                {
                    buf.Append("(").Append(Guard(g.Rhs)).Append(")");
                }
                else
                {
                    buf.Append(Guard(g.Rhs));
                }
            }


            return(buf.ToString());
        }
Beispiel #10
0
        public static string TranslateGuard(IGuard guard)
        {
            StringBuilder buf = new StringBuilder();
            if (guard is BasicGuard)
            {
                BasicGuard bg = guard as BasicGuard;
                switch (bg.Operator)
                {
                    case RelationalOperator.EQUAL: buf.Append("="); break;
                    case RelationalOperator.GEQ: buf.Append(">="); break;
                    case RelationalOperator.GT: buf.Append(">"); break;
                    case RelationalOperator.LEQ: buf.Append("<="); break;
                    case RelationalOperator.LT: buf.Append("<"); break;
                    case RelationalOperator.NOT_EQUAL: buf.Append("!"); break;
                }
                //buf.Append(" ");
                if (bg.Multiset.Count > 1)
                {
                    buf.Append("{").Append(TranslateMultiset(bg.Multiset)).Append("}");
                }
                else
                {
                    buf.Append(TranslateMultiset(bg.Multiset));
                }
            }
            else if (guard is NegatedGuard)
            {
                buf.Append("!(").Append(TranslateGuard((guard as NegatedGuard).Operand)).Append(")");
            }
            else if (guard is CompoundGuard)
            {
                CompoundGuard cg = guard as CompoundGuard;
                bool useP = false;
                if (cg.Lhs is CompoundGuard)
                {
                    CompoundGuard cgLhs = cg.Lhs as CompoundGuard;
                    if (cgLhs.Operator != cg.Operator)
                    {
                        useP = true;
                    }
                }

                if (useP)
                {
                    buf.Append("(").Append(TranslateGuard(cg.Lhs)).Append(")");
                }
                else
                {
                    buf.Append(TranslateGuard(cg.Lhs));
                }

                if (cg.Operator == BinaryGuardOperator.AND)
                {
                    buf.Append(" & ");
                }
                else
                {
                    buf.Append(" | ");
                }

                useP = false;
                if (cg.Rhs is CompoundGuard)
                {
                    CompoundGuard cgRhs = cg.Rhs as CompoundGuard;
                    if (cgRhs.Operator != cg.Operator)
                    {
                        useP = true;
                    }
                }

                if (useP)
                {
                    buf.Append("(").Append(TranslateGuard(cg.Rhs)).Append(")");
                }
                else
                {
                    buf.Append(TranslateGuard(cg.Rhs));
                }
            }

            return buf.ToString();
        }
Beispiel #11
0
        private string Guard(IGuard guard, bool negated = false, int ooperation = 0)
        {
            StringBuilder sb = new StringBuilder();

            if (guard is BasicGuard)
            {
                BasicGuard basicGuard = (BasicGuard)guard;
                int        rel        = 0;
                switch (basicGuard.Operator)
                {
                case RelationalOperator.LT: rel = 0; break;

                case RelationalOperator.LEQ: rel = 1; break;

                case RelationalOperator.EQUAL: rel = 2; break;

                case RelationalOperator.NOT_EQUAL: rel = 3; break;

                case RelationalOperator.GT: rel = 4; break;

                case RelationalOperator.GEQ: rel = 5; break;
                }
                if (negated)
                {
                    rel += 7;
                }
                sb.Append("{");
                sb.AppendFormat("{0}, ", rel);
                Multiset multiset = basicGuard.Multiset;
                sb.Append("{");
                string o = multiset.Objects.ToList()[0];
                sb.Append(string.Format("{0}, {1}", objectsId[o], multiset[o]));
                sb.Append("}");
                sb.AppendFormat(", {0}", ooperation);
                sb.Append("}");
            }
            else
            if (guard is NegatedGuard)
            {
                NegatedGuard negatedGuard = (NegatedGuard)guard;
                sb.Append(Guard(negatedGuard.Operand, true));
            }
            else
            if (guard is CompoundGuard)
            {
                CompoundGuard compoundGuard = (CompoundGuard)guard;
                int           op;
                if (compoundGuard.Operator == BinaryGuardOperator.AND)
                {
                    op = 0;
                }
                else
                {
                    op = 1;
                }
                sb.Append(Guard(compoundGuard.Lhs, negated, op));
                sb.Append(", ");
                sb.Append(Guard(compoundGuard.Rhs, negated, ooperation));
            }
            return(sb.ToString());
        }
Beispiel #12
0
        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);
                }
            }
        }