예제 #1
0
 public SearchPlanEdgeNode(PlanNodeType nodeType, int elemID, bool isPreset, PatternElement patternElem,
                           SearchPlanNodeNode patEdgeSrc, SearchPlanNodeNode patEdgeTgt)
     : base(nodeType, elemID, isPreset, patternElem)
 {
     PatternEdgeSource = patEdgeSrc;
     PatternEdgeTarget = patEdgeTgt;
 }
예제 #2
0
 public PatternClient(GameObject item, Vector3 theStart, Vector3 theEnd)
 {
     start      = theStart;
     end        = theEnd;
     ptrn       = PatternFactory.getPattern(item);
     gameObject = MonoBehaviour.Instantiate(ptrn.GetPattern(), start, ptrn.GetPattern().transform.rotation);
 }
예제 #3
0
        AggregateElement IBuilder <AggregateElement> .Build()
        {
            PatternElement   sourceElement    = _sourceBuilder?.Build();
            AggregateElement aggregateElement = Element.Aggregate(_resultType, _name, _expressions, sourceElement, _customFactoryType);

            return(aggregateElement);
        }
예제 #4
0
        protected override void VisitPattern(ReteBuilderContext context, PatternElement element)
        {
            context.RegisterDeclaration(element.Declaration);
            if (element.Source == null)
            {
                AlphaNode currentNode = BuildTypeNode(element.ValueType, _root);

                var betaConditions = new List <ConditionElement>();
                foreach (var conditionElement in element.Conditions)
                {
                    if (conditionElement.Declarations.Count() > 1)
                    {
                        betaConditions.Add(conditionElement);
                        continue;
                    }

                    var           alphaCondition = new AlphaCondition(conditionElement.Expression);
                    SelectionNode selectionNode  = BuildSelectionNode(alphaCondition, currentNode);
                    currentNode = selectionNode;
                }

                context.AlphaSource = BuildAlphaMemoryNode(currentNode);

                if (betaConditions.Count > 0)
                {
                    var joinNode = BuildJoinNode(context, betaConditions);
                    context.BetaSource = BuildBetaMemoryNode(context, joinNode);
                }
            }
            else
            {
                Visit(context, element.Source);
                //TODO: Handle a more generic case, when pattern adds its own conditions
            }
        }
예제 #5
0
        protected override void VisitPattern(ReteBuilderContext context, PatternElement element)
        {
            var conditions = element.Expressions.Find(PatternElement.ConditionName)
                             .Cast <ExpressionElement>().ToList();

            if (element.Source == null)
            {
                context.CurrentAlphaNode = _root;
                context.RegisterDeclaration(element.Declaration);

                BuildTypeNode(context, element, element.ValueType);
                var alphaConditions = conditions.Where(x => x.Imports.Count() == 1).ToList();
                foreach (var alphaCondition in alphaConditions)
                {
                    BuildSelectionNode(context, alphaCondition);
                }
                BuildAlphaMemoryNode(context);

                var betaConditions = conditions.Where(x => x.Imports.Count() > 1).ToList();
                if (betaConditions.Count > 0)
                {
                    BuildJoinNode(context, element, betaConditions);
                }
            }
            else
            {
                var isJoined = element.Imports.Any();
                if (isJoined)
                {
                    if (conditions.Any())
                    {
                        BuildSubnet(context, element);

                        context.RegisterDeclaration(element.Declaration);
                        BuildJoinNode(context, element, conditions);
                    }
                    else
                    {
                        Visit(context, element.Source);
                        context.RegisterDeclaration(element.Declaration);
                    }
                }
                else
                {
                    var joinContext = new ReteBuilderContext(context.Rule, _dummyNode);
                    BuildSubnet(joinContext, element);

                    joinContext.RegisterDeclaration(element.Declaration);
                    if (conditions.Any())
                    {
                        BuildJoinNode(joinContext, element, conditions);
                        BuildAdapter(joinContext);
                    }
                    context.AlphaSource = joinContext.AlphaSource;

                    context.RegisterDeclaration(element.Declaration);
                }
            }
        }
