/// <summary> /// Copies a Node using another Node Factory /// </summary> /// <param name="original">Node to copy</param> /// <param name="target">Factory to copy into</param> /// <returns></returns> /// <remarks> /// <para> /// <strong>Warning:</strong> Copying Blank Nodes may lead to unforseen circumstances since no remapping of IDs between Factories is done /// </para> /// </remarks> public static INode CopyNode(INode original, INodeFactory target) { if (ReferenceEquals(original.Graph, target)) return original; switch (original.NodeType) { case NodeType.Blank: return target.CreateBlankNode(((IBlankNode)original).InternalID); case NodeType.GraphLiteral: return target.CreateGraphLiteralNode(((IGraphLiteralNode)original).SubGraph); case NodeType.Literal: ILiteralNode lit = (ILiteralNode)original; if (lit.DataType != null) { return target.CreateLiteralNode(lit.Value, lit.DataType); } else if (!lit.Language.Equals(String.Empty)) { return target.CreateLiteralNode(lit.Value, lit.Language); } else { return target.CreateLiteralNode(lit.Value); } case NodeType.Uri: return target.CreateUriNode(((IUriNode)original).Uri); case NodeType.Variable: return target.CreateVariableNode(((IVariableNode)original).VariableName); default: throw new RdfException("Unable to Copy '" + original.GetType().ToString() + "' Nodes between Node Factories"); } }
/// <summary> /// Creates a new Blank Node for this Context /// </summary> /// <param name="id">ID</param> /// <returns></returns> /// <remarks> /// <para> /// If the same Blank Node ID is used multiple times in this Context you will always get the same Blank Node for that ID /// </para> /// </remarks> public INode GetBlankNode(String id) { if (_bnodeMap == null) { _bnodeMap = new Dictionary <string, INode>(); } if (_bnodeMap.ContainsKey(id)) { return(_bnodeMap[id]); } INode temp; if (_g != null) { temp = _g.CreateBlankNode(); } else if (_factory != null) { temp = _factory.CreateBlankNode(); } else if (_s != null) { temp = new BlankNode(_g, id.Substring(2) + _s.ID); } else { temp = new BlankNode(_g, id.Substring(2)); } _bnodeMap.Add(id, temp); return(temp); }
public static INode ToNode(this EntityId entityId, INodeFactory factory) { if (entityId is BlankId) { return(factory.CreateBlankNode(entityId.Uri.Authority)); } return(factory.CreateUriNode(entityId.Uri)); }
public static INode ToNode(this EntityId entityId, INodeFactory factory) { if (entityId is BlankId) { return factory.CreateBlankNode(entityId.Uri.Authority); } return factory.CreateUriNode(entityId.Uri); }
internal static INode TryParseNodeValue(INodeFactory factory, String value) { try { if (value.StartsWith("_:")) { return(factory.CreateBlankNode(value.Substring(2))); } else if (value.StartsWith("<")) { return(factory.CreateUriNode(new Uri(UnescapeValue(value.Substring(1, value.Length - 2))))); } else { if (value.EndsWith("\"")) { return(factory.CreateLiteralNode(UnescapeValue(value.Substring(1, value.Length - 2)))); } else if (value.EndsWith(">")) { String lit = value.Substring(1, value.LastIndexOf("^^<") - 2); String dt = value.Substring(lit.Length + 5, value.Length - lit.Length - 6); return(factory.CreateLiteralNode(UnescapeValue(lit), new Uri(UnescapeValue(dt)))); } else { String lit = value.Substring(1, value.LastIndexOf("\"@") - 1); String lang = value.Substring(lit.Length + 3); return(factory.CreateLiteralNode(UnescapeValue(lit), UnescapeValue(lang))); } } } catch (Exception ex) { throw new RdfParseException("Failed to parse the value '" + value + "' into a valid Node: " + ex.Message, ex); } }
/// <summary>Converts a RomanticWeb's <see cref="Node"/> into dotNetRDF's <see cref="INode"/>.</summary> public static INode UnWrapNode(this Node node, INodeFactory nodeFactory) { if (node.IsUri) { return nodeFactory.CreateUriNode(node.Uri); } if (node.IsLiteral) { if (node.Language != null) { return nodeFactory.CreateLiteralNode(node.Literal, node.Language); } if (node.DataType != null) { return nodeFactory.CreateLiteralNode(node.Literal, node.DataType); } return nodeFactory.CreateLiteralNode(node.Literal); } return nodeFactory.CreateBlankNode(node.BlankNode); }
/// <summary>Converts a RomanticWeb's <see cref="Node"/> into dotNetRDF's <see cref="VDS.RDF.INode"/>.</summary> public static VDS.RDF.INode UnWrapNode(this Model.INode node, INodeFactory nodeFactory) { if (node.IsUri) { return(nodeFactory.CreateUriNode(node.Uri)); } if (node.IsLiteral) { if (node.Language != null) { return(nodeFactory.CreateLiteralNode(node.Literal, node.Language)); } if (node.DataType != null) { return(nodeFactory.CreateLiteralNode(node.Literal, node.DataType)); } return(nodeFactory.CreateLiteralNode(node.Literal)); } return(nodeFactory.CreateBlankNode(node.BlankNode)); }
//OPT: Replace with usage of MapTriple instead? /// <summary> /// Helper method which rewrites Blank Node IDs for Describe Queries /// </summary> /// <param name="t">Triple</param> /// <param name="mapping">Mapping of IDs to new Blank Nodes</param> /// <param name="factory">Factory to create Nodes in</param> /// <returns></returns> protected Triple RewriteDescribeBNodes(Triple t, Dictionary <String, INode> mapping, INodeFactory factory) { INode s, p, o; String id; if (t.Subject.NodeType == NodeType.Blank) { id = t.Subject.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { s = mapping[id]; } else { s = factory.CreateBlankNode(id); mapping.Add(id, s); } } else { s = Tools.CopyNode(t.Subject, factory); } if (t.Predicate.NodeType == NodeType.Blank) { id = t.Predicate.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { p = mapping[id]; } else { p = factory.CreateBlankNode(id); mapping.Add(id, p); } } else { p = Tools.CopyNode(t.Predicate, factory); } if (t.Object.NodeType == NodeType.Blank) { id = t.Object.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { o = mapping[id]; } else { o = factory.CreateBlankNode(id); mapping.Add(id, o); } } else { o = Tools.CopyNode(t.Object, factory); } return(new Triple(s, p, o)); }
/// <summary> /// Creates a Blank Node /// </summary> /// <returns></returns> public virtual IBlankNode CreateBlankNode() { return(_factory.CreateBlankNode()); }
/// <summary> /// Copies a Node using another Node Factory /// </summary> /// <param name="original">Node to copy</param> /// <param name="target">Factory to copy into</param> /// <returns></returns> /// <remarks> /// <para> /// <strong>Warning:</strong> Copying Blank Nodes may lead to unforseen circumstances since no remapping of IDs between Factories is done /// </para> /// </remarks> public static INode CopyNode(INode original, INodeFactory target) { if (ReferenceEquals(original.Graph, target)) return original; switch (original.NodeType) { case NodeType.Blank: return target.CreateBlankNode(((IBlankNode)original).InternalID); case NodeType.GraphLiteral: return target.CreateGraphLiteralNode(((IGraphLiteralNode)original).SubGraph); case NodeType.Literal: ILiteralNode lit = (ILiteralNode)original; if (lit.DataType != null) { return target.CreateLiteralNode(lit.Value, lit.DataType); } else if (!lit.Language.Equals(String.Empty)) { return target.CreateLiteralNode(lit.Value, lit.Language); } else { return target.CreateLiteralNode(lit.Value); } case NodeType.Uri: return target.CreateUriNode(((IUriNode)original).Uri); case NodeType.Variable: return target.CreateVariableNode(((IVariableNode)original).VariableName); default: throw new RdfException("Unable to Copy '" + original.GetType().ToString() + "' Nodes between Node Factories"); } }
public static INode AsBlankNode(this INodeFactory nodeFactory, string nodeId) { return(nodeFactory.CreateBlankNode(nodeId)); }
internal static INode TryParseNodeValue(INodeFactory factory, String value) { try { if (value.StartsWith("_:")) { return factory.CreateBlankNode(value.Substring(2)); } else if (value.StartsWith("<")) { return factory.CreateUriNode(new Uri(UnescapeValue(value.Substring(1, value.Length - 2)))); } else { if (value.EndsWith("\"")) { return factory.CreateLiteralNode(UnescapeValue(value.Substring(1, value.Length - 2))); } else if (value.EndsWith(">")) { String lit = value.Substring(1, value.LastIndexOf("^^<") - 2); String dt = value.Substring(lit.Length + 5, value.Length - lit.Length - 6); return factory.CreateLiteralNode(UnescapeValue(lit), new Uri(UnescapeValue(dt))); } else { String lit = value.Substring(1, value.LastIndexOf("\"@") - 1); String lang = value.Substring(lit.Length + 3); return factory.CreateLiteralNode(UnescapeValue(lit), UnescapeValue(lang)); } } } catch (Exception ex) { throw new RdfParseException("Failed to parse the value '" + value + "' into a valid Node: " + ex.Message, ex); } }
//OPT: Replace with usage of MapTriple instead? /// <summary> /// Helper method which rewrites Blank Node IDs for Describe Queries /// </summary> /// <param name="t">Triple</param> /// <param name="mapping">Mapping of IDs to new Blank Nodes</param> /// <param name="factory">Factory to create Nodes in</param> /// <returns></returns> protected Triple RewriteDescribeBNodes(Triple t, Dictionary<String, INode> mapping, INodeFactory factory) { INode s, p, o; String id; if (t.Subject.NodeType == NodeType.Blank) { id = t.Subject.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { s = mapping[id]; } else { s = factory.CreateBlankNode(id); mapping.Add(id, s); } } else { s = Tools.CopyNode(t.Subject, factory); } if (t.Predicate.NodeType == NodeType.Blank) { id = t.Predicate.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { p = mapping[id]; } else { p = factory.CreateBlankNode(id); mapping.Add(id, p); } } else { p = Tools.CopyNode(t.Predicate, factory); } if (t.Object.NodeType == NodeType.Blank) { id = t.Object.GetHashCode() + "-" + t.Graph.GetHashCode(); if (mapping.ContainsKey(id)) { o = mapping[id]; } else { o = factory.CreateBlankNode(id); mapping.Add(id, o); } } else { o = Tools.CopyNode(t.Object, factory); } return new Triple(s, p, o); }
/// <summary> /// Decodes an Object into an appropriate Node /// </summary> /// <param name="factory">Node Factory to use to create Node</param> /// <param name="n">Object to convert</param> /// <returns></returns> private INode LoadNode(INodeFactory factory, Object n) { INode temp; if (n is SqlExtendedString) { SqlExtendedString iri = (SqlExtendedString)n; if (iri.IriType == SqlExtendedStringType.BNODE) { //Blank Node temp = factory.CreateBlankNode(n.ToString().Substring(9)); } else if (iri.IriType != iri.StrType) { //Literal temp = factory.CreateLiteralNode(n.ToString()); } else if (iri.IriType == SqlExtendedStringType.IRI) { //Uri Uri u = new Uri(n.ToString(), UriKind.RelativeOrAbsolute); if (!u.IsAbsoluteUri) { throw new RdfParseException("Virtuoso returned a URI Node which has a relative URI, unable to resolve the URI for this node"); } temp = factory.CreateUriNode(u); } else { //Assume a Literal temp = factory.CreateLiteralNode(n.ToString()); } } else if (n is SqlRdfBox) { SqlRdfBox lit = (SqlRdfBox)n; if (lit.StrLang != null) { //Language Specified Literal temp = factory.CreateLiteralNode(n.ToString(), lit.StrLang); } else if (lit.StrType != null) { //Data Typed Literal temp = factory.CreateLiteralNode(n.ToString(), new Uri(lit.StrType)); } else { //Literal temp = factory.CreateLiteralNode(n.ToString()); } } else if (n is String) { String s = n.ToString(); if (s.StartsWith("nodeID://")) { //Blank Node temp = factory.CreateBlankNode(s.Substring(9)); } else { //Literal temp = factory.CreateLiteralNode(s); } } else if (n is Int32) { temp = ((Int32)n).ToLiteral(factory); } else if (n is Int16) { temp = ((Int16)n).ToLiteral(factory); } else if (n is Single) { temp = ((Single)n).ToLiteral(factory); } else if (n is Double) { temp = ((Double)n).ToLiteral(factory); } else if (n is Decimal) { temp = ((Decimal)n).ToLiteral(factory); } else if (n is DateTime) { temp = ((DateTime)n).ToLiteral(factory); } else if (n is TimeSpan) { temp = ((TimeSpan)n).ToLiteral(factory); } else if (n is Boolean) { temp = ((Boolean)n).ToLiteral(factory); } else if (n is DBNull) { //Fix by Alexander Sidarov for Virtuoso's results for unbound variables in OPTIONALs temp = null; } else { throw new RdfStorageException("Unexpected Object Type '" + n.GetType().ToString() + "' returned from SPASQL SELECT query to the Virtuoso Quad Store"); } return temp; }