コード例 #1
0
        /// <summary>
        /// Selects Statements that match a given Template
        /// </summary>
        /// <param name="template">Statement Template</param>
        /// <param name="sink">Sink</param>
        public void Select(Statement template, StatementSink sink)
        {
            //Implement as a SPARQL SELECT
            List <ITriplePattern> patterns = this.TemplateToTriplePatterns(template);
            StringBuilder         query    = new StringBuilder();

            query.AppendLine("SELECT * WHERE {");
            foreach (ITriplePattern pattern in patterns)
            {
                query.AppendLine(pattern.ToString() + ".");
            }
            query.AppendLine("}");

            //Get the Results
            Object results = this._store.ExecuteQuery(query.ToString());

            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                foreach (SparqlResult r in rset)
                {
                    Entity    s    = (template.Subject != null) ? template.Subject : SemWebConverter.ToSemWebEntity(r["s"], this._mapping);
                    Entity    p    = (template.Predicate != null) ? template.Predicate : SemWebConverter.ToSemWebEntity(r["p"], this._mapping);
                    Resource  o    = (template.Object != null) ? template.Object : SemWebConverter.ToSemWeb(r["o"], this._mapping);
                    Statement stmt = new Statement(s, p, o);
                    //Keep returning stuff until the sink tells us to stop
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Helper method which converts a SemWeb resource into a PatternItem for use in a SPARQL Triple Pattern
 /// </summary>
 /// <param name="r">Resource</param>
 /// <param name="mapping">Mapping of Variables &amp; Blank Nodes to Pattern Items</param>
 /// <returns></returns>
 private PatternItem FromSemWeb(Resource r, Dictionary <String, PatternItem> mapping)
 {
     if (r is Variable)
     {
         if (mapping.ContainsKey(r.ToString()))
         {
             return(mapping[r.ToString()]);
         }
         else
         {
             PatternItem temp = new VariablePattern(r.ToString());
             mapping.Add(r.ToString(), temp);
             return(temp);
         }
     }
     else if (r is BNode)
     {
         if (mapping.ContainsKey(r.ToString()))
         {
             return(mapping[r.ToString()]);
         }
         else
         {
             PatternItem temp = new BlankNodePattern(r.ToString().Substring(2));
             mapping.Add(r.ToString(), temp);
             return(temp);
         }
     }
     else
     {
         return(new NodeMatchPattern(SemWebConverter.FromSemWeb(r, this._mapping)));
     }
 }
コード例 #3
0
        /// <summary>
        /// Selects all statements from this source and streams them into the given Sink
        /// </summary>
        /// <param name="sink">Statement Sink</param>
        public void Select(StatementSink sink)
        {
            foreach (IGraph g in this._store.Graphs)
            {
                //Get the Hash Code of the Graphs URI and create a new empty mapping if necessary
                Entity graphUri;
                int    hash;
                if (g.BaseUri == null)
                {
                    graphUri = new Entity(GraphCollection.DefaultGraphUri);
                    hash     = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
                }
                else
                {
                    graphUri = new Entity(g.BaseUri.ToString());
                    hash     = g.BaseUri.GetEnhancedHashCode();
                }

                SemWebMapping mapping = this.GetMapping(hash, g);
                foreach (Triple t in g.Triples)
                {
                    Statement stmt = SemWebConverter.ToSemWeb(t, mapping);
                    stmt.Meta = graphUri;
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Adds a Statement to the Sink
        /// </summary>
        /// <param name="statement">Statement</param>
        /// <returns></returns>
        public bool Add(Statement statement)
        {
            Triple t = SemWebConverter.FromSemWeb(statement, this._mapping);

            this._triples.Add(t);
            return(true);
        }
コード例 #5
0
 /// <summary>
 /// Selects all statements from this source and streams them into the given Sink
 /// </summary>
 /// <param name="sink">Statement Sink</param>
 /// <remarks>
 /// This is essentially the same code as the <see cref="SemWebConverter.ToSemWeb">ToSemWeb(IGraph g, StatementSink sink)</see> function but we need to maintain a consistent mapping of BNodes for the source
 /// </remarks>
 public void Select(StatementSink sink)
 {
     foreach (Triple t in this._g.Triples)
     {
         Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
         if (!sink.Add(stmt))
         {
             return;
         }
     }
 }
コード例 #6
0
 /// <summary>
 /// Checks whether the Triple exists in the Collection
 /// </summary>
 /// <param name="t">Triple to check for</param>
 /// <returns></returns>
 /// <exception cref="RdfStorageException">Thrown if the underlying StatementSource is not a SelectableSource</exception>
 public override bool Contains(Triple t)
 {
     if (this._source is SelectableSource)
     {
         Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
         return(((SelectableSource)this._source).Contains(stmt));
     }
     else
     {
         throw new RdfStorageException("The underlying StatementSource does not support the Contains() operation");
     }
 }
コード例 #7
0
 /// <summary>
 /// Removes a Triple from the underlying SemWeb StatementSource if the Source is modifiable
 /// </summary>
 /// <param name="t">Triple to remove</param>
 /// <exception cref="RdfStorageException">Thrown if the underlying SemWeb data source is not modifiable</exception>
 protected override void Delete(Triple t)
 {
     if (this._source is ModifiableSource)
     {
         Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
         ((ModifiableSource)this._source).Remove(stmt);
     }
     else
     {
         throw new RdfStorageException("The underlying StatementSource does not support the Remove() operation");
     }
 }
コード例 #8
0
 /// <summary>
 /// Gets the Enumerator for this Triple Collection
 /// </summary>
 /// <returns></returns>
 public override IEnumerator <Triple> GetEnumerator()
 {
     if (this._source is IEnumerable <Statement> )
     {
         IEnumerable <Statement> stmts = (IEnumerable <Statement>) this._source;
         return((from stmt in stmts
                 select SemWebConverter.FromSemWeb(stmt, this._mapping)).GetEnumerator());
     }
     else
     {
         throw new RdfStorageException("Underlying StatementSource does not support enumeration");
     }
 }
コード例 #9
0
 /// <summary>
 /// Gets all the Triples with a specific Object
 /// </summary>
 /// <param name="obj">Object</param>
 /// <returns></returns>
 public override IEnumerable <Triple> WithObject(INode obj)
 {
     if (this._source is SelectableSource)
     {
         EnumerationSink sink = new EnumerationSink(this._mapping);
         Statement       stmt = new Statement(null, null, SemWebConverter.ToSemWeb(obj, this._mapping));
         ((SelectableSource)this._source).Select(stmt, sink);
         return(sink);
     }
     else
     {
         return(base.WithObject(obj));
     }
 }
コード例 #10
0
 /// <summary>
 /// Gets all the Triples with a specific Subject and Predicate
 /// </summary>
 /// <param name="subj">Subject</param>
 /// <param name="pred">Predicate</param>
 /// <returns></returns>
 public override IEnumerable <Triple> WithSubjectPredicate(INode subj, INode pred)
 {
     if (this._source is SelectableSource)
     {
         EnumerationSink sink = new EnumerationSink(this._mapping);
         Statement       stmt = new Statement(SemWebConverter.ToSemWebEntity(subj, this._mapping), SemWebConverter.ToSemWebEntity(pred, this._mapping), null);
         ((SelectableSource)this._source).Select(stmt, sink);
         return(sink);
     }
     else
     {
         return(base.WithSubjectPredicate(subj, pred));
     }
 }
コード例 #11
0
        /// <summary>
        /// Selects Statements from the Source based on a Template
        /// </summary>
        /// <param name="template">Statement Template</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(Statement template, StatementSink sink)
        {
            //Convert Template to an Enumerable
            IEnumerable <Triple> ts = this.TemplateToEnumerable(template);

            foreach (Triple t in ts)
            {
                //Keep streaming Triples until the sink tells us to stop
                Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
                if (!sink.Add(stmt))
                {
                    return;
                }
            }
        }
コード例 #12
0
        /// <summary>
        /// Returns whether the Source contains any Statement which contains the given Resource
        /// </summary>
        /// <param name="resource">Resource</param>
        /// <returns></returns>
        public bool Contains(Resource resource)
        {
            //First we need to convert the Resource to a Node
            INode n = SemWebConverter.FromSemWeb(resource, this._mapping);

            if (this._g.Nodes.Contains(n))
            {
                //If it's in the Node collection check that a Triple with the given Node exists
                return(this._g.Triples.Any(t => t.Involves(n)));
            }
            else
            {
                //If it's not in the Node collection it can't be in the Graph
                return(false);
            }
        }
コード例 #13
0
        /// <summary>
        /// Executes a Graph Pattern style query against the Source
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// This is implemented by transforming the Graph Pattern which is a set of SemWeb Statement templates into a SPARQL Algebra BGP.  The resulting algebra is then executed using the Leviathan engine and the results converted into VariableBindings for SemWeb
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            ISparqlAlgebra algebra = this.ToAlgebra(graph);

            if (this._store == null)
            {
                this._store = new TripleStore();
                this._store.Add(this._g);
            }

            SparqlEvaluationContext context = new SparqlEvaluationContext(null, new InMemoryDataset(this._store));
            BaseMultiset            results = context.Evaluate(algebra);//algebra.Evaluate(context);

            sink.Init(results.Variables.Select(v => new Variable(v)).ToArray());
            if (results.Count > 0)
            {
                int c = 0;
                foreach (ISet s in results.Sets)
                {
                    //Apply Limit if applicable
                    if (options.Limit > 0 && c >= options.Limit)
                    {
                        sink.Finished();
                        return;
                    }

                    //Convert the Set to VariableBindings for SemWeb
                    Variable[] vars      = s.Variables.Select(v => new Variable(v)).ToArray();
                    Resource[] resources = s.Variables.Select(v => SemWebConverter.ToSemWeb(s[v], this._mapping)).ToArray();
                    SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                    //Keep adding results until the sink tells us to stop
                    if (!sink.Add(bindings))
                    {
                        sink.Finished();
                        return;
                    }
                    c++;
                }
                sink.Finished();
            }
            else
            {
                sink.Finished();
            }
        }
コード例 #14
0
        /// <summary>
        /// Adds a statement to this Source
        /// </summary>
        /// <param name="statement">Statement</param>
        /// <returns></returns>
        public bool Add(Statement statement)
        {
            IGraph g;
            Uri    graphUri;
            int    hash;

            //Set the Graph URI based on the Statement Meta field
            if (statement.Meta != Statement.DefaultMeta && statement.Meta.Uri != null)
            {
                if (statement.Meta.Uri.Equals(GraphCollection.DefaultGraphUri))
                {
                    graphUri = null;
                    hash     = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
                }
                else
                {
                    graphUri = new Uri(statement.Meta.Uri);
                    hash     = graphUri.GetEnhancedHashCode();
                }
            }
            else
            {
                graphUri = null;
                hash     = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
            }
            if (!this._store.HasGraph(graphUri))
            {
                g = new Graph();
                if (graphUri != null)
                {
                    g.BaseUri = graphUri;
                }
                this._store.Add(g);
            }
            else
            {
                g = this._store.Graph(graphUri);
            }

            //Assert into the appropriate Graph
            g.Assert(SemWebConverter.FromSemWeb(statement, this.GetMapping(hash, g)));

            //We never ask the sink to stop streaming statements so we always return true
            return(true);
        }
コード例 #15
0
        /// <summary>
        /// Selects all Statements from the underlying Store and adds them to a SemWeb Sink
        /// </summary>
        /// <param name="sink">Statement Sink</param>
        public void Select(StatementSink sink)
        {
            //Use a CONSTRUCT to get the Statements
            String query   = "CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}";
            Object results = this._store.ExecuteQuery(query);

            if (results is Graph)
            {
                Graph g = (Graph)results;
                foreach (Triple t in g.Triples)
                {
                    Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
                    //Keep returning stuff until it tells us to stop
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                }
            }
        }
コード例 #16
0
        /// <summary>
        /// Returns whether the Source contains any Statement which contains the given Resource
        /// </summary>
        /// <param name="resource">Resource</param>
        /// <returns></returns>
        public bool Contains(Resource resource)
        {
            //Have to check against each Graph until we find a match
            foreach (IGraph g in this._store.Graphs)
            {
                //First we need to convert the Resource to a Node
                INode n = SemWebConverter.FromSemWeb(resource, this.GetMapping(g));

                if (g.Nodes.Contains(n))
                {
                    //If it's in the Node collection check that a Triple with the given Node exists
                    if (g.Triples.Any(t => t.Involves(n)))
                    {
                        return(true);
                    }
                }
            }

            //If none of the Graphs contained it we return false
            return(false);
        }
コード例 #17
0
        private IEnumerable <Triple> FilterToEnumerable(SelectFilter filter, IGraph g)
        {
            //Want to build an IEnumerable based on the Filter
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();
            INode s, p, o;
            int   hash = (g.BaseUri == null) ? new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode() : g.BaseUri.GetEnhancedHashCode();

            if (filter.Subjects != null)
            {
                if (filter.Predicates != null)
                {
                    //Subject-Predicate filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this.GetMapping(hash, g));
                        foreach (Entity pred in filter.Predicates)
                        {
                            p  = SemWebConverter.FromSemWeb(pred, this.GetMapping(hash, g));
                            ts = ts.Concat(g.GetTriplesWithSubjectPredicate(s, p));
                        }
                    }
                }
                else if (filter.Objects != null)
                {
                    //Subject-Object filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this.GetMapping(hash, g));
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this.GetMapping(hash, g));
                            ts = ts.Concat(g.GetTriplesWithSubjectObject(s, o));
                        }
                    }
                }
                else
                {
                    //Subjects filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s  = SemWebConverter.FromSemWeb(subj, this.GetMapping(hash, g));
                        ts = ts.Concat(g.GetTriplesWithSubject(s));
                    }
                }
            }
            else if (filter.Predicates != null)
            {
                if (filter.Objects != null)
                {
                    //Predicate-Object Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p = SemWebConverter.FromSemWeb(pred, this.GetMapping(hash, g));
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this.GetMapping(hash, g));
                            ts = ts.Concat(g.GetTriplesWithPredicateObject(p, o));
                        }
                    }
                }
                else
                {
                    //Predicate Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p  = SemWebConverter.FromSemWeb(pred, this.GetMapping(hash, g));
                        ts = ts.Concat(g.GetTriplesWithPredicate(p));
                    }
                }
            }
            else if (filter.Objects != null)
            {
                //Object Filter
                foreach (Resource obj in filter.Objects)
                {
                    o  = SemWebConverter.FromSemWeb(obj, this.GetMapping(hash, g));
                    ts = ts.Concat(g.GetTriplesWithObject(o));
                }
            }
            else
            {
                //Everything is null so this is a Select All
                ts = g.Triples;
            }

            return(ts);
        }
