Exemple #1
0
        /**
         * Creates a new UNION element as a blank node in a given Model.
         * @param model  the Model to create the Union in
         * @param elements  the elements
         * @return a new Union
         */
        public static IUnion createUnion(SpinProcessor model, IElementList elements)
        {
            IUnion union = (IUnion)model.CreateResource(SP.ClassUnion).As(typeof(UnionImpl));

            union.AddProperty(SP.PropertyElements, elements);
            return(union);
        }
Exemple #2
0
        /// <summary>
        /// Optimises an Algebra to a form that uses <see cref="LazyBgp">LazyBgp</see> where possible
        /// </summary>
        /// <param name="algebra">Algebra</param>
        /// <param name="depth">Depth</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// By transforming a query to use <see cref="LazyBgp">LazyBgp</see> we can achieve much more efficient processing of some forms of queries
        /// </para>
        /// </remarks>
        protected override ISparqlAlgebra OptimiseInternal(ISparqlAlgebra algebra, int depth)
        {
            try
            {
                ISparqlAlgebra temp;

                //Note this first test is specifically for the default BGP implementation since other optimisers
                //may run before us and replace with other BGP implementations which we don't want to replace hence
                //why we don't check for IBgp here
                if (algebra is Bgp)
                {
                    temp = new LazyBgp(((Bgp)algebra).TriplePatterns);
                }
                //else if (algebra is ILeftJoin)
                //{
                //    ILeftJoin join = (ILeftJoin)algebra;
                //    temp = new LeftJoin(this.OptimiseInternal(join.Lhs, depth + 1), join.Rhs, ((LeftJoin)algebra).Filter);
                //}
                else if (algebra is IUnion)
                {
                    IUnion join = (IUnion)algebra;
                    temp = new LazyUnion(this.OptimiseInternal(join.Lhs, depth + 1), this.OptimiseInternal(join.Rhs, depth + 1));
                }
                else if (algebra is IJoin)
                {
                    IJoin join = (IJoin)algebra;
                    if (join.Lhs.Variables.IsDisjoint(join.Rhs.Variables))
                    {
                        //If the sides of the Join are disjoint then can fully transform the join since we only need to find the requisite number of
                        //solutions on either side to guarantee a product which meets/exceeds the required results
                        //temp = new Join(this.OptimiseInternal(join.Lhs, depth + 1), this.OptimiseInternal(join.Rhs, depth + 1));
                        temp = join.Transform(this);
                    }
                    else
                    {
                        //If the sides are not disjoint then the LHS must be fully evaluated but the RHS need only produce enough
                        //solutions that match
                        //temp = new Join(join.Lhs, this.OptimiseInternal(join.Rhs, depth + 1));
                        temp = join.TransformRhs(this);
                    }
                }
                else if (algebra is Algebra.Graph)
                {
                    //Algebra.Graph g = (Algebra.Graph)algebra;
                    //temp = new Algebra.Graph(this.OptimiseInternal(g.InnerAlgebra, depth + 1), g.GraphSpecifier);
                    IUnaryOperator op = (IUnaryOperator)algebra;
                    temp = op.Transform(this);
                }
                else
                {
                    temp = algebra;
                }
                return(temp);
            }
            catch
            {
                //If the Optimise fails return the current algebra
                return(algebra);
            }
        }
        /// <inheritdoc/>
        public override void VisitUnion(IUnion union)
        {
            if (union.Name == null)
            {
                // A union has been declared with no name. For example `union {}`.
                this.AddError(
                    CompilerMessageId.UnionMustHaveAName,
                    union.Node.UNION().Symbol);
            }
            else if (union.Parent.IsMemberNameAlreadyDeclared(union))
            {
                // Another type has already been declared with the same name.
                // For example:
                // ```
                // struct Request {}
                // union Request {}
                // ```
                this.AddError(
                    CompilerMessageId.NameAlreadyDeclared,
                    union.Node.name,
                    union.Name);
            }

            base.VisitUnion(union);
        }
Exemple #4
0
 FromUnion <T1, T2, T3, T4, T5, T6, T7>(
     IUnion <T1, T2, T3, T4, T5, T6, T7> union)
     where T1 : notnull
     where T2 : notnull
     where T3 : notnull
     where T4 : notnull
     where T5 : notnull
     where T6 : notnull
     where T7 : notnull
 {
     if (union is AnyOf <T1, T2, T3, T4, T5, T6, T7> anyOf)
     {
         return(anyOf);
     }
     else
     {
         return(union.Match <AnyOf <T1, T2, T3, T4, T5, T6, T7> >(
                    v1 => v1,
                    v2 => v2,
                    v3 => v3,
                    v4 => v4,
                    v5 => v5,
                    v6 => v6,
                    v7 => v7));
     }
 }
