Ejemplo n.º 1
0
        public Graph LoadExampleGraph(string uri)
        {
            Graph loadGraph = new Graph();

            connector.LoadGraph(loadGraph, UriFactory.Create(uri));
            return(loadGraph);
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 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 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);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// A function handling the APIGatewayProxyRequest
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
        {
            if (!_supportedExportTypes.Contains(request.QueryStringParameters["exportType"]))
            {
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = 400,
                    Body = "Invalid export type.",
                    Headers = new Dictionary <string, string>()
                    {
                        { "Access-Control-Allow-Origin", ALLOWED_ORIGIN }
                    }
                });
            }

            //
            // load selfies graph
            //
            IGraph selfiesGraph = new Graph();

            _blazegraph.LoadGraph(selfiesGraph, UriFactory.Create(SELFIES_GRAPH));

            //
            // create stream
            //
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        switch (request.QueryStringParameters["exportType"])
                        {
                        case "ttl":
                            CompressingTurtleWriter turtleWriter = new CompressingTurtleWriter(TurtleSyntax.W3C)
                            {
                                PrettyPrintMode = true
                            };
                            turtleWriter.Save(g: selfiesGraph, output: writer, leaveOpen: true);
                            break;

                        case "rdf":
                            PrettyRdfXmlWriter rdfXmlWriter = new PrettyRdfXmlWriter()
                            {
                                PrettyPrintMode = true
                            };
                            rdfXmlWriter.Save(g: selfiesGraph, output: writer, leaveOpen: true);
                            break;
                        }

                        //
                        // put stream into S3
                        //
                        await _s3Client.PutObjectAsync(new PutObjectRequest()
                        {
                            InputStream = stream,
                            BucketName  = TARGET_BUCKET,
                            Key         = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss", DateTimeFormatInfo.InvariantInfo) + "." + request.QueryStringParameters["exportType"]
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                context.Logger.LogLine(ex.Message);
                return(new APIGatewayProxyResponse()
                {
                    StatusCode = 500,
                    Body = ex.Message,
                    Headers = new Dictionary <string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Access-Control-Allow-Origin", ALLOWED_ORIGIN }
                    }
                });
            }

            //
            //  return successfull response
            //
            return(new APIGatewayProxyResponse()
            {
                StatusCode = 200,
                Headers = new Dictionary <string, string>()
                {
                    { "Access-Control-Allow-Origin", ALLOWED_ORIGIN }
                }
            });
        }