public void PopulateStateTree(GameWorld state, Guid playerId, EvalNode node, int recurseCount)
        {
            if (recurseCount <= 0)
            {
                return;
            }

            var availableActions = EnumUtil.GetList <PlayerAction>();

            foreach (var action in availableActions)
            {
                var pState = state.Copy();
                var player = pState.GetPlayer(playerId);

                player.SetAction(action);

                pState.Update();

                var data = new EvalData
                {
                    Action = action,
                    Value  = EvalGameState(pState)
                };

                var newNode = new EvalNode(data);
                node.AddChild(newNode);
                PopulateStateTree(pState, playerId, newNode, recurseCount - 1);
            }
        }
Beispiel #2
0
 public EvalMatchUntilNode(PatternAgentInstanceContext context, EvalMatchUntilFactoryNode factoryNode, EvalNode childNodeSub, EvalNode childNodeUntil)
     : base(context)
 {
     FactoryNode    = factoryNode;
     ChildNodeSub   = childNodeSub;
     ChildNodeUntil = childNodeUntil;
 }
 public override EvalNode MakeEvalNode(
     PatternAgentInstanceContext agentInstanceContext,
     EvalNode parentNode)
 {
     var child = EvalNodeUtil.MakeEvalNodeSingleChild(ChildNode, agentInstanceContext, parentNode);
     return new EvalEveryDistinctNode(this, child, agentInstanceContext);
 }
        public EvalNode GetStateTree(GameWorld state, Guid playerId)
        {
            var rootNode = new EvalNode();

            PopulateStateTree(state, playerId, rootNode, _recurseCount);
            return(rootNode);
        }
Beispiel #5
0
 public override EvalNode MakeEvalNode(
     PatternAgentInstanceContext agentInstanceContext,
     EvalNode parentNode)
 {
     EvalNode[] nodes = EvalNodeUtil.MakeEvalNodeChildren(children, agentInstanceContext, parentNode);
     return new EvalMatchUntilNode(agentInstanceContext, this, nodes[0], nodes.Length == 1 ? null : nodes[1]);
 }
Beispiel #6
0
 public static EvalNode MakeEvalNodeSingleChild(
     EvalFactoryNode child,
     PatternAgentInstanceContext agentInstanceContext,
     EvalNode parentNode)
 {
     return(child.MakeEvalNode(agentInstanceContext, parentNode));
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeShiftOp node.
 /// </summary>
 /// <param name="operation">Specifies the shift operation to represent.</param>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeShiftOp(ShiftOp operation,
                        EvalNode child,
                        Language language)
 {
     _operation = operation;
     _language = language;
     Append(child);
 }