Exemple #5
0
 FromUnion <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(
     IUnion <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> union)
     where T1 : notnull
     where T2 : notnull
     where T3 : notnull
     where T4 : notnull
     where T5 : notnull
     where T6 : notnull
     where T7 : notnull
     where T8 : notnull
     where T9 : notnull
     where T10 : notnull
     where T11 : notnull
 {
     if (union is AnyOf <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> anyOf)
     {
         return(anyOf);
     }
     else
     {
         return(union.Match <AnyOf <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> >(
                    v1 => v1,
                    v2 => v2,
                    v3 => v3,
                    v4 => v4,
                    v5 => v5,
                    v6 => v6,
                    v7 => v7,
                    v8 => v8,
                    v9 => v9,
                    v10 => v10,
                    v11 => v11));
     }
 }
 public SubstitutedUnion(IUnion original, ImmutableArrayDictionary <ITypeParameter, IType> substitutions, Dictionary <IType, IType> substituted)
 {
     substituted.Add(original, this);
     substituted.Add(this, this);
     _options = new Lazy <ImmutableArray <IType> >(
         () => original.Options.Select(x => x.Substitute(substitutions, substituted)).ToImmutableArray());
 }
Exemple #7
0
 /// <summary>
 /// Processes a Union
 /// </summary>
 /// <param name="union">Union</param>
 /// <param name="context">SPARQL Evaluation Context</param>
 public virtual BaseMultiset ProcessUnion(IUnion union, SparqlEvaluationContext context)
 {
     if (context == null)
     {
         context = this.GetContext();
     }
     return(union.Evaluate(context));
 }
Exemple #8
0
        private TResult ElseFunction(IUnion <T1, T2, T3, T4> union)
        {
            switch (union)
            {
            case Union <T1, T2> _: return(_u2ElseFunction(union as Union <T1, T2>));

            case Union <T1, T2, T3> _: return(_u3ElseFunction(union as Union <T1, T2, T3>));

            default: return(_u4ElseFunction(union as Union <T1, T2, T3, T4>));
            }
        }
Exemple #9
0
 private IParent(
     IUnion <TLeft, TRight> union,
     TFunctor <TRight> .THead <TLeft> .IParent <Either <TLeft, TRight> > eitherFunctor)
     : this(
         union,
         new Monad <TLeft, TRight> .TParent <E>(union),
         eitherFunctor,
         new Applicative <TLeft, TRight> .TParent <E>(union)
         )
 {
 }
Exemple #10
0
 private IParent(
     IUnion <TLeft, TRight> union,
     TMonad <TRight> .THead <TLeft> .IParent <E> eitherMonad,
     TFunctor <TRight> .THead <TLeft> .IParent <Either <TLeft, TRight> > eitherFunctor,
     TApplicative <TRight> .THead <TLeft> .IParent <E> applicative
     )
 {
     _union         = union;
     _eitherMonad   = eitherMonad;
     _eitherFunctor = eitherFunctor;
     _applicative   = applicative;
 }
 private TResult ElseFunction(IUnion <T1, T2, T3, T4> union)
 {
     if (union is Union <T1, T2> )
     {
         return(_u2ElseFunction(union as Union <T1, T2>));
     }
     if (union is Union <T1, T2, T3> )
     {
         return(_u3ElseFunction(union as Union <T1, T2, T3>));
     }
     return(_u4ElseFunction(union as Union <T1, T2, T3, T4>));
 }
Exemple #12
0
        private Option <TResult> DetermineResult(IUnion <T1, T2, T3, T4> union)
        {
            switch (union.Case)
            {
            case Case1: return(_case1Selector.DetermineResult(union.Case1));

            case Case2: return(_case2Selector.DetermineResult(union.Case2));

            case Case3: return(_case3Selector.DetermineResult(union.Case3));

            default: return(_case4Selector.DetermineResult(union.Case4));
            }
        }
