/// <summary>
 /// Creates a new Triple Collection as a wrapper around a SemWeb StatementSink and StatementSource
 /// </summary>
 /// <param name="g">Graph</param>
 /// <param name="sink">Sink</param>
 /// <param name="source">Source</param>
 public SemWebTripleCollection(IGraph g, StatementSink sink, StatementSource source) 
 {
     this._g = g;
     this._sink = sink;
     this._source = source;
     this._mapping = new SemWebMapping(this._g);
 }
Exemple #2
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;
                    }
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Creates a new Triple Collection as a wrapper around a SemWeb StatementSink and StatementSource
 /// </summary>
 /// <param name="g">Graph</param>
 /// <param name="sink">Sink</param>
 /// <param name="source">Source</param>
 public SemWebTripleCollection(IGraph g, StatementSink sink, StatementSource source)
 {
     this._g       = g;
     this._sink    = sink;
     this._source  = source;
     this._mapping = new SemWebMapping(this._g);
 }
Exemple #4
0
 /// <summary>
 /// Converts a SemWeb Resource into a dotNetRDF Node
 /// </summary>
 /// <param name="r">Resource</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static INode FromSemWeb(Resource r, SemWebMapping mapping)
 {
     if (r is Entity)
     {
         return(FromSemWebEntity((Entity)r, mapping));
     }
     else if (r is Literal)
     {
         Literal lit = (Literal)r;
         if (lit.DataType != null)
         {
             return(mapping.Graph.CreateLiteralNode(lit.Value, new Uri(lit.DataType)));
         }
         else if (lit.Language != null)
         {
             return(mapping.Graph.CreateLiteralNode(lit.Value, lit.Language));
         }
         else
         {
             return(mapping.Graph.CreateLiteralNode(lit.Value));
         }
     }
     else
     {
         throw new RdfException("Cannot convert an unknown SemWeb Resource type to a dotNetRDF Node");
     }
 }
Exemple #5
0
        /// <summary>
        /// Converts a SemWeb Statement to a dotNetRDF Triple
        /// </summary>
        /// <param name="stmt">Statement</param>
        /// <param name="mapping">Mapping of Blank Nodes</param>
        /// <returns></returns>
        public static Triple FromSemWeb(Statement stmt, SemWebMapping mapping)
        {
            INode s, p, o;

            s = FromSemWeb(stmt.Subject, mapping);
            p = FromSemWeb(stmt.Predicate, mapping);
            o = FromSemWeb(stmt.Object, mapping);
            return(new Triple(s, p, o));
        }
        /// <summary>
        /// Takes the contents of a dotNetRDF Graph and inputs it into a SemWeb StatementSink
        /// </summary>
        /// <param name="g">Graph</param>
        /// <param name="mapping">Blank Node Mapping</param>
        /// <param name="sink">Statement Sink</param>
        public static void ToSemWeb(IGraph g, SemWebMapping mapping, StatementSink sink)
        {
            Statement stmt;

            foreach (Triple t in g.Triples)
            {
                stmt = ToSemWeb(t, mapping);
                //Stop adding statements if the sink tells up to stop
                if (!sink.Add(stmt)) return;
            }
        }
