Exemplo n.º 1
0
        /// <summary>
        /// Gets the Text of the SPARQL Update Command which will be used to Save the Object to the underlying Store
        /// </summary>
        /// <param name="oc">Object</param>
        /// <param name="graphUri">URI of the Graph to save to</param>
        /// <returns></returns>
        public String GetSaveCommandText(OwlInstanceSupertype oc, String graphUri)
        {
            if (oc == null)
            {
                throw new ArgumentNullException("oc", "Cannot persist a Null Object");
            }

            Type t = oc.GetType();

            PropertyInfo[] propInfo = t.GetProperties();

            //Start building an INSERT DATA command
            StringBuilder persistCommand = new StringBuilder();

            persistCommand.AppendLine("INSERT DATA {");

            //Need to add a GRAPH clause if a Graph URI has been provided
            if (!String.IsNullOrEmpty(graphUri))
            {
                persistCommand.Append("GRAPH <");
                persistCommand.Append(this._formatter.FormatUri(graphUri));
                persistCommand.AppendLine("> {");
            }

            //Assert the Type Triple
            persistCommand.Append(this.FormatAsUri(oc.InstanceUri));
            persistCommand.Append(" a ");
            persistCommand.Append(this.FormatAsUri(OwlClassSupertype.GetOwlClassUri(t)));
            persistCommand.AppendLine(" .");

            //Assert Triples for annotated properties
            Graph g = new Graph();

            foreach (PropertyInfo info in propInfo)
            {
                this.GetTripleText(oc, info, g, persistCommand);
            }

            //Need to append an extra } to close the GRAPH clause if using one
            if (!String.IsNullOrEmpty(graphUri))
            {
                persistCommand.AppendLine("}");
            }

            //Close the INSERT DATA command
            persistCommand.AppendLine("}");

            return(persistCommand.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get those properties that were adorned by the <see cref="OwlResourceAttribute"/>.
        /// If there are none, then just get every property (and let later processors take their chances).
        /// </summary>
        /// <param name="originalType">the original type that the query was made against.</param>
        /// <param name="instanceType">the type of the object that must be returned.</param>
        /// <returns></returns>
        private IEnumerable <MemberInfo> GetPropertiesToPopulate(Type originalType, Type instanceType)
        {
            IEnumerable <MemberInfo> props;

            if (originalType == instanceType)
            //  i.e. identity projection, meaning we can use GetAllPersistentProperties safely
            {
                props = OwlClassSupertype.GetAllPersistentProperties(OriginalType);
            }
            else
            {
                props = instanceType.GetProperties();
            }
            return(props);
        }
Exemplo n.º 3
0
 public static void Add(this IInMemoryQueryableStore ms, OwlInstanceSupertype oc)
 {
     using (var ls = new LoggingScope("MemoryStoreExtensions.Add"))
     {
         Type t = oc.GetType();
         Console.WriteLine(oc.InstanceUri);
         PropertyInfo[] pia = t.GetProperties();
         IGraph         g   = ms.GetDefaultGraph();
         g.Assert(g.CreateUriNode(new Uri(oc.InstanceUri)), g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)), g.CreateUriNode(new Uri(OwlClassSupertype.GetOwlClassUri(t))));
         foreach (PropertyInfo pi in pia)
         {
             if (pi.IsOntologyResource())
             {
                 AddPropertyToStore(oc, pi, ms);
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the Text of the SPARQL Update Command which will be used to Delete the Object from the underlying Store
        /// </summary>
        /// <param name="oc">Object</param>
        /// <param name="graphUri">URI of the Graph to delete from</param>
        /// <param name="mode">Delete Mode</param>
        /// <returns></returns>
        public String GetDeletionCommandText(OwlInstanceSupertype oc, String graphUri, LinqDeleteMode mode)
        {
            if (oc == null)
            {
                throw new ArgumentNullException("oc", "Cannot delete a Null Object");
            }

            StringBuilder persistCommand = new StringBuilder();

            switch (mode)
            {
            case LinqDeleteMode.DeleteAll:
                //Delete all the Triples with this Object's Instance URI as the Subject/Object

                //Start building a pair of DELETE WHERE commands
                persistCommand.AppendLine("DELETE WHERE {");

                //Need to add a GRAPH clause if a Graph URI has been provided
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.Append("GRAPH <");
                    persistCommand.Append(this._formatter.FormatUri(graphUri));
                    persistCommand.AppendLine("> {");
                }

                persistCommand.Append(this.FormatAsUri(oc.InstanceUri));
                persistCommand.AppendLine(" ?p ?o .");

                //Need to append an extra } to close the GRAPH clause if using one
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.AppendLine("}");
                }

                //Close the first DELETE WHERE command
                persistCommand.AppendLine("} ;");

                //Create the second DELETE WHERE command
                persistCommand.AppendLine("DELETE WHERE {");

                //Need to add a GRAPH clause if a Graph URI has been provided
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.Append("GRAPH <");
                    persistCommand.Append(this._formatter.FormatUri(graphUri));
                    persistCommand.AppendLine("> {");
                }

                persistCommand.Append("?s ?p ");
                persistCommand.AppendLine(this.FormatAsUri(oc.InstanceUri) + " .");

                //Need to append an extra } to close the GRAPH clause if using one
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.AppendLine("}");
                }

                //Close the second DELETE WHERE command
                persistCommand.AppendLine("} ;");

                return(persistCommand.ToString());

            case LinqDeleteMode.DeleteValues:
                //Delete the specific Values associated with this Object

                Type           t        = oc.GetType();
                PropertyInfo[] propInfo = t.GetProperties();

                //Start building an DELETE DATA command
                persistCommand.AppendLine("DELETE DATA {");

                //Need to add a GRAPH clause if a Graph URI has been provided
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.Append("GRAPH <");
                    persistCommand.Append(this._formatter.FormatUri(graphUri));
                    persistCommand.AppendLine("> {");
                }

                //Assert the Type Triple
                persistCommand.Append(this.FormatAsUri(oc.InstanceUri));
                persistCommand.Append(" a ");
                persistCommand.Append(this.FormatAsUri(OwlClassSupertype.GetOwlClassUri(t)));
                persistCommand.AppendLine(" .");

                //Assert Triples for annotated properties
                Graph g = new Graph();
                foreach (PropertyInfo info in propInfo)
                {
                    this.GetTripleText(oc, info, g, persistCommand);
                }

                //Need to append an extra } to close the GRAPH clause if using one
                if (!String.IsNullOrEmpty(graphUri))
                {
                    persistCommand.AppendLine("}");
                }

                //Close the DELETE DATA command
                persistCommand.AppendLine("}");

                return(persistCommand.ToString());

            default:
                throw new LinqToRdfException("Not a valid Linq Delete Mode");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Builds the projection function (<see cref="ProjectionFunction"/>) and extracts the arguments to it
        /// for use elsewhere.
        /// </summary>
        /// <param name="expression">The Select expression.</param>
        protected void BuildProjection(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var ue = ((MethodCallExpression)expression).Arguments[1] as UnaryExpression;

            if (ue == null)
            {
                throw new LinqToRdfException("Incompatible expression type found when building ontology projection");
            }

            var projectionFunctionExpression = (LambdaExpression)ue.Operand;

            if (projectionFunctionExpression == null)
            {
                throw new LinqToRdfException("Incompatible expression type found when building ontology projection");
            }

            if (Expressions.ContainsKey("GroupBy"))
            {
                throw new NotSupportedException("Group By is not supported by VDS.RDF.Linq");
            }

            // compile the projection's lambda expression into a function that can be used to instantiate new objects
            ProjectionFunction = projectionFunctionExpression.Compile();

            // work out what kind of project is being performed (identity, member or anonymous type)
            // identity projection is from queries like this: "from a in ctx select a"
            // member projection from ones like this "from a in ctx select a.b"
            // anonymous type projection from this "from a in ctx select new {a.b, a.b}"
            if (projectionFunctionExpression.Body is ParameterExpression) //  ie an identity projection
            {
                foreach (PropertyInfo i in OwlClassSupertype.GetAllPersistentProperties(OriginalType))
                {
                    ProjectionParameters.Add(i);
                }
            }
            else if (projectionFunctionExpression.Body is MemberExpression) // a member projection
            {
                var memex = projectionFunctionExpression.Body as MemberExpression;
                if (memex == null)
                {
                    throw new LinqToRdfException("Expected MemberExpression was null");
                }

                ProjectionParameters.Add(memex.Member);
            }
            else if (projectionFunctionExpression.Body is NewExpression)
            {
                // create an anonymous type
                var mie = projectionFunctionExpression.Body as NewExpression;
                if (mie == null)
                {
                    throw new LinqToRdfException("Expected NewExpression was not present");
                }

                foreach (MemberExpression me in mie.Arguments)
                {
                    ProjectionParameters.Add(me.Member);
                }
            }
            else if (projectionFunctionExpression.Body is MethodCallExpression)
            {
                throw new NotSupportedException("Calling a method on the selected variable is not supported - use the Select() extension method on the results of the query to achieve this");
            }
            else
            {
                throw new LinqToRdfException("The Projection used in this LINQ expression is not executable by VDS.RDF.Linq");
            }
        }