public ActionResult Edit(Reconciliation Reconciliation)
        {
            Reconciliation oReconciliation = new Reconciliation();

            oReconciliation.ReconciliationID = System.Convert.ToInt32(Reconciliation.ReconciliationID);
            oReconciliation = ReconciliationData.Select_Record(Reconciliation);

            if (ModelState.IsValid)
            {
                bool bSucess = false;
                bSucess = ReconciliationData.Update(oReconciliation, Reconciliation);
                if (bSucess == true)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Can Not Update");
                }
            }
            // ComboBox
            ViewData["InvoiceID"] = new SelectList(Reconciliation_InvoiceData.List(), "InvoiceID", "InvoiceNo", Reconciliation.InvoiceID);

            return(View(Reconciliation));
        }
        // GET: /Reconciliation/Delete/<id>
        public ActionResult Delete(
            Int32?ReconciliationID
            )
        {
            if (
                ReconciliationID == null
                )
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            dtInvoice = Reconciliation_InvoiceData.SelectAll();

            Reconciliation Reconciliation = new Reconciliation();

            Reconciliation.ReconciliationID = System.Convert.ToInt32(ReconciliationID);
            Reconciliation         = ReconciliationData.Select_Record(Reconciliation);
            Reconciliation.Invoice = new Invoice()
            {
                InvoiceID   = (Int32)Reconciliation.InvoiceID
                , InvoiceNo = (from DataRow rowInvoice in dtInvoice.Rows
                               where Reconciliation.InvoiceID == (int)rowInvoice["InvoiceID"]
                               select(String) rowInvoice["InvoiceNo"]).FirstOrDefault()
            };

            if (Reconciliation == null)
            {
                return(HttpNotFound());
            }
            return(View(Reconciliation));
        }
        public ActionResult Create([Bind(Include =
                                             "InvoiceID"
                                             + "," + "PaymentDate"
                                             + "," + "PaymentAmount"
                                             + "," + "TDSAmount"
                                             + "," + "Remarks"
                                         )] Reconciliation Reconciliation)
        {
            if (ModelState.IsValid)
            {
                bool bSucess = false;
                bSucess = ReconciliationData.Add(Reconciliation);
                if (bSucess == true)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Can Not Insert");
                }
            }
            // ComboBox
            ViewData["InvoiceID"] = new SelectList(Reconciliation_InvoiceData.List(), "InvoiceID", "InvoiceNo", Reconciliation.InvoiceID);

            return(View(Reconciliation));
        }
        // GET: /Reconciliation/Edit/<id>
        public ActionResult Edit(
            Int32?ReconciliationID
            )
        {
            if (
                ReconciliationID == null
                )
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Reconciliation Reconciliation = new Reconciliation();

            Reconciliation.ReconciliationID = System.Convert.ToInt32(ReconciliationID);
            Reconciliation = ReconciliationData.Select_Record(Reconciliation);

            if (Reconciliation == null)
            {
                return(HttpNotFound());
            }
            // ComboBox
            ViewData["InvoiceID"] = new SelectList(Reconciliation_InvoiceData.List(), "InvoiceID", "InvoiceNo", Reconciliation.InvoiceID);

            return(View(Reconciliation));
        }