コード例 #18
0
        /// <summary>
        /// Adds a Triple to the Triple Collection
        /// </summary>
        /// <param name="t">Triple to add</param>
        protected override void Add(Triple t)
        {
            Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);

            this._sink.Add(stmt);
        }
コード例 #19
0
        /// <summary>
        /// Queries the Store using the Graph Pattern specified by the set of Statement Patterns
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// Implemented by converting the Statement Patterns into a SPARQL SELECT query and executing that against the underlying Store's SPARQL engine
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            //Implement as a SPARQL SELECT
            SparqlParameterizedString queryString = new SparqlParameterizedString();

            queryString.CommandText = "SELECT * WHERE {";

            int p = 0;

            foreach (Statement stmt in graph)
            {
                //Add Subject
                queryString.CommandText += "\n";
                if (stmt.Subject is Variable)
                {
                    queryString.CommandText += stmt.Subject.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Subject, this._mapping));
                    p++;
                }
                queryString.CommandText += " ";

                //Add Predicate
                if (stmt.Predicate is Variable)
                {
                    queryString.CommandText += stmt.Predicate.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Predicate, this._mapping));
                    p++;
                }
                queryString.CommandText += " ";

                //Add Object
                if (stmt.Object is Variable)
                {
                    queryString.CommandText += stmt.Object.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Object, this._mapping));
                    p++;
                }
                queryString.CommandText += " .";
            }

            queryString.CommandText += "}";

            //Execute the Query and convert the Results
            Object results = this._store.ExecuteQuery(queryString.ToString());

            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                sink.Init(rset.Variables.Select(v => new Variable(v)).ToArray());
                if (rset.Count > 0)
                {
                    int c = 0;
                    foreach (SparqlResult r in rset)
                    {
                        //Apply Limit if applicable
                        if (options.Limit > 0 && c >= options.Limit)
                        {
                            sink.Finished();
                            return;
                        }

                        //Convert the Set to VariableBindings for SemWeb
                        Variable[] vars      = r.Variables.Select(v => new Variable(v)).ToArray();
                        Resource[] resources = r.Variables.Select(v => SemWebConverter.ToSemWeb(r[v], this._mapping)).ToArray();
                        SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                        //Keep adding results until the sink tells us to stop
                        if (!sink.Add(bindings))
                        {
                            sink.Finished();
                            return;
                        }
                        c++;
                    }
                    sink.Finished();
                }
                else
                {
                    sink.Finished();
                }
            }
            else
            {
                throw new RdfQueryException("Query returned an unexpected result where a SPARQL Result Set was expected");
            }
        }
