コード例 #1
0
        /// <summary>
        /// Publica un RDF en Asio aplicado todos losprocedimientos pertinentes
        /// </summary>
        /// <param name="pDataGraph">Grafo con los datos a cargar</param>
        /// <param name="pOntologyGraph">Grafo con la ontología</param>
        /// <param name="pAttributedTo">Sujeto y nombre para atribuir los triples de los apis externos</param>
        /// <param name="pActivityStartedAtTime">Inicio del proceso</param>
        /// <param name="pActivityEndedAtTime">Fin del proceso</param>
        /// <param name="pDiscoverLinkData">Datos para trabajar con el descubrimiento de enlaces</param>
        /// <param name="pCallUrisFactoryApiService">Servicio para hacer llamadas a los métodos del Uris Factory</param>
        public void PublishRDF(RohGraph pDataGraph, RohGraph pOntologyGraph, KeyValuePair <string, string>?pAttributedTo, DateTime pActivityStartedAtTime, DateTime pActivityEndedAtTime, DiscoverLinkData pDiscoverLinkData, CallUrisFactoryApiService pCallUrisFactoryApiService)
        {
            RohGraph inferenceDataGraph = null;

            if (pOntologyGraph != null)
            {
                inferenceDataGraph = pDataGraph.Clone();
                RohRdfsReasoner reasoner = new RohRdfsReasoner();
                reasoner.Initialise(pOntologyGraph);
                reasoner.Apply(inferenceDataGraph);
            }

            // 1º Eliminamos de la BBDD las entidades principales que aparecen en el RDF
            HashSet <string> graphs = RemovePrimaryTopics(ref pDataGraph);

            graphs.Add(_Graph);

            // 2º Eliminamos todos los triples de la BBDD cuyo sujeto y predicado estén en el RDF a cargar y estén marcados como monovaluados.
            if (pOntologyGraph != null && inferenceDataGraph != null)
            {
                RemoveMonovaluatedProperties(pOntologyGraph, inferenceDataGraph);
            }

            //3º Insertamos los triples en la BBDD
            if (pAttributedTo.HasValue)
            {
                //Añadimos triples del softwareagent
                IUriNode t_subject           = pDataGraph.CreateUriNode(UriFactory.Create(pAttributedTo.Value.Key));
                IUriNode t_predicate_rdftype = pDataGraph.CreateUriNode(UriFactory.Create("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
                IUriNode t_object_rdftype    = pDataGraph.CreateUriNode(UriFactory.Create("http://www.w3.org/ns/prov#SoftwareAgent"));
                pDataGraph.Assert(new Triple(t_subject, t_predicate_rdftype, t_object_rdftype));
                IUriNode     t_predicate_name = pDataGraph.CreateUriNode(UriFactory.Create("http://purl.org/roh/mirror/foaf#name"));
                ILiteralNode t_object_name    = pDataGraph.CreateLiteralNode(pAttributedTo.Value.Value, new Uri("http://www.w3.org/2001/XMLSchema#string"));
                pDataGraph.Assert(new Triple(t_subject, t_predicate_name, t_object_name));
            }
            SparqlUtility.LoadTriples(SparqlUtility.GetTriplesFromGraph(pDataGraph), _SPARQLEndpoint, _QueryParam, _Graph, _Username, _Password);

            //4º Insertamos los triples con provenance en la BBDD
            if (pDiscoverLinkData != null && pDiscoverLinkData.entitiesProperties != null)
            {
                Dictionary <string, List <string> > graphDeletes = new Dictionary <string, List <string> >();
                Dictionary <string, List <string> > graphTriples = new Dictionary <string, List <string> >();
                foreach (string t_subject in pDiscoverLinkData.entitiesProperties.Keys)
                {
                    foreach (DiscoverLinkData.PropertyData property in pDiscoverLinkData.entitiesProperties[t_subject])
                    {
                        string t_property = property.property;
                        foreach (var prop in property.valueProvenance)
                        {
                            string           t_object    = prop.Key;
                            HashSet <string> t_sourceids = prop.Value;
                            foreach (string sourceId in t_sourceids)
                            {
                                string graph = pCallUrisFactoryApiService.GetUri("Graph", sourceId);
                                if (!graphTriples.ContainsKey(graph))
                                {
                                    graphTriples.Add(graph, new List <string>());
                                }
                                string bNodeid = "_:" + Guid.NewGuid().ToString();
                                graphTriples[graph].Add($@"<{t_subject}> <http://www.w3.org/ns/prov#wasUsedBy> {bNodeid} .");
                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/prov#Activity> .");
                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <{t_property}>.");
                                if (Uri.IsWellFormedUriString(t_object, UriKind.Absolute))
                                {
                                    graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> <{ t_object}>.");
                                }
                                else
                                {
                                    graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> ""{ t_object.Replace("\"", "\\\"").Replace("\n", "\\n") }""^^<http://www.w3.org/2001/XMLSchema#string>.");
                                }
                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/ns/prov#startedAtTime> ""{ pActivityStartedAtTime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz") }""^^<http://www.w3.org/2001/XMLSchema#datetime>.");
                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/ns/prov#endedAtTime> ""{ pActivityEndedAtTime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz") }""^^<http://www.w3.org/2001/XMLSchema#datetime>.");
                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/ns/prov#wasAssociatedWith> <{pAttributedTo.Value.Key}>.");

                                graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/ns/prov#wasAssociatedWith> <{pCallUrisFactoryApiService.GetUri("http://purl.org/roh/mirror/foaf#Organization", sourceId)}>.");

                                if (pAttributedTo.HasValue)
                                {
                                    graphTriples[graph].Add($@"{bNodeid} <http://www.w3.org/ns/prov#wasAssociatedWith> <{pAttributedTo.Value.Key}>.");
                                }

                                if (!graphDeletes.ContainsKey(graph))
                                {
                                    graphDeletes.Add(graph, new List <string>());
                                }

                                if (!Uri.IsWellFormedUriString(t_object, UriKind.Absolute))
                                {
                                    string stringDelete = $@"   {{
                                                                ?s ?p ?o. 
                                                                ?o <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <{t_property}>.
                                                                ?o <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> ""{ t_object.Replace("\"", "\\\"").Replace("\n", "\\n") }""^^<http://www.w3.org/2001/XMLSchema#string>.
                                                                FILTER(?s = <{t_subject}>)
                                                            }}";
                                    graphDeletes[graph].Add(stringDelete);
                                }
                            }
                        }
                    }
                }

                //Eliminamos aquellos triples de provenance que ya estén cargados
                foreach (string graph in graphDeletes.Keys)
                {
                    graphs.Add(graph);
                    string queryDeleteProvenance = $@"  DELETE {{ ?s ?p ?o. }}
                                                        WHERE 
                                                        {{
                                                            {{{string.Join("}UNION{", graphDeletes[graph])}}}
                                                        }}";
                    _SparqlUtility.SelectData(_SPARQLEndpoint, graph, queryDeleteProvenance, _QueryParam, _Username, _Password);
                }

                //Cargamos los nuevos triples
                foreach (string graph in graphTriples.Keys)
                {
                    SparqlUtility.LoadTriples(graphTriples[graph], _SPARQLEndpoint, _QueryParam, graph, _Username, _Password);
                }
            }

            //5º Limpiamos los blanknodes huerfanos, o que no tengan triples
            //TODO mover a una tarea que se ejecute continuamente
            //DeleteOrphanNodes(graphs);
        }
