public Tuple <bool, List <GradeKpiItem> > KpiItemOperator(User user, IList <KpiItem> kpiItems, string logicalOperator, TimeType timeType) { // 条件结果 var oldOperator = logicalOperator; var kpiItemResult = false; var gradeKpiItemList = new List <GradeKpiItem>(); foreach (var kpiItem in kpiItems) { var kpi = new Kpi { ModuleId = kpiItem.KpiConfigId, UserId = user.Id, Type = timeType }; // 最后一条Kpi考核记录 var lastKpi = Resolve <IKpiService>().GetLastSingle(kpi); if (lastKpi != null) { kpiItemResult = LinqHelper.CompareByOperator(kpiItem.OperatorCompare, kpiItem.Amount, lastKpi.TotalValue); // 记录记录 var kpiConfig = Resolve <IAutoConfigService>().GetList <KpiAutoConfig>().ToList() .FirstOrDefault(r => r.Id == kpiItem.KpiConfigId); var gradeKpiItem = new GradeKpiItem { Amount = lastKpi.TotalValue, KpiId = kpi.Id, KpiName = kpiConfig?.Name, KpiConfigId = kpiConfig?.Id }; gradeKpiItemList.Add(gradeKpiItem); } else { kpiItemResult = false; } // 将唯一标识,用条件结果替换 logicalOperator = logicalOperator.Replace(kpiItem.Key, kpiItemResult.ToString().ToLower()); } // 根据逻辑计算结算结果 try { kpiItemResult = LogicExpression.Operate(logicalOperator); } catch { throw new ValidException("计算报错,请确认逻辑运算符格式是否正确,逻辑预算符:" + oldOperator); } return(Tuple.Create(kpiItemResult, gradeKpiItemList)); }
public void TestVariables() { LogicExpression test = x & x; VariableLister <bool> lister = new VariableLister <bool>(); test.Accept(lister); Assert.AreEqual(1, lister.Variables.Count(), "There should only be one variable"); Assert.AreEqual("x", lister.Variables.First()); }
static void Define(string[] args) { if (args.Length == 0) { // Dump all variables. foreach (var definition in Definitions) { var exp = LogicExpression.Create(definition.Value); Console.WriteLine($"=> {definition.Key} = {definition.Value} ({exp.GetPropositionText()})"); } return; } var temp = string.Join(' ', args); var parts = temp.Split('=', StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { throw new ArgumentException("Incorrect syntax. Expected: def [variable] = [expression]."); } var variable = parts[0].Trim(); if (variable.IndexOf(' ') >= 0) { throw new ArgumentException("Variable name must not contain spaces."); } var expression = parts[1].Trim(); // Substitution foreach (var definition in Definitions) { expression = expression.Replace(definition.Key, definition.Value, StringComparison.CurrentCultureIgnoreCase); } if (!LogicExpression.IsValid(expression, out string validationMessage)) { Console.WriteLine($"Invalid expression: {validationMessage}"); return; } if (Definitions.ContainsKey(variable)) { Definitions[variable] = expression; } else { Definitions.Add(variable, expression); } Console.WriteLine($"=> {variable} = {expression}"); }
public void TestOr() { LogicExpression test1 = logicFalse | logicFalse; LogicExpression test2 = logicTrue | logicFalse; LogicExpression test3 = logicFalse | logicTrue; LogicExpression test4 = logicTrue | logicTrue; Assert.AreEqual(False, testAsync(test1, defaultTimeout).Result); Assert.AreEqual(True, testAsync(test2, defaultTimeout).Result); Assert.AreEqual(True, testAsync(test3, defaultTimeout).Result); Assert.AreEqual(True, testAsync(test4, defaultTimeout).Result); }
public void OperateTest_2() { var arrLogicValue = new bool[6] { true, false, false, true, false, true }; //假设所有bool值都用一个字母表示,也可以使用多个字母,但是需要统一字母的数量 var logicExpression = " ((AA && !BB)|| CC) && DD ||(EE && FF)"; var logicResult = LogicExpression.Operate(logicExpression, arrLogicValue.ToList()); Assert.Equal(logicResult, true); }
public void WriteRowExpressionValue(int row, LogicExpression expression, bool value) { int width = RemoveRedundantParenthesis(expression.Expression).Length + 2; int mid = width / 2; char valueChar = value ? 'T' : 'F'; for (int i = 0; i < width; i++) { _textWriter.Write(i == mid ? valueChar : ' '); } _textWriter.Write('|'); }
public static IFilterExpression Or(IEnumerable <IFilterExpression> filters) { if (filters?.Count() == 0) { return(null); } var filter = filters.First(); for (var i = 1; i < filters.Count(); i++) { filter = new LogicExpression(filter, Operator.Or, filters.ElementAt(i)); } return(filter); }
static void PrintTruthTable(string[] args) { if (args.Length == 0) { throw new ArgumentException("Expected expression."); } var expressionSyntax = string.Join(' ', args); if (!LogicExpression.IsValid(expressionSyntax, out string validationMessage)) { Console.WriteLine($"Invalid expression: {validationMessage}"); return; } var expression = LogicExpression.Create(expressionSyntax); var truthTableBuilder = new TruthTableBuilder(); truthTableBuilder.WriteTruthTable(expression, new AsciiTruthTableWriter(Console.Out)); }
public static LogicExpression AnyEqVarParam(this IEnumerable <IColumnExpression> cols) { IFilterExpression filter = null; LogicExpression logic = null; foreach (var col in cols) { if (filter == null) { filter = col.EqVarParam(); } else { logic = filter.Or(col.EqVarParam()); filter = logic; } //filter = filter == null ? col.EqVarParam() as IFilterExpression : filter.Or(col.EqVarParam()); } return(logic); }
public static LogicExpression AnyEq(this IEnumerable <IColumnExpression> cols, IEnumerable <IValueExpression> values) { IFilterExpression filter = null; LogicExpression logic = null; var em = values.GetEnumerator(); foreach (var col in cols) { em.MoveNext(); if (filter == null) { filter = col.Eq(em.Current); } else { logic = filter.Or(col.Eq(em.Current)); filter = logic; } //filter = filter == null ? col.Eq(em.Current) as IFilterExpression : filter.Or(col.Eq(em.Current)); } return(logic); }
static void PrintSubExpressions(string[] args) { if (args.Length == 0) { throw new ArgumentException("Expected expression."); } var expressionSyntax = string.Join(' ', args); if (!LogicExpression.IsValid(expressionSyntax, out string validationMessage)) { Console.WriteLine($"Invalid expression: {validationMessage}"); return; } var expression = LogicExpression.Create(expressionSyntax); var subExpressions = expression.GetSubExpressions(); foreach (var subExpression in subExpressions) { Console.WriteLine($"=> {subExpression}"); } }
public IfLogicStatement(LogicExpression condition, LogicStatement statement) { this.Condition = condition; this.Statement = statement; }
public Causes(Action action, LogicExpression result) : base(action, new AgentsList(), result, new True()) { }
public IfElseLogicStatement(LogicExpression condition, LogicStatement statement, LogicStatement elseStatement) { this.Condition = condition; this.Statement = statement; this.ElseStatement = elseStatement; }
public ReturnLogicStatement(LogicExpression expression) { this.Expression = expression; }
public WhileLogicStatement(LogicExpression condition, LogicStatement statement) { this.Condition = condition; this.Statement = statement; }
public Initially(LogicExpression condition) { Condition = condition; }
public CausesIf(Action action, LogicExpression result, LogicExpression condition) : base(action, new AgentsList(), result, condition) { }
public NotByIf(Action action, AgentsList agents, LogicExpression condition) { Action = action; Agents = agents; Condition = condition; }
public void CursorOnContextMenuItem(MenuButton sender) { switch (_setLabelSteps) { case SetLabelSteps.ChoiceLabelType: { _activeLabelType = _labelTypeMenuButtons[sender]; _setLabelSteps = SetLabelSteps.SetValue; _contextMenu.ClearContextMenu(); if (_activeLabelType == ActiveLabelType.Variable) { SetLabelVariableMenuItems(); } else if (_activeLabelType == ActiveLabelType.Expression) { if (_setLabelTarget.ValueType == ValueType.Bool) { _setLabelTarget.Value = new LogicExpression(); } else { _setLabelTarget.Value = new Expression(); } if (_setLabelTarget.Value.Parent == null) { _setLabelValueUi.Value = _setLabelTarget.Value; } _setLabelValueUi.RebuildAnValue(); _contextMenu.gameObject.SetActive(false); _setLabelSteps = SetLabelSteps.SetTarget; } else if (_activeLabelType == ActiveLabelType.Constant) { _mainController.SetValueFromDialog(_setLabelValueUi, _setLabelTarget.Value); _contextMenu.gameObject.SetActive(false); _setLabelSteps = SetLabelSteps.SetTarget; } else if (_activeLabelType == ActiveLabelType.Operation) { if (_setLabelTarget.LabelType == ActiveLabelType.Condition) { SetRelationMenuItems(); } else { if (_setLabelTarget.ValueType == ValueType.Bool) { SetLogicOperationMenuItems(); } else { SetNumberOperationMenuItems(); } } _setLabelSteps = SetLabelSteps.SetValue; } else if (_activeLabelType == ActiveLabelType.Condition) { _setLabelTarget.Value = new Condition(); if (_setLabelTarget.Value.Parent == null) { _setLabelValueUi.Value = _setLabelTarget.Value; } _setLabelValueUi.RebuildAnValue(); _contextMenu.gameObject.SetActive(false); _setLabelSteps = SetLabelSteps.SetTarget; } } break; case SetLabelSteps.SetValue: { if (_activeLabelType == ActiveLabelType.Operation) { if (_setLabelTarget.LabelType == ActiveLabelType.Condition) { Condition condition = _setLabelTarget.Value as Condition; condition.Relation = _relationMenuButtons[sender]; } else { if (_setLabelTarget.ValueType == ValueType.Bool) { LogicExpression logicExpression = _setLabelTarget.Value as LogicExpression; logicExpression.Operation = _logicOperationMenuButtons[sender]; } else { Expression numberExpression = _setLabelTarget.Value as Expression; numberExpression.Operation = _numberOperationMenuButtons[sender]; } } _setLabelValueUi.RebuildAnValue(); } else if (_activeLabelType == ActiveLabelType.Variable) { if (_isSetedLabelVariable) { _setLabelTarget.Value = _labelVariableMenuButtons[sender]; _setVariableBlock.SetBlock.Variable = _labelVariableMenuButtons[sender]; _setVariableBlock.RefreshParameter(); } else { _setLabelTarget.Value = _labelVariableMenuButtons[sender]; if (_setLabelTarget.Value.Parent == null) { _setLabelValueUi.Value = _setLabelTarget.Value; } _setLabelValueUi.RebuildAnValue(); } } _contextMenu.gameObject.SetActive(false); _setLabelSteps = SetLabelSteps.SetTarget; } break; } }
public NecessaryEngagedFrom(AgentsList agents, Instruction instructions, LogicExpression condition) { QueriedAgents = agents; Instructions = instructions; Condition = condition; }
public Always(LogicExpression condition) { Condition = condition; }
public void OperateTest(string logicExpression, bool result) { var logicResult = LogicExpression.Operate(logicExpression); Assert.Equal(logicResult, result); }
public PossiblyAfter(Instruction instructions, LogicExpression finaly) : base(instructions, finaly, new True()) { }
public ImpossibleByIf(Action action, AgentsList agents, LogicExpression condition) : base(action, agents, new False(), condition) { }
public ImpossibleIf(Action action, LogicExpression condition) : base(action, new AgentsList(), new False(), condition) { }
public PossiblyAfterFrom(Instruction instructions, LogicExpression finaly, LogicExpression condition) { Instructions = instructions; Result = finaly; Condition = condition; }
public ObservableAfter(LogicExpression finalCondition, Instruction instructions) { FinalCondition = finalCondition; Instructions = instructions; }
public ReleasesIf(Action action, Fluent fluent, LogicExpression condition) : base(action, new AgentsList(), fluent, condition) { }
public NecessaryExecutableFrom(Instruction instructions, LogicExpression condition) { Instructions = instructions; Condition = condition; }
public ByCauses(Action action, AgentsList agents, LogicExpression result) : base(action, agents, result, new True()) { }