コード例 #1
0
ファイル: Model.cs プロジェクト: nico-izo/KOIB
 public void Create(SourceData sourceData)
 {
     Ocr.Ocr.ClearError();
     if (MarkerType.Digital == _markerType)
     {
         int markerType = 1; // установим одноцифровой тип маркера
         foreach (Blank blank in sourceData.Blanks)
         {
             if (blank.Marker >= 10)
             {
                 markerType = 2;
                 break;
             }
         }
         Ocr.Ocr.InitModel(sourceData.Blanks.Length, markerType);
     }
     else
     {
         Ocr.Ocr.InitModel(sourceData.Blanks.Length, 0);
     }
     for (int i = 0; i < sourceData.Blanks.Length; i++)
     {
         CreateBulletinModel(i, sourceData);
     }
     Ocr.Ocr.SetDefaultStampGeometry();
     if (Ocr.Ocr.DefaultModelSave() < 0)
         Ocr.Ocr.ThrowLastError();
 }
コード例 #2
0
 public void ApplyChanges(SourceData targetSourceData)
 {
     if (targetSourceData == null)
         return;
     foreach (var election in targetSourceData.Elections)
     {
         var ecc = (ElectionChangesCache)_electionChangesCacheDict[election.ElectionId];
         if (ecc == null)
             continue;
         if (ecc.LineValues.Count > 0)
         {
             foreach (var line in election.Protocol.Lines)
             {
                 var value = ecc.LineValues[line.FullKey];
                 if (value == null)
                     continue;
                 line.Value = (int)value;
             }
         }
         if (ecc.CandidateDisabling.Count > 0)
         {
             foreach (var candidate in election.Candidates)
             {
                 var disabled = ecc.CandidateDisabling[candidate.Id];
                 if (disabled == null)
                     continue;
                 candidate.Disabled = (bool)disabled;
             }
         }
     }
 }
コード例 #3
0
        public string BuildCheckTypeText(SourceData sourceData)
        {
            const string ASSEMBLY_TEXT
                = @"public class LineExpression_SUFFIX{FUNCTIONS static public int check(){return EXPRESSION;}} ";

            if (Type != LineType.DontQueryUser)
            {
                return(null);
            }
            try
            {
                var regExpression      = new VotingCountReferenceRegex();
                var matches            = regExpression.Matches(Expression);
                var preparedExpression = new StringBuilder((matches.Count + 1) * 1024);
                preparedExpression.Append(ASSEMBLY_TEXT);
                preparedExpression.Replace(
                    "_SUFFIX",
                    String.Format("_{0}_{1}_{2}", Num, AdditionalNum, _template.ElectionLink.ElectionId));
                preparedExpression.Replace("EXPRESSION", Expression);
                var functions = new StringBuilder(matches.Count * 1024);
                int num       = 0;
                foreach (Match match in matches)
                {
                    string fullKey = match.Value;
                    var    blankId = sourceData.GetBlankIdByElectionNumber(_template.ElectionLink.ElectionId);
                    functions.Append(СompileSimpleExpression(blankId, fullKey, ++num));
                    preparedExpression.Replace(fullKey, " " + "Expression" + num + "()" + " " + "\n");
                }
                preparedExpression.Replace("FUNCTIONS", functions.ToString());
                return(preparedExpression.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(
                          String.Format(
                              "Ошибка сборки в выражении № {0}{1} \"{2}\". Выборы № {3}:\n{4}"
                              , Num
                              , AdditionalNum
                              , Expression
                              , _template.ElectionLink.ElectionId
                              , ex.Message)
                          , ex);
            }
        }
コード例 #4
0
ファイル: Line.cs プロジェクト: nico-izo/KOIB
 public string BuildCheckTypeText(SourceData sourceData)
 {
     const string ASSEMBLY_TEXT
         = @"public class LineExpression_SUFFIX{FUNCTIONS static public int check(){return EXPRESSION;}} ";
     if (Type != LineType.DontQueryUser) return null;
     try
     {
         var regExpression = new VotingCountReferenceRegex();
         var matches = regExpression.Matches(Expression);
         var preparedExpression = new StringBuilder((matches.Count + 1)*1024);
         preparedExpression.Append(ASSEMBLY_TEXT);
         preparedExpression.Replace(
             "_SUFFIX",
             String.Format("_{0}_{1}_{2}", Num, AdditionalNum, _template.ElectionLink.ElectionId));
         preparedExpression.Replace("EXPRESSION", Expression);
         var functions = new StringBuilder(matches.Count * 1024);
         int num = 0;
         foreach (Match match in matches)
         {
             string fullKey = match.Value;
             var blankId = sourceData.GetBlankIdByElectionNumber(_template.ElectionLink.ElectionId);
             functions.Append(СompileSimpleExpression(blankId, fullKey, ++num));
             preparedExpression.Replace(fullKey, " " + "Expression" + num + "()" + " " + "\n");
         }
         preparedExpression.Replace("FUNCTIONS", functions.ToString());
         return preparedExpression.ToString();
     }
     catch (Exception ex)
     {
         throw new Exception(
             String.Format(
                 "Ошибка сборки в выражении № {0}{1} \"{2}\". Выборы № {3}:\n{4}"
                 , Num
                 , AdditionalNum
                 , Expression
                 , _template.ElectionLink.ElectionId
                 , ex.Message)
             , ex);
     }
 }
コード例 #5
0
ファイル: SourceDataVerifier.cs プロジェクト: nico-izo/KOIB
 public SourceDataVerifier(SourceData sourceData, VotingModeTimeConfigCollection defaultVotingModeTimes)
 {
     CodeContract.Requires(sourceData != null);
     _sourceData = sourceData;
     _defaultVotingModeTimes = defaultVotingModeTimes;
 }
コード例 #6
0
ファイル: Model.cs プロジェクト: nico-izo/KOIB
 private void CreateBulletinModel(int bulletinNum, SourceData sourceData)
 {
     byte[] data = ConvertModelText(sourceData.Blanks[bulletinNum].Model);
     int buletinCode = sourceData.Blanks[bulletinNum].Marker;
     if (Ocr.Ocr.createBallotModel(data, bulletinNum, buletinCode) != 1)
         Ocr.Ocr.ThrowLastError();
     for (int i = 0; i < sourceData.Blanks[bulletinNum].Sections.Length; i++)
     {
         var election = sourceData.GetElectionByNum(sourceData.Blanks[bulletinNum].Sections[i]);
         var pd = new PollData();
         int candCount = election.Candidates.Length;
         int maxMarks = election.MaxMarks;
         if (candCount != 0)
         {
             pd.polltype = (maxMarks <= 1) ? (int)PollType.Single : (int)PollType.Multi;
             pd.totalNum = candCount;
             pd.MinValid = 1;
             pd.MaxValid = maxMarks;
         }
         else
         {
             pd.polltype = (int)PollType.Referendum;
         }
         if (Ocr.Ocr.SetPollData(bulletinNum, i, ref pd) != 1)
             Ocr.Ocr.ThrowLastError();
     }
 }
コード例 #7
0
ファイル: RecognitionManager.cs プロジェクト: nico-izo/KOIB
 private Model CreateModel(SourceData sourceData)
 {
     if (_ocr == null)
         throw new Exception("Модуль распознавания не создан");
     try
     {
         var model = new Model(_config.Ocr.Marker.Type);
         model.Create(sourceData);
         Logger.LogVerbose(Message.RecognizerModelCreated);
         return model;
     }
     catch (Exception ex)
     {
         throw new Exception("Ошибка создания модели: " + ex.Message, ex);
     }
 }
コード例 #8
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 private void SetSourceDataInternal( 
     SourceData newSd,  
     SourceDataFileDescriptor newSdFileDescriptor, 
     SourceDataChangesCache newSdChangesCache, 
     bool newIsSdCorrect)
 {
     var newSdFileDescriptorDiffer = SetSourceDataFileDescriptor(newSdFileDescriptor);
     var newSdChangesCacheDiffer = SetSourceDataChangesCache(newSdChangesCache);
     var newSdDiffer = true;
     if (newSd == null)
     {
         if (newSdFileDescriptor == null)
         {
             if (SourceData == null)
                 newSdDiffer = false;
         }
         else if (newSdFileDescriptorDiffer)
         {
             LoadSourceDataFromFile(_sourceDataFileDescriptor, false, out newSd);
         }
         else
         {
             newSdDiffer = false;
         }
     }
     if (newSdDiffer || newSdChangesCacheDiffer)
     {
         if (SourceData != null)
         {
             UnsubscribeFromSourceDataChanges();
         }
         if (newSdDiffer)
             SourceData = newSd;
         if (SourceData != null)
         {
             _sourceDataChangesCache.ApplyChanges(SourceData);
             SubscribeToSourceDataChanges();
         }
     }
     _isSourceDataCorrect = newIsSdCorrect;
 }
コード例 #9
0
ファイル: RecognitionManager.cs プロジェクト: nico-izo/KOIB
 public void CheckCreateModel(SourceData sourceData)
 {
     CreateModel(sourceData);
 }
コード例 #10
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 private static string SerializeSourceData(SourceData sd)
 {
     var oSerializer = new XmlSerializer(typeof(SourceData), SourceData.XMLNS);
     using (var memStream = new MemoryStream())
     {
         var writer = new StreamWriter(memStream);
         oSerializer.Serialize(writer, sd);
         memStream.Seek(0, SeekOrigin.Begin);
         var reader = new StreamReader(memStream);
         return reader.ReadToEnd();
     }
 }
コード例 #11
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 private static Guid GenerateSourceDataId(SourceData sd)
 {
     var data = SerializeSourceData(sd);
     var hash = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(data));
     var bytes = hash.Take(16).ToArray();
     return new Guid(bytes);
 }