コード例 #20
0
        /// <summary>
        /// Selects Statements from the Source based on a Template
        /// </summary>
        /// <param name="template">Statement Template</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(Statement template, StatementSink sink)
        {
            //Convert Template to an Enumerable
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();
            int hash;

            if (template.Meta != Statement.DefaultMeta && template.Meta != null)
            {
                //Select from the specific Graph if it exists
                Uri graphUri;
                if (template.Meta.Uri == null)
                {
                    hash     = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
                    graphUri = null;
                }
                else
                {
                    graphUri = new Uri(template.Meta.Uri);
                    hash     = graphUri.GetEnhancedHashCode();
                }
                if (this._store.HasGraph(graphUri))
                {
                    ts = this.TemplateToEnumerable(template, this._store.Graph(graphUri));
                    SemWebMapping mapping = this.GetMapping(hash, this._store.Graph(graphUri));

                    foreach (Triple t in ts)
                    {
                        //Keep streaming Triples until the sink tells us to stop
                        Statement stmt = SemWebConverter.ToSemWeb(t, mapping);
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                    }
                }
            }
            else
            {
                //Output the results from each Graph in turn
                foreach (IGraph g in this._store.Graphs)
                {
                    Entity graphUri;
                    if (g.BaseUri == null)
                    {
                        hash     = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
                        graphUri = new Entity(GraphCollection.DefaultGraphUri);
                    }
                    else
                    {
                        hash     = g.BaseUri.GetEnhancedHashCode();
                        graphUri = new Entity(g.BaseUri.ToString());
                    }
                    SemWebMapping mapping = this.GetMapping(hash, g);
                    foreach (Triple t in this.TemplateToEnumerable(template, g))
                    {
                        Statement stmt = SemWebConverter.ToSemWeb(t, mapping);
                        stmt.Meta = graphUri;
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                    }
                }
            }
        }
