Esempio n. 1
0
 /// <summary>
 /// Gets an enumeration of dynamic objects, where each is bound to the data attached to each result in the provided SPARQL query.
 /// </summary>
 /// <param name="sparqlExpression">The sparql query that indentifies the objects to return.</param>
 /// <returns>A collection of dynamic objects bound to the SPARQL result.</returns>
 public IEnumerable <dynamic> BindObjectsWithSparql(string sparqlExpression)
 {
     return(_store.BindDataObjectsWithSparql(sparqlExpression).Select(dataObject => new BrightstarDynamicObject(dataObject)).Cast <dynamic>());
 }
Esempio n. 2
0
        /// <summary>
        /// Executes a SPARQL query against the underlying store and binds the results to
        /// domain context objects
        /// </summary>
        /// <typeparam name="T">The type of domain context object to bind to</typeparam>
        /// <param name="sparqlQueryContext">The query to be executed</param>
        /// <returns>An enumeration over the bound objects</returns>
        /// <remarks>The SPARQL query should be written to return a single variable binding.
        /// Results that bind the variable to anything other than a resource URI are ignored.</remarks>
        public override IEnumerable <T> ExecuteQuery <T>(SparqlQueryContext sparqlQueryContext)
        {
            var bindType = GetImplType(typeof(T));

            if (typeof(BrightstarEntityObject).IsAssignableFrom(bindType))
            {
                foreach (var dataObject in _store.BindDataObjectsWithSparql(sparqlQueryContext.SparqlQuery))
                {
                    yield return(BindDataObject <T>(dataObject, bindType));
                }
            }
            else if (IsAnonymousType(typeof(T)))
            {
                var anonymousConstructorArgs = new List <AnonymousConstructorArg>();
                foreach (var tuple in sparqlQueryContext.AnonymousMembersMap)
                {
                    var propertyInfo = typeof(T).GetProperty(tuple.Item1);
                    if (propertyInfo == null)
                    {
                        throw new EntityFrameworkException("No property named '{0}' on anonymous type", tuple.Item1);
                    }
                    var propertyType = propertyInfo.PropertyType;
                    var converter    = GetStringConverter(propertyType);
                    if (converter == null)
                    {
                        throw new EntityFrameworkException("No converter available for type '{0}'", propertyType.FullName);
                    }
                    object defaultValue = propertyType.IsValueType ? Activator.CreateInstance(propertyType) : null;
                    anonymousConstructorArgs.Add(new AnonymousConstructorArg
                    {
                        PropertyName   = tuple.Item1,
                        VariableName   = tuple.Item2,
                        ValueConverter = converter,
                        DefaultValue   = defaultValue
                    });
                }

                var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery);
                var resultDoc    = sparqlResult.ResultDocument;
                foreach (var row in resultDoc.SparqlResultRows())
                {
                    var args = new object[anonymousConstructorArgs.Count];
                    for (int i = 0; i < anonymousConstructorArgs.Count; i++)
                    {
                        var argInfo  = anonymousConstructorArgs[i];
                        var colValue = row.GetColumnValue(argInfo.VariableName);
                        var colLang  = row.GetLiteralLanguageCode(argInfo.VariableName);
                        args[i] = colValue == null ? argInfo.DefaultValue : argInfo.ValueConverter(colValue.ToString(), colLang);
                    }
                    yield return((T)Activator.CreateInstance(typeof(T), args));
                }
            }
            else if (sparqlQueryContext.HasMemberInitExpression)
            {
                var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery);
                var resultDoc    = sparqlResult.ResultDocument;
                foreach (var row in resultDoc.SparqlResultRows())
                {
                    var values = new Dictionary <string, object>();
                    foreach (var c in resultDoc.GetVariableNames())
                    {
                        values[c] = row.GetColumnValue(c);
                    }
                    var value = sparqlQueryContext.ApplyMemberInitExpression <T>(values, ConvertString);
                    yield return((T)value);
                }
            }
            else
            {
                var sparqlResult = _store.ExecuteSparql(sparqlQueryContext.SparqlQuery);
                var resultDoc    = sparqlResult.ResultDocument;
                var converter    = GetStringConverter(typeof(T));
                if (converter == null)
                {
                    throw new EntityFrameworkException("No SPARQL results conversion found from string to type '{0}'", typeof(T).FullName);
                }
                foreach (var row in resultDoc.SparqlResultRows())
                {
                    var value = row.GetColumnValue(0);
                    if (value.GetType() == typeof(T))
                    {
                        yield return((T)value);
                    }
                    else
                    {
                        yield return((T)converter(row.GetColumnValue(0).ToString(), row.GetLiteralLanguageCode(0)));
                    }
                }
            }
        }