예제 #6
0
 protected internal virtual void VisitPattern(TContext context, PatternElement element)
 {
     foreach (ConditionElement condition in element.Conditions)
     {
         condition.Accept(context, this);
     }
     element.Source?.Accept(context, this);
 }
예제 #7
0
        protected SearchPlanNode(PlanNodeType nodeType, int elemID, bool isPreset, PatternElement patternElem)
        {
            NodeType  = nodeType;
            ElementID = elemID;
            IsPreset  = isPreset;

            PatternElement = patternElem;
        }
예제 #8
0
        protected SearchPlanNode(PlanNode planNode)
        {
            NodeType  = planNode.NodeType;
            ElementID = planNode.ElementID;
            IsPreset  = planNode.IsPreset;

            PatternElement = planNode.PatternElement;
        }
예제 #9
0
        /// <summary>
        /// Instantiates a node plan node.
        /// </summary>
        /// <param name="patNode">The pattern node for this plan node.</param>
        /// <param name="elemID">The element ID for this plan node.</param>
        /// <param name="isPreset">True, if this element is a known element.</param>
        public PlanNode(PatternNode patNode, int elemID, bool isPreset)
        {
            NodeType  = PlanNodeType.Node;
            ElementID = elemID;
            IsPreset  = isPreset;

            PatternElement = patNode;
        }
예제 #10
0
 protected internal virtual void VisitPattern(TContext context, PatternElement element)
 {
     foreach (var expression in element.Expressions)
     {
         expression.Accept(context, this);
     }
     element.Source?.Accept(context, this);
 }
예제 #11
0
        AggregateElement IBuilder <AggregateElement> .Build()
        {
            Validate();
            IBuilder <PatternElement> sourceBuilder = _sourceBuilder;
            PatternElement            sourceElement = sourceBuilder.Build();
            var aggregateElement = new AggregateElement(_resultType, _aggregateType, sourceElement);

            return(aggregateElement);
        }
예제 #12
0
 public override void Visit(CypherVisitor cypherVisitor)
 {
     cypherVisitor.Enter(this);
     PatternElement.Visit(cypherVisitor);
     Where?.Visit(cypherVisitor);
     Operator.Pipe.Visit(cypherVisitor);
     Expression?.Visit(cypherVisitor);
     cypherVisitor.Leave(this);
 }
예제 #13
0
        protected internal override void VisitPattern(Context context, PatternElement element)
        {
            var source = Transform <RuleElement>(context, element.Source);

            if (context.IsModified)
            {
                var newElement = Element.Pattern(element.Declaration, element.Expressions, source);
                Result(context, newElement);
            }
        }
예제 #14
0
        AggregateElement IBuilder <AggregateElement> .Build()
        {
            Validate();
            IBuilder <PatternElement> sourceBuilder = _sourceBuilder;
            PatternElement            sourceElement = sourceBuilder.Build();
            var elements         = _expressions.Select(x => ToNamedExpression(x.Key, x.Value));
            var expressionMap    = new ExpressionMap(elements);
            var aggregateElement = new AggregateElement(Scope.VisibleDeclarations, _resultType, _name, expressionMap, sourceElement, _customFactoryType);

            return(aggregateElement);
        }
예제 #15
0
        private async void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(ViewModel.SelectedPatternElement) && ViewModel.SelectedPatternElement != null)
            {
                PatternElement elem  = ViewModel.SelectedPatternElement;
                var            point = new MapPoint(elem.Longitude, elem.Latitude, MapHelper.BUS_ROUTES_SR);
                await MainMapView.SetViewpointCenterAsync(point);

                await MainMapView.SetViewpointScaleAsync(2000);
            }
        }