コード例 #21
0
 /// <summary>
 /// Adds a statement to this Source
 /// </summary>
 /// <param name="statement">Statement</param>
 /// <returns></returns>
 public bool Add(Statement statement)
 {
     this._g.Assert(SemWebConverter.FromSemWeb(statement, this._mapping));
     //We never ask the sink to stop streaming statements so we always return true
     return(true);
 }
コード例 #22
0
        /// <summary>
        /// Selects Statements from the Source based on a Filter
        /// </summary>
        /// <param name="filter">Statement Filter</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(SelectFilter filter, StatementSink sink)
        {
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();

            if (filter.Metas != null)
            {
                //This applies over some Graphs
                foreach (Entity meta in filter.Metas)
                {
                    if (meta.Uri != null)
                    {
                        Uri graphUri = new Uri(meta.Uri);
                        if (this._store.HasGraph(graphUri))
                        {
                            ts = ts.Concat(this.FilterToEnumerable(filter, this._store.Graph(graphUri)));
                        }
                    }
                }
            }
            else
            {
                //This applies over all Graphs
                foreach (IGraph g in this._store.Graphs)
                {
                    ts = ts.Concat(this.FilterToEnumerable(filter, g));
                }
            }

            int count = 0;

            foreach (Triple t in ts)
            {
                //Apply limit if applicable
                if (filter.Limit > 0 && count >= filter.Limit)
                {
                    return;
                }

                Statement stmt = SemWebConverter.ToSemWeb(t, this.GetMapping(t.Graph));
                stmt.Meta = new Entity(t.GraphUri.ToString());

                if (filter.LiteralFilters != null)
                {
                    if (LiteralFilter.MatchesFilters(stmt.Object, filter.LiteralFilters, this))
                    {
                        //If the Object matched the filters then we return the Triple and stop
                        //streaming if the sink tells us to
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                        count++;
                    }
                }
                else
                {
                    //Just add the statement and stop if the sink tells us to stop streaming
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                    count++;
                }
            }
        }