Example #5
0
        public void Reconciliate()
        {
            ReconciliationData reco    = new ReconciliationData();
            decimal            credit  = dialog.ReconciliationGrid.LeftAmount;
            decimal            debit   = dialog.ReconciliationGrid.RightAmount;
            decimal            balance = dialog.ReconciliationGrid.BalanceAmount;

            if (balance != 0 && this.EditedObject.acceptWriteOff)
            {
                if (!dialog.WriteOffBlock.Validate())
                {
                    return;
                }
                reco.writeOffFields = dialog.WriteOffBlock.Fill();
                reco.writeOffAmount = balance;
            }

            reco.ids      = dialog.ReconciliationGrid.GridBrowser.GetSelectedOis();
            reco.recoType = this.EditedObject.reconciliationType;
            WriteOffFieldValueType type = this.EditedObject.writeoffDefaultMeasureTypeEnum;

            if (type == WriteOffFieldValueType.CUSTOM)
            {
                reco.writeOffMeasure = this.EditedObject.writeoffMeasure;
            }
            else if (type == WriteOffFieldValueType.LEFT_SIDE)
            {
                reco.writeOffMeasure = this.EditedObject.leftMeasure;
            }
            else if (type == WriteOffFieldValueType.RIGHT_SIDE)
            {
                reco.writeOffMeasure = this.EditedObject.rightMeasure;
            }
            try
            {
                bool result = this.Service.reconciliate(reco);
                if (result)
                {
                    this.LeftGrid.Search(this.LeftGrid.EditedObject.GrilleFilter != null ? this.LeftGrid.EditedObject.GrilleFilter.page : 1);
                    this.RightGrid.Search(this.RightGrid.EditedObject.GrilleFilter != null ? this.RightGrid.EditedObject.GrilleFilter.page : 1);
                    this.BottomGrid.Clear();
                    this.BottomGrid.ReconciliateButton.IsEnabled = false;
                    this.BottomGrid.ResetButton.IsEnabled        = false;
                    this.BottomGrid.ClearButton.IsEnabled        = false;
                    dialog.ReconciliateButton.Click -= OnDialogReconciliate;
                    dialog.CancelButton.Click       -= OnDialogCancel;
                    this.dialog.Close();
                    dialog = null;
                    BuildBalance(this.LeftGrid);
                    BuildBalance(this.RightGrid);
                }
            }
            catch (Exception)
            {
                MessageDisplayer.DisplayWarning("Reconciliation Error", "An error occurred while trying to reconciliate!");
            }
        }
 /// <summary>
 /// Constructor del resultado del descubirmiento
 /// </summary>
 /// <param name="pDataGraph">Grafo con los datos</param>
 /// <param name="pDataInferenceGraph">Grafo con los datos (con inferencia)</param>
 /// <param name="pOntologyGraph">Grafo con la ontología</param>
 /// <param name="pReconciliationData">Datos obtenidos de la reconciliación</param>
 /// <param name="pDiscoveredEntitiesProbability">Probabilidades de descubriiento</param>
 /// <param name="pDateStart">Fecha inicio descubirmiento</param>
 /// <param name="pDateEnd">Fecha fin descubirmiento</param>
 /// <param name="pDiscoverLinkData">Datos para trabajar con el descubrimiento de enlaces</param>
 public DiscoverResult(RohGraph pDataGraph, RohGraph pDataInferenceGraph, RohGraph pOntologyGraph, ReconciliationData pReconciliationData, Dictionary <string, Dictionary <string, float> > pDiscoveredEntitiesProbability, DateTime pDateStart, DateTime pDateEnd, DiscoverLinkData pDiscoverLinkData)
 {
     dataGraph                     = pDataGraph;
     dataInferenceGraph            = pDataInferenceGraph;
     ontologyGraph                 = pOntologyGraph;
     reconciliationData            = pReconciliationData;
     discoveredEntitiesProbability = pDiscoveredEntitiesProbability;
     secondsProcessed              = (pDateEnd - pDateStart).TotalSeconds;
     start            = pDateStart;
     end              = pDateEnd;
     discoverLinkData = pDiscoverLinkData;
 }
        public bool reconciliate(ReconciliationData data)
        {
            try
            {
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                var request = new RestRequest(ResourcePath + "/reconciliate", Method.POST);
                serializer.MaxJsonLength = int.MaxValue;
                string json = serializer.Serialize(data);
                request.AddParameter("application/json", json, ParameterType.RequestBody);

                var          response    = RestClient.ExecuteTaskAsync(request);
                RestResponse queryResult = (RestResponse)response.Result;
                bool         result      = RestSharp.SimpleJson.DeserializeObject <Boolean>(queryResult.Content);
                return(result);
            }
            catch (Exception e)
            {
                logger.Error("Unable to reconciliate postings.", e);
                throw new ServiceExecption("Unable to reconciliate postings.", e);
            }
        }
        public ActionResult DeleteConfirmed(
            Int32?ReconciliationID
            )
        {
            Reconciliation Reconciliation = new Reconciliation();

            Reconciliation.ReconciliationID = System.Convert.ToInt32(ReconciliationID);
            Reconciliation = ReconciliationData.Select_Record(Reconciliation);

            bool bSucess = false;

            bSucess = ReconciliationData.Delete(Reconciliation);
            if (bSucess == true)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                ModelState.AddModelError("", "Can Not Delete");
            }
            return(null);
        }
