示例#1
0
        public bool Select <TSelect>(Action <MemoryNode <TSelect> > callback)
            where TSelect : class
        {
            if (typeof(TSelect) == typeof(T) && _node == null)
            {
                _left.Match <T, TDiscard>(n => _node = n);
            }

            var node = _node as MemoryNode <TSelect>;

            if (node != null)
            {
                callback(node);
                return(true);
            }

            if (_parent == null)
            {
                if (!typeof(T).IsGenericType || typeof(T).GetGenericTypeDefinition() != typeof(Token <,>))
                {
                    return(false);
                }

                Type[] arguments = typeof(T).GetGenericArguments();

                _parent = (RuleNodeSelector)Activator.CreateInstance(
                    typeof(DiscardRuleNodeSelector <,>).MakeGenericType(arguments),
                    _configurator, this);
            }

            return(_parent.Select(callback));
        }
        public void Find(Action <LeftJoinNode <T, TDiscard> > callback)
        {
            if (_node == null)
            {
                LeftJoinNode <T, TDiscard> joinNode = _left.Successors
                                                      .OfType <LeftJoinNode <T, TDiscard> >()
                                                      .Where(node => _matchRight(node.RightActivation))
                                                      .FirstOrDefault();

                if (joinNode != null)
                {
                    _node = joinNode;
                }
                else
                {
                    RightActivation <T> rightActivation = _rightActivation();
                    _node = _configurator.Left <T, TDiscard>(rightActivation);
                    _left.AddActivation(_node);
                }
            }

            if (_node != null)
            {
                callback(_node);
            }
        }
示例#3
0
        public void Match <TT, TTDiscard>(Action <LeftJoinNode <TT, TTDiscard> > callback)
            where TT : class
        {
            var self = this.CastAs <DiscardRuleNodeSelector <Token <TT, TTDiscard>, TDiscard> >();

            if (_node == null)
            {
                _left.Match <T, TDiscard>(leftJoin => { _node = leftJoin; });
            }

            _configurator.MatchLeftJoinNode(self._node, callback);
        }
        public override bool Visit <T, TDiscard>(LeftJoinNode <T, TDiscard> node, Func <RuntimeVisitor, bool> next)
        {
            _current = _vertices.Get(node.Id,
                                     id => new Vertex(typeof(LeftJoinNode <,>), typeof(T), typeof(T).Tokens()));

            if (_rightActivation == node.Id)
            {
                _edges.Add(new Edge(_current, _stack.Peek(), _current.TargetType.Name));
            }
            else if (_stack.Count > 0)
            {
                _edges.Add(new Edge(_stack.Peek(), _current, _current.TargetType.Name));
            }

            return(Next(node.RightActivation.Id, () => base.Visit(node, next)));
        }
        public void CanLeftJoinTwoTablesWithNoMatchingKeysInRightTable()
        {
            //Arrange
            HashTable <string> testTableOne = new HashTable <string>(20);

            testTableOne.Add("fond", "enamored");
            testTableOne.Add("wrath", "anger");
            testTableOne.Add("diligent", "employed");
            testTableOne.Add("outfit", "garb");
            testTableOne.Add("guide", "usher");

            HashTable <string> testTableTwo = new HashTable <string>(20);

            testTableTwo.Add("frond", "averse");
            testTableTwo.Add("wraith", "delight");
            testTableTwo.Add("Dillinger", "idle");
            testTableTwo.Add("guess", "follow");
            testTableTwo.Add("float", "jam");

            string[][] expectedArray = new string[][]
            {
                new string[] { "fond", "enamored", "" },
                new string[] { "wrath", "anger", "" },
                new string[] { "diligent", "employed", "" },
                new string[] { "outfit", "garb", "" },
                new string[] { "guide", "usher", "" }
            };

            //Act
            HashTable <LeftJoinNode> resultTable = LeftJoin.LeftJoin.LeftJoinHashTables(testTableOne, testTableTwo);

            //Assert
            Assert.NotNull(resultTable);
            bool expectedResultsMatch = true;

            foreach (string[] stringArray in expectedArray)
            {
                LeftJoinNode leftJoinNode = resultTable.Get(stringArray[0]);
                if (leftJoinNode.LeftValue != stringArray[1] || leftJoinNode.RightValue != stringArray[2])
                {
                    expectedResultsMatch = false;
                    break;
                }
            }
            Assert.True(expectedResultsMatch);
        }
示例#6
0
        public void Setup()
        {
            _called = null;

            var configurator = new RuntimeConfiguratorImpl();

            var engine = configurator.RulesEngine;

            PropertyNode <A, decimal> propertyNode = configurator.Property <A, decimal>(x => x.Amount);

            ConditionNode <Token <A, decimal> > conditionNode = configurator.Condition <A, decimal>(x => x > 10000.0m);

            propertyNode.AddActivation(conditionNode);

            AlphaNode <Token <A, decimal> > edgeAlpha = configurator.Alpha <A, decimal>();

            conditionNode.AddActivation(edgeAlpha);

            AlphaNode <A> alphaNode = configurator.GetAlphaNode <A>();

            alphaNode.AddActivation(propertyNode);

            JoinNode <A> joinNode = configurator.Join(alphaNode);

            DelegateProductionNode <A> productionNode = configurator.Delegate <A>((session, x) => _called = x);

            joinNode.AddActivation(productionNode);

            LeftJoinNode <A, decimal> leftNode = configurator.Left <A, decimal>(alphaNode);

            leftNode.AddActivation(joinNode);

            edgeAlpha.AddActivation(leftNode);

            using (StatefulSession session = engine.CreateSession())
            {
                session.Add(new A(10001.0m));
                session.Run();
            }
        }
        /// <summary>
        /// Left joins two HashTable<string> objects.
        /// </summary>
        /// <param name="leftTable">
        /// HashTable<string>: the HashTable<string> to be used as the left table in the left join
        /// </param>
        /// <param name="rightTable">
        /// HashTable<string>: the HashTable<string> to be used as the right table in the left join
        /// </param>
        /// <returns>
        /// HashTable<string>: a HashTable<LeftJoinNode>, containing the keys and values from leftTable and rightTable, left joined together
        /// </returns>
        public static HashTable <LeftJoinNode> LeftJoinHashTables(HashTable <string> leftTable, HashTable <string> rightTable)
        {
            HashTable <LeftJoinNode> leftJoin = new HashTable <LeftJoinNode>();

            for (int i = 0; i < leftTable.HashMap.Length; i++)
            {
                if (leftTable.HashMap[i] != null)
                {
                    var currNode = leftTable.HashMap[i].First;
                    while (currNode != null)
                    {
                        LeftJoinNode newLeftJoinNode = new LeftJoinNode(currNode.Value.Value);
                        if (rightTable.Contains(currNode.Value.Key))
                        {
                            newLeftJoinNode.RightValue = rightTable.Get(currNode.Value.Key);
                        }
                        leftJoin.Add(currNode.Value.Key, newLeftJoinNode);
                        currNode = currNode.Next;
                    }
                }
            }
            return(leftJoin);
        }
        public override bool Visit <T, TOutput>(LeftJoinNode <T, TOutput> node, Func <RuntimeVisitor, bool> next)
        {
            Append("LeftJoinNode[{0}] => {1}", Tokens <T>(), Tokens <TOutput>());

            return(Indent(next));
        }
 public virtual bool Visit <T, TDiscard>(LeftJoinNode <T, TDiscard> node, Func <RuntimeVisitor, bool> next)
     where T : class
 {
     return(next(this));
 }