Exemple #13
0
        private TResult DetermineResultUsingDefaultIfRequired(IUnion <T1, T2, T3, T4> union)
        {
            switch (union.Case)
            {
            case Case1: return(_case1Selector.DetermineResultUsingDefaultIfRequired(union.Case1));

            case Case2: return(_case2Selector.DetermineResultUsingDefaultIfRequired(union.Case2));

            case Case3: return(_case3Selector.DetermineResultUsingDefaultIfRequired(union.Case3));

            default: return(_case4Selector.DetermineResultUsingDefaultIfRequired(union.Case4));
            }
        }
Exemple #14
0
 public A Apply <T2>(IUnion <TLeft, IFunc <TRight, T2> > app)
     where T2 : class
 {
     return(app.Match(
                (l) => new Factory <A, TLeft>(l).Instance(),
                (r) => _functor
                .Match(
                    (ll) =>
                    new Factory <A, TLeft>(ll).Instance(),
                    (rr) =>
                    new Factory <A, T2>(r.Run(rr)).Instance()
                    )
                ));
 }
Exemple #15
0
        public static TResult Match <T0, TResult>(
            this IUnion <T0> u,
            Func <T0, TResult> f1
            )
            where T0 : class
        {
            var value = u.Value();

            switch (value)
            {
            case T0 variable:
                return(f1(variable));
            }
            throw new Exception("can't match");
        }
Exemple #16
0
 FromUnion <T1, T2, T3>(
     IUnion <T1, T2, T3> union)
     where T1 : notnull
     where T2 : notnull
     where T3 : notnull
 {
     if (union is AnyOf <T1, T2, T3> anyOf)
     {
         return(anyOf);
     }
     else
     {
         return(union.Match <AnyOf <T1, T2, T3> >(
                    v1 => v1,
                    v2 => v2,
                    v3 => v3));
     }
 }
Exemple #17
0
 FromUnion <T1, T2, T3, T4, T5>(
     IUnion <T1, T2, T3, T4, T5> union)
     where T1 : notnull
     where T2 : notnull
     where T3 : notnull
     where T4 : notnull
     where T5 : notnull
 {
     if (union is AnyOf <T1, T2, T3, T4, T5> anyOf)
     {
         return(anyOf);
     }
     else
     {
         return(union.Match <AnyOf <T1, T2, T3, T4, T5> >(
                    v1 => v1,
                    v2 => v2,
                    v3 => v3,
                    v4 => v4,
                    v5 => v5));
     }
 }
Exemple #18
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // Check for null first
            JToken jToken = JToken.Load(reader);

            if (jToken.Type == JTokenType.Null)
            {
                return(null);
            }

            // Cast to an object
            JObject jObject = (JObject)jToken;

            // Try to convert to
            object value = null;

            foreach (Type t in objectType.GenericTypeArguments)
            {
                try
                {
                    value = jObject.ToObject(t);
                    break;
                }
                catch (Exception)
                {
                    // Ignore any failure to cast, we'll throw if we exhaust all the conversion options
                }
            }

            if (value == null)
            {
                throw new InvalidCastException();
            }

            IUnion result = (IUnion)Activator.CreateInstance(objectType);

            result.Value = value;
            return(result);
        }
        public void SparqlBindScope6()
        {
            String query = @"PREFIX : <http://example.org>
SELECT *
WHERE
{
  {
    GRAPH ?g { :s :p ?o }
    BIND (?g AS ?in)
  }
  UNION
  {
    :s :p ?o .
    BIND('default' AS ?in)
  }
}";

            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

            Console.WriteLine(q.ToString());

            ISparqlAlgebra algebra = q.ToAlgebra();

            Console.WriteLine(algebra.ToString());
            Assert.IsInstanceOfType(algebra, typeof(Select));

            algebra = ((IUnaryOperator)algebra).InnerAlgebra;
            Assert.IsInstanceOfType(algebra, typeof(Union));

            IUnion         union = (Union)algebra;
            ISparqlAlgebra lhs   = union.Lhs;

            Assert.IsInstanceOfType(lhs, typeof(Extend));

            ISparqlAlgebra rhs = union.Rhs;

            Assert.IsInstanceOfType(rhs, typeof(Join));
        }