コード例 #2
0
        //TODO mover a otro sitio


        /// <summary>
        /// Realiza el proceso completo de desubrimiento sobre un RDF
        /// </summary>
        /// <param name="pDiscoverItem">Item de descubrimiento</param>
        /// <param name="pCallEtlApiService">Servicio para hacer llamadas a los métodos del controlador etl del API_CARGA </param>
        /// <param name="pCallUrisFactoryApiService">Servicio para hacer llamadas a los métodos del Uris Factory</param>
        /// <returns>DiscoverResult con los datos del descubrimiento</returns>
        private DiscoverResult Init(DiscoverItem pDiscoverItem, CallEtlApiService pCallEtlApiService, CallUrisFactoryApiService pCallUrisFactoryApiService)
        {
            #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();
            float          MaxScore          = ConfigService.GetMaxScore();
            float          MinScore          = ConfigService.GetMinScore();
            string         UnidataDomain     = ConfigService.GetUnidataDomain();
            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();

            DateTime discoverInitTime = DateTime.Now;

            //Cargamos la ontología
            if (_ontologyGraph == null)
            {
                _ontologyGraph = pCallEtlApiService.CallGetOntology();
            }

            //Cargamos datos del RDF
            RohGraph dataGraph = new RohGraph();
            Dictionary <string, HashSet <string> > discardDissambiguations = new Dictionary <string, HashSet <string> >();
            if (!string.IsNullOrEmpty(pDiscoverItem.DiscoverRdf))
            {
                //Si tenemos valor en DiscoverRdf, trabajamos con este RDF, ya que estamos reprocesando un rdf validado
                dataGraph.LoadFromString(pDiscoverItem.DiscoverRdf, new RdfXmlParser());
                if (pDiscoverItem.DiscardDissambiguations != null)
                {
                    foreach (DiscoverItem.DiscardDissambiguation discardDissambiguation in pDiscoverItem.DiscardDissambiguations)
                    {
                        if (!discardDissambiguations.ContainsKey(discardDissambiguation.IDOrigin))
                        {
                            discardDissambiguations.Add(discardDissambiguation.IDOrigin, new HashSet <string>());
                        }
                        discardDissambiguations[discardDissambiguation.IDOrigin].UnionWith(discardDissambiguation.DiscardCandidates);
                    }
                }
            }
            else
            {
                dataGraph.LoadFromString(pDiscoverItem.Rdf, new RdfXmlParser());
            }


            //Cargamos el razonador para inferir datos en la ontología
            RohRdfsReasoner reasoner = new RohRdfsReasoner();
            reasoner.Initialise(_ontologyGraph);

            //Cargamos los datos con inferencia
            RohGraph dataInferenceGraph = dataGraph.Clone();
            reasoner.Apply(dataInferenceGraph);

            //Datos para trabajar con la reconciliación
            ReconciliationData reconciliationData = new ReconciliationData();

            //Datos para trabajar con el descubrimiento de enlaces
            DiscoverLinkData discoverLinkData = new DiscoverLinkData();

            //Almacenamos las entidades con dudas acerca de su reonciliación
            Dictionary <string, Dictionary <string, float> > reconciliationEntitiesProbability = new Dictionary <string, Dictionary <string, float> >();

            //Cargamos la caché global
            if (_discoverCacheGlobal == null)
            {
                _discoverCacheGlobal = new DiscoverCacheGlobal();
                discoverUtility.LoadPersonWithName(_discoverCacheGlobal, SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword);
                discoverUtility.LoadEntitiesWithTitle(_discoverCacheGlobal, SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword);
            }


            if (!pDiscoverItem.DissambiguationProcessed)
            {
                bool hasChanges = true;

                //Cache del proceso de descubrimiento
                DiscoverCache discoverCache = new DiscoverCache();

                //Se realizarán este proceso iterativamente hasta que no haya ningún cambio en lo que a reconciliaciones se refiere
                while (hasChanges)
                {
                    hasChanges = false;

                    //Preparamos los datos para proceder con la reconciliazción
                    discoverUtility.PrepareData(dataGraph, reasoner, out dataInferenceGraph,
                                                out Dictionary <string, HashSet <string> > entitiesRdfTypes,
                                                out Dictionary <string, string> entitiesRdfType,
                                                out Dictionary <string, List <DisambiguationData> > disambiguationDataRdf, false);

                    //Carga los scores de las personas
                    //Aquí se almacenarán los nombres de las personas del RDF, junto con los candidatos de la BBDD y su score
                    Dictionary <string, Dictionary <string, float> > namesScore = new Dictionary <string, Dictionary <string, float> >();
                    discoverUtility.LoadNamesScore(ref namesScore, dataInferenceGraph, discoverCache, _discoverCacheGlobal, MinScore, MaxScore);

                    //0.- Macamos como reconciliadas aquellas que ya estén cargadas en la BBDD con los mismos identificadores
                    List <string> entidadesCargadas = discoverUtility.LoadEntitiesDB(entitiesRdfType.Keys.ToList().Except(reconciliationData.reconciliatedEntityList.Keys.Union(reconciliationData.reconciliatedEntityList.Values)), SGI_SPARQLEndpoint, SGI_SPARQLGraph, SGI_SPARQLQueryParam, SGI_SPARQLUsername, SGI_SPARQLPassword).Keys.ToList();
                    foreach (string entitiID in entidadesCargadas)
                    {
                        reconciliationData.reconciliatedEntityList.Add(entitiID, entitiID);
                        reconciliationData.reconciliatedEntitiesWithSubject.Add(entitiID);
                    }

                    //1.- Realizamos reconciliación con los identificadores configurados (y el roh:identifier) y marcamos como reconciliadas las entidades seleccionadas para no intentar reconciliarlas posteriormente
                    discoverUtility.ReconciliateIDs(ref hasChanges, ref reconciliationData, entitiesRdfType, disambiguationDataRdf, discardDissambiguations, _ontologyGraph, ref dataGraph, discoverCache, SGI_SPARQLEndpoint, SGI_SPARQLQueryParam, SGI_SPARQLGraph, SGI_SPARQLUsername, SGI_SPARQLPassword);

                    //2.- Realizamos la reconciliación con los datos del Propio RDF
                    discoverUtility.ReconciliateRDF(ref hasChanges, ref reconciliationData, _ontologyGraph, ref dataGraph, reasoner, discardDissambiguations, discoverCache, _discoverCacheGlobal, MinScore, MaxScore);

                    //3.- Realizamos la reconciliación con los datos de la BBDD
                    discoverUtility.ReconciliateBBDD(ref hasChanges, ref reconciliationData, out reconciliationEntitiesProbability, _ontologyGraph, ref dataGraph, reasoner, namesScore, discardDissambiguations, discoverCache, _discoverCacheGlobal, MinScore, MaxScore, SGI_SPARQLEndpoint, SGI_SPARQLQueryParam, SGI_SPARQLGraph, SGI_SPARQLUsername, SGI_SPARQLPassword);

                    //4.- Realizamos la reconciliación con los datos de las integraciones externas
                    //TODO descomentar
                    //discoverUtility.ExternalIntegration(ref hasChanges, ref reconciliationData, ref discoverLinkData, ref reconciliationEntitiesProbability, ref dataGraph, reasoner, namesScore,entitiesWithTitle, ontologyGraph, out Dictionary<string, ReconciliationData.ReconciliationScore> entidadesReconciliadasConIntegracionExternaAux, discardDissambiguations, discoverCache,discoverCacheGlobal, ScopusApiKey, ScopusUrl, CrossrefUserAgent, WOSAuthorization, MinScore, MaxScore, SGI_SPARQLEndpoint, SGI_SPARQLQueryParam, SGI_SPARQLGraph, SGI_SPARQLUsername, SGI_SPARQLPassword,pCallUrisFactoryApiService);

                    //Eliminamos de las probabilidades aquellos que ya estén reconciliados
                    foreach (string key in reconciliationData.reconciliatedEntityList.Keys)
                    {
                        reconciliationEntitiesProbability.Remove(key);
                    }
                }

                //5.-Realizamos la detección de equivalencias con Unidata
                //TODO descomentar cuando esté habilitaado Unidata
                //TODO descomentar y revisar en unidata no tienen roh:identifier
                //discoverUtility.EquivalenceDiscover(ontologyGraph, ref dataGraph, reasoner, discoverCache, ref reconciliationEntitiesProbability, discardDissambiguations, UnidataDomain, MinScore, MaxScore, Unidata_SPARQLEndpoint, Unidata_SPARQLQueryParam, Unidata_SPARQLGraph, Unidata_SPARQLUsername, Unidata_SPARQLPassword);
            }
            //TODO comrpobar cuando esté habilitaado Unidata
            DateTime       discoverEndTime = DateTime.Now;
            DiscoverResult resultado       = new DiscoverResult(dataGraph, dataInferenceGraph, _ontologyGraph, reconciliationData, reconciliationEntitiesProbability, discoverInitTime, discoverEndTime, discoverLinkData);

            return(resultado);
        }
コード例 #3
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);
            }
        }
コード例 #4
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));
                }
            }
        }