// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** internal void PutRequest(TermBaseRequest request) { if (_Paused) { return; } lock (_NewRequests) { if (_NewRequests.Count > 0 && _NewRequests[0].Type == RequestType.TermList) { // Don't throw TermList request away if (_NewRequests.Count > 1) { _NewRequests.RemoveAt(1); } _NewRequests.Add(request); _DataAvailableEvent.Set(); return; } _NewRequests.Clear(); // Clear all outstanding requests _NewRequests.Add(request); _DataAvailableEvent.Set(); } }
// ******************************************************************************** /// <summary> /// /// </summary> /// <typeparam name="TerminologyResultArgs"></typeparam> /// <param name="term"></param> /// <param name="requestid"></param> /// <returns></returns> /// <created>UPh,17.11.2015</created> /// <changed>UPh,17.11.2015</changed> // ******************************************************************************** public List <TerminologyResultArgs> RequestSyncSingleTerm(string term, long requestid) { List <TerminologyResultArgs> result = new List <TerminologyResultArgs>(); _Worker.HandleSingleTermRequest(TermBaseRequest.MakeTerminologyRequest(term, requestid), result); return(result); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** static internal TermBaseRequest MakeTermListRequest(long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.TermList; return(request); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <typeparam name="TerminologyResultArgs"></typeparam> /// <param name="text"></param> /// <param name="nLangSet"></param> /// <param name="requestid"></param> /// <returns></returns> /// <created>UPh,20.03.2016</created> /// <changed>UPh,20.03.2016</changed> // ******************************************************************************** public List <TerminologyResultArgs> RequestSyncProhibitedTerminology(string text, long requestid) { List <TerminologyResultArgs> result = new List <TerminologyResultArgs>(); _Worker.HandleProhibitedTerminologyRequest(TermBaseRequest.MakeProhibitedTerminologyRequest(text, requestid), result); return(result); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="term"></param> /// <param name="requestid"></param> /// <returns></returns> /// <created>UPh,20.03.2016</created> /// <changed>UPh,20.03.2016</changed> // ******************************************************************************** static internal TermBaseRequest MakeProhibitedTerminologyRequest(string term, long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.ProhibitedTerminology; request.Term = term; return(request); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** public void RequestTermList(long requestid) { if (!_Worker.IsStarted) { _Worker.Start(); } _Worker.PutRequest(TermBaseRequest.MakeTermListRequest(requestid)); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="item"></param> /// <returns></returns> /// <created>UPh,31.10.2015</created> /// <changed>UPh,31.10.2015</changed> // ******************************************************************************** public void RequestTermInfo(TermListItem item, long requestid) { if (!_Worker.IsStarted) { _Worker.Start(); } _Worker.PutRequest(TermBaseRequest.MakeTermInfoRequest(item.TermBaseID, item.TermID, requestid)); }
// ******************************************************************************** /// <summary> /// Put terminology request to FiFo /// </summary> /// <param name="text"></param> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** public void RequestTerminology(string text, long requestid) { if (!_Worker.IsStarted) { _Worker.Start(); } _Worker.PutRequest(TermBaseRequest.MakeTerminologyRequest(text, requestid)); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="term"></param> /// <param name="id"></param> /// <returns></returns> /// <created>UPh,30.10.2015</created> /// <changed>UPh,30.10.2015</changed> // ******************************************************************************** static internal TermBaseRequest MakeTermInfoRequest(int termbaseID, int termid, long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.TermInfo; request.TermBaseID = termbaseID; request.TermID = termid; return(request); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="result"></param> /// <created>UPh,20.03.2016</created> /// <changed>UPh,20.03.2016</changed> // ******************************************************************************** internal void HandleProhibitedTerminologyRequest(TermBaseRequest request, List <TerminologyResultArgs> result = null) { bool bSync = (result != null); bool bTargetLanguage = (_TermbaseQuery.LCID2 > 0); // Build index if necessary (works on target language) if (_Index2.LCID != _TermbaseQuery.LCID2) { TermListItems items = RetrieveTermList(false, bTargetLanguage); if (!bSync && _shouldStop) { return; } // Index from items _Index2.BuildIndex(bTargetLanguage ? _TermbaseQuery.LCID2 : _TermbaseQuery.LCID1, items); } WordSegments wordSegments = new WordSegments(request.Term); int nWords = wordSegments.Count; if (nWords == 0) { return; } // Start with max term word count (3) for (int nTermWords = 3; nTermWords >= 1; nTermWords--) { for (int iWord0 = 0; iWord0 < nWords - nTermWords + 1; iWord0++) { int iWord1 = iWord0 + nTermWords - 1; int from = wordSegments.GetWordStart(iWord0); int to = wordSegments.GetWordEnd(iWord1); foreach (IndexItem match in _Index2.Matches(request.Term, from, to - from)) { HandleProhibitedTerminologyMatch(bTargetLanguage, request.ID, match, from, to - from, result); } } } }
// ******************************************************************************** /// <summary> /// /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** private TermBaseRequest GetRequest() { lock (_NewRequests) { if (_NewRequests.Count == 0) { return(null); } TermBaseRequest request = _NewRequests[0]; _NewRequests.RemoveAt(0); return(request); } }
// ******************************************************************************** /// <summary> /// Handle request to get term list items /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** private void HandleTermListRequest(TermBaseRequest request) { TermListItems items = RetrieveTermList(false, false); if (items == null) { return; } if (_Paused || _shouldStop) { return; } items.Sort((a, b) => string.Compare(a.Term, b.Term, true)); _TermbaseQuery.FireTermListResult(request.ID, items); }
// ******************************************************************************** /// <summary> /// Handle request for a single term /// </summary> /// <param name="request"></param> /// <returns></returns> /// <created>UPh,30.10.2015</created> /// <changed>UPh,30.10.2015</changed> // ******************************************************************************** private void HandleTermInfoRequest(TermBaseRequest request) { TermBase tb = _TermBases.FindTermBase(request.TermBaseID); if (tb == null) { return; } TermInfo info; if (!tb.GetTermInfo(request.TermID, out info, this)) { return; } info.TermID = request.TermID; _TermbaseQuery.FireTermInfoResult(request.ID, request.TermBaseID, info); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="result"></param> /// <returns></returns> /// <created>UPh,17.11.2015</created> /// <changed>UPh,17.11.2015</changed> // ******************************************************************************** internal void HandleSingleTermRequest(TermBaseRequest request, List <TerminologyResultArgs> result = null) { bool bSync = (result != null); // Build index if necessary if (_Index.LCID != _TermbaseQuery.LCID1) { TermListItems items = RetrieveTermList(true, false); if (!bSync && _shouldStop) { return; } // Index from items _Index.BuildIndex(_TermbaseQuery.LCID1, items); } foreach (IndexItem match in _Index.Matches(request.Term)) { HandleTerminologyMatch(request.ID, match, 0, request.Term.Length, result); } }
// ******************************************************************************** /// <summary> /// /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** private void WorkerThread() { while (!_shouldStop) { _DataAvailableEvent.WaitOne(1000); if (_shouldStop) { return; } TermBaseRequest request = GetRequest(); if (request == null) { continue; } if (request.Type == RequestType.TermList) { HandleTermListRequest(request); } else if (request.Type == RequestType.TermInfo) { HandleTermInfoRequest(request); } else if (request.Type == RequestType.Terminology) { HandleTerminologyRequest(request); } else if (request.Type == RequestType.ProhibitedTerminology) { HandleProhibitedTerminologyRequest(request); } // TODO Bei _PauseRequest die aktuelle Suche abbrechen } }
// ******************************************************************************** /// <summary> /// /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** internal static TermBaseRequest MakeTermListRequest(long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.TermList; return request; }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="term"></param> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** internal static TermBaseRequest MakeTerminologyRequest(string term, long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.Terminology; request.Term = term; return request; }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="term"></param> /// <param name="id"></param> /// <returns></returns> /// <created>UPh,30.10.2015</created> /// <changed>UPh,30.10.2015</changed> // ******************************************************************************** internal static TermBaseRequest MakeTermInfoRequest(int termbaseID, int termid, long requestid) { TermBaseRequest request = new TermBaseRequest(); request.ID = requestid; request.Type = RequestType.TermInfo; request.TermBaseID = termbaseID; request.TermID = termid; return request; }
// ******************************************************************************** /// <summary> /// Handle request to get term list items /// </summary> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** private void HandleTermListRequest(TermBaseRequest request) { TermListItems items = RetrieveTermList(false, false); if (items == null) return; if (_Paused || _shouldStop) return; items.Sort((a,b) => string.Compare(a.Term, b.Term, true)); _TermbaseQuery.FireTermListResult(request.ID, items); }
// ******************************************************************************** /// <summary> /// Handle request for a single term /// </summary> /// <param name="request"></param> /// <returns></returns> /// <created>UPh,30.10.2015</created> /// <changed>UPh,30.10.2015</changed> // ******************************************************************************** private void HandleTermInfoRequest(TermBaseRequest request) { TermBase tb = _TermBases.FindTermBase(request.TermBaseID); if (tb == null) return; TermInfo info; if (!tb.GetTermInfo(request.TermID, out info, this)) return; info.TermID = request.TermID; _TermbaseQuery.FireTermInfoResult(request.ID, request.TermBaseID, info); }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="result"></param> /// <returns></returns> /// <created>UPh,17.11.2015</created> /// <changed>UPh,17.11.2015</changed> // ******************************************************************************** internal void HandleSingleTermRequest(TermBaseRequest request, List<TerminologyResultArgs> result = null) { bool bSync = (result != null); // Build index if necessary if (_Index.LCID != _TermbaseQuery.LCID1) { TermListItems items = RetrieveTermList(true, false); if (!bSync && _shouldStop) return; // Index from items _Index.BuildIndex(_TermbaseQuery.LCID1, items); } foreach (IndexItem match in _Index.Matches(request.Term)) { HandleTerminologyMatch(request.ID, match, 0, request.Term.Length, result); } }
// ******************************************************************************** /// <summary> /// Handle request for terminology /// </summary> /// <param name="request"></param> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,17.11.2015</changed> // ******************************************************************************** internal void HandleTerminologyRequest(TermBaseRequest request, List<TerminologyResultArgs> result = null) { bool bSync = (result != null); // Build index if necessary if (_Index.LCID != _TermbaseQuery.LCID1) { TermListItems items = RetrieveTermList(true, false); if (!bSync && _shouldStop) return; // Index from items _Index.BuildIndex(_TermbaseQuery.LCID1, items); } WordSegments wordSegments = new WordSegments(request.Term); int nWords = wordSegments.Count; if (nWords == 0) return; // Start with max term word count (3) for (int nTermWords = 3; nTermWords >= 1; nTermWords--) { for (int iWord0 = 0; iWord0 < nWords - nTermWords + 1; iWord0++) { int iWord1 = iWord0 + nTermWords - 1; int from = wordSegments.GetWordStart(iWord0); int to = wordSegments.GetWordEnd(iWord1); foreach (IndexItem match in _Index.Matches(request.Term, from, to - from)) { HandleTerminologyMatch(request.ID, match, from, to - from, result); } } } }
// ******************************************************************************** /// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> /// <created>UPh,25.10.2015</created> /// <changed>UPh,25.10.2015</changed> // ******************************************************************************** internal void PutRequest(TermBaseRequest request) { if (_Paused) return; lock (_NewRequests) { if (_NewRequests.Count > 0 && _NewRequests[0].Type == RequestType.TermList) { // Don't throw TermList request away if (_NewRequests.Count > 1) _NewRequests.RemoveAt(1); _NewRequests.Add(request); _DataAvailableEvent.Set(); return; } _NewRequests.Clear(); // Clear all outstanding requests _NewRequests.Add(request); _DataAvailableEvent.Set(); } }