Exemple #20
0
 /// <summary>
 /// Optimises an Algebra to a form that uses <see cref="AskBgp">AskBgp</see> where possible
 /// </summary>
 /// <param name="algebra">Algebra</param>
 /// <param name="depth">Depth</param>
 /// <returns></returns>
 /// <remarks>
 /// <para>
 /// By transforming a query to use <see cref="AskBgp">AskBgp</see> we can achieve much more efficient processing of some forms of queries
 /// </para>
 /// </remarks>
 protected override ISparqlAlgebra OptimiseInternal(ISparqlAlgebra algebra, int depth)
 {
     try
     {
         ISparqlAlgebra temp;
         if (algebra is Bgp)
         {
             //Bgp is transformed into AskBgp
             //This tries to find 1 possible solution
             temp = new AskBgp(((Bgp)algebra).TriplePatterns);
         }
         else if (algebra is ILeftJoin)
         {
             //LeftJoin is transformed to just be the LHS as the RHS is irrelevant for ASK queries
             //UNLESS the LeftJoin occurs inside a Filter/Minus BUT we should never get called to transform a
             //LeftJoin() for those branches of the algebra as the Optimiseer does not transform
             //Filter()/Minus() operators
             temp = this.OptimiseInternal(((ILeftJoin)algebra).Lhs, depth + 1);
         }
         else if (algebra is IUnion)
         {
             IUnion join = (IUnion)algebra;
             temp = new AskUnion(this.OptimiseInternal(join.Lhs, depth + 1), this.OptimiseInternal(join.Rhs, depth + 1));
         }
         else if (algebra is IJoin)
         {
             IJoin join = (IJoin)algebra;
             if (join.Lhs.Variables.IsDisjoint(join.Rhs.Variables))
             {
                 //If the sides of the Join are disjoint then can fully transform the join since we only need to find at least
                 //one solution on either side in order for the query to match
                 //temp = new Join(this.OptimiseInternal(join.Lhs, depth + 1), this.OptimiseInternal(join.Rhs, depth + 1));
                 temp = join.Transform(this);
             }
             else
             {
                 //If the sides are not disjoint then the LHS must be fully evaluated but the RHS need only produce at least
                 //one solution based on the full input from the LHS for the query to match
                 //temp = new Join(join.Lhs, this.OptimiseInternal(join.Rhs, depth + 1));
                 temp = join.TransformRhs(this);
             }
         }
         else if (algebra is Algebra.Graph)
         {
             //Algebra.Graph g = (Algebra.Graph)algebra;
             //temp = new Algebra.Graph(this.OptimiseInternal(g.InnerAlgebra, depth + 1), g.GraphSpecifier);
             IUnaryOperator op = (IUnaryOperator)algebra;
             temp = op.Transform(this);
         }
         else
         {
             temp = algebra;
         }
         return(temp);
     }
     catch
     {
         //If the Optimise fails return the current algebra
         return(algebra);
     }
 }
Exemple #21
0
 public IAcceptWord UnionNea(IUnion A) => base.Union(A);
Exemple #22
0
 public override IAcceptWord Union(IUnion A) => ProductDea(this, (DFA)A, eProductDeaMode.Union);
Exemple #23
0
 public IAcceptWord Union(IUnion automat) => JoinConcatUnion(automat, JoinConcatUnionKind.Union);
Exemple #24
0
 public TParent(IUnion <TLeft, TRight> union)
 {
     _union = union;
 }
Exemple #25
0
 public TParent(IUnion <TLeft, TRight> functor)
 {
     _functor = functor;
 }
Exemple #26
0
        internal void ExecUsingElse(IUnion <T1, T2, T3, T4> union)
        {
            var possibleResult = DetermineResult(union);

            Ignore(possibleResult.HasValue ? possibleResult.Value : ElseFunction(union));
        }
Exemple #27
0
 internal void ExecNoElse(IUnion <T1, T2, T3, T4> union) =>
 DetermineResultUsingDefaultIfRequired(union);
Exemple #28
0
 public IAcceptWord Union(IUnion cfg) => Combine(cfg, new string[] { "{0}", "{1}" }, "union");
Exemple #29
0
 /// <summary>
 /// Processes a Union.
 /// </summary>
 /// <param name="union">Union.</param>
 /// <param name="context">SPARQL Evaluation Context.</param>
 public override BaseMultiset ProcessUnion(IUnion union, SparqlEvaluationContext context)
 {
     return(ExplainAndEvaluate <IUnion>(union, context, base.ProcessUnion));
 }
Exemple #30
0
        internal TResult ResultUsingElse(IUnion <T1, T2, T3, T4> union)
        {
            var possibleResult = DetermineResult(union);

            return(possibleResult.HasValue ? possibleResult.Value : ElseFunction(union));
        }