public AbstractExpressionCondition DeepCopyCondition()
        {
            ExpressionCondition copy = new ExpressionCondition() { Expression = this.Expression };

            foreach (AbstractStatement statement in Statements)
            {
                copy.AddStatement(statement.DeepCopyStatement());
            }

            return copy;
        }
        public void AddAction()
        {
            bool collectionChanged = false;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            condition.Statements.CollectionChanged += (o, e) => collectionChanged = true;

            Action action = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            condition.AddStatement(action);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual(1, condition.Statements.Count);
        }
        public void AddElseIfCondition()
        {
            bool collectionChanged = false;

            ConditionGroup group = new ConditionGroup();
            group.Statements.CollectionChanged += (o, e) => collectionChanged = true;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            group.AddStatement(condition);

            Assert.IsTrue(collectionChanged);
            Assert.AreEqual("test > 1", condition.Expression);
            Assert.AreEqual(1, group.Statements.Count);
        }
Example #4
0
        public void AbstractConditionGroup_RemoveStatement()
        {
            var entity = new Entity();
            var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent)));
            var group = new ConditionGroup();
            var elif = new ExpressionCondition();
            var el = new BasicCondition();
            group.AddStatement(elif);
            group.Else = el;
            evt.AddStatement(group);

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(elif, group.Statements.First()),
                () => entity.DeleteStatementCommand.Execute(elif),
                () => Assert.AreEqual(0, group.Statements.Count)
            );

            CommandHelper.TestUndoableCommand(
                () => Assert.AreEqual(el, group.Else),
                () => entity.DeleteStatementCommand.Execute(el),
                () => Assert.IsNull(group.Else)
            );
        }
        protected AbstractConditionGroup(AbstractConditionGroup sourceGroup = null)
            : base(sourceGroup)
        {
            this.sourceGroup = sourceGroup;

            AddElseIfCommand = new DelegateCommand((parameter) => IsEditable,
            (parameter) =>
            {
                if (IsEditable)
                {
                    var condition = new ExpressionCondition();
                    AddStatement(condition);

                    Workspace.Instance.CommandHistory.Log(
                        "add else if",
                        () => AddStatement(condition),
                        () => RemoveStatement(condition)
                    );
                }
            });

            AddElseCommand = new DelegateCommand((parameter) => IsEditable && null == Else,
            (parameter) =>
            {
                if (IsEditable && null == Else)
                {
                    var condition = new BasicCondition();
                    Else = condition;

                    Workspace.Instance.CommandHistory.Log(
                        "add else",
                        () => Else = condition,
                        () => Else = null
                    );
                }
            });
        }
        private void CreateStatement(ParseTreeNode statementNode, Event evt, CompositeStatement compositeStatement = null)
        {
            AbstractStatement statement = null;
            if (statementNode.Term == grammar.Action)
            {
                statement = createAction(statementNode);
            }
            else if (statementNode.Term == grammar.Condition)
            {
                ConditionGroup conditionGroup = new ConditionGroup();
                conditionGroup.If.Expression = getStrVal(statementNode.ChildNodes.First(child => child.Term == grammar.Expr));
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions)) CreateStatement(child, evt, conditionGroup.If);
                foreach (ParseTreeNode elseNode in grammar.GetOfType(statementNode, grammar.Else))
                {
                    string expr = getStrVal(elseNode.ChildNodes.FirstOrDefault(child => child.Term == grammar.Expr));
                    AbstractCondition cond;

                    if (null == expr)
                    {
                        conditionGroup.Else = new BasicCondition();
                        cond = conditionGroup.Else;
                    }
                    else
                    {
                        cond = new ExpressionCondition() { Expression = expr };
                        conditionGroup.AddStatement(cond);
                    }

                    foreach (ParseTreeNode child in grammar.GetOfType(elseNode, grammar.Actions)) CreateStatement(child, evt, cond);
                }
                statement = conditionGroup;
            }
            else if (statementNode.Term == grammar.Assignment)
            {
                Assignment assignment = new Assignment();
                assignment.Key = getStrVal(statementNode.ChildNodes[0]);
                assignment.Value = getStrVal(statementNode.ChildNodes.Last());

                assignment.Operator = statementNode.ChildNodes.Count == 2 ? AssignmentOperator.Assign :
                    AbstractAssignment.AssignmentValues[grammar.OpLookup[statementNode.ChildNodes[1].Term]];

                statement = assignment;
            }
            else
            {
                CompositeStatement loop = null;
                switch (statementNode.ChildNodes.Count)
                {
                    case 2:
                        WhileLoop wl = new WhileLoop();
                        wl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = wl;
                        break;
                    case 3:
                        ForLoop fl = new ForLoop();
                        fl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        //First assignment, expression, assignment, actions
                        fl.PostExpression = getStrVal(statementNode.ChildNodes[1]);
                        statement = loop = fl;
                        break;
                    case 4:
                        ForLoop forLoop = new ForLoop();
                        forLoop.Expression = getStrVal(statementNode.ChildNodes[1]);
                        //First assignment, expression, assignment, actions
                        forLoop.PostExpression = getStrVal(statementNode.ChildNodes[2]);
                        forLoop.PreExpression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = forLoop;
                        break;
                }
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions))
                {
                    // TODO: This is a hack to skip the Actions found in the first line of the for() statement.
                    if (!statementNode.ChildNodes.Contains(child))
                    {
                        CreateStatement(child, evt, loop);
                    }
                }
            }
            if (compositeStatement == null) evt.AddStatement(statement);
            else compositeStatement.AddStatement(statement);
        }
        public void AddReadOnlyAction()
        {
            ExpressionCondition parentCondition = new ExpressionCondition() { Expression = "test > 1" };
            ReadOnlyExpressionCondition childCondition = new ReadOnlyExpressionCondition(parentCondition);

            parentCondition.AddStatement(new Action(Workspace.Instance.GetPlugin(FireTriggerActionType)));

            Assert.AreEqual(1, childCondition.Statements.Count);
        }
        public void RemoveElseIfCondition()
        {
            int eventsFired = 0;

            ConditionGroup group = new ConditionGroup();
            group.Statements.CollectionChanged += (o, e) => eventsFired++;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            group.AddStatement(condition);
            group.RemoveStatement(condition);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, group.Statements.Count);
        }
        public void RemoveAction()
        {
            int eventsFired = 0;

            ExpressionCondition condition = new ExpressionCondition() { Expression = "test > 1" };
            condition.Statements.CollectionChanged += (o, e) => eventsFired++;

            Action action = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            condition.AddStatement(action);
            condition.RemoveStatement(action);

            Assert.AreEqual(2, eventsFired);
            Assert.AreEqual(0, condition.Statements.Count);
        }