コード例 #23
0
        /// <summary>
        /// Converts a SemWeb Statement Template to an IEnumerable of Triples
        /// </summary>
        /// <param name="template">Statement Template</param>
        /// <returns></returns>
        private IEnumerable <Triple> TemplateToEnumerable(Statement template)
        {
            INode s, p, o;

            if (template.Subject is Variable)
            {
                if (template.Predicate is Variable)
                {
                    if (template.Object is Variable)
                    {
                        //All three things are variables so this just checks that some Triple(s) are present
                        return(this._g.Triples);
                    }
                    else
                    {
                        //Subject & Predicate are Variables
                        //Convert the Object and do a WithObject().Any() call
                        o = SemWebConverter.FromSemWeb(template.Object, this._mapping);

                        return(this._g.GetTriplesWithObject(o));
                    }
                }
                else if (template.Object is Variable)
                {
                    //Subject & Object are variables
                    //Convert the Predicate and do a WithPredicate() call
                    p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping);

                    return(this._g.GetTriplesWithPredicate(p));
                }
                else
                {
                    //Subject is a Variable
                    //Convert the Predicate and Object and do a WithPredicateObject() call
                    p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping);
                    o = SemWebConverter.FromSemWeb(template.Object, this._mapping);

                    return(this._g.GetTriplesWithPredicateObject(p, o));
                }
            }
            else if (template.Predicate is Variable)
            {
                if (template.Object is Variable)
                {
                    //Predicate & Object are Variables
                    //Convert the Subject and do a WithSubject() call
                    s = SemWebConverter.FromSemWeb(template.Subject, this._mapping);

                    return(this._g.GetTriplesWithSubject(s));
                }
                else
                {
                    //Predicate is a Variable
                    //Convert the Subject and Object and do a WithSubjectObject() call
                    s = SemWebConverter.FromSemWeb(template.Subject, this._mapping);
                    o = SemWebConverter.FromSemWeb(template.Object, this._mapping);

                    return(this._g.GetTriplesWithSubjectObject(s, o));
                }
            }
            else if (template.Object is Variable)
            {
                //Object is a Variable
                //Convert the Subject and Predicate and do a WithSubjectPredicate() call
                s = SemWebConverter.FromSemWeb(template.Subject, this._mapping);
                p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping);

                return(this._g.GetTriplesWithSubjectPredicate(s, p));
            }
            else
            {
                //Just convert the Triple and do a Contains() call
                Triple t = SemWebConverter.FromSemWeb(template, this._mapping);
                if (this._g.ContainsTriple(t))
                {
                    return(t.AsEnumerable());
                }
                else
                {
                    return(Enumerable.Empty <Triple>());
                }
            }
        }
