Exemplo n.º 1
0
 public virtual int addNewPartialMatch(IHashIndex index, IFact fact)
 {
     IGenericMap<object, object> matches = CollectionFactory.newMap();
     matches.Put(fact, fact);
     memory.Put(index, matches);
     return 1;
 }
Exemplo n.º 2
0
 /// <summary> 
 /// </summary>
 /// <param name="source">- the source should be either the workingMemory or Rete
 /// </param>
 /// <param name="typeCode">- event type
 /// </param>
 /// <param name="sourceNode">- the node which initiated the event
 /// 
 /// </param>
 public EngineEvent(Object source, int typeCode, BaseNode sourceNode, IFact[] facts)
 {
     InitBlock();
     this.typeCode = typeCode;
     this.sourceNode = sourceNode;
     this.facts = facts;
 }
Exemplo n.º 3
0
 /// <summary> addPartialMatch stores the fact with the factId as the
 /// key.
 /// </summary>
 public virtual int addPartialMatch(NotEqHashIndex index, IFact fact)
 {
     IGenericMap<Object, Object> matches = (IGenericMap<Object, Object>) memory.Get(index);
     int count = 0;
     if (matches == null)
     {
         count = addNewPartialMatch(index, fact);
     }
     else
     {
         IGenericMap<object, object> submatch = (IGenericMap<object, object>)matches.Get(index.SubIndex);
         if (submatch == null)
         {
             submatch = CollectionFactory.newHashMap();
             submatch.Put(fact, fact);
             matches.Put(index.SubIndex, submatch);
             count = matches.Count;
         }
         else
         {
             submatch.Put(fact, fact);
             count = submatch.Count;
         }
     }
     counter++;
     return count;
 }