Example #10
0
        public void ReadOnlyConditionFollowsRuleChange()
        {
            bool propertyChanged = false;

            ExpressionCondition parentCondition = new ExpressionCondition() { Expression = "test > 1" };

            ReadOnlyExpressionCondition childCondition = new ReadOnlyExpressionCondition(parentCondition);
            childCondition.PropertyChanged += (o, e) => propertyChanged = (e.PropertyName == "Expression");

            Assert.AreEqual("test > 1", childCondition.Expression);

            parentCondition.Expression = "test <= 1";

            Assert.IsTrue(propertyChanged);
            Assert.AreEqual("test <= 1", childCondition.Expression);
        }
Example #11
0
        public void CannotRemoveInheritedActionFromInheritingCondition()
        {
            ExpressionCondition parentCondition = new ExpressionCondition() { Expression = "test > 1" };
            ReadOnlyExpressionCondition childCondition = new ReadOnlyExpressionCondition(parentCondition);

            Action parentAction = new Action(Workspace.Instance.GetPlugin(FireTriggerActionType));
            parentCondition.AddStatement(parentAction);

            AbstractStatement childAction = childCondition.Statements.Single();
            childCondition.RemoveStatement(childAction);

            Assert.AreEqual(1, childCondition.Statements.Count);
        }
Example #12
0
 public ConditionGroup()
 {
     If = new ExpressionCondition();
 }