コード例 #24
0
        /// <summary>
        /// Converts a SemWeb Statement Template to an IEnumerable of Triples
        /// </summary>
        /// <param name="template">Statement Template</param>
        /// <param name="g">Graph the Template should be created for</param>
        /// <returns></returns>
        private IEnumerable <Triple> TemplateToEnumerable(Statement template, IGraph g)
        {
            INode s, p, o;
            int   hash;

            if (g.BaseUri == null)
            {
                hash = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode();
            }
            else
            {
                hash = g.BaseUri.GetEnhancedHashCode();
            }
            if (template.Subject is Variable)
            {
                if (template.Predicate is Variable)
                {
                    if (template.Object is Variable)
                    {
                        //All three things are variables so this just checks that some Triple(s) are present
                        return(g.Triples);
                    }
                    else
                    {
                        //Subject & Predicate are Variables
                        //Convert the Object and do a WithObject().Any() call
                        o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g));

                        return(g.GetTriplesWithObject(o));
                    }
                }
                else if (template.Object is Variable)
                {
                    //Subject & Object are variables
                    //Convert the Predicate and do a WithPredicate() call
                    p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g));

                    return(g.GetTriplesWithPredicate(p));
                }
                else
                {
                    //Subject is a Variable
                    //Convert the Predicate and Object and do a WithPredicateObject() call
                    p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g));
                    o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g));

                    return(g.GetTriplesWithPredicateObject(p, o));
                }
            }
            else if (template.Predicate is Variable)
            {
                if (template.Object is Variable)
                {
                    //Predicate & Object are Variables
                    //Convert the Subject and do a WithSubject() call
                    s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g));

                    return(g.GetTriplesWithSubject(s));
                }
                else
                {
                    //Predicate is a Variable
                    //Convert the Subject and Object and do a WithSubjectObject() call
                    s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g));
                    o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g));

                    return(g.GetTriplesWithSubjectObject(s, o));
                }
            }
            else if (template.Object is Variable)
            {
                //Object is a Variable
                //Convert the Subject and Predicate and do a WithSubjectPredicate() call
                s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g));
                p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g));

                return(g.GetTriplesWithSubjectPredicate(s, p));
            }
            else
            {
                //Just convert the Triple and do a Contains() call
                Triple t = SemWebConverter.FromSemWeb(template, this.GetMapping(hash, g));
                if (g.ContainsTriple(t))
                {
                    return(t.AsEnumerable());
                }
                else
                {
                    return(Enumerable.Empty <Triple>());
                }
            }
        }