예제 #16
0
        protected internal override void VisitPattern(Context context, PatternElement element)
        {
            var conditions = element.Conditions.Select(x => Transform <ConditionElement>(context, x)).ToList();
            var source     = Transform <PatternSourceElement>(context, element.Source);

            if (context.IsModified)
            {
                var newElement = new PatternElement(element.Declaration, element.Declarations, conditions, source);
                Result(context, newElement);
            }
        }
예제 #17
0
        /// <summary>
        /// Instantiates an edge plan node.
        /// </summary>
        /// <param name="patEdge">The pattern edge for this plan node.</param>
        /// <param name="elemID">The element ID for this plan node.</param>
        /// <param name="isPreset">True, if this element is a known element.</param>
        /// <param name="patternEdgeSource">The plan node corresponding to the source of the pattern edge.</param>
        /// <param name="patternEdgeTarget">The plan node corresponding to the target of the pattern edge.</param>
        public PlanNode(PatternEdge patEdge, int elemID, bool isPreset,
                        PlanNode patternEdgeSource, PlanNode patternEdgeTarget)
        {
            NodeType  = PlanNodeType.Edge;
            ElementID = elemID;
            IsPreset  = isPreset;

            PatternElement = patEdge;

            PatternEdgeSource = patternEdgeSource;
            PatternEdgeTarget = patternEdgeTarget;
        }
예제 #18
0
        static PatternElement[] CreatePatternElements(ExtractionPattern pattern)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            var patternElements = new PatternElement[pattern.Elements.Count];

            for (var i = pattern.Elements.Count - 1; i >= 0; --i)
            {
                var element = pattern.Elements[i];
                switch (element)
                {
                case LiteralTextPatternExpression text:
                    patternElements[i] = new SimplePatternElement(Matchers.LiteralText(text.Text));
                    break;

                case CapturePatternExpression capture
                    when capture.Content is NonGreedyContentExpression ngc:
                    patternElements[i] = new SimplePatternElement(
                        Matchers.NonGreedyContent(patternElements.Skip(i + 1).Take(ngc.Lookahead).ToArray()),
                        capture.Name);
                    break;

                case CapturePatternExpression capture
                    when capture.Content is MatchTypeContentExpression mtc:
                    patternElements[i] = new SimplePatternElement(
                        Matchers.GetByType(mtc.Type),
                        capture.Name);
                    break;

                case CapturePatternExpression capture
                    when capture.Content is GroupedContentExpression gc:
                    patternElements[i] = new GroupedPatternElement(
                        CreatePatternElements(gc.ExtractionPattern),
                        capture.Name);
                    break;

                case CapturePatternExpression capture
                    when capture.Content == null:
                    patternElements[i] = new SimplePatternElement(
                        Matchers.Token,
                        capture.Name);
                    break;

                default:
                    throw new InvalidOperationException($"Element `{element}` not recognized.");
                }
            }

            return(patternElements);
        }
    protected void Evaluate(Action <PatternElement> action)
    {
        for (int i = 0; i < Pattern.Elements.Count; i++)
        {
            PatternElement element = Pattern.Elements[i];

            // This'll happen if it has been destroyed
            if (element != null)
            {
                action(element);
            }
        }
    }
예제 #20
0
파일: Element.cs 프로젝트: arastoul/NRules
        /// <summary>
        /// Creates an element that aggregates matching facts into a collection.
        /// </summary>
        /// <param name="resultType">Type of the aggregate result.</param>
        /// <param name="source">Pattern that matches facts for aggregation.</param>
        /// <returns>Created element.</returns>
        public static AggregateElement Collect(Type resultType, PatternElement source)
        {
            if (resultType == null)
            {
                var enumerableType = typeof(IEnumerable <>);
                resultType = enumerableType.MakeGenericType(source.ValueType);
            }

            var expressions = Array.Empty <KeyValuePair <string, LambdaExpression> >();
            var element     = Aggregate(resultType, AggregateElement.CollectName, expressions, source);

            return(element);
        }
