예제 #1
0
        /// <summary>
        /// Obtiene los detalles de un rdf
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsRdf(Guid itemId)
        {
            //Cargamos la ontología
            RohGraph ontologyGraph = new RohGraph();

            ontologyGraph = _callEDtlPublishService.CallGetOntology();

            SparqlResultSet sparqlResultSetNombresPropiedades = (SparqlResultSet)ontologyGraph.ExecuteQuery("select distinct ?entidad ?nombre where { ?entidad <http://www.w3.org/2000/01/rdf-schema#label> ?nombre. FILTER(lang(?nombre) = 'es')}");

            //Guardamos todos los nombres de las propiedades en un diccionario
            Dictionary <string, string> communNamePropierties = new Dictionary <string, string>();

            foreach (SparqlResult sparqlResult in sparqlResultSetNombresPropiedades.Results)
            {
                communNamePropierties.Add(sparqlResult["entidad"].ToString(), ((LiteralNode)(sparqlResult["nombre"])).Value);
            }

            //Cargamos los datos
            DiscoverItem discoveryGraph = _discoverItemService.GetDiscoverItemById(itemId);
            RohGraph     dataGraph      = new RohGraph();

            dataGraph.LoadFromString(discoveryGraph.DiscoverRdf, new RdfXmlParser());

            //Guardamos todas las entidades que no son blankNodes
            List <String>   entities = new List <string>();
            SparqlResultSet sparqlResultSetListaEntidadesNotBN = (SparqlResultSet)dataGraph.ExecuteQuery("select ?s count(?p) as ?num where { ?s ?p ?o. FILTER (!isBlank(?s)) }group by ?s order by desc(?num) ");

            foreach (SparqlResult sparqlResult in sparqlResultSetListaEntidadesNotBN.Results)
            {
                entities.Add(sparqlResult["s"].ToString());
            }
            List <DiscoverRdfViewModel> model = new List <DiscoverRdfViewModel>();

            //Guardamos todas las entidades
            List <String>   allEntities = new List <string>();
            SparqlResultSet sparqlResultSetEntidades = (SparqlResultSet)dataGraph.ExecuteQuery("select distinct ?s where { ?s ?p ?o }");

            foreach (SparqlResult sparqlResult in sparqlResultSetEntidades.Results)
            {
                allEntities.Add(sparqlResult["s"].ToString());
            }
            SparqlResultSet sparqlResultSet = (SparqlResultSet)dataGraph.ExecuteQuery("select ?s ?p ?o where { ?s ?p ?o }");
            Dictionary <string, List <SparqlResult> > entitySparqlResult = new Dictionary <string, List <SparqlResult> >();

            foreach (SparqlResult sparqlResult in sparqlResultSet.Results)
            {
                if (!entitySparqlResult.ContainsKey(sparqlResult["s"].ToString()))
                {
                    entitySparqlResult.Add(sparqlResult["s"].ToString(), new List <SparqlResult>());
                }
                entitySparqlResult[sparqlResult["s"].ToString()].Add(sparqlResult);
            }

            foreach (var idEntity in entities)
            {
                DiscoverRdfViewModel entidad = createDiscoverRdfViewModel(idEntity, entitySparqlResult, new List <string>(), allEntities, communNamePropierties, discoveryGraph.LoadedEntities);
                model.Add(entidad);
            }
            return(View(model));
        }
        /// <summary>
        /// Elimina todos los triples de la BBDD cuyo sujeto y predicado estén en el RDF a cargar y estén marcados como monovaluados.
        /// </summary>
        /// <param name="pOntologyGraph">Grafo con la ontología</param>
        /// <param name="pDataInferenceGraph">Grafo con los datos a cargar (con inferencia)</param>
        private void RemoveMonovaluatedProperties(RohGraph pOntologyGraph, RohGraph pDataInferenceGraph)
        {
            //1º Obtnemos las propiedades monovaluadas de las clases
            Dictionary <string, HashSet <string> > classMonovaluateProperty = new Dictionary <string, HashSet <string> >();

            SparqlResultSet sparqlResultSet2 = (SparqlResultSet)pOntologyGraph.ExecuteQuery(
                @"  select distinct ?class ?onProperty
                                {
                                    ?class a <http://www.w3.org/2002/07/owl#Class>.
                                    {
                                        ?class <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?restriction.
                                    }
                                    UNION
                                    {
                                        ?class <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?intersection.
                                        ?intersection <http://www.w3.org/2002/07/owl#intersectionOf> ?intersectionOf.
                                        ?intersectionOf <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest>*/<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> ?restriction.
                                    }
                                    ?restriction a <http://www.w3.org/2002/07/owl#Restriction>.
                                    ?restriction <http://www.w3.org/2002/07/owl#onProperty> ?onProperty.
                                    {
                                        ?restriction ?propCardinality ""1""^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger>.
                                        FILTER(?propCardinality in (<http://www.w3.org/2002/07/owl#maxQualifiedCardinality>, <http://www.w3.org/2002/07/owl#qualifiedCardinality>,<http://www.w3.org/2002/07/owl#maxCardinality>,<http://www.w3.org/2002/07/owl#cardinality>))
                                    }
                                }");

            foreach (SparqlResult sparqlResult in sparqlResultSet2.Results)
            {
                if (!classMonovaluateProperty.ContainsKey(sparqlResult["class"].ToString()))
                {
                    classMonovaluateProperty.Add(sparqlResult["class"].ToString(), new HashSet <string>());
                }
                classMonovaluateProperty[sparqlResult["class"].ToString()].Add(sparqlResult["onProperty"].ToString());
            }

            //2º Obtenemos del grafo a cargar los triples (con ?s ?p) de las propiedades monovaluadas
            Dictionary <string, HashSet <string> > entityMonovaluateProperty = new Dictionary <string, HashSet <string> >();

            foreach (string clas in classMonovaluateProperty.Keys)
            {
                SparqlResultSet sparqlResultSet3 = (SparqlResultSet)pDataInferenceGraph.ExecuteQuery(
                    @$ " select distinct ?entityID ?property
                                    {{
예제 #3
0
        /// <summary>
        /// Obtiene los detalles de un error de descubrimiento
        /// </summary>
        /// <returns></returns>
        public IActionResult Details(Guid itemId)
        {
            var discovery = _discoverItemService.GetDiscoverItemById(itemId);
            DiscoverItemViewModel model = new DiscoverItemViewModel();

            model.DissambiguationProblems       = new Dictionary <string, List <string> >();
            model.DissambiguationProblemsTitles = new Dictionary <string, string>();
            if (discovery.Status.Equals("Error"))
            {
                model.Error          = discovery.Error;
                model.JobId          = discovery.JobID;
                model.IdDiscoverItem = discovery.ID;
            }
            else
            {
                RohGraph dataGraph = new RohGraph();
                dataGraph.LoadFromString(discovery.DiscoverRdf, new RdfXmlParser());
                model.JobId = discovery.JobID;

                foreach (var item in discovery.DissambiguationProblems)
                {
                    model.IdDiscoverItem = item.DiscoverItemID;
                    if (!model.DissambiguationProblems.ContainsKey(item.IDOrigin))
                    {
                        model.DissambiguationProblems.Add(item.IDOrigin, new List <string>());
                        model.DissambiguationProblemsTitles.Add(item.IDOrigin, "");
                        SparqlResultSet sparqlResultSet = (SparqlResultSet)dataGraph.ExecuteQuery("select ?title where{<" + item.IDOrigin + "> ?prop ?title. FILTER(?prop in (<http://purl.org/roh#title>,<http://purl.org/roh/mirror/foaf#name>))}");
                        foreach (SparqlResult sparqlResult in sparqlResultSet.Results)
                        {
                            model.DissambiguationProblemsTitles[item.IDOrigin] = ((LiteralNode)(sparqlResult["title"])).Value;
                        }
                    }
                    foreach (var problem in item.DissambiguationCandiates)
                    {
                        string opcion = $"{problem.IDCandidate} || {Math.Round(problem.Score, 3)}";
                        model.DissambiguationProblems[item.IDOrigin].Add(opcion);
                    }
                }
            }
            return(View(model));
        }
        /// <summary>
        /// Elimina los triples http://purl.org/roh/mirror/foaf#primaryTopic del RDF a cargar y los triples que tenían cagados en la BBDD
        /// </summary>
        /// <param name="pDataGraph">Grafo con los datos a cargar</param>
        /// <returns>Lista de grafos afectados</returns>
        private HashSet <string> RemovePrimaryTopics(ref RohGraph pDataGraph)
        {
            HashSet <string> graphs          = new HashSet <string>();
            List <string>    mainEntities    = new List <string>();
            string           query           = @"select distinct * where{?s <http://purl.org/roh/mirror/foaf#primaryTopic> ""true""^^<http://www.w3.org/2001/XMLSchema#boolean>}";
            SparqlResultSet  sparqlResultSet = (SparqlResultSet)pDataGraph.ExecuteQuery(query.ToString());

            foreach (SparqlResult sparqlResult in sparqlResultSet.Results)
            {
                mainEntities.Add(sparqlResult["s"].ToString());
            }

            //Se eliminan todas las referncias a las entidades principales
            if (mainEntities.Count > 0)
            {
                foreach (string mainEntity in mainEntities)
                {
                    graphs.UnionWith(DeleteUpdatedEntity(mainEntity));
                }
            }

            //Eliminamos el triple que marca las entidades principales para que no se inserte en la BBDD
            {
                TripleStore store = new TripleStore();
                store.Add(pDataGraph);
                SparqlUpdateParser parser = new SparqlUpdateParser();
                //Actualizamos los sujetos
                SparqlUpdateCommandSet updateSubject = parser.ParseFromString(
                    @"  DELETE { ?s ?p ?o. }
                                    WHERE 
                                    {
                                        ?s ?p ?o. FILTER(?p =<http://purl.org/roh/mirror/foaf#primaryTopic>)
                                    }");
                LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
                processor.ProcessCommandSet(updateSubject);
            }
            return(graphs);
        }
예제 #5
0
        public SparqlObject SelectData(string pSPARQLEndpoint, string pGraph, string pConsulta, string pQueryParam, string pUsername, string pPassword)
        {
            SparqlResultSet sparqlResultSet = (SparqlResultSet)_dataGraph.ExecuteQuery(pConsulta.ToString());
            SparqlObject    sparqlObject    = new SparqlObject();

            sparqlObject.results          = new SparqlObject.Results();
            sparqlObject.results.bindings = new List <Dictionary <string, SparqlObject.Data> >();
            foreach (var sparqlResult in sparqlResultSet)
            {
                Dictionary <string, SparqlObject.Data> dict = new Dictionary <string, SparqlObject.Data>();
                foreach (var variable in sparqlResult.Variables)
                {
                    SparqlObject.Data data = new SparqlObject.Data();
                    if (sparqlResult[variable] != null)
                    {
                        data.type = variable;
                        if (sparqlResult[variable] is LiteralNode)
                        {
                            data.value = ((LiteralNode)(sparqlResult[variable])).Value;
                            if (((LiteralNode)sparqlResult[variable]).DataType != null)
                            {
                                data.datatype = ((LiteralNode)sparqlResult[variable]).DataType.ToString();
                            }
                        }
                        else
                        {
                            data.value = sparqlResult[data.type].ToString();
                        }
                        dict.Add(data.type, data);
                    }
                }

                sparqlObject.results.bindings.Add(dict);
            }
            return(sparqlObject);
        }
예제 #6
0
        /// <summary>
        /// Aplica el descubrimiento sobre las entidades cargadas en el SGI
        /// </summary>
        /// <param name="pSecondsSleep">Segundos para dormir después de procesar una entidad</param>
        /// <param name="pCallUrisFactoryApiService">Servicio para hacer llamadas a los métodos del Uris Factory</param>
        public void ApplyDiscoverLoadedEntities(int pSecondsSleep, CallUrisFactoryApiService pCallUrisFactoryApiService)
        {
            CallEtlApiService callEtlApiService = _serviceScopeFactory.CreateScope().ServiceProvider.GetRequiredService <CallEtlApiService>();

            #region Cargamos configuraciones
            ConfigSparql ConfigSparql             = new ConfigSparql();
            string       SGI_SPARQLEndpoint       = ConfigSparql.GetEndpoint();
            string       SGI_SPARQLGraph          = ConfigSparql.GetGraph();
            string       SGI_SPARQLQueryParam     = ConfigSparql.GetQueryParam();
            string       SGI_SPARQLUsername       = ConfigSparql.GetUsername();
            string       SGI_SPARQLPassword       = ConfigSparql.GetPassword();
            string       Unidata_SPARQLEndpoint   = ConfigSparql.GetUnidataEndpoint();
            string       Unidata_SPARQLGraph      = ConfigSparql.GetUnidataGraph();
            string       Unidata_SPARQLQueryParam = ConfigSparql.GetUnidataQueryParam();
            string       Unidata_SPARQLUsername   = ConfigSparql.GetUnidataUsername();
            string       Unidata_SPARQLPassword   = ConfigSparql.GetUnidataPassword();

            ConfigService ConfigService       = new ConfigService();
            string        UnidataDomain       = ConfigService.GetUnidataDomain();
            string        UnidataUriTransform = ConfigService.GetUnidataUriTransform();
            float         MaxScore            = ConfigService.GetMaxScore();
            float         MinScore            = ConfigService.GetMinScore();

            ConfigScopus   ConfigScopus      = new ConfigScopus();
            string         ScopusApiKey      = ConfigScopus.GetScopusApiKey();
            string         ScopusUrl         = ConfigScopus.GetScopusUrl();
            ConfigCrossref ConfigCrossref    = new ConfigCrossref();
            string         CrossrefUserAgent = ConfigCrossref.GetCrossrefUserAgent();
            ConfigWOS      ConfigWOS         = new ConfigWOS();
            string         WOSAuthorization  = ConfigWOS.GetWOSAuthorization();
            #endregion

            DiscoverUtility discoverUtility = new DiscoverUtility();

            //Cargar todas las personas en la lista de manera aleatoria.
            List <string> personList       = discoverUtility.GetPersonList(SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword);
            List <string> randomPersonList = GetRandomOrderList(personList);
            RohGraph      ontologyGraph    = callEtlApiService.CallGetOntology();
            foreach (string person in randomPersonList)
            {
                try
                {
                    //Hora de inicio de la ejecución
                    DateTime startTime = DateTime.Now;

                    //Obtener el RohGraph de una única persona.
                    RohGraph dataGraph = discoverUtility.GetDataGraphPersonLoadedForDiscover(person, SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword);
                    //Clonamos el grafo original para hacer luego comprobaciones
                    RohGraph originalDataGraph = dataGraph.Clone();

                    RohRdfsReasoner reasoner = new RohRdfsReasoner();
                    reasoner.Initialise(ontologyGraph);
                    RohGraph dataInferenceGraph = dataGraph.Clone();
                    reasoner.Apply(dataInferenceGraph);

                    bool hasChanges = false;
                    //Dictionary<string, string> discoveredEntityList = new Dictionary<string, string>();
                    Dictionary <string, Dictionary <string, float> >            discoveredEntitiesProbability = new Dictionary <string, Dictionary <string, float> >();
                    Dictionary <string, ReconciliationData.ReconciliationScore> entidadesReconciliadasConIntegracionExternaAux;
                    Dictionary <string, HashSet <string> > discardDissambiguations = new Dictionary <string, HashSet <string> >();
                    DiscoverCache       discoverCache       = new DiscoverCache();
                    DiscoverCacheGlobal discoverCacheGlobal = new DiscoverCacheGlobal();

                    //Obtención de la integración externa
                    ReconciliationData reconciliationData = new ReconciliationData();
                    DiscoverLinkData   discoverLinkData   = new DiscoverLinkData();
                    Dictionary <string, List <DiscoverLinkData.PropertyData> > integration = discoverUtility.ExternalIntegration(ref hasChanges, ref reconciliationData, ref discoverLinkData, ref discoveredEntitiesProbability, ref dataGraph, reasoner, null, ontologyGraph, out entidadesReconciliadasConIntegracionExternaAux, discardDissambiguations, discoverCache, discoverCacheGlobal, ScopusApiKey, ScopusUrl, CrossrefUserAgent, WOSAuthorization, MinScore, MaxScore, SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword, pCallUrisFactoryApiService, false);

                    //Limpiamos 'integration' para no insertar triples en caso de que ya estén cargados
                    foreach (string entity in integration.Keys.ToList())
                    {
                        foreach (DiscoverLinkData.PropertyData propertyData in integration[entity].ToList())
                        {
                            string           p       = propertyData.property;
                            HashSet <string> objetos = new HashSet <string>(propertyData.valueProvenance.Keys.ToList());
                            foreach (string o in objetos)
                            {
                                if (((SparqlResultSet)originalDataGraph.ExecuteQuery($@"ASK
                                        WHERE 
                                        {{
                                            ?s ?p ?o.
                                            FILTER(?s=<{entity}>)
                                            FILTER(?p=<{p}>)
                                            FILTER(str(?o)='{o}')
                                        }}")).Result)
                                {
                                    //Elimiamos el valor porque ya estaba cargado
                                    propertyData.valueProvenance.Remove(o);
                                }
                            }
                            if (propertyData.valueProvenance.Count == 0)
                            {
                                integration[entity].Remove(propertyData);
                            }
                        }
                        if (integration[entity].Count == 0)
                        {
                            integration.Remove(entity);
                        }
                    }


                    //Creación de dataGraph con el contenido de 'integration' + RdfTypes + SameAS
                    RohGraph dataGraphIntegration = new RohGraph();
                    foreach (string sujeto in integration.Keys)
                    {
                        IUriNode s = dataGraphIntegration.CreateUriNode(UriFactory.Create(sujeto));

                        //Agregamos SameAs y RDFType de las entidades
                        SparqlResultSet sparqlResultSet = (SparqlResultSet)dataGraph.ExecuteQuery("select ?rdftype ?sameas where {?s a ?rdftype. OPTIONAL{?s <http://www.w3.org/2002/07/owl#sameAs> ?sameAS} FILTER(?s=<" + sujeto + ">)}");
                        foreach (SparqlResult sparqlResult in sparqlResultSet.Results)
                        {
                            string   rdfType  = sparqlResult["rdftype"].ToString();
                            IUriNode pRdfType = dataGraphIntegration.CreateUriNode(UriFactory.Create("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
                            IUriNode oRdfType = dataGraphIntegration.CreateUriNode(UriFactory.Create(rdfType));
                            dataGraphIntegration.Assert(new Triple(s, pRdfType, oRdfType));
                            if (sparqlResult.Variables.Contains("sameas"))
                            {
                                string   sameas  = sparqlResult["sameas"].ToString();
                                IUriNode pSameAs = dataGraphIntegration.CreateUriNode(UriFactory.Create("http://www.w3.org/2002/07/owl#sameAs"));
                                IUriNode oSameAs = dataGraphIntegration.CreateUriNode(UriFactory.Create(sameas));
                                dataGraphIntegration.Assert(new Triple(s, pSameAs, oSameAs));
                            }
                        }

                        foreach (DiscoverLinkData.PropertyData propertyData in integration[sujeto])
                        {
                            foreach (string valor in propertyData.valueProvenance.Keys)
                            {
                                IUriNode p = dataGraphIntegration.CreateUriNode(UriFactory.Create(propertyData.property));
                                if (Uri.IsWellFormedUriString(valor, UriKind.Absolute))
                                {
                                    IUriNode uriNode = dataGraphIntegration.CreateUriNode(UriFactory.Create(propertyData.property));
                                    dataGraphIntegration.Assert(new Triple(s, p, uriNode));
                                }
                                else
                                {
                                    ILiteralNode literalNode = dataGraphIntegration.CreateLiteralNode(valor, new Uri("http://www.w3.org/2001/XMLSchema#string"));
                                    dataGraphIntegration.Assert(new Triple(s, p, literalNode));
                                }

                                foreach (string org in propertyData.valueProvenance[valor])
                                {
                                    //Agregamos los datos de las organizaciones y los grafos
                                    SparqlResultSet sparqlResultSetOrgs = (SparqlResultSet)dataGraph.ExecuteQuery("select ?s ?p ?o where {?s ?p ?o. FILTER(?s in(<" + pCallUrisFactoryApiService.GetUri("http://purl.org/roh/mirror/foaf#Organization", org) + ">,<" + pCallUrisFactoryApiService.GetUri("Graph", org) + "> ))}");
                                    foreach (SparqlResult sparqlResult in sparqlResultSetOrgs.Results)
                                    {
                                        INode sOrg = dataGraphIntegration.CreateUriNode(UriFactory.Create(sparqlResult["s"].ToString()));
                                        INode pOrg = dataGraphIntegration.CreateUriNode(UriFactory.Create(sparqlResult["p"].ToString()));
                                        if (sparqlResult["o"] is UriNode)
                                        {
                                            INode oOrg = dataGraphIntegration.CreateUriNode(UriFactory.Create(sparqlResult["o"].ToString()));
                                            dataGraphIntegration.Assert(new Triple(sOrg, pOrg, oOrg));
                                        }
                                        else if (sparqlResult["o"] is LiteralNode)
                                        {
                                            INode oOrg = dataGraphIntegration.CreateLiteralNode(((LiteralNode)sparqlResult["o"]).Value, ((LiteralNode)sparqlResult["o"]).DataType);
                                            dataGraphIntegration.Assert(new Triple(sOrg, pOrg, oOrg));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //Hora fin de la ejecución
                    DateTime endTime = DateTime.Now;
                    if (integration.Count > 0)
                    {
                        //Si hay datos nuevos los cargamos
                        string urlDiscoverAgent = pCallUrisFactoryApiService.GetUri("Agent", "discover");

                        //Publicamos en el SGI
                        AsioPublication asioPublication = new AsioPublication(SGI_SPARQLEndpoint, SGI_SPARQLQueryParam, SGI_SPARQLGraph, SGI_SPARQLUsername, SGI_SPARQLPassword);
                        asioPublication.PublishRDF(dataGraphIntegration, null, new KeyValuePair <string, string>(urlDiscoverAgent, "Algoritmos de descubrimiento"), startTime, endTime, discoverLinkData, pCallUrisFactoryApiService);


                        //Preparamos los datos para cargarlos en Unidata
                        RohGraph unidataGraph = dataGraphIntegration.Clone();
                        #region Si no tiene un sameAs apuntando a Unidata lo eliminamos, no hay que cargar la entidad
                        SparqlResultSet           sparqlResultSet           = (SparqlResultSet)unidataGraph.ExecuteQuery("select ?s ?rdftype ?sameas where {?s a ?rdftype. OPTIONAL{?s <http://www.w3.org/2002/07/owl#sameAs> ?sameAS} }");
                        Dictionary <string, bool> entidadesConSameAsUnidata = new Dictionary <string, bool>();
                        foreach (SparqlResult sparqlResult in sparqlResultSet.Results)
                        {
                            string s = sparqlResult["s"].ToString();
                            if (!entidadesConSameAsUnidata.ContainsKey(s))
                            {
                                entidadesConSameAsUnidata.Add(s, false);
                            }
                            if (sparqlResult.Variables.Contains("sameas"))
                            {
                                if (sparqlResult["sameas"].ToString().StartsWith(UnidataDomain))
                                {
                                    entidadesConSameAsUnidata[s] = true;
                                }
                            }
                        }
                        TripleStore store = new TripleStore();
                        store.Add(unidataGraph);
                        foreach (string entity in entidadesConSameAsUnidata.Keys)
                        {
                            if (!entidadesConSameAsUnidata[entity])
                            {
                                //Cambiamos candidato.Key por entityID
                                SparqlUpdateParser       parser    = new SparqlUpdateParser();
                                SparqlUpdateCommandSet   delete    = parser.ParseFromString(@"DELETE { ?s ?p ?o. }
                                                                                WHERE 
                                                                                {
                                                                                    ?s ?p ?o. 
                                                                                    FILTER(?s = <" + entity + @">)
                                                                                
                                                                                }");
                                LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
                                processor.ProcessCommandSet(delete);
                            }
                        }
                        #endregion

                        //TODO descomentar cuando esté habilitaado Unidata
                        ////Si hay triples para cargar en Unidata procedemos
                        //if (unidataGraph.Triples.ToList().Count > 0)
                        //{
                        //    //Publicamos en UNIDATA
                        //    AsioPublication asioPublicationUnidata = new AsioPublication(Unidata_SPARQLEndpoint, Unidata_SPARQLQueryParam, Unidata_SPARQLGraph, Unidata_SPARQLUsername, Unidata_SPARQLPassword);
                        //    // Prepara el grafo para su carga en Unidata, para ello coge las URIs de Unidata del SameAs y la aplica a los sujetos y los antiguos sujetos se agregan al SameAs
                        //    unidataGraph = AsioPublication.TransformUrisToUnidata(unidataGraph, UnidataDomain, UnidataUriTransform);
                        //    asioPublicationUnidata.PublishRDF(unidataGraph, null, new KeyValuePair<string, string>(urlDiscoverAgent, "Algoritmos de descubrimiento"), startTime, endTime, discoverLinkData,pCallUrisFactoryApiService);
                        //}
                    }
                }
                catch (Exception exception)
                {
                    Logging.Error(exception);
                }
                Thread.Sleep(pSecondsSleep * 1000);
            }
        }
예제 #7
0
        public IActionResult Index()
        {
            //Obtenemos la URL de la entidad
            string url = Request.GetEncodedUrl();
            //string url = Request.GetDisplayUrl();
            string urlParam = HttpUtility.ParseQueryString(Request.QueryString.Value).Get("url");

            if (!string.IsNullOrEmpty(urlParam))
            {
                url = urlParam;
            }
            ViewBag.UrlHome = mConfigService.GetUrlHome();

            //Customizamos Header
            if (!string.IsNullOrEmpty(mConfigService.GetConstrainedByUrl()))
            {
                HttpContext.Response.Headers.Add("Link", "<http://www.w3.org/ns/ldp#BasicContainer>; rel=\"type\", <http://www.w3.org/ns/ldp#Resource>; rel=\"type\", <" + mConfigService.GetConstrainedByUrl() + ">; rel=\"http://www.w3.org/ns/ldp#constrainedBy\"");
            }
            else
            {
                HttpContext.Response.Headers.Add("Link", "<http://www.w3.org/ns/ldp#BasicContainer>; rel=\"type\", <http://www.w3.org/ns/ldp#Resource>; rel=\"type\"");
            }
            HashSet <string> methodsAvailable = new HashSet <string>()
            {
                "GET", "HEAD", "OPTIONS"
            };

            HttpContext.Response.Headers.Add("allow", string.Join(", ", methodsAvailable));
            if (!methodsAvailable.Contains(Request.HttpContext.Request.Method))
            {
                return(StatusCode(StatusCodes.Status405MethodNotAllowed));
            }

            //Cargamos la ontología
            RohGraph        ontologyGraph = LoadGraph(mConfigService.GetOntologyGraph());
            SparqlResultSet sparqlResultSetNombresPropiedades = (SparqlResultSet)ontologyGraph.ExecuteQuery(@"select distinct ?entidad ?nombre lang(?nombre) as ?lang where 
                                                                                                            { 
                                                                                                                ?entidad <http://www.w3.org/2000/01/rdf-schema#label> ?nombre. 
                                                                                                            }");

            //Guardamos todos los nombres de las propiedades en un diccionario
            Dictionary <string, string> communNamePropierties = new Dictionary <string, string>();

            foreach (SparqlResult sparqlResult in sparqlResultSetNombresPropiedades.Results)
            {
                string entity = sparqlResult["entidad"].ToString();
                if (!communNamePropierties.ContainsKey(entity))
                {
                    List <SparqlResult> filas = sparqlResultSetNombresPropiedades.Results.Where(x => x["entidad"].ToString() == entity).ToList();
                    if (filas.FirstOrDefault(x => x["lang"].ToString() == "es") != null)
                    {
                        communNamePropierties[entity] = ((LiteralNode)filas.FirstOrDefault(x => x["lang"].ToString() == "es")["nombre"]).Value.ToString();
                    }
                    else if (filas.FirstOrDefault(x => x["lang"].ToString() == "en") != null)
                    {
                        communNamePropierties[entity] = ((LiteralNode)filas.FirstOrDefault(x => x["lang"].ToString() == "en")["nombre"]).Value.ToString();
                    }
                    else if (filas.FirstOrDefault(x => string.IsNullOrEmpty(x["lang"].ToString())) != null)
                    {
                        communNamePropierties[entity] = ((LiteralNode)filas.FirstOrDefault(x => string.IsNullOrEmpty(x["lang"].ToString()))["nombre"]).Value.ToString();
                    }
                }
            }

            //Cargamos las entidades propias
            Dictionary <string, string>       entitiesNames;
            Dictionary <string, SparqlObject> sparqlObjectDictionary = GetEntityData(url, out entitiesNames);

            if (sparqlObjectDictionary.Count == 1 && sparqlObjectDictionary[url].results.bindings.Count == 0)
            {
                //No existe la entidad
                HttpContext.Response.StatusCode = 404;
                ViewData["Title"]     = "Error 404 página no encontrada para la entidad " + url;
                ViewData["NameTitle"] = mConfigService.GetNameTitle();
                return(View(new EntityModelTemplate()));
            }
            else
            {
                //Cargamos los datos en un grafo en Local
                RohGraph dataGraph = new RohGraph();
                createDataGraph(url, new List <string>(), false, dataGraph, sparqlObjectDictionary);

                //Generamos el RDF
                System.IO.StringWriter sw           = new System.IO.StringWriter();
                RdfXmlWriter           rdfXmlWriter = new RdfXmlWriter();
                rdfXmlWriter.Save(dataGraph, sw);
                string rdf = sw.ToString();

                Microsoft.Extensions.Primitives.StringValues stringvalues;
                HttpContext.Request.Headers.TryGetValue("accept", out stringvalues);
                if (stringvalues == "application/rdf+xml")
                {
                    //Añadimos la etiquetqa ETag al header
                    using (SHA256 sha256Hash = SHA256.Create())
                    {
                        string etag        = GetHash(sha256Hash, rdf);
                        string ifNoneMatch = HttpContext.Request.Headers["If-None-Match"];
                        if (ifNoneMatch == etag)
                        {
                            HttpContext.Response.StatusCode = 304;
                        }
                        HttpContext.Response.Headers.Add("ETag", etag);
                    }

                    //Devolvemos en formato RDF
                    return(File(Encoding.UTF8.GetBytes(rdf), "application/rdf+xml"));
                }
                else
                {
                    RohRdfsReasoner reasoner = new RohRdfsReasoner();
                    reasoner.Initialise(ontologyGraph);
                    RohGraph dataInferenceGraph = dataGraph.Clone();
                    reasoner.Apply(dataInferenceGraph);

                    //Obtenemos datos del resto de grafos (para los provenance)
                    Dictionary <string, List <Dictionary <string, SparqlObject.Data> > > sparqlObjectDictionaryGraphs = GetEntityDataGraphs(url);

                    //Obtenemos las tablas configuradas
                    List <Table> dataTables = GetDataTables(dataInferenceGraph, url);

                    //Obtenemos los arborGrah configurados
                    List <ArborGraph> dataArborGrahs = GetDataArborGraphs(dataInferenceGraph, dataGraph, url);

                    //Obtenemos las 10 primeras entidades que apuntan a la entidad
                    HashSet <string> inverseEntities = new HashSet <string>();
                    SparqlResultSet  sparqlRdfType   = (SparqlResultSet)dataInferenceGraph.ExecuteQuery("select distinct ?o where {<" + url + "> a ?o. }");
                    HashSet <string> rdfTypesEntity  = new HashSet <string>();
                    foreach (SparqlResult sparqlResult in sparqlRdfType.Results)
                    {
                        rdfTypesEntity.Add(sparqlResult["o"].ToString());
                    }
                    if (mLinked_Data_Server_Config.ExcludeRelatedEntity.Intersect(rdfTypesEntity).Count() == 0)
                    {
                        inverseEntities = GetInverseEntities(dataGraph, new HashSet <string>()
                        {
                            url
                        }, new HashSet <string>(sparqlObjectDictionary.Keys), new Dictionary <string, SparqlObject>(), 10);
                    }

                    //Devolvemos en formato HTML
                    List <String>   allEntities = new List <string>();
                    SparqlResultSet sparqlResultSetEntidades = (SparqlResultSet)dataGraph.ExecuteQuery("select distinct ?p ?o where { ?s ?p ?o. FILTER (!isBlank(?o)) }");
                    foreach (SparqlResult sparqlResult in sparqlResultSetEntidades.Results)
                    {
                        if ((sparqlResult["o"] is UriNode) && (sparqlResult["p"].ToString() != "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))
                        {
                            allEntities.Add(sparqlResult["o"].ToString());
                        }
                    }

                    //Preparamos el modelo de la entidad principal
                    List <LinkedDataRdfViewModel> modelEntities = new List <LinkedDataRdfViewModel>();
                    LinkedDataRdfViewModel        entidad       = createLinkedDataRdfViewModel(url, dataGraph, sparqlObjectDictionaryGraphs, new List <string>(), allEntities, communNamePropierties, entitiesNames);
                    modelEntities.Add(entidad);
                    KeyValuePair <string, List <string> > titulo = entidad.stringPropertiesEntity.FirstOrDefault(x => mLinked_Data_Server_Config.PropsTitle.Contains(x.Key));
                    ViewData["Title"] = "About: " + url;
                    if (titulo.Key != null)
                    {
                        ViewData["Title"] = "About: " + titulo.Value[0];
                    }
                    ViewData["NameTitle"] = mConfigService.GetNameTitle();

                    //Preparamos el modelo del resto de entidades
                    foreach (string entity in inverseEntities)
                    {
                        LinkedDataRdfViewModel entidadInversa = createLinkedDataRdfViewModel(entity, dataGraph, null, new List <string>(), allEntities, communNamePropierties, entitiesNames);
                        modelEntities.Add(entidadInversa);
                    }


                    EntityModelTemplate entityModel = new EntityModelTemplate();
                    entityModel.linkedDataRDF  = modelEntities;
                    entityModel.propsTransform = mLinked_Data_Server_Config.PropsTransform;
                    entityModel.tables         = dataTables;
                    entityModel.arborGraphs    = dataArborGrahs;

                    //Añadimos la etiquetqa ETag al header
                    using (SHA256 sha256Hash = SHA256.Create())
                    {
                        string stringToHash = JsonConvert.SerializeObject(entityModel.linkedDataRDF);
                        stringToHash += JsonConvert.SerializeObject(entityModel.propsTransform);
                        stringToHash += JsonConvert.SerializeObject(entityModel.tables);
                        stringToHash += JsonConvert.SerializeObject(entityModel.arborGraphs);
                        string etag        = GetHash(sha256Hash, stringToHash);
                        string ifNoneMatch = HttpContext.Request.Headers["If-None-Match"];
                        if (ifNoneMatch == etag)
                        {
                            HttpContext.Response.StatusCode = 304;
                        }
                        HttpContext.Response.Headers.Add("ETag", etag);
                    }

                    return(View(entityModel));
                }
            }
        }
        public IActionResult Convert(IFormFile pXmlFile, string pType)
        {
            try
            {
                // Ruta del fichero de configuración JSON.
                string        rutaJson             = String.Empty;
                List <string> listaConfiguraciones = ConfigFilesList();
                if (listaConfiguraciones.Contains(pType))
                {
                    rutaJson = "Config/" + pType + ".json";
                }
                else
                {
                    return(Problem("El fichero de configuración JSON no existe."));
                }

                // Conversión a JSON.
                ConversorConfig objJson = JsonConvert.DeserializeObject <ConversorConfig>(System.IO.File.ReadAllText(rutaJson));

                // Lectura del archivo XML.
                StringBuilder resultXml = new StringBuilder();
                using (StreamReader reader = new StreamReader(pXmlFile.OpenReadStream()))
                {
                    while (reader.Peek() >= 0)
                    {
                        resultXml.AppendLine(reader.ReadLine());
                    }
                }
                string ficheroXml = resultXml.ToString();

                // Conversión a XML.
                XmlDocument documento = new XmlDocument();
                documento.LoadXml(ficheroXml);

                // Namespace.
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(documento.NameTable);

                // Creación del RDF virtual.
                RohGraph dataGraph = new RohGraph();
                CreateEntities(dataGraph, objJson.entities, documento, nsmgr); // Método Recursivo.

                // Consulta de comprobación.
                Object consulta = dataGraph.ExecuteQuery("SELECT * WHERE {?s ?p ?o}");

                // Creación del RDF.
                System.IO.StringWriter sw           = new System.IO.StringWriter();
                RdfXmlWriter           rdfXmlWriter = new RdfXmlWriter();
                rdfXmlWriter.Save(dataGraph, sw);

                // Comprobación RDF.
                string graphRDF = sw.ToString();

                // Obtener RDF.
                byte[] array = Encoding.UTF8.GetBytes(graphRDF);
                return(File(array, "application/rdf+xml"));
            }
            catch (Exception ex)
            {
                return(Problem(ex.ToString()));
            }
        }