コード例 #25
0
        /// <summary>
        /// Selects Statements from the Source based on a Filter
        /// </summary>
        /// <param name="filter">Statement Filter</param>
        /// <param name="sink">Sink to stream results to</param>
        public void Select(SelectFilter filter, StatementSink sink)
        {
            //Don't support filters on Metas for the Graph Source
            if (filter.Metas != null)
            {
                throw new RdfException("The dotNetRDF GraphSource does not support SemWeb filters which use Meta filters");
            }

            //Want to build an IEnumerable based on the Filter
            IEnumerable <Triple> ts = Enumerable.Empty <Triple>();
            INode s, p, o;

            if (filter.Subjects != null)
            {
                if (filter.Predicates != null)
                {
                    //Subject-Predicate filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this._mapping);
                        foreach (Entity pred in filter.Predicates)
                        {
                            p  = SemWebConverter.FromSemWeb(pred, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithSubjectPredicate(s, p));
                        }
                    }
                }
                else if (filter.Objects != null)
                {
                    //Subject-Object filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s = SemWebConverter.FromSemWeb(subj, this._mapping);
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithSubjectObject(s, o));
                        }
                    }
                }
                else
                {
                    //Subjects filter
                    foreach (Entity subj in filter.Subjects)
                    {
                        s  = SemWebConverter.FromSemWeb(subj, this._mapping);
                        ts = ts.Concat(this._g.GetTriplesWithSubject(s));
                    }
                }
            }
            else if (filter.Predicates != null)
            {
                if (filter.Objects != null)
                {
                    //Predicate-Object Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p = SemWebConverter.FromSemWeb(pred, this._mapping);
                        foreach (Resource obj in filter.Objects)
                        {
                            o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                            ts = ts.Concat(this._g.GetTriplesWithPredicateObject(p, o));
                        }
                    }
                }
                else
                {
                    //Predicate Filter
                    foreach (Entity pred in filter.Predicates)
                    {
                        p  = SemWebConverter.FromSemWeb(pred, this._mapping);
                        ts = ts.Concat(this._g.GetTriplesWithPredicate(p));
                    }
                }
            }
            else if (filter.Objects != null)
            {
                //Object Filter
                foreach (Resource obj in filter.Objects)
                {
                    o  = SemWebConverter.FromSemWeb(obj, this._mapping);
                    ts = ts.Concat(this._g.GetTriplesWithObject(o));
                }
            }
            else
            {
                //Everything is null so this is a Select All
                ts = this._g.Triples;
            }

            int count = 0;

            foreach (Triple t in ts)
            {
                //Apply limit if applicable
                if (filter.Limit > 0 && count >= filter.Limit)
                {
                    return;
                }

                //Convert to a Statement and apply applicable Literal Filters
                Statement stmt = SemWebConverter.ToSemWeb(t, this._mapping);
                if (filter.LiteralFilters != null)
                {
                    if (LiteralFilter.MatchesFilters(stmt.Object, filter.LiteralFilters, this))
                    {
                        //If the Object matched the filters then we return the Triple and stop
                        //streaming if the sink tells us to
                        if (!sink.Add(stmt))
                        {
                            return;
                        }
                        count++;
                    }
                    //If it doesn't match the filter it is ignored
                }
                else
                {
                    //Just add the statement and stop if the sink tells us to stop streaming
                    if (!sink.Add(stmt))
                    {
                        return;
                    }
                    count++;
                }
            }
        }