internal static string GetRulePreview(Rule rule) { StringBuilder builder = new StringBuilder(); if (rule != null) { builder.Append("IF "); if (rule.Condition != null) { builder.Append(rule.Condition.ToString() + " "); } builder.Append("THEN "); foreach (RuleAction action in rule.ThenActions) { builder.Append(action.ToString()); builder.Append(' '); } if (rule.ElseActions.Count > 0) { builder.Append("ELSE "); foreach (RuleAction action2 in rule.ElseActions) { builder.Append(action2.ToString()); builder.Append(' '); } } } return builder.ToString(); }
private string GetRulePreview(System.Workflow.Activities.Rules.Rule rule) { StringBuilder rulePreview = new StringBuilder(); if (rule != null) { rulePreview.Append("IF "); if (rule.Condition != null) { rulePreview.Append(rule.Condition.ToString() + " "); } rulePreview.Append("THEN "); foreach (RuleAction action in rule.ThenActions) { rulePreview.Append(action.ToString()); rulePreview.Append(' '); } if (rule.ElseActions.Count > 0) { rulePreview.Append("ELSE "); foreach (RuleAction action in rule.ElseActions) { rulePreview.Append(action.ToString()); rulePreview.Append(' '); } } } return(rulePreview.ToString()); }
public static Rule Deserialize(this RuleContract contract) { Assembly ruleAssembly = typeof(Rule).Assembly; Type parserType = ruleAssembly.GetType("System.Workflow.Activities.Rules.Parser"); RuleValidation validation = new RuleValidation(typeof(Person), null); var internalRuleParser = Activator.CreateInstance(parserType, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { validation }, null); var parseConditionMethodInfo = parserType.GetMethod("ParseCondition", BindingFlags.Instance | BindingFlags.NonPublic); RuleExpressionCondition condition = parseConditionMethodInfo.Invoke(internalRuleParser, new object[] { contract.Condition }) as RuleExpressionCondition; Rule rule = new Rule { Name = contract.Name, Priority = contract.Priority, ReevaluationBehavior = (RuleReevaluationBehavior)Enum.Parse(typeof(RuleReevaluationBehavior), contract.Reevaluation), Active = contract.Active, Condition = condition, }; var parseSingleStatementMethodInfo = parserType.GetMethod("ParseSingleStatement", BindingFlags.Instance | BindingFlags.NonPublic); foreach (string str in contract.ThenActions) { RuleAction action = parseSingleStatementMethodInfo.Invoke(internalRuleParser, new object[] { str }) as RuleAction; rule.ThenActions.Add(action); } var parseStatementListMethodInfo = parserType.GetMethod("ParseStatementList", BindingFlags.Instance | BindingFlags.NonPublic); IList<RuleAction> elseActions = parseStatementListMethodInfo.Invoke(internalRuleParser, new object[] { string.Join("\n", contract.ElseActions) }) as IList<RuleAction>; if (elseActions != null) { foreach (RuleAction action in elseActions) { rule.ElseActions.Add(action); } } return rule; }
private static RuleSymbolInfo AnalyzeRule(RuleChainingBehavior behavior, Rule rule, RuleValidation validator, Tracer tracer) { RuleSymbolInfo rsi = new RuleSymbolInfo(); if (rule.Condition != null) { rsi.conditionDependencies = rule.Condition.GetDependencies(validator); if ((rsi.conditionDependencies != null) && (tracer != null)) tracer.TraceConditionSymbols(rule.Name, rsi.conditionDependencies); } if (rule.thenActions != null) { rsi.thenSideEffects = GetActionSideEffects(behavior, rule.thenActions, validator); if ((rsi.thenSideEffects != null) && (tracer != null)) tracer.TraceThenSymbols(rule.Name, rsi.thenSideEffects); } if (rule.elseActions != null) { rsi.elseSideEffects = GetActionSideEffects(behavior, rule.elseActions, validator); if ((rsi.elseSideEffects != null) && (tracer != null)) tracer.TraceElseSymbols(rule.Name, rsi.elseSideEffects); } return rsi; }
internal RuleState(Rule rule) { this.Rule = rule; }
private void button1_Click(object sender, EventArgs e) { Excel.Range cell; RuleSet myRuleset = new RuleSet("RuleSet1"); // Define property and activity reference expressions through CodeDom functionality CodeThisReferenceExpression refexp = new CodeThisReferenceExpression(); CodePropertyReferenceExpression refTotalCost = new CodePropertyReferenceExpression(refexp, "totalCost"); CodePropertyReferenceExpression refParamCategory = new CodePropertyReferenceExpression(refexp, "paramCategory"); CodePropertyReferenceExpression refParamPrivilege = new CodePropertyReferenceExpression(refexp, "paramPrivilege"); // Example : // IF paramCategory == 3 // THEN totalCost = totalCost + 300 for (int row = 4; row <= 8; row++) { cell = (Excel.Range)this.Cells[row, 2]; if (String.IsNullOrEmpty((string)cell.Value2)) break; Rule myRule = new Rule("Rule" + row); myRuleset.Rules.Add(myRule); // Example : // paramCategory == 3 CodeBinaryOperatorExpression ruleCondition = new CodeBinaryOperatorExpression(); if ((string)cell.Value2 == "種別 (category)") ruleCondition.Left = refParamCategory; else if ((string)cell.Value2 == "特典 (privilige)") ruleCondition.Left = refParamPrivilege; ruleCondition.Operator = CodeBinaryOperatorType.ValueEquality; cell = (Excel.Range)this.Cells[row, 3]; ruleCondition.Right = new CodePrimitiveExpression((int)(double)cell.Value2); myRule.Condition = new RuleExpressionCondition(ruleCondition); // Example : // totalCost = totalCost + 300 CodeAssignStatement ruleAction = new CodeAssignStatement(); ruleAction.Left = refTotalCost; CodeBinaryOperatorExpression actionRight = new CodeBinaryOperatorExpression(); actionRight.Left = refTotalCost; cell = (Excel.Range)this.Cells[row, 4]; if((string)cell.Value2 == "+") actionRight.Operator = CodeBinaryOperatorType.Add; else if ((string)cell.Value2 == "-") actionRight.Operator = CodeBinaryOperatorType.Subtract; else if ((string)cell.Value2 == "*") actionRight.Operator = CodeBinaryOperatorType.Multiply; else if ((string)cell.Value2 == "/") actionRight.Operator = CodeBinaryOperatorType.Divide; cell = (Excel.Range)this.Cells[row, 5]; actionRight.Right = new CodePrimitiveExpression((int)(double)cell.Value2); ruleAction.Right = actionRight; myRule.ThenActions.Add(new RuleStatementAction(ruleAction)); } // 必要に応じ、RuleValidation オブジェクトを使ってチェック! // (今回は省略 . . . . . . .) // RuleDefinitions を設定して保存 RuleDefinitions ruleDef = new RuleDefinitions(); ruleDef.RuleSets.Add(myRuleset); WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer(); cell = (Excel.Range) this.Cells[11, 2]; XmlTextWriter writer = new XmlTextWriter((string) cell.Value2, System.Text.Encoding.Unicode); serializer.Serialize(writer, ruleDef); writer.Close(); // ここでは、すぐにコンパイルして実行 // (Custom WorkflowRuntime Service と Custom Policy Activity を作成して、データベースなど独自の Rule Cache を作成することも可能 . . .) MessageBox.Show("ルールを反映しました"); }
public static RuleSet BuildRuleSet(DataTable dtRules, RuleValidation validation) { RuleSet rules = new RuleSet("RuleSet 1"); RulesParser parser = new RulesParser(validation); DataView vwRules = new DataView(dtRules); vwRules.RowFilter = "ACTIVE = 1"; vwRules.Sort = "PRIORITY asc"; foreach ( DataRowView row in vwRules ) { string sRULE_NAME = Sql.ToString (row["RULE_NAME" ]); int nPRIORITY = Sql.ToInteger(row["PRIORITY" ]); string sREEVALUATION = Sql.ToString (row["REEVALUATION"]); bool bACTIVE = Sql.ToBoolean(row["ACTIVE" ]); string sCONDITION = Sql.ToString (row["CONDITION" ]); string sTHEN_ACTIONS = Sql.ToString (row["THEN_ACTIONS"]); string sELSE_ACTIONS = Sql.ToString (row["ELSE_ACTIONS"]); RuleExpressionCondition condition = parser.ParseCondition (sCONDITION ); List<RuleAction> lstThenActions = parser.ParseStatementList(sTHEN_ACTIONS); List<RuleAction> lstElseActions = parser.ParseStatementList(sELSE_ACTIONS); System.Workflow.Activities.Rules.Rule r = new System.Workflow.Activities.Rules.Rule(sRULE_NAME, condition, lstThenActions, lstElseActions); r.Priority = nPRIORITY; r.Active = bACTIVE ; //r.ReevaluationBehavior = (RuleReevaluationBehavior) Enum.Parse(typeof(RuleReevaluationBehavior), sREEVALUATION); // 12/04/2010 Play it safe and never-reevaluate. r.ReevaluationBehavior = RuleReevaluationBehavior.Never; rules.Rules.Add(r); } rules.Validate(validation); if ( validation.Errors.HasErrors ) { throw(new Exception(RulesUtil.GetValidationErrors(validation))); } return rules; }
// 12/12/2012 For security reasons, we want to restrict the data types available to the rules wizard. public static void RulesValidate(Guid gID, string sRULE_NAME, int nPRIORITY, string sREEVALUATION, bool bACTIVE, string sCONDITION, string sTHEN_ACTIONS, string sELSE_ACTIONS, Type thisType, SplendidRulesTypeProvider typeProvider) { RuleSet rules = new RuleSet("RuleSet 1"); RuleValidation validation = new RuleValidation(thisType, typeProvider); RulesParser parser = new RulesParser(validation); RuleExpressionCondition condition = parser.ParseCondition (sCONDITION ); List<RuleAction> lstThenActions = parser.ParseStatementList(sTHEN_ACTIONS); List<RuleAction> lstElseActions = parser.ParseStatementList(sELSE_ACTIONS); System.Workflow.Activities.Rules.Rule r = new System.Workflow.Activities.Rules.Rule(sRULE_NAME, condition, lstThenActions, lstElseActions); r.Priority = nPRIORITY; r.Active = bACTIVE ; //r.ReevaluationBehavior = (RuleReevaluationBehavior) Enum.Parse(typeof(RuleReevaluationBehavior), sREEVALUATION); // 12/04/2010 Play it safe and never-reevaluate. r.ReevaluationBehavior = RuleReevaluationBehavior.Never; rules.Rules.Add(r); rules.Validate(validation); if ( validation.Errors.HasErrors ) { throw(new Exception(GetValidationErrors(validation))); } }
private Rule SetUpRule(Rule rule, RuleStatementAction action, RuleObject ruleObj) { Rule thisRule = rule.Clone(); thisRule.ThenActions.Clear(); thisRule.ElseActions.Clear(); thisRule.Name = ruleObj.Name; thisRule.Condition = SetRuleCondition((RuleExpressionCondition)thisRule.Condition, ruleObj.Condition); foreach (ActionObject actionObj in ruleObj.ThenActions) { RuleStatementAction thisAction = (RuleStatementAction)action.Clone(); thisAction = SetRuleAction(thisAction, actionObj); thisRule.ThenActions.Add(thisAction); } foreach (ActionObject actionObj in ruleObj.ElseActions) { RuleStatementAction thisAction = (RuleStatementAction)action.Clone(); thisAction = SetRuleAction(thisAction, actionObj); thisRule.ElseActions.Add(thisAction); } return thisRule; }
private void UpdateItem(ListViewItem listViewItem, Rule rule) { listViewItem.SubItems[0].Text = rule.Name; listViewItem.SubItems[1].Text = rule.Priority.ToString(CultureInfo.CurrentCulture); listViewItem.SubItems[2].Text = (string) this.reevaluationComboBox.Items[(int) rule.ReevaluationBehavior]; listViewItem.SubItems[3].Text = rule.Active.ToString(CultureInfo.CurrentCulture); listViewItem.SubItems[4].Text = System.Workflow.Activities.Rules.Design.DesignerHelpers.GetRulePreview(rule); }
private void newRuleToolStripButton_Click(object sender, EventArgs e) { if (this.rulesToolStrip.Focus()) { Rule rule = new Rule { Name = this.CreateNewName() }; this.dialogRuleSet.Rules.Add(rule); ListViewItem item = this.AddNewItem(rule); item.Selected = true; item.Focused = true; int index = this.rulesListView.Items.IndexOf(item); this.rulesListView.EnsureVisible(index); } }
private ListViewItem AddNewItem(Rule rule) { ListViewItem item = new ListViewItem(new string[] { rule.Name, string.Empty, string.Empty, string.Empty, string.Empty }); this.rulesListView.Items.Add(item); item.Tag = rule; this.UpdateItem(item, rule); return item; }