예제 #1
0
        public T ExecuteScalar <T>(QueryModel queryModel)
        {
            Type t = typeof(T);

            if (t == typeof(bool))
            {
                // Generate and execute ASK query.
                SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new AskQueryGenerator());
                visitor.VisitQueryModel(queryModel);

                ISparqlQuery       query  = visitor.GetQuery();
                ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled);

                return(new object[] { result.GetAnwser() }.OfType <T>().First());
            }
            else if (queryModel.ResultOperators.Any(o => o is CountResultOperator))
            {
                SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectBindingsQueryGenerator());
                visitor.VisitQueryModel(queryModel);

                ISparqlQuery       query  = visitor.GetQuery();
                ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled);

                BindingSet b = result.GetBindings().FirstOrDefault();

                if (b != null && b.Any())
                {
                    return(new object[] { b.First().Value }.OfType <T>().First());
                }
                else
                {
                    return(new object[] { 0 }.OfType <T>().First());
                }
            }
            else
            {
                // Unknown scalar type.
                throw new NotImplementedException();
            }
        }
예제 #2
0
        public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel)
        {
            Type t = queryModel.SelectClause.Selector.Type;

            if (typeof(Resource).IsAssignableFrom(t))
            {
                // Handle queries which return instances of resources.
                SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectTriplesQueryGenerator());
                visitor.VisitQueryModel(queryModel);

                MethodInfo getResources = _getResourceMethod.MakeGenericMethod(typeof(T));
                object[]   args         = new object[] { visitor.GetQuery(), _inferenceEnabled, null };

                foreach (T value in getResources.Invoke(Model, args) as IEnumerable <T> )
                {
                    yield return(value);
                }
            }
            else
            {
                // Handle queries which return value type objects.
                SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectBindingsQueryGenerator());
                visitor.VisitQueryModel(queryModel);

                ISparqlQuery       query  = visitor.GetQuery();
                ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled);

                // TODO: This works correctly for single bindings, check with multiple bindings.
                foreach (BindingSet bindings in result.GetBindings())
                {
                    foreach (var value in bindings.Values.OfType <T>())
                    {
                        yield return(value);
                    }
                }
            }
        }