Exemplo n.º 4
0
        /// <summary> Assert from the right side is always going to be from an Alpha node.
        /// 
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        /// 
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            // Get the memory for the node
            HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory) mem.getBetaRightMemory(this);
            NotEqHashIndex inx = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact));

            rightmem.addPartialMatch(inx, rfact);
            bool zm = rightmem.zeroMatch(inx);
            IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();
            while (itr.MoveNext())
            {
                Index linx = (Index) itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    if (!zm)
                    {
                        try
                        {
                            propogateRetract(linx, engine, mem);
                        }
                        catch (RetractException e)
                        {
                            throw new AssertException("NotJion - " + e.Message);
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
 public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
     long time = LeftTime;
     TemporalHashedAlphaMem rightmem = (TemporalHashedAlphaMem) mem.getBetaRightMemory(this);
     EqHashIndex inx = new EqHashIndex(NodeUtils.getRightValues(binds, rfact));
     rightmem.addPartialMatch(inx, rfact);
     // now that we've added the facts to the list, we
     // proceed with evaluating the fact
     IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
     // since there may be key collisions, we iterate over the
     // values of the HashMap. If we used keySet to iterate,
     // we could encounter a ClassCastException in the case of
     // key collision.
     IEnumerator itr = leftmem.Values.GetEnumerator();
     try
     {
         while (itr.MoveNext())
         {
             Index linx = (Index) itr.Current;
             if (evaluate(linx.Facts, rfact, time))
             {
                 // now we propogate
                 propogateAssert(linx.add(rfact), engine, mem);
             }
             else
             {
                 propogateRetract(linx.add(rfact), engine, mem);
             }
         }
     }
     catch (RetractException e)
     {
         // we shouldn't Get a retract exception. if we do, it's a bug
     }
 }
Exemplo n.º 6
0
        public void Add(IFact fact, TElement element)
        {
            var nodePair = new NodePair(fact, element);

            _facts.AddLast(nodePair.FactNode);
            _elements.AddLast(nodePair.ElementNode);
            _nodeLookup[fact] = nodePair;
        }
Exemplo n.º 7
0
 /// <summary> Retract simply propogates it down the network
 /// </summary>
 public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     IAlphaMemory alpha = (IAlphaMemory) mem.getAlphaMemory(this);
     if (alpha.removePartialMatch(fact) != null)
     {
         propogateRetract(fact, engine, mem);
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Parses an XML Rule element
        /// </summary>
        /// <param name="a_rule">XML Rule element</param>
        /// <returns>Corresponding Rule object or null if parsing is unsuccessful</returns>
        protected Rule ParseRule(XElement a_rule)
        {
            // Variables pour le parsing
            string          name;
            XElement        premisesElement;
            List <XElement> factListElements = new List <XElement>();
            XElement        factElement;
            List <IFact>    premises = new List <IFact>();
            IFact           conclusion;

            // Parsing de la balise <Rule>
            name = a_rule.Descendants("Name").FirstOrDefault().Value;
            if (name == null || name == string.Empty)
            {
                return(null);
            }

            premisesElement = a_rule.Descendants("Premises").FirstOrDefault();
            if (premisesElement == null)
            {
                return(null);
            }

            factListElements = premisesElement.Descendants("Fact").ToList();
            if (factListElements.Count == 0)
            {
                return(null);
            }

            factElement = a_rule.Descendants("Conclusion").FirstOrDefault().Descendants("Fact").FirstOrDefault();
            if (factElement == null)
            {
                return(null);
            }

            // Si on arrive ici, la balise <Rule> est conforme (parsing des balises <Fact>)
            // Parsing des balises <Fact>
            foreach (XElement elementFact in factListElements)
            {
                IFact newFact = ParseFact(elementFact);
                if (newFact == null)
                {
                    return(null);
                }
                premises.Add(newFact);
            }

            conclusion = ParseFact(factElement);
            if (conclusion == null)
            {
                return(null);
            }

            // Si on arrive ici, la balise <Rule> est conforme
            Rule newRule = new Rule(name, premises, conclusion);

            return(newRule);
        }
Exemplo n.º 9
0
 /// <summary> Get the values from the left side for nodes that do not have
 /// joins with !=
 /// </summary>
 /// <param name="">facts
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static Object[] getLeftValues(Binding[] binds, IFact[] facts)
 {
     Object[] vals = new Object[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = facts[binds[idx].LeftRow].getSlotValue(binds[idx].LeftIndex);
     }
     return vals;
 }
Exemplo n.º 10
0
        /* (non-Javadoc)
         * @see woolfel.engine.rete.BaseAlpha#assertFact(woolfel.engine.rete.Fact, woolfel.engine.Creshendo.Util.Rete.Rete, woolfel.engine.rete.WorkingMemory)
         */

        public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            if (evaluate(fact, engine))
            {
                IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
                alpha.addPartialMatch(fact);
                propogateAssert(fact, engine, mem);
            }
        }
Exemplo n.º 11
0
 /// <summary> convienance method for getting the values based on the
 /// bindings
 /// </summary>
 /// <param name="">ft
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static BindValue[] getRightBindValues(Binding[] binds, IFact ft)
 {
     BindValue[] vals = new BindValue[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = new BindValue(ft.getSlotValue(binds[idx].RightIndex), binds[idx].negated());
     }
     return vals;
 }
Exemplo n.º 12
0
 public virtual int addNewPartialMatch(NotEqHashIndex index, IFact fact)
 {
     IGenericMap<object, object> matches = CollectionFactory.newHashMap();
     IGenericMap<object, object> submatch = CollectionFactory.newHashMap();
     submatch.Put(fact, fact);
     matches.Put(index.SubIndex, submatch);
     memory.Put(index, matches);
     return 1;
 }
Exemplo n.º 13
0
 /// <summary> convienance method for getting the values based on the
 /// bindings
 /// </summary>
 /// <param name="">ft
 /// </param>
 /// <returns>
 ///
 /// </returns>
 public static BindValue[] getRightBindValues(Binding[] binds, IFact ft)
 {
     BindValue[] vals = new BindValue[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = new BindValue(ft.getSlotValue(binds[idx].RightIndex), binds[idx].negated());
     }
     return(vals);
 }
Exemplo n.º 14
0
        /// <summary> Retract simply propogates it down the network
        /// </summary>
        public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            if (alpha.removePartialMatch(fact) != null)
            {
                propogateRetract(fact, engine, mem);
            }
        }
Exemplo n.º 15
0
 /// <summary> convienance method for getting the values based on the bindings
 /// for nodes that do not have !=
 /// </summary>
 /// <param name="">ft
 /// </param>
 /// <returns>
 ///
 /// </returns>
 public static Object[] getRightValues(Binding[] binds, IFact ft)
 {
     Object[] vals = new Object[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = ft.getSlotValue(binds[idx].RightIndex);
     }
     return(vals);
 }
Exemplo n.º 16
0
 /// <summary>
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         // if watch is on, we notify the engine. Rather than
         // create an event class here, we let Rete do that.
         propogateAssert(fact, engine, mem);
     }
 }
