예제 #1
0
        public void TryCatchTypeBody()
        {
            var expected = LinqExpression.TryCatch(
                LinqExpression.Constant(0L),
                LinqExpression.Catch(
                    typeof(Exception),
                    LinqExpression.Constant(0L)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :tryBody _:zero ;
    :tryHandlers (
        [
            :catchType [
                :typeName ""System.Exception"" ;
            ] ;
            :catchBody _:zero ;
        ]
    ) ;
.
_:zero
    :constantValue 0 ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #2
0
        public void NewArrayInit()
        {
            var expected = LinqExpression.NewArrayInit(typeof(long), LinqExpression.Constant(0L));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    a :NewArrayInit ;
    :newArrayType [
        :typeName ""System.Int64"" ;
    ] ;
    :newArrayExpressions (
        [
            :constantValue 0 ;
        ]
    ) ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #3
0
        public void Label()
        {
            var expected = LinqExpression.Label(
                LinqExpression.Label(
                    typeof(int),
                    "target"),
                LinqExpression.Parameter(
                    typeof(int)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :labelTarget [
        :targetName ""target"" ;
        :targetType _:int ;
    ] ;
    :labelDefaultValue [
        :parameterType _:int ;
    ] ;
.

_:int
    :typeName ""System.Int32"" ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #4
0
        public void ConditionType()
        {
            var expected = LinqExpression.Condition(
                LinqExpression.Parameter(typeof(bool)),
                LinqExpression.Parameter(typeof(SampleClass)),
                LinqExpression.Parameter(typeof(SampleDerivedClass)),
                typeof(SampleClass));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :conditionTest [:parameterType [:typeName ""System.Boolean"" ;]] ;
    :conditionIfTrue [:parameterType _:C1] ;
    :conditionIfFalse [:parameterType [:typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ;]] ;
    :conditionType _:C1 ;
.

_:C1 :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" .
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #5
0
        public void ArrayIndex_index()
        {
            var expected = LinqExpression.ArrayIndex(
                LinqExpression.Parameter(
                    typeof(int[])),
                LinqExpression.Parameter(
                    typeof(int)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :arrayIndexArray [
        :parameterType [
            :typeName ""System.Int32[]"" ;
        ] ;
    ] ;
    :arrayIndexIndex [
        :parameterType [
            :typeName ""System.Int32"" ;
        ] ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #6
0
        public void Assign()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :binaryExpressionType xt:Assign ;
    :binaryLeft [
        :parameterType [
            :typeName ""System.Int64"" ;
        ] ;
    ] ;
    :binaryRight [
        :constantValue 0;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var result = Expression.Parse(s).LinqExpression;

            Console.WriteLine(result.GetDebugView());
        }
예제 #7
0
        public void POC()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :blockExpressions (
        [
            :binaryExpressionType xt:Subtract ;
            :binaryLeft [
                :binaryExpressionType xt:Add ;
                :binaryLeft [
                    :constantValue 1;
                ] ;
                :binaryRight [
                    :constantValue 2;
                ] ;
            ] ;
            :binaryRight [
                :constantValue 3;
            ] ;
        ]
    ) ;
.
");

            var s = g.GetUriNode(":s");

            var result = Expression.Parse(s).LinqExpression;

            Console.WriteLine(result.GetDebugView());
        }
예제 #8
0
        public void IfThenElse()
        {
            var param    = LinqExpression.Parameter(typeof(bool));
            var expected = LinqExpression.IfThenElse(param, param, param);

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    a :IfThenElse ;
    :conditionTest _:param ;
    :conditionIfTrue _:param ;
    :conditionIfFalse _:param ;
.

_:param
    :parameterType [
        :typeName ""System.Boolean"" ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #9
0
        public void EatYourOwnDogfood()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :lambdaBody [
        :callInstance _:g ;
        :callMethodName ""Clear"" ;
    ] ;
    :lambdaParameters (
        _:g
    ) ;
.

_:g
    :parameterType [
        :typeName ""VDS.RDF.IGraph, dotNetRDF"" ;
    ] ;
.
");

            var s = g.GetUriNode(":s");

            var parsed           = Expression.Parse(s).LinqExpression;
            var lambdaExpression = (Linq.LambdaExpression)parsed;
            var lambda           = lambdaExpression.Compile();

            Assert.AreEqual(g.Triples.Count, 12); // 8 explicit, 4 implicit
            var result = lambda.DynamicInvoke(g);

            Assert.AreEqual(g.Triples.Count, 0);
        }
예제 #10
0
        public void Lambda()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :lambdaBody [
        :binaryExpressionType xt:Add ;
        :binaryLeft [
            :constantValue 1;
        ] ;
        :binaryRight [
            :constantValue 2;
        ] ;
    ]
.
");

            var s      = g.GetUriNode(":s");
            var result = (Linq.LambdaExpression)Expression.Parse(s).LinqExpression;

            var a = result.Compile().DynamicInvoke();

            Console.WriteLine(a);
        }
예제 #11
0
        public void TryFinally()
        {
            var expected = LinqExpression.TryFinally(
                LinqExpression.Constant(0L),
                LinqExpression.Constant(0L));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :tryBody _:zero ;
    :tryFinally _:zero ;
.
_:zero
    :constantValue 0 ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #12
0
        public void All()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/> .

:s
    :arrayIndexArray [
        :parameterType [
            :typeName ""System.Int32[]"" ;
        ] ;
    ] ;
    :arrayIndexIndexes (
        [
            :parameterType [
                :typeName ""System.Int32"" ;
            ] ;
        ]
        [
            :parameterType [
                :typeName ""System.Int64"" ;
            ] ;
        ]
    ) ;
.
");
            var s = g.GetUriNode(":s");

            var aa = new Collection <Expression>(s, Vocabulary.ArrayIndexIndexes, Expression.Parse);

            foreach (Parameter index in aa)
            {
                Console.WriteLine(index.Type.Name);
            }

            var item = new Parameter(s.Graph.CreateBlankNode());
            var type = new GraphEngine.Type(s.Graph.CreateBlankNode());

            type.Name = "X";
            var typeA = new GraphEngine.Type(s.Graph.CreateBlankNode());

            typeA.Name = "Y";
            type.Arguments.Add(typeA);
            item.Type = type;

            aa.Add(item);

            foreach (Parameter index in aa)
            {
                Console.WriteLine(index.Type.Name);
            }
        }
예제 #13
0
        protected static void ShouldBe(string rdf, LinqExpression expected)
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(rdf);
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
        private static void ShouldRoundrip(LinqExpression original)
        {
            using var g = new GraphEngine.Graph();
            var s = g.CreateBlankNode();

            new SerialisingVisitor(s).Visit(original);
            new CompressingTurtleWriter(WriterCompressionLevel.Medium).Save(g, Console.Out, true);
            Console.WriteLine();

            var processed = GraphEngine.Expression.Parse(s).LinqExpression;

            Console.WriteLine(processed.GetDebugView());

            processed.Should().Be(original);
        }
예제 #15
0
        public void ReasoningPOC()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromEmbeddedResource("GraphEngine.Tests.Resources.Examples.FibonacciSequenceUntyped.ttl, GraphEngine.Tests");

            var s = g.GetUriNode(new Uri("http://example.com/s"));

            var expression = Expression.Parse(s).LinqExpression;

            Console.WriteLine(expression.GetDebugView());

            var lambda = LinqExpression.Lambda(expression);
            var actual = lambda.Compile().DynamicInvoke();

            Assert.AreEqual(21L, actual);
        }
예제 #16
0
        public void TryCatchVariableBodyFilter()
        {
            var expected = LinqExpression.TryCatch(
                LinqExpression.Constant(0L),
                LinqExpression.Catch(
                    LinqExpression.Parameter(
                        typeof(Exception)),
                    LinqExpression.Constant(0L),
                    LinqExpression.Equal(
                        LinqExpression.Constant(0L),
                        LinqExpression.Constant(0L))));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

:s
    :tryBody _:zero ;
    :tryHandlers (
        [
            :catchVariable [
                :parameterType [
                    :typeName ""System.Exception"" ;
                ] ;
            ] ;
            :catchBody _:zero ;
            :catchFilter [
                :binaryExpressionType xt:Equal ;
                :binaryLeft _:zero ;
                :binaryRight _:zero ;
            ] ;
        ]
    ) ;
.
_:zero
    :constantValue 0 ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #17
0
        public void NewWithoutArguments()
        {
            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/> .

:s
    :newType [
        :typeName ""System.Text.StringBuilder"" ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var result = Expression.Parse(s).LinqExpression;

            Console.WriteLine(result.GetDebugView());
        }
예제 #18
0
        public void Empty()
        {
            var expected = LinqExpression.Empty();

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    a :Empty ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #19
0
        public void Default()
        {
            var expected = LinqExpression.Default(typeof(byte));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :defaultType [
        :typeName ""System.Byte"" ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
예제 #20
0
        public void All()
        {
            var expected =
                LinqExpression.Switch(
                    typeof(SampleClass),
                    LinqExpression.Constant(0L),
                    LinqExpression.Default(typeof(SampleDerivedClass)),
                    typeof(SampleClass).GetMethod("Equal"),
                    LinqExpression.SwitchCase(
                        LinqExpression.Default(typeof(SampleDerivedClass)),
                        LinqExpression.Constant(0L)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/> .

:s
    :switchType [
        :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" ;
    ] ;
    :switchSwitchValue _:zero ;
    :switchDefaultBody [
        :defaultType [
            :typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ;
        ]
    ] ;
    :switchCases (
        [
            :caseTestValues (
                _:zero
            ) ;
            :caseBody [
                :defaultType [
                    :typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ;
                ]
            ] ;
        ]
    ) ;
    :switchComparison [
        :memberType [
            :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" ;
        ] ;
        :memberName ""Equal"" ;
    ] ;
.

_:zero
    :constantValue 0 ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            actual.Should().Be(expected);

            Console.WriteLine(actual.GetDebugView());

            // Make sure custom type is used
            Assert.AreEqual(typeof(SampleClass), actual.Type);

            // Make sure custom comparison is used
            Assert.ThrowsException <TargetInvocationException>(() => LinqExpression.Lambda(actual).Compile().DynamicInvoke());
        }
예제 #21
0
        public void Factorial()
        {
            var value    = LinqExpression.Parameter(typeof(int));
            var result   = LinqExpression.Parameter(typeof(int));
            var label    = LinqExpression.Label(typeof(int));
            var expected = LinqExpression.Block(
                new[] { result },
                LinqExpression.Assign(
                    result,
                    LinqExpression.Constant(1)),
                LinqExpression.Loop(
                    LinqExpression.Condition(
                        LinqExpression.GreaterThan(
                            value,
                            LinqExpression.Constant(1)),
                        LinqExpression.MultiplyAssign(
                            result,
                            LinqExpression.PostDecrementAssign(
                                value)),
                        LinqExpression.Break(
                            label,
                            result),
                        typeof(void)),
                    label));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/> .
@prefix xt: <http://example.com/ExpressionTypes/> .

_:constantValue
    :parameterType [
        :typeName ""System.Int32"" ;
    ] ;
.

_:result
    :parameterType [
        :typeName ""System.Int32"" ;
    ] ;
.

_:label
    :targetType [
        :typeName ""System.Int32"" ;
    ] ;
.

_:one
    :constantValue ""1""^^xsd:int ;
.

:s
    :blockVariables (
        _:result
    ) ;
    :blockExpressions (
        [
            :binaryExpressionType xt:Assign ;
            :binaryLeft _:result ;
            :binaryRight _:one ;
        ]
        [
            :loopBody [
                :conditionTest [
                    :binaryExpressionType xt:GreaterThan ;
                    :binaryLeft _:constantValue ;
                    :binaryRight _:one ;
                ] ;
                :conditionIfTrue [
                    :binaryExpressionType xt:MultiplyAssign ;
                    :binaryLeft _:result ;
                    :binaryRight [
                        :unaryExpressionType xt:PostDecrementAssign ;
                        :unaryOperand _:constantValue ;
                    ] ;
                ] ;
                :conditionIfFalse [
                    a :Break ;
                    :gotoTarget _:label ;
                    :gotoValue _:result ;
                ] ;
                :conditionType [
                    :typeName ""System.Void"" ;
                ] ;
            ] ;
            :loopBreak _:label ;
        ]
    ) ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }