public void TearDown() { // Close the application and delete the driver //if (WindowsDriver != null) //{ // WindowsDriver.Quit(); // WindowsDriver = null; //} WindowsDriver.Quit(); DesktopSession.Quit(); CalculatorSession.Quit(); //Kill process Process.Kill(); }
public RuleContracts.CalculationData Provide(Employee emp, out CalculationData calculationData, Calculation calculation, bool withCalculationPoint, CalculatorSession calculationSession) { //empRep.Attach(employee); var employee = empRep.GetBy(emp.Id); calculationData = empRep.ProvideDataForRule(employee, calculation.Id, withCalculationPoint); if (calculationSession.CalculationPoints != null && calculationSession.CalculationPoints.Any()) { calculationData.CalculationPoints.AddRange(calculationSession.CalculationPoints); } var res = new MITD.PMS.RuleContracts.CalculationData { Employee = new PMS.RuleContracts.Employee { FirstName = calculationData.employee.FirstName, LastName = calculationData.employee.LastName, EmployeeNo = calculationData.employee.Id.EmployeeNo }, CustomFields = (from k in pmsAdminService.GetSharedEmployeeCustomField( employee.CustomFieldValues.Select(m => m.Key).ToList()) join l in employee.CustomFieldValues on k.Id equals l.Key select new { k, l }).ToDictionary(j => j.k.DictionaryName, j => j.l.Value), JobPositions = calculationData.JobPositions.Select(jobPositionAndData => new JobPosition { Name = jobPositionAndData.Key.Name, DictionaryName = jobPositionAndData.Key.DictionaryName, Job = new Job { Name = jobPositionAndData.Value.Job.Name, DictionaryName = jobPositionAndData.Value.Job.DictionaryName }, Unit = new Unit { Id = jobPositionAndData.Value.Unit.Id.SharedUnitId.Id, ParentId = jobPositionAndData.Value.Unit.Parent != null ? jobPositionAndData.Value.Unit.Parent.Id.SharedUnitId.Id : 0, Name = jobPositionAndData.Value.Unit.Name, DictionaryName = jobPositionAndData.Value.Unit.DictionaryName, Indices = jobPositionAndData.Value.UnitIndices.ToDictionary( j => new UnitIndex { Name = j.Key.Name, DictionaryName = j.Key.DictionaryName, IsInquireable = j.Key.IsInquireable, //Group = // new JobIndexGroup { Name = j.Key.Group.Name, DictionaryName = j.Key.Group.DictionaryName }, CustomFields = (from k in pmsAdminService.GetSharedCutomFieldListForUnitIndex(j.Key.SharedUnitIndexId, j.Key.CustomFieldValues.Select(m => m.Key).ToList()) join l in j.Key.CustomFieldValues on k.Id equals l.Key select new { k, l }).ToDictionary(m => m.k.DictionaryName, m => m.l.Value) }, j => (j.Value != null ? new Tuple <PMS.RuleContracts.Employee, string>(new RuleContracts.Employee() { EmployeeNo = j.Value.Item1.Id.EmployeeNo, FirstName = j.Value.Item1.FirstName, LastName = j.Value.Item1.LastName }, j.Value.Item2) //j.Value.ToDictionary( // k => new PMS.RuleContracts.Employee // { // FirstName = k.Key.FirstName, // LastName = k.Key.LastName, // EmployeeNo = k.Key.Id.EmployeeNo // }, // k => k.Value.Select(f => // new Inquiry // { // JobPosition = // new InquirerJobPosition // { // Name = f.JobPosition.Name, // DictionaryName = f.JobPosition.DictionaryName, // JobPositionLevel = (int)jobPositionAndData.Key.ConfigurationItemList.Single(cil => cil.Id == f.Point.ConfigurationItemId).InquirerJobPositionLevel // }, // Value = f.Point.JobIndexValue // }).ToList()) : null)) }, CustomFields = (from k in pmsAdminService.GetSharedCutomFieldListForJob(jobPositionAndData.Value.Job.SharedJob.Id, jobPositionAndData.Value.CustomFields.Select( m => m.JobCustomFieldId.SharedJobCustomFieldId).ToList()) join l in jobPositionAndData.Value.CustomFields on k.Id equals l.JobCustomFieldId.SharedJobCustomFieldId select new { k, l }).ToDictionary(j => j.k.DictionaryName, j => j.l.JobCustomFieldValue), Indices = jobPositionAndData.Value.Indices.ToDictionary( j => new JobIndex { Name = j.Key.Name, DictionaryName = j.Key.DictionaryName, IsInquireable = j.Key.IsInquireable, Group = new JobIndexGroup { Name = j.Key.Group.Name, DictionaryName = j.Key.Group.DictionaryName }, CustomFields = (from k in pmsAdminService.GetSharedCutomFieldListForJobIndex(j.Key.SharedJobIndexId, j.Key.CustomFieldValues.Select(m => m.Key).ToList()) join l in j.Key.CustomFieldValues on k.Id equals l.Key select new { k, l }).ToDictionary(m => m.k.DictionaryName, m => m.l.Value) }, j => (j.Value != null ? j.Value.ToDictionary( k => new PMS.RuleContracts.Employee { FirstName = k.Key.FirstName, LastName = k.Key.LastName, EmployeeNo = k.Key.Id.EmployeeNo }, k => k.Value.Select(f => new Inquiry { JobPosition = new InquirerJobPosition { Name = f.JobPosition.Name, DictionaryName = f.JobPosition.DictionaryName, JobPositionLevel = (int) jobPositionAndData.Key.ConfigurationItemList.Single( cil => cil.Id == f.Point.ConfigurationItemId && cil.Id.InquirerJobPositionId.SharedJobPositionId == f.JobPosition.Id).InquirerJobPositionLevel }, Value = f.Point.JobIndexValue }).ToList()) : null)) }).ToList() }; var ruleResult = addPreviousEmployeePointsToCalculationData(calculationData, calculationSession.CalculationPoints); res.Points = ruleResult; res.PathNo = calculationSession.PathNo; return(res); }
public CalculationPointPersistanceHolder CalculateFor(Employee employee, Period period, Calculation calculation, ICalculationDataProvider calculationDataProvider, CalculatorSession calculationSession) { if (!hasBeenSetup) { throw new PolicyEngineHasNotBeenSetupException(); } if (!preCalculationDone) { var lst = ruleClasses.Where(x => x.Value.RuleType == RuleType.PreCalculation); foreach (var item in lst) { ruleEngine.ExecuteRule(item.Key); } preCalculationDone = true; } ruleEngine.Clear(); PMSReport.Domain.Model.CalculationData employeeData; var calculationData = calculationDataProvider.Provide(employee, out employeeData, calculation, true, calculationSession); var rules = ruleClasses.Where(x => x.Value.RuleType == RuleType.PerCalculation); foreach (var item in rules) { ruleEngine.ExecuteRule(item.Key, calculationData); } var res = ruleEngine.GetResult <PMS.RuleContracts.RuleResult>(); rules = ruleClasses.Where(x => x.Value.RuleType == RuleType.PostCalculation); foreach (var item in rules) { ruleEngine.ExecuteRule(item.Key); } return(calculationDataProvider.Convert(res, employeeData, employee, period, calculation)); }
public void RuleTest() { using (var scope = new TransactionScope()) { using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PMSDBConnection"].ConnectionString)) { var uows = new MITD.Domain.Repository.UnitOfWorkScope( new Data.NH.NHUnitOfWorkFactory(() => PMSAdmin.Persistence.NH.PMSAdminSession.GetSession(con))); using (var uow = new NHUnitOfWork(PMSSession.GetSession(con))) using (var uow2 = uows.CurrentUnitOfWork) { con.Open(); var pmsAdminService = new PMS.ACL.PMSAdmin.PMSAdminService( new PMSAdmin.Application.UnitService(new PMSAdmin.Persistence.NH.UnitRepository(uows), new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)), new PMSAdmin.Application.JobService(new PMSAdmin.Persistence.NH.JobRepository(uows), new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)), new PMSAdmin.Application.CustomFieldService(new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)), new PMSAdmin.Application.JobPositionService(new PMSAdmin.Persistence.NH.JobPositionRepository(uows)), new PMSAdmin.Application.JobIndexService(new PMSAdmin.Persistence.NH.JobIndexRepository(uows), new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)), new PMSAdmin.Application.UnitIndexService(new PMSAdmin.Persistence.NH.UnitIndexRepository(uows), new PMSAdmin.Persistence.NH.CustomFieldRepository(uows)) ); EventPublisher publisher = new EventPublisher(); var rep = new PMS.Persistence.NH.EmployeeRepository(uow); var periodRep = new PMS.Persistence.NH.PeriodRepository(uow); var calcRep = new PMS.Persistence.NH.CalculationRepository(uow); var policyRep = new MITD.PMS.Persistence.NH.PolicyRepository(uow, new PolicyConfigurator( new RuleBasedPolicyEngineService(new LocatorProvider("PMSDbConnection"), publisher))); var provider = new PMS.Application.CalculationDataProvider(rep, pmsAdminService, new PMS.Persistence.NH.JobIndexPointRepository(uow)); var policy = policyRep.GetById(new PolicyId(1)); var period = periodRep.GetBy(c => c.Active); var emp = rep.GetBy(new EmployeeId("150554", period.Id)); // if period has no calculation var calculation = new Calculation(calcRep.GetNextId(), period, policy, Guid.NewGuid().ToString(), DateTime.Now, "150554"); calcRep.Add(calculation); uow.Commit(); // if period has calculation , get it by its identifier //var calculation = calcRep.GetById(new CalculationId(1)); MITD.PMSReport.Domain.Model.CalculationData empData; var pathNo = 1; List <SummaryCalculationPoint> calcList = new List <SummaryCalculationPoint>(); var session = new CalculatorSession(); while (pathNo <= 2) { Utils.Res = new MITD.PMS.RuleContracts.RuleResult(); session.AddCalculationPoints(calcList); session.PathNo = pathNo; var data = provider.Provide(emp, out empData, calculation, true, session); var rule1 = new Rule10(); rule1.Execute(data); var rule2 = new Rule11(); rule2.Execute(data); var rule3 = new Rule12(); rule3.Execute(data); //var rule4 = new Rule13(); //rule4.Execute(data); var res = provider.Convert(Utils.Res, empData, emp, period, calculation); calcList = res.CalculationPoints.OfType <SummaryCalculationPoint>().ToList(); var jipRep = new JobIndexPointRepository(uow); if (res.EmployeePointsForAdd != null) { foreach (var point in res.EmployeePointsForAdd) { jipRep.Add(point); } } if (res.EmployeePointsForUpdate != null) { foreach (var point in res.EmployeePointsForUpdate) { var employeePoint = jipRep.GetById(point.Key); employeePoint.SetValue(point.Value); } } uow.Commit(); pathNo++; } } } } }
public CalculationData Provide(Employee employee, out PMSReport.Domain.Model.CalculationData employeeData, Calculation calculation, bool withCalculationPoint, CalculatorSession pathNo) { throw new NotImplementedException(); }
public virtual CalculationPointPersistanceHolder CalculateFor(DateTime startDate, Employee employee, Period period, Calculation calculation, ICalculationDataProvider calculationDataProvider, IEventPublisher publisher, CalculatorSession calculationSession) { if (!PolicyEngine.HasBeenSetup) { PolicyEngine.SetupForCalculation(this, publisher); } var res = PolicyEngine.CalculateFor(employee, period, calculation, calculationDataProvider, calculationSession); return(res); }
public CalculationPointPersistanceHolder CalculateIndices(Calculation calculation, Policy policy, Period period, Employee employee, IEventPublisher publisher, CalculatorSession calculationSession) { CalculationPointPersistanceHolder pointsHolder; using (var transaction = new TransactionScope()) { var provider = calculationDataProviderFactory.Create(); try { pointsHolder = policy.CalculateFor(DateTime.Now, employee, period, calculation, provider, publisher, calculationSession); } finally { calculationDataProviderFactory.Release(provider); } transaction.Complete(); } return(pointsHolder); }
private CalculationPointPersistanceHolder calculateIndices(Calculation calculation, Policy policy, Period period, Employee employee, CalculatorSession calculationSession) { CalculationPointPersistanceHolder pointsHolder; var engine = calculatorEngineFactory.Create(); try { pointsHolder = engine.CalculateIndices(calculation, policy, period, employee, publisher, calculationSession); } finally { calculatorEngineFactory.Release(engine); } return(pointsHolder); }
private void start(Calculation calculation, bool doResume) { long totalCalcOperationCount = 0; long preCalcOperationDoneCount = 0; calculatorSession = new CalculatorSession { PathNo = 1 }; if (doResume) { var calcResult = calculation.CalculationResult; foreach (string s in calcResult.Messages) { messages.Add(s); } messages.Add("====================================================="); totalCalcOperationCount = calcResult.TotalEmployeesCount; preCalcOperationDoneCount = calcResult.EmployeesCalculatedCount; progress.SetProgress(totalCalcOperationCount, preCalcOperationDoneCount); if (calcResult.LastCalculatedPath.HasValue) { progress.SetLastCalculatedEmployee(calcResult.LastCalculatedEmployeeId, calcResult.LastCalculatedPath.Value); calculatorSession.PathNo = calcResult.LastCalculatedPath.Value; } var engine = calculatorEngineFactory.Create(); try { calculatorSession.AddCalculationPoints(engine.GetCalculationPiontBy(calculation.Id)); } finally { calculatorEngineFactory.Release(engine); } } Policy policy = null; messages.Add(DateTime.Now + "شروع محاسبه "); try { startTrigger.Set(); CalculationCompletedSub = new DelegateHandler <CalculationCompleted>(e => { var engine = calculatorEngineFactory.Create(); try { engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints); engine.UpdateCalculationResult(calculation, progress, this, messages); } finally { calculatorEngineFactory.Release(engine); } }); publisher.RegisterHandler(CalculationCompletedSub); rulsCompiledSub = new DelegateHandler <RulesCompiled>(e => { if (!doResume) { Task.Factory.StartNew(() => { var engine = calculatorEngineFactory.Create(); try { engine.UpdateCompileResult(calculation.Id, e.CompileResult.LibraryText, e.Rules); } finally { calculatorEngineFactory.Release(engine); } messages.Add(DateTime.Now + " قوانین کامپایل شدند"); }); } }); publisher.RegisterHandler(rulsCompiledSub); publisher.RegisterHandler <JobIndexPointsReady>(jobIndexPointPersister); publisher.RegisterHandler <CalculationExceptionReady>(calculationExceptionPersister); publisher.RegisterHandler <CalculationCompleted>(jobIndexPointPersister); publisher.RegisterHandler <CalculationCompleted>(calculationExceptionPersister); publisher.RegisterHandler <CalculationForPathCompleted>(jobIndexPointPersister); publisher.RegisterHandler <PathPersisteCompleted>(this); Dictionary <int, IList <Employee> > employeesWithPath; Period period; fetchPolicyAndEmployees(calculation, doResume, calculatorSession.PathNo, out policy, out employeesWithPath, out period); deleteCalculationException(calculation); long currentCalcOperationCount = 0; foreach (var employees in employeesWithPath) { currentCalcOperationCount = currentCalcOperationCount + employees.Value.Count(); } if (!doResume) { totalCalcOperationCount = currentCalcOperationCount; progress.SetProgress(totalCalcOperationCount, 0); } messages.Add(DateTime.Now + " تعداد " + currentCalcOperationCount + " عملیات محاسبه برای " + calculation.EmployeeCount + " کارمند آماده می باشد"); var currentCalcOperationDoneCount = preCalcOperationDoneCount; var pathCount = employeesWithPath.Count(); foreach (var emlpoyees in employeesWithPath) { calculatorSession.PathNo = emlpoyees.Key; foreach (var employee in emlpoyees.Value) { try { if (doStop || doPause) { break; } var pointsHolder = calculateIndices(calculation, policy, period, employee, calculatorSession); currentCalcOperationDoneCount++; progress.SetProgress(totalCalcOperationCount, currentCalcOperationDoneCount); addOrUpdateCalculationPoints(pointsHolder.CalculationPoints); publisher.Publish(new JobIndexPointsReady(pointsHolder, calculation.Id, employee.Id, calculatorSession.PathNo)); progress.SetLastCalculatedEmployee(employee.Id, calculatorSession.PathNo); } catch (Exception ex) { publisher.Publish(new CalculationExceptionReady(calculation.Id, employee.Id, calculatorSession.PathNo, ex)); calculatorSession.HasEmployeeCalculationFailed = true; messages.Add("*** خطا در محاسبه شاخص های کارمند " + employee.Id.EmployeeNo + " **** "); var logServiceMngt = LogServiceFactory.Create(); try { var logService = logServiceMngt.GetService(); logService.AddEventLog("JobIndexPointCalculator_EmpId:" + employee.Id.EmployeeNo, LogLevel.Error, null, this.GetType().Name, "start", ex.Message, ex.StackTrace); logService.AddExceptionLog(ex); } finally { LogServiceFactory.Release(logServiceMngt); } } } if (pathCount > calculatorSession.PathNo) { publisher.Publish(new CalculationForPathCompleted()); pathPersisterTrigger.WaitOne(); } if (calculatorSession.HasEmployeeCalculationFailed) { break; } } messages.Add(DateTime.Now + " تعداد " + currentCalcOperationDoneCount + " عملیات محاسبه انجام شد"); if (!(doStop || doPause)) { publisher.Publish(new CalculationCompleted()); } else if (doStop) { messages.Add(DateTime.Now + " لغو محاسبه."); } else if (doPause) { messages.Add(DateTime.Now + " وقفه در محاسبه."); } if (doPause || doStop) { var engine = calculatorEngineFactory.Create(); try { engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints); } finally { calculatorEngineFactory.Release(engine); } } pauseTrigger.Set(); } catch (Exception e) { startTrigger.Set(); calculatorSession.HasEmployeeCalculationFailed = true; messages.Add(DateTime.Now + "در آماده سازی محاسبه با مشکل مواجه شده است"); publisher.Publish(new CalculationCompleted()); } finally { state = CalculatorState.Completed; if (policy != null) { policy.Dispose(); } } }