Exemplo n.º 17
0
 /// <summary> Get the values from the left side
 /// </summary>
 /// <param name="">facts
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public static BindValue[] getLeftBindValues(Binding[] binds, IFact[] facts)
 {
     BindValue[] vals = new BindValue[binds.Length];
     for (int idx = 0; idx < binds.Length; idx++)
     {
         vals[idx] = new BindValue(facts[binds[idx].LeftRow].getSlotValue(binds[idx].LeftIndex), binds[idx].negated());
     }
     return vals;
 }
Exemplo n.º 18
0
        public void Modify(IFact fact, TElement element)
        {
            var nodePair = _nodeLookup[fact];

            if (!ReferenceEquals(nodePair.ElementNode.Value, element))
            {
                nodePair.ElementNode.Value = element;
            }
        }
Exemplo n.º 19
0
        public virtual int addNewPartialMatch(NotEqHashIndex index, IFact fact)
        {
            IGenericMap <object, object> matches  = CollectionFactory.newHashMap();
            IGenericMap <object, object> submatch = CollectionFactory.newHashMap();

            submatch.Put(fact, fact);
            matches.Put(index.SubIndex, submatch);
            memory.Put(index, matches);
            return(1);
        }
Exemplo n.º 20
0
        public override int CompareSameFact(IFact <ChessGame> that)
        {
            SimpleFact fact = that as SimpleFact;

            if (PropertyName == fact.PropertyName)
            {
                return(this.Value.CompareTo(fact.Value));
            }
            return(PropertyName.CompareTo(fact.PropertyName));
        }
 private void AddFactsSuggestionHighlighting(
     IHighlightingConsumer consumer, string message, IFact startElement, IFact endElement)
 {
     var highlighting = new HintRangeHighlighting<IFact>(startElement, endElement, message);
     IFile file = startElement.GetContainingFile();
     if (file != null)
     {
         consumer.AddHighlighting(highlighting, file);
     }
 }
Exemplo n.º 22
0
        private void RaiseForBaseHandlers(IFact fact)
        {
            using (var scope = _lifetimeScope.BeginLifetimeScope())
            {
                var handlers = (IEnumerable) scope.Resolve(typeof (IEnumerable<IHandle<IFact>>));

                foreach (var handler in handlers)
                    ((dynamic) handler).Handle((dynamic) fact);
            }
        }
Exemplo n.º 23
0
        private object[] GetKey(AggregationContext context, ITuple tuple, IFact fact)
        {
            var key = new object[_sortConditions.Length];

            for (int i = 0; i < _sortConditions.Length; i++)
            {
                key[i] = _sortConditions[i].KeySelector.Invoke(context, tuple, fact);
            }
            return(key);
        }
Exemplo n.º 24
0
        private void AddFactsSuggestionHighlighting(
            IHighlightingConsumer consumer, string message, IFact startElement, IFact endElement)
        {
            var   highlighting = new HintRangeHighlighting <IFact>(startElement, endElement, message);
            IFile file         = startElement.GetContainingFile();

            if (file != null)
            {
                consumer.AddHighlighting(highlighting, file);
            }
        }
Exemplo n.º 25
0
        /// <summary> Retract a fact from the node
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        ///
        /// </param>
        public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
        {
            IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);

            if (alpha.removePartialMatch(fact) != null)
            {
                // if watch is on, we notify the engine. Rather than
                // create an event class here, we let Rete do that.
                propogateRetract(fact, engine, mem);
            }
        }
Exemplo n.º 26
0
        public object Value(string name)
        {
            IFact f = Search(name);

            if (f == null)
            {
                return(null);
            }

            return(f.Value);
        }
Exemplo n.º 27
0
        public void RaiseFactRetracted(ISession session, IFact fact)
        {
            var handler = FactRetractedEvent;

            if (handler != null)
            {
                var @event = new WorkingMemoryEventArgs(fact);
                handler(session, @event);
            }
            _parent?.RaiseFactRetracted(session, fact);
        }
Exemplo n.º 28
0
        public override bool Implies(IFact <ChessGame> that)
        {
            if (that == null)
            {
                return(false);
            }

            OpeningFact fact = that as OpeningFact;

            return(Implies(fact));
        }
Exemplo n.º 29
0
        public void testIndex()
        {
            Defclass    dc    = new Defclass(typeof(TestBean2));
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            TestBean2   bean  = new TestBean2();

            bean.Attr1 = ("testString");
            bean.Attr2 = (1);
            short a3 = 3;

            bean.Attr3 = (a3);
            long a4 = 101;

            bean.Attr4 = (a4);
            float a5 = 10101;

            bean.Attr5 = (a5);
            double a6 = 101.101;

            bean.Attr6 = (a6);

            IFact fact = dtemp.createFact(bean, dc, 1);

            Assert.IsNotNull(fact);
            Console.WriteLine(fact.toFactString());
            CompositeIndex ci =
                new CompositeIndex("attr1", Constants.EQUAL, fact.getSlotValue(0));

            Assert.IsNotNull(ci);
            Console.WriteLine(ci.toPPString());
            GenericHashMap <object, object> map = new GenericHashMap <object, object>();

            map.Put(ci, bean);

            CompositeIndex ci2 =
                new CompositeIndex("attr1", Constants.EQUAL, fact.getSlotValue(0));

            Assert.IsTrue(map.ContainsKey(ci2));

            CompositeIndex ci3 =
                new CompositeIndex("attr1", Constants.NOTEQUAL, fact.getSlotValue(0));

            Assert.IsFalse(map.ContainsKey(ci3));

            CompositeIndex ci4 =
                new CompositeIndex("attr1", Constants.NILL, fact.getSlotValue(0));

            Assert.IsFalse(map.ContainsKey(ci4));

            CompositeIndex ci5 =
                new CompositeIndex("attr1", Constants.NOTNILL, fact.getSlotValue(0));

            Assert.IsFalse(map.ContainsKey(ci5));
        }
 public void RaiseWithinUnitOfWork(IFact fact, IUnitOfWork unitOfWork)
 {
     var handlerType = typeof(IHandleDuringUnitOfWork<>).MakeGenericType(fact.GetType());
     var handlersType = typeof(IEnumerable<>).MakeGenericType(handlerType);
     using (var scope = _lifetimeScope.BeginLifetimeScope())
     {
         var handlers = (IEnumerable)scope.Resolve(handlersType, new Parameter[] { new TypedParameter(typeof(IUnitOfWork), unitOfWork) });
         foreach (var handler in handlers)
             ((dynamic)handler).Handle((dynamic)fact);
     }
 }
Exemplo n.º 31
0
 /// <summary> the implementation will first check to see if the fact already matched.
 /// If it did, the fact stops and doesn't go any further. If it doesn't,
 /// it will attempt to evaluate it and Add the fact if it matches.
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 ///
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         IAlphaMemory alpha = (IAlphaMemory)mem.getAlphaMemory(this);
         alpha.addPartialMatch(fact);
         // if watch is on, we notify the engine. Rather than
         // create an event class here, we let Rete do that.
         propogateAssert(fact, engine, mem);
     }
 }
Exemplo n.º 32
0
        private AggregationResult RemoveElementOnly(IFact fact, TKey key, TElement element)
        {
            var group = _groups[key];

            group.Remove(fact, element);
            if (group.Count == 0)
            {
                return(AggregationResult.Removed(group));
            }
            return(AggregationResult.Modified(group, group, group.Facts));
        }
Exemplo n.º 33
0
            public With_no_projection_facts()
            {
                correctFact = new MockSimpleFact("Correct!");
                factA       = new MockSimpleFact("A");
                factB       = new MockSimpleFact("B");
                factAB      = new MockSimpleFact("AB");
                factC       = new MockSimpleFact("C");

                mockRuleGenerator = new MockRuleGenerator();
                mockFilterer      = new MockThresholdFilterer();
            }
Exemplo n.º 34
0
 /// <summary> Method will evaluate a single slot from the left against the right.
 /// </summary>
 /// <param name="">left
 /// </param>
 /// <param name="">leftId
 /// </param>
 /// <param name="">right
 /// </param>
 /// <param name="">rightId
 /// </param>
 /// <returns>
 ///
 /// </returns>
 public virtual bool evaluate(IFact left, int leftId, IFact right, int rightId, int opr)
 {
     if (opr == Constants.NOTEQUAL)
     {
         return(Evaluate.evaluateNotEqual(left.getSlotValue(leftId), right.getSlotValue(rightId)));
     }
     else
     {
         return(Evaluate.evaluateEqual(left.getSlotValue(leftId), right.getSlotValue(rightId)));
     }
 }