Example #9
0
        public void TestReconciliation()
        {
            //Cargamos el RDF sobre el que aplicar la reconciliación
            RohGraph dataGraph = new RohGraph();

            dataGraph.LoadFromString(System.IO.File.ReadAllText("rdfFiles/rdfFileRecon.rdf"), new RdfXmlParser());

            //Cargamos el RDF de la ontología
            RohGraph ontologyGraph = new RohGraph();

            ontologyGraph.LoadFromFile("Ontology/roh-v2.owl");

            //Cargamos el RDF que simula la BBDD
            RohGraph dataGraphBBDD = new RohGraph();

            dataGraph.LoadFromString(System.IO.File.ReadAllText("rdfFiles/rdfFile.rdf"), new RdfXmlParser());

            //Cargamos configuraciones necesarias
            ConfigService ConfigService = new ConfigService();
            float         maxScore      = ConfigService.GetMaxScore();
            float         minScore      = ConfigService.GetMinScore();

            //Construimos el objeto DiscoverUtility
            DiscoverUtility discoverUtility = new DiscoverUtility();

            //Sustituimos mSparqlUtility por un MOCK en el que cargamos el grafo en memoria 'dataGraphBBDD'
            //Si se quiere ejecutar sobre la BBDD no habría que modificar discoverUtility.mSparqlUtility y sería necesario especificar los datos del SPARQL endpoint
            discoverUtility.mSparqlUtility = new SparqlUtilityMock(dataGraphBBDD);
            discoverUtility.test           = true;

            //Aplicamos la reconciliación
            //Los datos de configuración de SPARQL se mandan vacíos porque utilizamos el MOCK
            ReconciliationData reconciliationData = discoverUtility.ApplyReconciliation(ref dataGraph, ontologyGraph, "", "", "", "", "", minScore, maxScore, out Dictionary <string, Dictionary <string, float> > reconciliationEntitiesProbability);

            //En 'reconciliationData' estarán los datos que se han modificado fruto de la reconciliación
            //En 'dataGraph' estará el grafo modificado tras la reconciliación
            //En 'reconciliationEntitiesProbability' estarán las entidades para las que ha habido problemas de desambiguación
        }
        // GET: /Reconciliation/
        public ActionResult Index(string sortOrder,
                                  String SearchField,
                                  String SearchCondition,
                                  String SearchText,
                                  String Export,
                                  int?PageSize,
                                  int?page,
                                  string command)
        {
            if (command == "Show All")
            {
                SearchField                = null;
                SearchCondition            = null;
                SearchText                 = null;
                Session["SearchField"]     = null;
                Session["SearchCondition"] = null;
                Session["SearchText"]      = null;
            }
            else if (command == "Add New Record")
            {
                return(RedirectToAction("Create"));
            }
            else if (command == "Export")
            {
                Session["Export"] = Export;
            }
            else if (command == "Search" | command == "Page Size")
            {
                if (!string.IsNullOrEmpty(SearchText))
                {
                    Session["SearchField"]     = SearchField;
                    Session["SearchCondition"] = SearchCondition;
                    Session["SearchText"]      = SearchText;
                }
            }
            if (command == "Page Size")
            {
                Session["PageSize"] = PageSize;
            }

            ViewData["SearchFields"]     = GetFields((Session["SearchField"] == null ? "Reconciliation I D" : Convert.ToString(Session["SearchField"])));
            ViewData["SearchConditions"] = Library.GetConditions((Session["SearchCondition"] == null ? "Contains" : Convert.ToString(Session["SearchCondition"])));
            ViewData["SearchText"]       = Session["SearchText"];
            ViewData["Exports"]          = Library.GetExports((Session["Export"] == null ? "Pdf" : Convert.ToString(Session["Export"])));
            ViewData["PageSizes"]        = Library.GetPageSizes();

            ViewData["CurrentSort"] = sortOrder;
            ViewData["ReconciliationIDSortParm"] = sortOrder == "ReconciliationID_asc" ? "ReconciliationID_desc" : "ReconciliationID_asc";
            ViewData["InvoiceIDSortParm"]        = sortOrder == "InvoiceID_asc" ? "InvoiceID_desc" : "InvoiceID_asc";
            ViewData["PaymentDateSortParm"]      = sortOrder == "PaymentDate_asc" ? "PaymentDate_desc" : "PaymentDate_asc";
            ViewData["PaymentAmountSortParm"]    = sortOrder == "PaymentAmount_asc" ? "PaymentAmount_desc" : "PaymentAmount_asc";
            ViewData["TDSAmountSortParm"]        = sortOrder == "TDSAmount_asc" ? "TDSAmount_desc" : "TDSAmount_asc";
            ViewData["RemarksSortParm"]          = sortOrder == "Remarks_asc" ? "Remarks_desc" : "Remarks_asc";

            dtReconciliation = ReconciliationData.SelectAll();
            dtInvoice        = Reconciliation_InvoiceData.SelectAll();

            try
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["SearchField"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchCondition"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchText"])))
                {
                    dtReconciliation = ReconciliationData.Search(Convert.ToString(Session["SearchField"]), Convert.ToString(Session["SearchCondition"]), Convert.ToString(Session["SearchText"]));
                }
            }
            catch { }

            var Query = from rowReconciliation in dtReconciliation.AsEnumerable()
                        join rowInvoice in dtInvoice.AsEnumerable() on rowReconciliation.Field <Int32>("InvoiceID") equals rowInvoice.Field <Int32>("InvoiceID")
                        select new Reconciliation()
            {
                ReconciliationID = rowReconciliation.Field <Int32>("ReconciliationID")
                ,
                Invoice = new Invoice()
                {
                    InvoiceID   = rowInvoice.Field <Int32>("InvoiceID")
                    , InvoiceNo = rowInvoice.Field <String>("InvoiceNo")
                }
                , PaymentDate   = rowReconciliation.Field <DateTime>("PaymentDate")
                , PaymentAmount = rowReconciliation.Field <Decimal>("PaymentAmount")
                , TDSAmount     = rowReconciliation.Field <Decimal>("TDSAmount")
                , Remarks       = rowReconciliation.Field <String>("Remarks")
            };

            switch (sortOrder)
            {
            case "ReconciliationID_desc":
                Query = Query.OrderByDescending(s => s.ReconciliationID);
                break;

            case "ReconciliationID_asc":
                Query = Query.OrderBy(s => s.ReconciliationID);
                break;

            case "InvoiceID_desc":
                Query = Query.OrderByDescending(s => s.Invoice.InvoiceNo);
                break;

            case "InvoiceID_asc":
                Query = Query.OrderBy(s => s.Invoice.InvoiceNo);
                break;

            case "PaymentDate_desc":
                Query = Query.OrderByDescending(s => s.PaymentDate);
                break;

            case "PaymentDate_asc":
                Query = Query.OrderBy(s => s.PaymentDate);
                break;

            case "PaymentAmount_desc":
                Query = Query.OrderByDescending(s => s.PaymentAmount);
                break;

            case "PaymentAmount_asc":
                Query = Query.OrderBy(s => s.PaymentAmount);
                break;

            case "TDSAmount_desc":
                Query = Query.OrderByDescending(s => s.TDSAmount);
                break;

            case "TDSAmount_asc":
                Query = Query.OrderBy(s => s.TDSAmount);
                break;

            case "Remarks_desc":
                Query = Query.OrderByDescending(s => s.Remarks);
                break;

            case "Remarks_asc":
                Query = Query.OrderBy(s => s.Remarks);
                break;

            default:      // Name ascending
                Query = Query.OrderBy(s => s.ReconciliationID);
                break;
            }

            if (command == "Export")
            {
                GridView  gv = new GridView();
                DataTable dt = new DataTable();
                dt.Columns.Add("Reconciliation I D", typeof(string));
                dt.Columns.Add("Invoice I D", typeof(string));
                dt.Columns.Add("Payment Date", typeof(string));
                dt.Columns.Add("Payment Amount", typeof(string));
                dt.Columns.Add("T D S Amount", typeof(string));
                dt.Columns.Add("Remarks", typeof(string));
                foreach (var item in Query)
                {
                    dt.Rows.Add(
                        item.ReconciliationID
                        , item.Invoice.InvoiceNo
                        , item.PaymentDate
                        , item.PaymentAmount
                        , item.TDSAmount
                        , item.Remarks
                        );
                }
                gv.DataSource = dt;
                gv.DataBind();
                ExportData(Export, gv, dt);
            }

            int pageNumber = (page ?? 1);
            int?pageSZ     = (Convert.ToInt32(Session["PageSize"]) == 0 ? 5 : Convert.ToInt32(Session["PageSize"]));

            return(View(Query.ToPagedList(pageNumber, (pageSZ ?? 5))));
        }
Example #11
0
 protected override void Page_Load(object sender, EventArgs e)
 {
     ReconciliationData.Activate();
 }
Example #12
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);
            }
        }
Example #13
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);
        }