Share via


IRuleExpression 인터페이스

정의

사용자 지정 식을 작성하기 위해 사용자 지정 식 작성기가 파생되어야 하는 기본 클래스를 나타냅니다.

public interface class IRuleExpression
public interface IRuleExpression
type IRuleExpression = interface
Public Interface IRuleExpression

예제

다음 코드는 선언적 조건과 규칙 집합에 사용할 수 있는 식을 만듭니다. 이 식은 이름이 TwoOfThree이고 3개의 매개 변수를 사용하며, 세 매개 변수 모두 부울로 계산되어야 합니다. 이 식은 3개 중 2개 식이 true를 반환하면 true를 반환합니다.

이 코드를 사용하려면 클래스 라이브러리 프로젝트에 추가하고 워크플로 프로젝트에서 라이브러리를 참조합니다.

using System.CodeDom;  
using System.Text;  
using System.Workflow.Activities.Rules;  
using System.Workflow.ComponentModel.Compiler;  

namespace TwoOfThreeRuleExpression  
{  
    public class TwoOfThree : CodeExpression, IRuleExpression  
    {  
        CodeExpression expression1, expression2, expression3;  

        public CodeExpression First  
        {  
            get { return expression1; }  
            set { expression1 = value; }  
        }  

        public CodeExpression Second  
        {  
            get { return expression2; }  
            set { expression2 = value; }  
        }  

        public CodeExpression Third  
        {  
            get { return expression3; }  
            set { expression3 = value; }  
        }  

        public TwoOfThree()  
        {  
            // constructor required for deserialization  
        }  

        public TwoOfThree(CodeExpression first, CodeExpression second, CodeExpression third)  
        {  
            // constructor required by parser  
            expression1 = first;  
            expression2 = second;  
            expression3 = third;  
        }  

        public void AnalyzeUsage(RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)  
        {  
            // check what the 3 expressions use  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression1, true, false, null);  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression2, true, false, null);  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);  
        }  

        public CodeExpression Clone()  
        {  
            TwoOfThree result = new TwoOfThree();  
            result.expression1 = RuleExpressionWalker.Clone(expression1);  
            result.expression2 = RuleExpressionWalker.Clone(expression2);  
            result.expression3 = RuleExpressionWalker.Clone(expression3);  
            return result;  
        }  

        public void Decompile(StringBuilder stringBuilder, CodeExpression parentExpression)  
        {  
            // what should be displayed by the parser  
            stringBuilder.Append("TwoOfThree(");  
            RuleExpressionWalker.Decompile(stringBuilder, expression1, this);  
            stringBuilder.Append(", ");  
            RuleExpressionWalker.Decompile(stringBuilder, expression2, this);  
            stringBuilder.Append(", ");  
            RuleExpressionWalker.Decompile(stringBuilder, expression3, this);  
            stringBuilder.Append(")");  
        }  

        static RuleLiteralResult resultTrue = new RuleLiteralResult(true);  
        static RuleLiteralResult resultFalse = new RuleLiteralResult(false);  

        public RuleExpressionResult Evaluate(RuleExecution execution)  
        {  
            // start by doing the first 2 expressions  
            RuleExpressionResult r1 = RuleExpressionWalker.Evaluate(execution, expression1);  
            RuleExpressionResult r2 = RuleExpressionWalker.Evaluate(execution, expression2);  
            bool b1 = (bool)r1.Value;  
            bool b2 = (bool)r2.Value;  
            if (b1 && b2)  
            {  
                // both are true, so result is true  
                return resultTrue;  
            }  
            else if (b1 || b2)  
            {  
                // only one of the first 2 is true, evaluate the third to determine result  
                return RuleExpressionWalker.Evaluate(execution, expression3);  
            }  
            else  
                // both e1 and e2 are false, so skip e3 and return false;  
                return resultFalse;  
        }  

        public bool Match(CodeExpression expression)  
        {  
            TwoOfThree other = expression as TwoOfThree;  
            return (other != null) &&  
                RuleExpressionWalker.Match(expression1, other.expression1) &&  
                RuleExpressionWalker.Match(expression2, other.expression2) &&  
                RuleExpressionWalker.Match(expression3, other.expression3);  
        }  

        public RuleExpressionInfo Validate(RuleValidation validation, bool isWritten)  
        {  
            ValidateExpression(validation, expression1, "First");  
            ValidateExpression(validation, expression2, "Second");  
            ValidateExpression(validation, expression3, "Third");  
            return new RuleExpressionInfo(typeof(bool));  
        }  

        private void ValidateExpression(RuleValidation validation, CodeExpression expression, string propertyName)  
        {  
            ValidationError error;  
            if (expression == null)  
            {  
                error = new ValidationError(propertyName + " cannot be null", 123);  
                validation.Errors.Add(error);  
            }  
            else  
            {  
                RuleExpressionInfo result = RuleExpressionWalker.Validate(validation, expression, false);  
                if ((result == null) || (result.ExpressionType != typeof(bool)))  
                {  
                    error = new ValidationError(propertyName + " must return boolean result", 123);  
                    validation.Errors.Add(error);  
                }  
            }  
        }  
    }  
}  

메서드

AnalyzeUsage(RuleAnalysis, Boolean, Boolean, RulePathQualifier)

파생된 클래스에서 재정의되면 개체가 컨텍스트 형식으로 필드와 속성을 사용하는 방법을 보고합니다.

Clone()

파생 클래스에서 재정의하는 경우 현재 CodeExpression의 전체 복사본을 만듭니다.

Decompile(StringBuilder, CodeExpression)

파생 클래스에서 재정의하는 경우 사용자 지정 식을 문자열 형식으로 디컴파일합니다.

Evaluate(RuleExecution)

파생 클래스에서 재정의된 경우 사용자 지정 식을 계산합니다.

Match(CodeExpression)

현재 식과 다른 식을 비교하여 같은지 확인합니다.

Validate(RuleValidation, Boolean)

파생 클래스에서 재정의된 경우 식이 올바로 구성되고 오류가 없는지 확인합니다.

적용 대상