Exemplo n.º 35
0
        public ILabel GetLabel(IPredicate predicate, IFact fact)
        {
            var key = LabelImpl.GetKey(predicate, fact);

            if (!_labels.ContainsKey(key))
            {
                _labels.Add(key, new LabelImpl(predicate, fact));
            }

            return(_labels[key]);
        }
Exemplo n.º 36
0
        public override bool Implies(IFact <string> that)
        {
            if (that == null)
            {
                return(false);
            }

            MockSimpleFact fact = that as MockSimpleFact;

            return(Implies(fact));
        }
Exemplo n.º 37
0
        public void testAssertLeftOne()
        {
            // first create a rule engine instance
            Rete    engine = new Rete();
            NotJoin bn     = new NotJoin(engine.nextNodeId());

            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof(TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");

            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding   b1    = new Binding();

            b1.LeftIndex   = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow     = (0);
            b1.RightIndex  = (0);
            b1.VarName     = ("var1");
            binds[0]       = b1;

            // set the binding
            bn.Bindings = (binds);

            TestBean2 bean = new TestBean2();

            bean.Attr1 = ("random1");
            bean.Attr2 = (101);
            short s = 10001;

            bean.Attr3 = (s);
            long l = 10101018;

            bean.Attr4 = (l);
            bean.Attr5 = (1010101);
            bean.Attr6 = (1001.1001);
            IFact f1 = dtemp.createFact(bean, dc, engine.nextFactId());

            try
            {
                bn.assertLeft(new Index(new IFact[] { f1 }), engine, engine.WorkingMemory);
                IGenericMap <Object, Object> bmem = (IGenericMap <Object, Object>)engine.WorkingMemory.getBetaLeftMemory(bn);
                Assert.AreEqual(1, bmem.Count);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }
            engine.close();
        }
Exemplo n.º 38
0
        /// <summary> Remove a partial match from the memory
        /// </summary>
        public virtual int removePartialMatch(IHashIndex index, IFact fact)
        {
            IGenericMap <Object, Object> list = (IGenericMap <Object, Object>)memory[index];

            list.Remove(fact);
            if (list.Count == 0)
            {
                memory.Remove(index);
            }
            counter--;
            return(list.Count);
        }
Exemplo n.º 39
0
        public void AddFact(TKey key, IFact fact)
        {
            if (!_items.TryGetValue(key, out var list))
            {
                list = new LinkedList <IFact>();
                _items.Add(key, list);
            }

            var linkedListNode = list.AddLast(fact);

            _dataMap[fact] = new SortedFactData(key, linkedListNode);
        }
Exemplo n.º 40
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            IList <object> facts = engine.AllFacts;

            Object[] sorted = FactUtils.sortFacts(facts);
            for (int idx = 0; idx < sorted.Length; idx++)
            {
                IFact ft = (IFact)sorted[idx];
                engine.writeMessage(ft.toFactString() + Constants.LINEBREAK);
            }
            engine.writeMessage("for a total of " + sorted.Length + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
            return(new DefaultReturnVector());
        }
Exemplo n.º 41
0
        public virtual bool isPartialMatch(IHashIndex index, IFact fact)
        {
            IGenericMap <Object, Object> list = (IGenericMap <Object, Object>)memory[index];

            if (list != null)
            {
                return(list.ContainsKey(fact));
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 42
0
        /// <summary> find the matching fact in the array
        /// </summary>
        /// <param name="">temp
        /// </param>
        /// <param name="">facts
        /// </param>
        /// <returns>
        ///
        /// </returns>
        public static IFact findFact(Deftemplate temp, IFact[] facts)
        {
            IFact ft = null;

            for (int idx = 0; idx < facts.Length; idx++)
            {
                if (facts[idx].Deftemplate == temp)
                {
                    ft = facts[idx];
                }
            }
            return(ft);
        }
Exemplo n.º 43
0
 public object Invoke(ITuple tuple, IFact fact)
 {
     try
     {
         var factValue = fact.Value;
         var result    = _compiledExpression.Delegate(factValue);
         return(result);
     }
     catch (Exception e)
     {
         throw new RuleExpressionEvaluationException("Failed to evaluate expression", _expression.ToString(), e);
     }
 }
Exemplo n.º 44
0
        public void Raise(IFact fact)
        {
            RaiseForBaseHandlers(fact);

            var handlerType = typeof (IHandle<>).MakeGenericType(fact.GetType());
            var handlersType = typeof (IEnumerable<>).MakeGenericType(handlerType);
            using (var scope = _lifetimeScope.BeginLifetimeScope())
            {
                var handlers = (IEnumerable) scope.Resolve(handlersType);

                foreach (var handler in handlers)
                    ((dynamic) handler).Handle((dynamic) fact);
            }
        }
Exemplo n.º 45
0
 /// <summary> addPartialMatch stores the fact with the factId as the
 /// key.
 /// </summary>
 public virtual int addPartialMatch(IHashIndex index, IFact fact)
 {
     IGenericMap<Object, Object> matches = (IGenericMap<Object, Object>) memory.Get(index);
     int count = 0;
     if (matches == null)
     {
         count = addNewPartialMatch(index, fact);
     }
     else
     {
         matches.Put(fact, fact);
         count = matches.Count;
     }
     counter++;
     return count;
 }
Exemplo n.º 46
0
        /// <summary> Assert from the right side is always going to be from an
        /// Alpha node.
        /// </summary>
        /// <param name="">factInstance
        /// </param>
        /// <param name="">engine
        /// 
        /// </param>
        public override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem)
        {
            HashedNeqAlphaMemory rightmem = (HashedNeqAlphaMemory) mem.getBetaRightMemory(this);
            NotEqHashIndex inx = new NotEqHashIndex(NodeUtils.getRightBindValues(binds, rfact));

            rightmem.addPartialMatch(inx, rfact);
            IGenericMap<Object, Object> leftmem = (IGenericMap<Object, Object>) mem.getBetaLeftMemory(this);
            IEnumerator itr = leftmem.Values.GetEnumerator();
            int after = rightmem.count(inx);
            while (itr.MoveNext())
            {
                Index linx = (Index) itr.Current;
                if (evaluate(linx.Facts, rfact))
                {
                    if (after == 1)
                    {
                        propogateAssert(linx, engine, mem);
                    }
                }
            }
        }
Exemplo n.º 47
0
 /// <summary>
 /// Asserts the event.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="facts">The facts.</param>
 public virtual void assertEvent(BaseNode node, IFact[] facts)
 {
     if (debug)
     {
         if (node is TerminalNode)
         {
             Trace.WriteLine(((TerminalNode) node).Rule.Name + " fired");
         }
         else
         {
         }
     }
     IEnumerator itr = listeners.GetEnumerator();
     while (itr.MoveNext())
     {
         EngineEventListener eel = (EngineEventListener) itr.Current;
         eel.eventOccurred(new EngineEvent(this, EngineEvent.ASSERT_EVENT, node, facts));
     }
 }
Exemplo n.º 48
0
 /// <summary> Retract the fact to the succeeding nodes. ObjectTypeNode does not call
 /// assertEvent, since it's not that important and doesn't really
 /// help debugging.
 /// </summary>
 /// <param name="">fact
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void retractFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (fact.Deftemplate == deftemplate)
     {
         ((IAlphaMemory) mem.getAlphaMemory(this)).removePartialMatch(fact);
         for (int idx = 0; idx < successorNodes.Length; idx++)
         {
             Object node = successorNodes[idx];
             if (node is BaseAlpha)
             {
                 ((BaseAlpha) node).retractFact(fact, engine, mem);
             }
             else if (node is BaseJoin)
             {
                 ((BaseJoin) node).retractRight(fact, engine, mem);
             }
         }
         IEnumerator itr2 = successor2.GetEnumerator();
         while (itr2.MoveNext())
         {
             BaseNode node = (BaseNode) itr2.Current;
             if (node is BaseAlpha)
             {
                 ((BaseAlpha) node).retractFact(fact, engine, mem);
             }
             else if (node is BaseJoin)
             {
                 ((BaseJoin) node).retractRight(fact, engine, mem);
             }
             else if (node is TerminalNode)
             {
                 Index inx = new Index(new IFact[] {fact});
                 ((TerminalNode) node).retractFacts(inx, engine, mem);
             }
         }
     }
 }
Exemplo n.º 49
0
 public virtual void assertSecondSuccessors(IFact fact, Rete engine, IWorkingMemory mem)
 {
     IEnumerator itr = successor2.GetEnumerator();
     while (itr.MoveNext())
     {
         BaseNode node = (BaseNode) itr.Current;
         if (node is BaseAlpha)
         {
             ((BaseAlpha) node).assertFact(fact, engine, mem);
         }
         else if (node is BaseJoin)
         {
             ((BaseJoin) node).assertRight(fact, engine, mem);
         }
         else if (node is TerminalNode)
         {
             Index inx = new Index(new IFact[] {fact});
             ((TerminalNode) node).assertFacts(inx, engine, mem);
         }
     }
 }
Exemplo n.º 50
0
 /// <summary> Propogate the fact using the normal way of iterating over the
 /// successors and calling assert on AlphaNodes and assertRight on
 /// BetaNodes.
 /// </summary>
 /// <param name="">fact
 /// </param>
 /// <param name="">engine
 /// </param>
 /// <param name="">mem
 /// @throws AssertException
 /// 
 /// </param>
 public virtual void assertAllSuccessors(IFact fact, Rete engine, IWorkingMemory mem)
 {
     for (int idx = 0; idx < successorNodes.Length; idx++)
     {
         Object node = successorNodes[idx];
         if (node is BaseAlpha)
         {
             ((BaseAlpha) node).assertFact(fact, engine, mem);
         }
         else if (node is BaseJoin)
         {
             ((BaseJoin) node).assertRight(fact, engine, mem);
         }
         else if (node is TerminalNode)
         {
             Index inx = new Index(new IFact[] {fact});
             ((TerminalNode) node).assertFacts(inx, engine, mem);
         }
     }
     assertSecondSuccessors(fact, engine, mem);
 }
Exemplo n.º 51
0
        /// <summary> assert using HashMap approach
        /// 
        /// </summary>
        /// <param name="">fact
        /// </param>
        /// <param name="">engine
        /// </param>
        /// <param name="">mem
        /// 
        /// </param>
        public virtual void assertFactWithMap(IFact fact, Rete engine, IWorkingMemory mem)
        {
            Slot[] slots = fact.Deftemplate.AllSlots;
            // iterate over the slots
            for (int idx = 0; idx < slots.Length; idx++)
            {
                // only if the slot's node count is greater than zero 
                // do we go ahead and lookup in the HashMap
                if (slots[idx].NodeCount > 0)
                {
                    // iterate over the operators
                    for (int ops = 0; ops < operators.Length; ops++)
                    {
                        CompositeIndex comIndex = new CompositeIndex(slots[idx].Name, operators[ops], fact.getSlotValue(idx));

                        Object node = entries.Get(comIndex);
                        if (node != null)
                        {
                            if (node is BaseAlpha)
                            {
                                ((BaseAlpha) node).assertFact(fact, engine, mem);
                            }
                            else if (node is BaseJoin)
                            {
                                ((BaseJoin) node).assertRight(fact, engine, mem);
                            }
                            else if (node is TerminalNode)
                            {
                                Index inx = new Index(new IFact[] {fact});
                                ((TerminalNode) node).assertFacts(inx, engine, mem);
                            }
                        }
                    }
                }
            }
            assertSecondSuccessors(fact, engine, mem);
        }
Exemplo n.º 52
0
 /// <summary> assert the fact and propogate. ObjectTypeNode does not call
 /// assertEvent, since it's not that important and doesn't really
 /// help debugging.
 /// </summary>
 /// <param name="">fact
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     // ObjectTypeNode doesn't bother checking the deftemplate.
     ((IAlphaMemory) mem.getAlphaMemory(this)).addPartialMatch(fact);
     // if the number of succesor nodes is less than (slot count * opCount)
     if (gauranteeUnique && fact.Deftemplate.AllSlots.Length > 0 && successorNodes.Length > (fact.Deftemplate.AllSlots.Length*operators.Length))
     {
         assertFactWithMap(fact, engine, mem);
     }
     else
     {
         assertAllSuccessors(fact, engine, mem);
     }
 }