Exemple #7
0
        /// <summary>
        /// Takes the contents of a dotNetRDF Graph and inputs it into a SemWeb StatementSink
        /// </summary>
        /// <param name="g">Graph</param>
        /// <param name="mapping">Blank Node Mapping</param>
        /// <param name="sink">Statement Sink</param>
        public static void ToSemWeb(IGraph g, SemWebMapping mapping, StatementSink sink)
        {
            Statement stmt;

            foreach (Triple t in g.Triples)
            {
                stmt = ToSemWeb(t, mapping);
                //Stop adding statements if the sink tells up to stop
                if (!sink.Add(stmt))
                {
                    return;
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Takes the contents of a SemWeb Statement Source and inputs it into a dotNetRDF Graph
        /// </summary>
        /// <param name="source">Statement Source</param>
        /// <param name="g">Graph</param>
        public static void FromSemWeb(StatementSource source, IGraph g)
        {
            SemWebMapping mapping = new SemWebMapping(g);
            Triple        t;

            MemoryStore mem = new MemoryStore();

            source.Select(mem);

            foreach (Statement stmt in mem)
            {
                t = FromSemWeb(stmt, mapping);
                g.Assert(t);
            }
        }
        /// <summary>
        /// Converts a dotNetRDF Triple to a SemWeb Statement
        /// </summary>
        /// <param name="t">Triple</param>
        /// <param name="mapping">Mapping of Blank Nodes</param>
        /// <returns></returns>
        public static Statement ToSemWeb(Triple t, SemWebMapping mapping)
        {
            Entity s, p;
            Resource o;
            Statement stmt;
            if (t.IsGroundTriple)
            {
                //Easy to map across without need for BNode mapping
                stmt = new Statement(ToSemWebEntity(t.Subject), ToSemWebEntity(t.Predicate), ToSemWeb(t.Object));
            }
            else
            {
                s = ToSemWebEntity(t.Subject, mapping);
                p = ToSemWebEntity(t.Predicate, mapping);
                o = ToSemWeb(t.Object, mapping);

                stmt = new Statement(s, p, o);
            }
            return stmt;
        }
Exemple #10
0
        /// <summary>
        /// Converts a dotNetRDF Triple to a SemWeb Statement
        /// </summary>
        /// <param name="t">Triple</param>
        /// <param name="mapping">Mapping of Blank Nodes</param>
        /// <returns></returns>
        public static Statement ToSemWeb(Triple t, SemWebMapping mapping)
        {
            Entity    s, p;
            Resource  o;
            Statement stmt;

            if (t.IsGroundTriple)
            {
                //Easy to map across without need for BNode mapping
                stmt = new Statement(ToSemWebEntity(t.Subject), ToSemWebEntity(t.Predicate), ToSemWeb(t.Object));
            }
            else
            {
                s = ToSemWebEntity(t.Subject, mapping);
                p = ToSemWebEntity(t.Predicate, mapping);
                o = ToSemWeb(t.Object, mapping);

                stmt = new Statement(s, p, o);
            }
            return(stmt);
        }
Exemple #11
0
 /// <summary>
 /// Converts a SemWeb Entity into a dotNetRDF Node
 /// </summary>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <param name="e">Entity</param>
 /// <returns></returns>
 public static INode FromSemWebEntity(Entity e, SemWebMapping mapping)
 {
     if (e.Uri == null)
     {
         if (mapping.InputMapping.ContainsKey(e.ToString()))
         {
             return(mapping.InputMapping[e.ToString()]);
         }
         else
         {
             INode temp = mapping.Graph.CreateBlankNode();
             mapping.InputMapping.Add(e.ToString(), temp);
             if (!mapping.OutputMapping.ContainsKey(temp))
             {
                 mapping.OutputMapping.Add(temp, e);
             }
             return(temp);
         }
     }
     else
     {
         return(mapping.Graph.CreateUriNode(new Uri(e.Uri)));
     }
 }
Exemple #12
0
 /// <summary>
 /// Converts a dotNetRDF Node to a SemWeb Resource
 /// </summary>
 /// <param name="n">Node</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static Resource ToSemWeb(INode n, SemWebMapping mapping)
 {
     if (n.NodeType == NodeType.Blank)
     {
         if (mapping.OutputMapping.ContainsKey(n))
         {
             return(mapping.OutputMapping[n]);
         }
         else
         {
             Entity temp = ToSemWebEntity(n);
             mapping.OutputMapping.Add(n, temp);
             if (!mapping.InputMapping.ContainsKey(temp.ToString()))
             {
                 mapping.InputMapping.Add(temp.ToString(), n);
             }
             return(temp);
         }
     }
     else
     {
         return(ToSemWeb(n));
     }
 }
 /// <summary>
 /// Takes the contents of a dotNetRDF Graph and inputs it into a SemWeb StatementSink
 /// </summary>
 /// <param name="g">Graph</param>
 /// <param name="sink">Statement Sink</param>
 public static void ToSemWeb(IGraph g, StatementSink sink)
 {
     SemWebMapping mapping = new SemWebMapping(g);
     ToSemWeb(g, mapping, sink);
 }
 /// <summary>
 /// Converts a SemWeb Entity into a dotNetRDF Node
 /// </summary>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <param name="e">Entity</param>
 /// <returns></returns>
 public static INode FromSemWebEntity(Entity e, SemWebMapping mapping)
 {
     if (e.Uri == null)
     {
         if (mapping.InputMapping.ContainsKey(e.ToString()))
         {
             return mapping.InputMapping[e.ToString()];
         }
         else
         {
             INode temp = mapping.Graph.CreateBlankNode();
             mapping.InputMapping.Add(e.ToString(), temp);
             if (!mapping.OutputMapping.ContainsKey(temp)) mapping.OutputMapping.Add(temp, e);
             return temp;
         }
     }
     else
     {
         return mapping.Graph.CreateUriNode(new Uri(e.Uri));
     }
 }
 /// <summary>
 /// Converts a SemWeb Resource into a dotNetRDF Node
 /// </summary>
 /// <param name="r">Resource</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static INode FromSemWeb(Resource r, SemWebMapping mapping)
 {
     if (r is Entity)
     {
         return FromSemWebEntity((Entity)r, mapping);
     }
     else if (r is Literal)
     {
         Literal lit = (Literal)r;
         if (lit.DataType != null)
         {
             return mapping.Graph.CreateLiteralNode(lit.Value, new Uri(lit.DataType));
         }
         else if (lit.Language != null)
         {
             return mapping.Graph.CreateLiteralNode(lit.Value, lit.Language);
         }
         else
         {
             return mapping.Graph.CreateLiteralNode(lit.Value);
         }
     }
     else
     {
         throw new RdfException("Cannot convert an unknown SemWeb Resource type to a dotNetRDF Node");
     }
 }
 /// <summary>
 /// Converts a SemWeb Statement to a dotNetRDF Triple
 /// </summary>
 /// <param name="stmt">Statement</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static Triple FromSemWeb(Statement stmt, SemWebMapping mapping)
 {
     INode s, p, o;
     s = FromSemWeb(stmt.Subject, mapping);
     p = FromSemWeb(stmt.Predicate, mapping);
     o = FromSemWeb(stmt.Object, mapping);
     return new Triple(s, p, o);
 }
        /// <summary>
        /// Takes the contents of a SemWeb Statement Source and inputs it into a dotNetRDF Graph
        /// </summary>
        /// <param name="source">Statement Source</param>
        /// <param name="g">Graph</param>
        public static void FromSemWeb(StatementSource source, IGraph g)
        {
            SemWebMapping mapping = new SemWebMapping(g);
            Triple t;
            
            MemoryStore mem = new MemoryStore();
            source.Select(mem);

            foreach (Statement stmt in mem)
            {
                t = FromSemWeb(stmt, mapping);
                g.Assert(t);
            }
        }
 /// <summary>
 /// Converts a dotNetRDF Node to a SemWeb Entity
 /// </summary>
 /// <param name="n">Node</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static Entity ToSemWebEntity(INode n, SemWebMapping mapping)
 {
     Resource r = ToSemWeb(n, mapping);
     return (Entity)r;
 }
Exemple #19
0
        /// <summary>
        /// Converts a dotNetRDF Node to a SemWeb Entity
        /// </summary>
        /// <param name="n">Node</param>
        /// <param name="mapping">Mapping of Blank Nodes</param>
        /// <returns></returns>
        public static Entity ToSemWebEntity(INode n, SemWebMapping mapping)
        {
            Resource r = ToSemWeb(n, mapping);

            return((Entity)r);
        }
Exemple #20
0
 /// <summary>
 /// Creates a new Native Store Source
 /// </summary>
 /// <param name="store">Natively Queryable Store</param>
 public NativeStoreSource(INativelyQueryableStore store)
 {
     this._store   = store;
     this._mapping = new SemWebMapping(this._g);
 }
Exemple #21
0
        /// <summary>
        /// Takes the contents of a dotNetRDF Graph and inputs it into a SemWeb StatementSink
        /// </summary>
        /// <param name="g">Graph</param>
        /// <param name="sink">Statement Sink</param>
        public static void ToSemWeb(IGraph g, StatementSink sink)
        {
            SemWebMapping mapping = new SemWebMapping(g);

            ToSemWeb(g, mapping, sink);
        }
Exemple #22
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;
                        }
                    }
                }
            }
        }