예제 #21
0
        private void BuildSubnet(ReteBuilderContext context, PatternElement element)
        {
            var isJoined      = element.Source.Imports.Any();
            var subnetContext = isJoined ? new ReteBuilderContext(context) : new ReteBuilderContext(context.Rule, _dummyNode);

            Visit(subnetContext, element.Source);

            if (subnetContext.AlphaSource == null)
            {
                BuildAdapter(subnetContext);
                context.HasSubnet = isJoined;
            }
            context.AlphaSource = subnetContext.AlphaSource;
        }
예제 #22
0
    public void SpawnChildren(PatternElement prefab, PatternObject pattern)
    {
        System.Action <Vector2> spawnChildDelegate = x =>
        {
            PatternElement instance = Instantiate(prefab);
            instance.transform.position = Utility.ScaleToOrthographicVector(x);
            instance.AssignStartingPosition(x);
            instance.name = $"Pattern ({pattern.Elements.Count})";

            pattern.AddElement(instance);
        };

        DoSpawnChildren(spawnChildDelegate);
    }
예제 #23
0
    private object ParseI(Match m, PatternElement e)
    {
        string sign = "";

        if (m.Groups["sign"].Success && m.Groups["sign"].Value == "-")
        {
            sign = "-";
        }
        string val;
        int    fromBase;

        if (m.Groups["hex"].Success)
        {
            val      = sign + m.Groups["hex"].Value;
            fromBase = 16;
        }
        else if (m.Groups["oct"].Success)
        {
            val      = sign + m.Groups["oct"].Value;
            fromBase = 8;
        }
        else if (m.Groups["dec"].Success)
        {
            val      = sign + m.Groups["dec"].Value;
            fromBase = 10;
        }
        else
        {
            throw new System.ArgumentException(string.Format("Could not convert '{0}' to an int", m.Value));
        }

        switch (e.size)
        {
        case 0:
            return(System.Convert.ToSByte(val, fromBase));

        case 1:
            return(System.Convert.ToInt16(val, fromBase));

        case 3:
            return(System.Convert.ToInt32(val, fromBase));

        case 4:
            return(System.Convert.ToInt64(val, fromBase));

        default:
            return(System.Convert.ToInt32(val, fromBase));
        }
    }
예제 #24
0
        public static void ValidatePattern(PatternElement element)
        {
            if (element.Source != null)
            {
                switch (element.Source.ElementType)
                {
                case ElementType.Aggregate:
                case ElementType.Binding:
                    break;

                default:
                    throw new ArgumentException($"Invalid source element. ElementType={element.Source.ElementType}");
                }
            }
        }
예제 #25
0
        ForAllElement IBuilder <ForAllElement> .Build()
        {
            PatternElement basePatternElement = _sourceBuilder?.Build();

            var patternElements = new List <PatternElement>();

            foreach (var patternBuilder in _patternBuilders)
            {
                patternElements.Add(patternBuilder.Build());
            }

            var forAllElement = Element.ForAll(basePatternElement, patternElements);

            return(forAllElement);
        }
예제 #26
0
파일: Element.cs 프로젝트: arastoul/NRules
        /// <summary>
        /// Creates an element that represents a universal quantifier.
        /// Facts that match the <c>basePattern</c> must also match all other <c>patterns</c>.
        /// </summary>
        /// <param name="basePattern">Base patterns of the universal quantifier that defines the universe of facts to consider.</param>
        /// <param name="patterns">Additional patterns of the universal quantifier that the fact matched by the base pattern must also satisfy.</param>
        /// <returns>Created element.</returns>
        public static ForAllElement ForAll(PatternElement basePattern, IEnumerable <PatternElement> patterns)
        {
            if (basePattern == null)
            {
                throw new ArgumentNullException(nameof(basePattern), "Base pattern not provided");
            }
            if (patterns == null)
            {
                throw new ArgumentNullException(nameof(patterns), "Patterns not provided");
            }

            var element = new ForAllElement(basePattern, patterns);

            ElementValidator.ValidateForAll(element);
            return(element);
        }
