public void RetractLinked(Activation activation, object key, object fact) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (fact == null) { throw new ArgumentNullException(nameof(fact)); } var factWrapper = _workingMemory.GetFact(fact); if (factWrapper == null) { throw new ArgumentException($"Linked fact does not exist. Key={key}", nameof(fact)); } _network.PropagateRetract(_executionContext, new List <Fact> { factWrapper }); _workingMemory.RemoveLinkedFact(activation, key, factWrapper); }
public void Fire() { while (_agenda.HasActiveRules()) { Activation activation = _agenda.NextActivation(); ICompiledRule rule = activation.Rule; var actionContext = new ActionContext(rule.Definition, this); _eventAggregator.RaiseRuleFiring(this, activation); foreach (IRuleAction action in rule.Actions) { action.Invoke(_executionContext, actionContext, activation.Tuple, activation.TupleFactMap, rule.Dependencies); } _eventAggregator.RaiseRuleFired(this, activation); if (actionContext.IsHalted) { break; } } }
public int Fire(int maxRulesNumber) { int ruleFiredCount = 0; while (!_agenda.IsEmpty && ruleFiredCount < maxRulesNumber) { Activation activation = _agenda.Pop(); IActionContext actionContext = new ActionContext(this, activation); _actionExecutor.Execute(_executionContext, actionContext); ruleFiredCount++; UnlinkFacts(); if (actionContext.IsHalted) { break; } } return(ruleFiredCount); }
private bool AcceptActivation(Activation activation) { foreach (var filter in _globalFilters) { if (!filter.Accept(activation)) { return(false); } } if (!_ruleFilters.TryGetValue(activation.Rule, out var ruleFilters)) { return(true); } foreach (var filter in ruleFilters) { if (!filter.Accept(activation)) { return(false); } } return(true); }
public void InsertLinked(Activation activation, object key, object fact) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (fact == null) { throw new ArgumentNullException(nameof(fact)); } var factWrapper = _workingMemory.GetLinkedFact(activation, key); if (factWrapper != null) { throw new ArgumentException($"Linked fact already exists. Key={key}", nameof(fact)); } factWrapper = new Fact(fact); _workingMemory.AddLinkedFact(activation, key, factWrapper); _network.PropagateAssert(_executionContext, new List <Fact> { factWrapper }); }
public void UpdateLinked(Activation activation, IEnumerable <KeyValuePair <object, object> > keyedFacts) { var toUpdate = new List <Tuple <object, Fact, object> >(); var toPropagate = new List <Fact>(); foreach (var keyedFact in keyedFacts) { var key = keyedFact.Key; var factWrapper = _workingMemory.GetLinkedFact(activation, key); if (factWrapper == null) { throw new ArgumentException($"Linked fact does not exist. Key={key}"); } factWrapper.Source = new LinkedFactSource(activation); toUpdate.Add(System.Tuple.Create(key, factWrapper, keyedFact.Value)); toPropagate.Add(factWrapper); } foreach (var item in toUpdate) { _workingMemory.UpdateLinkedFact(activation, item.Item1, item.Item2, item.Item3); } _network.PropagateUpdate(_executionContext, toPropagate); }
public void Execute(IExecutionContext executionContext, IActionContext actionContext) { ISession session = executionContext.Session; Activation activation = actionContext.Activation; var invocations = CreateInvocations(executionContext, actionContext); executionContext.EventAggregator.RaiseRuleFiring(session, activation); var interceptor = session.ActionInterceptor; if (interceptor != null) { interceptor.Intercept(actionContext, invocations); } else { foreach (var invocation in invocations) { invocation.Invoke(); } } executionContext.EventAggregator.RaiseRuleFired(session, activation); }
public void Execute(IExecutionContext executionContext, IActionContext actionContext) { ISession session = executionContext.Session; Activation activation = actionContext.Activation; var invocations = CreateInvocations(executionContext, actionContext); executionContext.EventAggregator.RaiseRuleFiring(session, activation); var interceptor = session.ActionInterceptor; if (interceptor != null) { interceptor.Intercept(actionContext, invocations); } else { foreach (var invocation in invocations) { try { invocation.Invoke(); } catch (Exception e) { bool isHandled = false; var expression = invocation.RuleAction.Expression; executionContext.EventAggregator.RaiseActionFailed(executionContext.Session, e, expression, actionContext.Activation, ref isHandled); if (!isHandled) { throw new RuleActionEvaluationException("Failed to evaluate rule action", actionContext.Rule.Name, expression.ToString(), e); } } } } executionContext.EventAggregator.RaiseRuleFired(session, activation); }
public void InsertLinked(Activation activation, IEnumerable <KeyValuePair <object, object> > keyedFacts) { var toAdd = new List <Tuple <object, Fact> >(); var toPropagate = new List <Fact>(); foreach (var keyedFact in keyedFacts) { var key = keyedFact.Key; var factWrapper = _workingMemory.GetLinkedFact(activation, key); if (factWrapper != null) { throw new ArgumentException($"Linked fact already exists. Key={key}"); } factWrapper = new SyntheticFact(keyedFact.Value); factWrapper.Source = new LinkedFactSource(activation); toAdd.Add(System.Tuple.Create(key, factWrapper)); toPropagate.Add(factWrapper); } foreach (var item in toAdd) { _workingMemory.AddLinkedFact(activation, item.Item1, item.Item2); } _network.PropagateAssert(_executionContext, toPropagate); }
public object GetLinked(Activation activation, object key) { var factWrapper = _workingMemory.GetLinkedFact(activation, key); return(factWrapper?.Object); }
public IEnumerable <object> GetLinkedKeys(Activation activation) { var keys = _workingMemory.GetLinkedKeys(activation); return(keys); }
public ActionContext(ISession session, Activation activation) { _session = session; _activation = activation; _isHalted = false; }
public LinkedFactSource(Activation activation) { _activation = activation; }
public void Remove(Activation activation) { activation.IsEnqueued = false; }
public void Activate(Activation activation) { _activationQueue.Enqueue(activation.Rule.Priority, activation); }
public IMatch Peek() { Activation activation = _activationQueue.Peek(); return(activation); }
public void Remove(IExecutionContext context, Activation activation) { _activationQueue.Remove(activation); UnlinkFacts(context.Session, activation); }
public void Deactivate(Activation activation) { _activationQueue.Remove(activation); }
public ActivationEventArgs(Activation activation) { Activation = activation; }
public ActionContext(ISessionInternal session, Activation activation) { _session = session; Activation = activation; IsHalted = false; }
public Activation NextActivation() { Activation activation = _activationQueue.Dequeue(); return(activation); }
public void Remove(Activation activation) { _activations.Remove(activation); _refractions.Remove(activation); }
public Activation Peek() { Activation activation = _queue.Peek(); return(activation); }
public Activation Pop() { Activation activation = _activationQueue.Dequeue(); return(activation); }
public void Enqueue(int priority, Activation activation) { _activations.Add(activation); _queue.Enqueue(priority, activation); }