Exemple #23
0
 /// <summary>
 /// Creates a new Graph Source
 /// </summary>
 /// <param name="g">Graph</param>
 public GraphSource(IGraph g)
 {
     this._g       = g;
     this._mapping = new SemWebMapping(this._g);
 }
 /// <summary>
 /// Creates a new Native Store Source
 /// </summary>
 /// <param name="store">Natively Queryable Store</param>
 public NativeStoreSource(INativelyQueryableStore store)
 {
     this._store = store;
     this._mapping = new SemWebMapping(this._g);
 }
 /// <summary>
 /// Creates a new Graph Source
 /// </summary>
 /// <param name="g">Graph</param>
 public GraphSource(IGraph g)
 {
     this._g = g;
     this._mapping = new SemWebMapping(this._g);
 }
Exemple #26
0
 /// <summary>
 /// Creates a new Enumeration Sink
 /// </summary>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 public EnumerationSink(SemWebMapping mapping)
 {
     this._mapping = mapping;
 }
 /// <summary>
 /// Converts a dotNetRDF Node to a SemWeb Resource
 /// </summary>
 /// <param name="n">Node</param>
 /// <param name="mapping">Mapping of Blank Nodes</param>
 /// <returns></returns>
 public static Resource ToSemWeb(INode n, SemWebMapping mapping)
 {
     if (n.NodeType == NodeType.Blank)
     {
         if (mapping.OutputMapping.ContainsKey(n))
         {
             return mapping.OutputMapping[n];
         }
         else
         {
             Entity temp = ToSemWebEntity(n);
             mapping.OutputMapping.Add(n, temp);
             if (!mapping.InputMapping.ContainsKey(temp.ToString())) mapping.InputMapping.Add(temp.ToString(), n);
             return temp;
         }
     }
     else
     {
         return ToSemWeb(n);
     }
 }