Beispiel #8
0
 /// <summary>
 ///     Constructor.
 /// </summary>
 /// <param name="rootNode">root node</param>
 /// <param name="rootSingleChildNode">is the root nodes single child node</param>
 public EvalRootStateNode(
     EvalRootNode rootNode,
     EvalNode rootSingleChildNode)
     : base(null)
 {
     this.rootNode = rootNode;
     this.rootSingleChildNode = rootSingleChildNode;
 }
        public override void Start(MatchedEventMap beginState)
        {
            EvalNode      child      = EvalFollowedByNode.ChildNodes[0];
            EvalStateNode childState = child.NewState(this, null, 0L);

            Nodes.Put(childState, 0);
            childState.Start(beginState);
        }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeUnaryOp node.
 /// </summary>
 /// <param name="operation">Specifies the unary operation to represent.</param>
 /// <param name="child">Specifies the child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeUnaryOp(UnaryOp operation,
                        EvalNode child,
                        Language language)
 {
     _operation = operation;
     _language = language;
     Append(child);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeCondLogicOp node.
 /// </summary>
 /// <param name="operation">Specifies the binary compare to represent.</param>
 /// <param name="child1">Specifies the first child node.</param>
 /// <param name="child2">Specifies the second child node.</param>
 public EvalNodeCondLogicOp(CompareOp operation,
                            EvalNode child1,
                            EvalNode child2)
 {
     _operation = operation;
     Append(child1);
     Append(child2);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeCondLogicOp node.
 /// </summary>
 /// <param name="operation">Specifies the binary compare to represent.</param>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeCondLogicOp(CompareOp operation,
                            EvalNode child,
                            Language language)
 {
     _language = language;
     _operation = operation;
     Append(child);
 }
Beispiel #13
0
        private List <DateTime> EvalNodes(EvalNode currentNode)
        {
            if (currentNode.Occurrences == null)
            {
                currentNode.Occurrences = new List <DateTime>();
            }
            var occurs = currentNode.Occurrences;

            if (currentNode.Child != null)
            {
                occurs = EvalNodes(currentNode.Child);
            }
            if (currentNode.Sibling != null && currentNode.Operator != null)
            {
                if (currentNode.Operator == ":" || currentNode.Operator == "|")
                {
                    occurs.AddRange(EvalNodes(currentNode.Sibling));
                }
                else if (currentNode.Operator == "%" || currentNode.Operator == "!")
                {
                    var matchList = EvalNodes(currentNode.Sibling);
                    occurs = occurs.Except(matchList).ToList();
                }
                else if (currentNode.Operator == ";" || currentNode.Operator == "&")
                {
                    var matchList = EvalNodes(currentNode.Sibling);
                    //occurs.RemoveAll(dt => !matchList.Contains(dt));
                    occurs = occurs.Intersect(matchList).ToList();
                }
                else if (currentNode.Operator == ">")
                {
                    var matchList = EvalNodes(currentNode.Sibling);
                    if (matchList.Count > 0 && occurs.Count > 0)
                    {
                        var start = occurs.Last();
                        var end   = matchList.First();
                        var inc   = start.AddMinutes(1).AddSeconds(-start.Second).AddMilliseconds(-start.Millisecond);
                        while (end.ToUniversalTime().ToString(FORMAT_DATETIME) !=
                               inc.ToUniversalTime().ToString(FORMAT_DATETIME)
                               ) //(Math.Floor((end - inc).TotalMinutes) != 0)
                        {
                            occurs.Add(inc);
                            if (inc.Hour == 23 && inc.Minute == 59)
                            {
                                inc = inc.AddHours(-23);
                                inc = inc.AddMinutes(-59);
                            }
                            else
                            {
                                inc = inc.AddMinutes(1);
                            }
                        }
                        occurs.AddRange(matchList);
                    }
                }
            }
            return(occurs);
        }
Beispiel #14
0
 public EvalEveryDistinctNode(
     EvalEveryDistinctFactoryNode factoryNode,
     EvalNode childNode,
     PatternAgentInstanceContext agentInstanceContext)
     : base(agentInstanceContext)
 {
     this.factoryNode = factoryNode;
     ChildNode = childNode;
 }
Beispiel #15
0
        protected void WhenBuildingTree(string input = null)
        {
            if (input != null)
            {
                WhenParsingTokens(input);
            }

            evalTree = new EvalNodeBuilder(postfix).Root;
        }
Beispiel #16
0
 public EvalEveryNode(
     PatternAgentInstanceContext context,
     EvalEveryFactoryNode factoryNode,
     EvalNode childNode)
     : base(context)
 {
     this.factoryNode = factoryNode;
     ChildNode = childNode;
 }
Beispiel #17
0
 public static EvalNode[] MakeEvalNodeChildren(
     IEnumerable <EvalFactoryNode> factories,
     PatternAgentInstanceContext agentInstanceContext,
     EvalNode parentNode)
 {
     return(factories
            .Select(f => f.MakeEvalNode(agentInstanceContext, parentNode))
            .ToArray());
 }
Beispiel #18
0
 public EvalRootNode(
     PatternAgentInstanceContext context,
     EvalRootFactoryNode factoryNode,
     EvalNode childNode)
 {
     this.factoryNode = factoryNode;
     this.childNode = childNode;
     agentInstanceContext = context.AgentInstanceContext;
 }
 public override EvalNode MakeEvalNode(PatternAgentInstanceContext agentInstanceContext, EvalNode parentNode)
 {
     if (_distinctExpressionsArray == null)
     {
         _distinctExpressionsArray = ExprNodeUtility.GetEvaluators(_distinctExpressions);
     }
     EvalNode child = EvalNodeUtil.MakeEvalNodeSingleChild(ChildNodes, agentInstanceContext, parentNode);
     return new EvalEveryDistinctNode(this, child, agentInstanceContext);
 }
Beispiel #20
0
        public static EvalNode[] MakeEvalNodeChildren(IList <EvalFactoryNode> childNodes, PatternAgentInstanceContext agentInstanceContext, EvalNode parentNode)
        {
            var children = new EvalNode[childNodes.Count];

            for (int i = 0; i < childNodes.Count; i++)
            {
                children[i] = childNodes[i].MakeEvalNode(agentInstanceContext, parentNode);
            }
            return(children);
        }
        public void DecreaseCount(EvalNode evalNode, AgentInstanceContext agentInstanceContext)
        {
            long newMax = _poolCount.DecrementAndGet();

            if ((ExecutionPathDebugLog.IsEnabled) && Log.IsDebugEnabled)
            {
                PatternSubexpressionPoolStmtHandler stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
                String stmtName = agentInstanceContext.StatementContext.StatementName;
                Log.Debug(".decreaseCount For statement '" + stmtName + "' pool count decreases to " + newMax +
                          " statement count was " + stmtHandler.Count);
            }
        }
Beispiel #22
0
 public override void Start(MatchedEventMap beginState)
 {
     Instrument.With(
         i => i.QPatternFollowedByStart(EvalFollowedByNode, beginState),
         i => i.APatternFollowedByStart(),
         () =>
     {
         EvalNode child           = EvalFollowedByNode.ChildNodes[0];
         EvalStateNode childState = child.NewState(this, null, 0L);
         Nodes.Put(childState, 0);
         childState.Start(beginState);
     });
 }
Beispiel #23
0
        public bool TryIncreaseCount(
            EvalNode evalNode,
            AgentInstanceContext agentInstanceContext)
        {
            // test pool max
            var newMax = poolCount.IncrementAndGet();
            if (newMax > maxPoolCountConfigured && maxPoolCountConfigured >= 0) {
                var counts = Counts;
                agentInstanceContext.StatementContext.ExceptionHandlingService.HandleCondition(
                    new ConditionPatternRuntimeSubexpressionMax(maxPoolCountConfigured, counts),
                    agentInstanceContext.StatementContext);
                if (ExecutionPathDebugLog.IsDebugEnabled &&
                    Log.IsDebugEnabled &&
                    ExecutionPathDebugLog.IsTimerDebugEnabled) {
                    var stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
                    var stmtName = agentInstanceContext.StatementContext.StatementName;
                    Log.Debug(
                        ".tryIncreaseCount For statement '" +
                        stmtName +
                        "' pool count overflow at " +
                        newMax +
                        " statement count was " +
                        stmtHandler.Count +
                        " preventStart=" +
                        preventStart);
                }

                if (preventStart) {
                    poolCount.DecrementAndGet();
                    return false;
                }

                return true;
            }

            if (ExecutionPathDebugLog.IsDebugEnabled && Log.IsDebugEnabled) {
                var stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
                var stmtName = agentInstanceContext.StatementContext.StatementName;
                Log.Debug(
                    ".tryIncreaseCount For statement '" +
                    stmtName +
                    "' pool count increases to " +
                    newMax +
                    " statement count was " +
                    stmtHandler.Count);
            }

            return true;
        }
Beispiel #24
0
 // Relevant for recovery of state
 public void ForceIncreaseCount(
     EvalNode evalNode,
     AgentInstanceContext agentInstanceContext)
 {
     var newMax = poolCount.IncrementAndGet();
     if (ExecutionPathDebugLog.IsDebugEnabled && Log.IsDebugEnabled) {
         var stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
         var stmtName = agentInstanceContext.StatementContext.StatementName;
         Log.Debug(
             ".forceIncreaseCount For statement '" +
             stmtName +
             "' pool count increases to " +
             newMax +
             " statement count was " +
             stmtHandler.Count);
     }
 }
Beispiel #25
0
        public void EvaluateTrue(MatchedEventMap matchEvent, EvalStateNode fromNode, bool isQuitted)
        {
            int index;
            var hasIndex = Nodes.TryGetValue(fromNode, out index);

            bool[] isFollowedByQuitted = { false };

            using (Instrument.With(
                       i => i.QPatternFollowedByEvaluateTrue(EvalFollowedByNode, matchEvent, index),
                       i => i.APatternFollowedByEvaluateTrue(isFollowedByQuitted[0])))
            {
                if (isQuitted)
                {
                    Nodes.Remove(fromNode);
                }

                // the node may already have quit as a result of an outer state quitting this state,
                // however the callback may still be received; It is fine to ignore this callback.
                if (!hasIndex)
                {
                    return;
                }

                // If the match came from the very last filter, need to escalate
                int numChildNodes = EvalFollowedByNode.ChildNodes.Length;
                if (index == (numChildNodes - 1))
                {
                    if (Nodes.IsEmpty())
                    {
                        isFollowedByQuitted[0] = true;
                    }

                    ParentEvaluator.EvaluateTrue(matchEvent, this, isFollowedByQuitted[0]);
                }
                // Else start a new sub-expression for the next-in-line filter
                else
                {
                    EvalNode      child      = EvalFollowedByNode.ChildNodes[index + 1];
                    EvalStateNode childState = child.NewState(this, null, 0L);
                    Nodes.Put(childState, index + 1);
                    childState.Start(matchEvent);
                }
            }
        }
 private void HandleSunrise(EvalNode evalNode, DateTime start, DateTime dateEnd, double addMinutes)
 {
     if (evalNode.Occurrences == null)
     {
         evalNode.Occurrences = new List <DateTime>();
     }
     while (start.Ticks < dateEnd.Ticks)
     {
         var solarTimes = new SolarTimes(start.ToLocalTime(), Location["latitude"].Value, Location["longitude"].Value);
         var sunrise    = solarTimes.Sunrise;
         sunrise = sunrise.AddMinutes(addMinutes);
         if (IsBetween(sunrise, start, dateEnd))
         {
             sunrise = sunrise.AddSeconds(-sunrise.Second).AddMilliseconds(-sunrise.Millisecond);
             evalNode.Occurrences.Add(sunrise);
         }
         start = start.AddHours(24);
     }
 }
Beispiel #27
0
 private void HandleSolarNoon(EvalNode evalNode, DateTime start, DateTime dateEnd, double addMinutes)
 {
     if (evalNode.Occurrences == null)
     {
         evalNode.Occurrences = new List <DateTime>();
     }
     while (start.Ticks < dateEnd.Ticks)
     {
         var solarTimes = new SolarTimes(start.ToLocalTime(), Location["latitude"].Value,
                                         Location["longitude"].Value);
         var solarNoon = solarTimes.SolarNoon;
         solarNoon = solarNoon.AddMinutes(addMinutes);
         if (IsBetween(solarNoon, start, dateEnd))
         {
             solarNoon = TruncateDate(solarNoon);
             evalNode.Occurrences.Add(solarNoon);
         }
         start = start.AddHours(24);
     }
 }
        public override void Start(MatchedEventMap beginState)
        {
            Instrument.With(
                i => i.QPatternMatchUntilStart(_evalMatchUntilNode, beginState),
                i => i.APatternMatchUntilStart(),
                () =>
            {
                _beginState = beginState;

                EvalNode childMatcher = _evalMatchUntilNode.ChildNodeSub;
                _stateMatcher         = childMatcher.NewState(this, null, 0L);

                if (_evalMatchUntilNode.ChildNodeUntil != null)
                {
                    EvalNode childUntil = _evalMatchUntilNode.ChildNodeUntil;
                    _stateUntil         = childUntil.NewState(this, null, 0L);
                }

                // start until first, it controls the expression
                // if the same event fires both match and until, the match should not count
                if (_stateUntil != null)
                {
                    _stateUntil.Start(beginState);
                }

                EvalMatchUntilStateBounds bounds =
                    EvalMatchUntilStateBounds.InitBounds(
                        _evalMatchUntilNode.FactoryNode, beginState, _evalMatchUntilNode.Context);
                _lowerbounds = bounds.Lowerbounds;
                _upperbounds = bounds.Upperbounds;

                if (_stateMatcher != null)
                {
                    _stateMatcher.Start(beginState);
                }
            });
        }
        public bool TryIncreaseCount(EvalNode evalNode, AgentInstanceContext agentInstanceContext)
        {
            // test pool max
            long newMax = _poolCount.IncrementAndGet();

            if (newMax > _maxPoolCountConfigured && _maxPoolCountConfigured >= 0)
            {
                var counts = Counts;
                agentInstanceContext.StatementContext.ExceptionHandlingService.HandleCondition(
                    new ConditionPatternEngineSubexpressionMax(_maxPoolCountConfigured, counts), agentInstanceContext.StatementContext.EpStatementHandle);
                if ((ExecutionPathDebugLog.IsEnabled) &&
                    (Log.IsDebugEnabled && (ExecutionPathDebugLog.IsTimerDebugEnabled)))
                {
                    PatternSubexpressionPoolStmtHandler stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
                    String stmtName = agentInstanceContext.StatementContext.StatementName;
                    Log.Debug(".tryIncreaseCount For statement '" + stmtName + "' pool count overflow at " + newMax +
                              " statement count was " + stmtHandler.Count + " preventStart=" + _preventStart);
                }

                if (_preventStart)
                {
                    _poolCount.DecrementAndGet();
                    return(false);
                }

                return(true);
            }
            if ((ExecutionPathDebugLog.IsEnabled) && Log.IsDebugEnabled)
            {
                PatternSubexpressionPoolStmtHandler stmtHandler = agentInstanceContext.StatementContext.PatternSubexpressionPoolSvc.StmtHandler;
                String stmtName = agentInstanceContext.StatementContext.StatementName;
                Log.Debug(".tryIncreaseCount For statement '" + stmtName + "' pool count increases to " + newMax +
                          " statement count was " + stmtHandler.Count);
            }
            return(true);
        }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeExponent node.
 /// </summary>
 /// <param name="child">Specifies the first child node.</param>
 /// <param name="language">Language syntax and semantics to use.</param>
 public EvalNodeExponent(EvalNode child,
                         Language language)
 {
     _language = language;
     Append(child);
 }
 /// <summary>
 /// Initialize a new instance of the EvalNodeArgList class.
 /// </summary>
 /// <param name="argument">Initial argument to store.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeArgList(EvalNode argument,
                        Language language)
 {
     _language = language;
     Append(argument);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeConcatenate node.
 /// </summary>
 /// <param name="child">Specifies the first child node.</param>
 public EvalNodeConcatenate(EvalNode child)
 {
     Append(child);
 }
        public override EvalNode MakeEvalNode(PatternAgentInstanceContext agentInstanceContext, EvalNode parentNode)
        {
            if (_opType == null)
            {
                InitOpType();
            }

            EvalNode[] children = EvalNodeUtil.MakeEvalNodeChildren(ChildNodes, agentInstanceContext, parentNode);
            return(new EvalFollowedByNode(agentInstanceContext, this, children));
        }
 public override EvalNode MakeEvalNode(PatternAgentInstanceContext agentInstanceContext, EvalNode parentNode)
 {
     EvalNode[] children = EvalNodeUtil.MakeEvalNodeChildren(ChildNodes, agentInstanceContext, parentNode);
     return(new EvalMatchUntilNode(agentInstanceContext, this, children[0], children.Length == 1 ? null : children[1]));
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeNullCoalescing node.
 /// </summary>
 /// <param name="child">Specifies the child node.</param>
 public EvalNodeNullCoalescing(EvalNode child)
 {
     Append(child);
 }
Beispiel #36
0
 public override EvalNode MakeEvalNode(
     PatternAgentInstanceContext agentInstanceContext,
     EvalNode parentNode)
 {
     return new EvalObserverNode(agentInstanceContext, this);
 }
 /// <summary>
 /// Instantiate a new instance of the EvalNodeConditional node.
 /// </summary>
 /// <param name="child1">Specifies the first child.</param>
 /// <param name="child2">Specifies the second child.</param>
 public EvalNodeConditional(EvalNode child1,
                            EvalNode child2)
 {
     Append(child1);
     Append(child2);
 }
Beispiel #38
0
 /// <summary>Constructor. </summary>
 /// <param name="rootSingleChildNode">is the root nodes single child node</param>
 public EvalRootStateNode(EvalNode rootSingleChildNode)
     : base(null)
 {
     RootSingleChildNode = rootSingleChildNode;
 }
 /// <summary>
 /// Initialize a new instance of the EvalNodeArrayIndex class.
 /// </summary>
 /// <param name="child">Specifies the child node.</param>
 /// <param name="language">Language used for evaluation.</param>
 public EvalNodeArrayIndex(EvalNode child,
                           Language language)
 {
     _language = language;
     Append(child);
 }
Beispiel #40
0
        public override EvalNode MakeEvalNode(PatternAgentInstanceContext agentInstanceContext, EvalNode parentNode)
        {
            EvalNode child = EvalNodeUtil.MakeEvalNodeSingleChild(ChildNodes, agentInstanceContext, parentNode);

            return(new EvalEveryNode(agentInstanceContext, this, child));
        }
        /// <summary>
        /// Parse the provided input string.
        /// </summary>
        /// <param name="input">Input text.</param>
        public virtual void Parse(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                // Generate entire set of tokens in one go
                _tokeniser = new Tokeniser(input, Language);

                // Parse into abstract syntax tree
                EvalNode eval = ParseExpression();

                // Should be no more input available
                if (Tokeniser.Peek.Type != TokenType.EndOfInput)
                    throw new ParseException(Tokeniser.Peek.Index, "Found '" + Tokeniser.Peek.Type.ToString() + "' instead of end of input");
                else
                    _root = eval;
            }
            else
                _root = null;
        }