/// <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 Node for the Context /// </summary> /// <param name="n">Node</param> /// <returns></returns> /// <remarks> /// <para> /// In effect all this does is ensure that all Nodes end up in the same Graph which may occassionally not happen otherwise when Graph wrappers are involved /// </para> /// </remarks> public INode GetNode(INode n) { if (_nodeMap == null) { _nodeMap = new MultiDictionary <INode, INode>(new FastVirtualNodeComparer()); } if (_nodeMap.ContainsKey(n)) { return(_nodeMap[n]); } INode temp; switch (n.NodeType) { case NodeType.Blank: temp = GetBlankNode(((IBlankNode)n).InternalID); break; case NodeType.Variable: IVariableNode v = (IVariableNode)n; temp = _factory.CreateVariableNode(v.VariableName); break; case NodeType.GraphLiteral: IGraphLiteralNode g = (IGraphLiteralNode)n; temp = _factory.CreateGraphLiteralNode(g.SubGraph); break; case NodeType.Uri: IUriNode u = (IUriNode)n; temp = _factory.CreateUriNode(u.Uri); break; case NodeType.Literal: ILiteralNode l = (ILiteralNode)n; if (l.DataType != null) { temp = _factory.CreateLiteralNode(l.Value, l.DataType); } else if (!l.Language.Equals(String.Empty)) { temp = _factory.CreateLiteralNode(l.Value, l.Language); } else { temp = _factory.CreateLiteralNode(l.Value); } break; default: throw new RdfQueryException("Cannot construct unknown Node Types"); } _nodeMap.Add(n, temp); return(temp); }
public void SparqlAggregatesOverNullGroupConcat1() { SparqlQuery q = _parser.ParseFromString("SELECT (GROUP_CONCAT(?s) AS ?GroupConcat) WHERE { ?s ?p ?o }"); SparqlResultSet results = _processor.ProcessQuery(q) as SparqlResultSet; Assert.NotNull(results); Assert.Equal(1, results.Count); SparqlResult r = results.First(); Assert.Equal(_factory.CreateLiteralNode(string.Empty), r["GroupConcat"]); }
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)); }
/// <summary> /// Creates a Literal Node with the given Datatype /// </summary> /// <param name="literal">Value</param> /// <param name="datatype">Datatype URI</param> /// <returns></returns> public virtual ILiteralNode CreateLiteralNode(string literal, Uri datatype) { return(_factory.CreateLiteralNode(literal, datatype)); }
/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="s3Event"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(S3Event s3Event, ILambdaContext context) { foreach (var record in s3Event.Records) { try { using (GetObjectResponse response = await _s3Client.GetObjectAsync(new GetObjectRequest() { BucketName = record.S3.Bucket.Name, Key = record.S3.Object.Key })) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { // // get text detail // var textDetail = JsonConvert.DeserializeObject <TextDetail>(await reader.ReadToEndAsync()); // // load selfies graph // IGraph textsGraph = new Graph(); _blazegraph.LoadGraph(textsGraph, UriFactory.Create(TEXTS_GRAPH)); // // create triples // INode textNode = null; try { textNode = _nodeFactory.CreateUriNode(UriFactory.Create(Uri.EscapeUriString("http://blazegraph-webapp/" + textDetail.Id))); } catch (Exception ex) { context.Logger.LogLine(ex.Message); return; } var triples = new List <Triple>(); // text instance is of type Text class triples.Add(new Triple( subj: textNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text")) )); // text instance label triples.Add(new Triple( subj: textNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDFS + "label")), obj: _nodeFactory.CreateLiteralNode(textDetail.Id, new Uri(XmlSpecsHelper.XmlSchemaDataTypeString)) )); // text instance has an entityCollection instance triples.Add(new Triple( subj: textNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/hasEntityCollection")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/" + textDetail.Id)) )); // entityCollection instance is of type EntityCollection class triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/" + textDetail.Id)), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection")) )); // filling entityCollection attributes foreach (var entity in textDetail.Entities) { triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/" + textDetail.Id)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/hasEntity")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/" + entity.Type.Value.ToLower())) )); // entity instance is of type Entity class triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/" + entity.Type.Value.ToLower())), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/entityCollection/entity")) )); } // text instance has an sentiment instance triples.Add(new Triple( subj: textNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/hasSentiment")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/sentiment/" + textDetail.Id)) )); // sentiment instance is of type sentiment that the text has triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/sentiment/" + textDetail.Id)), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/" + textDetail.Sentiment.Value.ToLower())) )); // the class sentiment that the text has is a sublcass of Sentiment class triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/" + textDetail.Sentiment.Value.ToLower())), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDFS + "subClassOf")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/text/sentiment")) )); // // updated triples in graph // try { _blazegraph.UpdateGraph(UriFactory.Create(TEXTS_GRAPH), triples, new List <Triple>()); context.Logger.LogLine("Updated triples successfully."); } catch (Exception ex) { context.Logger.LogLine(ex.Message); context.Logger.LogLine(ex.InnerException.Message); return; } } } } catch (AmazonS3Exception ex) { context.Logger.LogLine(ex.Message); } } }
/// <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 AsLiteralNode(this INodeFactory nodeFactory, string value, string langspec) { return(nodeFactory.CreateLiteralNode(value, langspec)); }
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> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="s3Event"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(S3Event s3Event, ILambdaContext context) { foreach (var record in s3Event.Records) { try { using (GetObjectResponse response = await _s3Client.GetObjectAsync(new GetObjectRequest() { BucketName = record.S3.Bucket.Name, Key = record.S3.Object.Key })) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { // // get main face // var selfieDetail = JsonConvert.DeserializeObject <SelfieDetail>(await reader.ReadToEndAsync()); var mainFace = selfieDetail.FacesDetails.OrderByDescending(faceDetail => faceDetail.BoundingBox.Width * faceDetail.BoundingBox.Height).First(); // // load selfies graph // IGraph selfiesGraph = new Graph(); _blazegraph.LoadGraph(selfiesGraph, UriFactory.Create(SELFIES_GRAPH)); // // create triples // INode imageNode = null; try { imageNode = _nodeFactory.CreateUriNode(UriFactory.Create(Uri.EscapeUriString("http://blazegraph-webapp/" + selfieDetail.ImageName))); } catch (Exception ex) { context.Logger.LogLine(ex.Message); return; } var triples = new List <Triple>(); // image instance is of type Selfie class triples.Add(new Triple( subj: imageNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie")) )); // image instance label triples.Add(new Triple( subj: imageNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDFS + "label")), obj: _nodeFactory.CreateLiteralNode(selfieDetail.ImageName, new Uri(XmlSpecsHelper.XmlSchemaDataTypeString)) )); // image instance has a faceDetail instance triples.Add(new Triple( subj: imageNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/hasFaceDetail")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)) )); // faceDetail instance is of type FaceDetail class triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail")) )); // filling faceDetail attributes triples.AddRange(new List <Triple>() { new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/isGender")), obj: _nodeFactory.CreateLiteralNode(mainFace.Gender.Value.ToString().ToLower(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeString)) ), new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/hasMinAge")), obj: _nodeFactory.CreateLiteralNode(mainFace.AgeRange.Low.ToString(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeInteger)) ), new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/hasMaxAge")), obj: _nodeFactory.CreateLiteralNode(mainFace.AgeRange.High.ToString(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeInteger)) ), new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/isSmiling")), obj: _nodeFactory.CreateLiteralNode(Convert.ToInt32(mainFace.Smile.Value).ToString(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeBoolean)) ), new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/hasSunglasses")), obj: _nodeFactory.CreateLiteralNode(Convert.ToInt32(mainFace.Sunglasses.Value).ToString(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeBoolean)) ), new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/faceDetail/isFeeling")), obj: _nodeFactory.CreateLiteralNode(mainFace.Emotions.OrderByDescending(emotion => emotion.Confidence).First().Type.ToString().ToLower(), new Uri(XmlSpecsHelper.XmlSchemaDataTypeString)) ) }); // image instance has scene instance triples.Add(new Triple( subj: imageNode, pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/hasScene")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + selfieDetail.ImageName)) )); // scene instance is of type Scene class triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene")) )); // filling scence attributes foreach (var label in selfieDetail.Labels) { triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + selfieDetail.ImageName)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/isDescribedBy")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + label.Name)) )); foreach (var parentLabel in label.Parents) { triples.Add(new Triple( subj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + label.Name)), pred: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/hasParent")), obj: _nodeFactory.CreateUriNode(UriFactory.Create("http://blazegraph-webapp/selfie/scene/" + parentLabel.Name)) )); } } // // updated triples in graph // try { _blazegraph.UpdateGraph(UriFactory.Create(SELFIES_GRAPH), triples, new List <Triple>()); context.Logger.LogLine("Updated triples successfully."); } catch (Exception ex) { context.Logger.LogLine(ex.Message); context.Logger.LogLine(ex.InnerException.Message); return; } } } } catch (AmazonS3Exception ex) { context.Logger.LogLine(ex.Message); } } }
/// <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; }