コード例 #12
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 public bool SetSourceData(SourceData sourceData, SourceDataFileDescriptor sourceDataFileDescriptor)
 {
     CodeContract.Requires(sourceData != null);
     CodeContract.Requires(sourceDataFileDescriptor != null);
     try
     {
         SetSourceDataInternal(
             sourceData,
             sourceDataFileDescriptor,
             null,
             false);
     }
     catch (Exception ex)
     {
         Logger.LogError(Message.Election_SetSourceDataFailed, ex);
         return false;
     }
     RaiseStateChanged();
     Logger.LogInfo(
         Message.Election_SetSourceDataSucceeded,
         SourceData.Uik,
         SourceData.ElectionDate.ToString("dd.MM.yyyy"),
         SourceData.GetVotingModeStartTime(VotingMode.Main),
         SourceData.GetVotingModeStartTime(VotingMode.Portable));
     return true;
 }
コード例 #13
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 public bool LoadSourceDataFromFile( 
     SourceDataFileDescriptor sdFileDescriptor, bool needVerify, out SourceData sd)
 {
     needVerify = !QuietMode && needVerify;
     try
     {
         using (var stream = new FileStream(sdFileDescriptor.FilePath, FileMode.Open, FileAccess.Read))
         {
             var uncompressedStream = ZipCompressor.Uncompress(stream);
             sd = DeserializeSourceData(uncompressedStream);
             uncompressedStream.Close();
             if (sd.Id == Guid.Empty)
             {
                 if (_votingResultManager.PackResultsEnabled)
                     throw new SourceDataVerifierException(
                         "Исходные данные не содержат уникального идентификатора");
                 sd.Id = GenerateSourceDataId(sd);
             }
         }
         sd.Init(sdFileDescriptor.Uik);
         var verifier = new SourceDataVerifier(sd, _config.DefaultVotingModeTimes);
         Logger.LogVerbose(Message.Election_SourceDataRepairing);
         verifier.Repair();
         if (needVerify)
         {
             Logger.LogVerbose(Message.Election_SourceDataVerifying);
             verifier.Verify();
         }
     }
     catch (SourceDataVerifierException ex)
     {
         sd = null;
         Logger.LogWarning(Message.Election_SourceDataIncorrect, sdFileDescriptor.FilePath, ex.Message);
         return false;
     }
     catch (Exception ex)
     {
         sd = null;
         Logger.LogWarning(Message.Election_SourceDataLoadFromFileFailed, ex, sdFileDescriptor.FilePath);
         return false;
     }
     if (needVerify)
     {
         try
         {
             Logger.LogVerbose(Message.Election_CheckCreateModel);
             _recognitionManager.CheckCreateModel(sd);
         }
         catch (Exception ex)
         {
             sd = null;
             Logger.LogWarning(Message.Election_SourceDataIncorrect, sdFileDescriptor.FilePath, ex.Message);
             return false;
         }
     }
     Logger.LogVerbose(Message.Election_SourceDataSuccessfullyLoadedFromFile, sdFileDescriptor.FilePath);
     return true;
 }
コード例 #14
0
ファイル: ElectionManager.cs プロジェクト: nico-izo/KOIB
 public ElectionDayСomming IsElectionDay(SourceData sourceData)
 {
     if (sourceData == null)
         return ElectionDayСomming.NotComeYet;
     var electionDay = sourceData.ElectionDate.Date;
     var currentDay = sourceData.LocalTimeNow.Date;
     if (currentDay < electionDay)
         return ElectionDayСomming.NotComeYet;
     if (currentDay == electionDay)
         return ElectionDayСomming.ItsElectionDay;
     var lastExtraElectionDay = electionDay.AddDays(_config.ElectionDayDuration.Value - 1).Date;
     if (currentDay <= lastExtraElectionDay)
         return ElectionDayСomming.ItsExtraElectionDay;
     return ElectionDayСomming.AlreadyPassed;
 }