示例#1
0
        public void UnifiesNamespaceTest()
        {
            Literal l         = new LiteralImpl("literal");
            bool    resultado = un.UnifiesNamespace(l, l);

            Assert.AreEqual(true, resultado);
        }
示例#2
0
        public void BindTest()
        {
            VarTerm v = new VarTerm("key");
            ITerm   t = new LiteralImpl(false, "value");

            bool resultado = un.Bind(v, t);

            Assert.AreEqual(true, resultado);
        }
示例#3
0
        public override double Evaluate(Reasoner reasoner, ITerm[] args)
        {
            // create a literal to perform the query
            Literal r;

            if (literal.IndexOf(".") > 0) // is internal action
            {
                r = new InternalActionLiteral(literal);
            }
            else
            {
                r = new LiteralImpl(literal);
            }

            r.AddTerms(args);
            VarTerm answer = new VarTerm("__RuleToFunctionResult");

            r.AddTerm(answer);

            // query the BB
            IEnumerator <Unifier> i = r.LogicalConsequence((reasoner == null ? null : reasoner.GetAgent()), new Unifier());

            if (i.MoveNext())
            {
                ITerm value = i.Current.Get(answer);
                if (value.IsNumeric())
                {
                    return(((INumberTerm)value).Solve());
                }
                else
                {
                    throw new JasonityException("The result of " + r + " (=" + value + ") is not numeric!");
                }
            }
            else
            {
                throw new JasonityException("No solution was found for rule " + r);
            }
        }
示例#4
0
        public void ParseLiteralTest()
        {
            // Use the Assert class to test conditions
            Literal l = new LiteralImpl("tall");
            string  s = "tall(john)";
            //string eof = "^Z";

            //string.Concat(s, (char)26);
            //byte[] b = Encoding.ASCII.GetBytes(s);
            //byte[] array = new byte[b.Length + 1];
            //byte eof = 0x1A;
            //for (int i = 0; i < b.Length; i++)
            //{
            //    array[i] = b[i];
            //}
            //array[b.Length] = eof;

            //s = Encoding.ASCII.GetString(array, 0, array.Length);
            Literal resultado = AsSyntax.ParseLiteral(s);

            Assert.AreEqual(l.ToString(), resultado.ToString());
        }
示例#5
0
        public static ExpressionSyntax GetExpression(LiteralImpl asg)
        {
            if (asg == null)
            {
                throw new ArgumentNullException(nameof(asg));
            }


            if (asg.getCtx().STRINGLITERAL() != null)
            {
                //return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(asg.getCtx().STRINGLITERAL().getText());
                return(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(asg.getValue())));
            }
            else if (asg.getCtx().INTEGERLITERAL() != null)
            {
                if (asg.getValue().EndsWith("#", false, CultureInfo.InvariantCulture))
                {
                    return
                        (SyntaxFactory.LiteralExpression(
                             SyntaxKind.NumericLiteralExpression,
                             SyntaxFactory.Literal(double.Parse(asg.getValue().Replace("#", ""), NumberFormatInfo.InvariantInfo))));
                }
                else
                {
                    return(SyntaxFactory.LiteralExpression(
                               SyntaxKind.NumericLiteralExpression,
                               SyntaxFactory.Literal(int.Parse(asg.getValue(), NumberFormatInfo.InvariantInfo))));
                }
            }
            else if (asg.getCtx().DOUBLELITERAL() != null)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(double.Parse(asg.getValue(), NumberFormatInfo.InvariantInfo))));
            }
            else if (asg.getCtx().FILENUMBER() != null)
            {
                var num = asg.getValue().Trim('#');
                if (num.All(c => char.IsDigit(c)))
                {
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(int.Parse(num, NumberFormatInfo.InvariantInfo))));
                }
                else
                {
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(asg.getValue())));
                }
            }
            else if (asg.getCtx().COLORLITERAL() != null)
            {
                var value = asg.getValue();
                if (int.TryParse(value, out var result))
                {
                    return(SyntaxFactory.ParseExpression("System.Drawing.Color.FromArgb(" + result + ")"));
                }
                else if (value.StartsWith("&H", StringComparison.InvariantCulture))
                {
                    var hexValue = value.Trim('&').Trim('H');
                    return(SyntaxFactory.ParseExpression("(Color)ColorConverter.ConvertFromString(\"" + hexValue + "\")"));
                }
                else
                {
                    throw new System.NotImplementedException("Unknown color literal value type");
                }

                //throw new InvalidOperationException("color literal");
                //return SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(asg.getCtx().COLORLITERAL().getText()));
            }
            else if (asg.getCtx().TRUE() != null)
            {
                //return SyntaxFactory.LiteralExpression(SyntaxKind.BoolKeyword, SyntaxFactory.Token(SyntaxKind.TrueKeyword));
                return(SyntaxFactory.ParseExpression("true"));
            }
            else if (asg.getCtx().FALSE() != null)
            {
                return(SyntaxFactory.ParseExpression("false"));
            }
            else
            {
                throw new NotImplementedException("node type: " + asg.getCtx().GetType().Name + ": " + asg.getCtx().getText());
                // TODO: A bit risky. Assumes that literals are same in VB6 and C#.
                //return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(asg.getValue()));
            }

            //return GetExpression(asg.getCtx().getChild(0), statements);
        }