Exemplo n.º 53
0
 /// <summary> evaluate will first compare the timestamp of the last fact in the fact
 /// array of the left and make sure the fact is still fresh. if it is not
 /// fresh, the method returns false.
 /// </summary>
 /// <param name="">leftlist
 /// </param>
 /// <param name="">right
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public virtual bool evaluate(IFact[] leftlist, IFact right, long time)
 {
     bool eval = true;
     // first we compare the timestamp of the last fact in the
     // fact array. the last fact should be the fact with a
     // relative time window
     if (leftlist[leftlist.Length - 1].timeStamp() > time)
     {
         // we iterate over the binds and evaluate the facts
         for (int idx = 0; idx < binds.Length; idx++)
         {
             // we got the binding
             Binding bnd = binds[idx];
             eval = bnd.evaluate(leftlist, right);
             if (!eval)
             {
                 break;
             }
         }
         return eval;
     }
     else
     {
         return false;
     }
 }
Exemplo n.º 54
0
 /// <summary> Assert from the right side is always going to be from an Alpha node.
 /// 
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public abstract override void assertRight(IFact rfact, Rete engine, IWorkingMemory mem);
Exemplo n.º 55
0
 /// <summary> evaluate will extra the values from each side and evaluate it
 /// </summary>
 /// <param name="">left
 /// </param>
 /// <param name="">right
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public virtual bool evaluate(IFact[] left, IFact right)
 {
     if (left[leftrow] == right)
     {
         return false;
     }
     if (negated_Renamed_Field)
     {
         return Evaluate.evaluateNotEqual(left[leftrow].getSlotValue(leftIndex), right.getSlotValue(rightIndex));
     }
     else
     {
         return Evaluate.evaluateEqual(left[leftrow].getSlotValue(leftIndex), right.getSlotValue(rightIndex));
     }
 }