예제 #27
0
        /// <summary>
        /// Creates a pattern element that represents a match over results of the source element.
        /// </summary>
        /// <param name="declaration">Declaration that references the pattern.</param>
        /// <param name="conditions">Condition elements that represent conditions applied to the elements matched by the pattern.</param>
        /// <param name="source">Source of the elements matched by the pattern. If it's <c>null</c>, the pattern matches facts in rules engine's working memory.</param>
        /// <returns>Created element.</returns>
        public static PatternElement Pattern(Declaration declaration, IEnumerable <ConditionElement> conditions, PatternSourceElement source)
        {
            if (declaration == null)
            {
                throw new ArgumentNullException(nameof(declaration), "Pattern declaration not provided");
            }
            if (conditions == null)
            {
                throw new ArgumentNullException(nameof(conditions), "Pattern conditions not provided");
            }

            var element = new PatternElement(declaration, conditions, source);

            declaration.Target = element;
            return(element);
        }
예제 #28
0
        ForAllElement IBuilder <ForAllElement> .Build()
        {
            Validate();
            IBuilder <PatternElement> basePatternBuilder = _basePatternBuilder;
            PatternElement            basePatternElement = basePatternBuilder.Build();

            var patternElements = new List <PatternElement>();

            foreach (IBuilder <PatternElement> patternBuilder in _patternBuilders)
            {
                patternElements.Add(patternBuilder.Build());
            }

            var forAllElement = new ForAllElement(Scope.VisibleDeclarations, basePatternElement, patternElements);

            return(forAllElement);
        }
예제 #29
0
파일: Element.cs 프로젝트: arastoul/NRules
        /// <summary>
        /// Creates a pattern element that represents a match over results of the source element.
        /// </summary>
        /// <param name="declaration">Declaration that references the pattern.</param>
        /// <param name="expressions">Expressions used by the pattern to match elements.</param>
        /// <param name="source">Source of the elements matched by the pattern. If it's <c>null</c>, the pattern matches facts in rules engine's working memory.</param>
        /// <returns>Created element.</returns>
        public static PatternElement Pattern(Declaration declaration, IEnumerable <NamedExpressionElement> expressions, RuleElement source)
        {
            if (declaration == null)
            {
                throw new ArgumentNullException(nameof(declaration), "Pattern declaration not provided");
            }
            if (expressions == null)
            {
                throw new ArgumentNullException(nameof(expressions), "Pattern expressions not provided");
            }

            var expressionCollection = new ExpressionCollection(expressions);
            var element = new PatternElement(declaration, expressionCollection, source);

            declaration.Target = element;
            ElementValidator.ValidatePattern(element);
            return(element);
        }
예제 #30
0
        /// <summary>
        /// Creates an element that represents a universal quantifier.
        /// Facts that match the <c>basePattern</c> must also match all other <c>patterns</c>.
        /// </summary>
        /// <param name="basePattern">Base patterns of the universal quantifier that defines the universe of facts to consider.</param>
        /// <param name="patterns">Additional patterns of the universal quantifier that the fact matched by the base pattern must also satisfy.</param>
        /// <returns>Created element.</returns>
        public static ForAllElement ForAll(PatternElement basePattern, IEnumerable <PatternElement> patterns)
        {
            if (basePattern == null)
            {
                throw new ArgumentNullException(nameof(basePattern), "Base pattern not provided");
            }
            if (patterns == null)
            {
                throw new ArgumentNullException(nameof(patterns), "Patterns not provided");
            }

            var forAllElement = new ForAllElement(basePattern, patterns);

            if (!forAllElement.Patterns.Any())
            {
                throw new InvalidOperationException("At least one FORALL pattern must be specified");
            }

            return(forAllElement);
        }