Ejemplo n.º 1
0
        void SetupRules()
        {
            IRule accessRightsViolationRule = new Rule(Guid.NewGuid().ToString());

            accessRightsViolationRule
            .AddVariableScope(_componentVar)
            .AddVariableScope(_eventNameVar)
            .AddVariableScope(_isAdminVar, true)
            .AddVariableScope(_accessRightsVar, true);

            //Event-trigger condition
            IBooleanBase componentCondition     = EqualToExpression.New(_componentVar, new Literal(AUTHENTICATION_COMPONENT_NAME));
            IBooleanBase eventCondition         = EqualToExpression.New(_eventNameVar, new Literal(AUTHENTICATION_EVENT_NAME));
            IBooleanBase matchingEventCondition = AndExpression.New(componentCondition, eventCondition);

            //Parameter-trigger condition
            IBooleanBase accessRightsCondition           = EqualToExpression.New(_accessRightsVar, new Literal(Rights.Full));
            IBooleanBase isAdministratorCondition        = EqualToExpression.New(_isAdminVar, new Literal(true));
            IBooleanBase notAllowedAccessRightsCondition = AndExpression.New(accessRightsCondition, NotExpression.New(isAdministratorCondition));

            //Trigger condition
            IBooleanBase triggerCondition = AndExpression.New(matchingEventCondition, notAllowedAccessRightsCondition);

            accessRightsViolationRule.SetCondition(triggerCondition,
                                                   (resultContext, resultRule) =>
            {
                Console.WriteLine("Access rights violation alert!");
            },
                                                   (resultContext, resultRule) =>
            {
                Console.WriteLine("All is well...");
            });

            RuleManager.GetInstance().AddRule(accessRightsViolationRule);
        }
Ejemplo n.º 2
0
        public void TestCondition()
        {
            Variable     x = new Variable("index");
            IBooleanBase resultExpression = EqualToExpression.New(x, new Literal("L1", "1"));
            IBooleanBase all     = AndExpression.New(resultExpression, BooleanExpression.True());
            Context      context = new Context();

            context.Assign(x.Name, "1");

            bool invertResult = (NotExpression.New(all)).Evaluate(context);
            bool result       = all.Evaluate(context);

            Assert.IsTrue(result);
        }