Example #1
0
        public void UpdateExampleGraph(string uri)
        {
            Graph newGraph = new Graph();

            newGraph.BaseUri = UriFactory.Create(uri);

            Triple triple2remove = new Triple(
                newGraph.CreateUriNode(UriFactory.Create("http://example/book1")),
                newGraph.CreateUriNode(UriFactory.Create("http://example.org/ns#price")),
                newGraph.CreateLiteralNode("42", new Uri(XmlSpecsHelper.XmlSchemaDataTypeInteger))
                );
            Triple triple2add = new Triple(
                newGraph.CreateUriNode(UriFactory.Create("http://example/book1")),
                newGraph.CreateUriNode(UriFactory.Create("http://purl.org/dc/elements/1.1/title")),
                newGraph.CreateLiteralNode("Fundamentals of Compiler Design", new Uri(XmlSpecsHelper.XmlSchemaDataTypeString))
                );

            connector.UpdateGraph(
                UriFactory.Create(uri),
                new List <Triple>()
            {
                triple2add
            },
                new List <Triple>()
            {
                triple2remove
            }
                );
        }
        private void UpdateObject <T>(T obj1, Func <T, string> idFunc)
            where T : class
        {
            using (var _graphConn = new BlazegraphConnector("http://51.144.44.26:8080/blazegraph", "oqtopus-main"))
            {
                var grap1 = new Graph();
                grap1.BaseUri = new Uri("http://graph.oqtopus.io/raw/logic");

                var lstTriples = new List <Triple>();

                _graphConn.UpdateGraph("http://graph.oqtopus.io/raw/logic", lstTriples, Enumerable.Empty <Triple>());
            }
        }
Example #3
0
        /// <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);
                }
            }
        }
Example #4
0
        /// <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);
                }
            }
        }