Exemplo n.º 56
0
 /// <summary> the implementation will first check to see if the fact already matched.
 /// If it did, the fact stops and doesn't go any further. If it doesn't,
 /// it will attempt to evaluate it and Add the fact if it matches.
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// 
 /// </param>
 public override void assertFact(IFact fact, Rete engine, IWorkingMemory mem)
 {
     if (evaluate(fact))
     {
         IAlphaMemory alpha = (IAlphaMemory) mem.getAlphaMemory(this);
         // we set the time of the last match
         alpha.addPartialMatch(fact);
         // if watch is on, we notify the engine. Rather than
         // create an event class here, we let Rete do that.
         propogateAssert(fact, engine, mem);
     }
 }
Exemplo n.º 57
0
 /// <summary> evaluate the node's value against the slot's value. The method
 /// uses Evaluate class to perform the evaluation
 /// </summary>
 /// <param name="">factInstance
 /// </param>
 /// <param name="">engine
 /// </param>
 /// <returns>
 /// 
 /// </returns>
 public virtual bool evaluate(IFact factInstance)
 {
     bool not = slot.NotEqualList.Contains(factInstance.getSlotValue(slot.Id));
     bool eq = slot.EqualList.Contains(factInstance.getSlotValue(slot.Id));
     if (!not && eq)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Exemplo n.º 58
0
 /// <summary>
 /// Method will process the retractEvent, preferably with an event queue
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="fact">The fact.</param>
 public virtual void assertEvent(BaseNode node, IFact fact)
 {
     if (debug)
     {
         Trace.WriteLine("\"assert at nodeid=" + node.nodeID + " - " + node.ToString().Replace("\"", "'") + ":: with fact -" + fact.toFactString().Replace("\"", "'") + "::\"");
     }
     IEnumerator itr = listeners.GetEnumerator();
     while (itr.MoveNext())
     {
         EngineEventListener eel = (EngineEventListener) itr.Current;
         eel.eventOccurred(new EngineEvent(this, EngineEvent.ASSERT_EVENT, node, new IFact[] {fact}));
     }
 }
Exemplo n.º 59
0
 /// <summary> retract right is a dummy, so it does nothing.
 /// </summary>
 public override void retractRight(IFact rfact, Rete engine, IWorkingMemory mem)
 {
 }
Exemplo n.º 60
0
 /// <summary>
 /// Retracts the event.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="facts">The facts.</param>
 public virtual void retractEvent(BaseNode node, IFact[] facts)
 {
     IEnumerator itr = listeners.GetEnumerator();
     while (itr.MoveNext())
     {
         EngineEventListener eel = (EngineEventListener) itr.Current;
         eel.eventOccurred(new EngineEvent(this, EngineEvent.ASSERT_EVENT, node, facts));
     }
 }