Example #1
0
        public static string ToString(LogicalQueryNode expr)
        {
            switch (expr.Kind)
            {
            case KindOfLogicalQueryNode.UnaryOperator:
                return(UnaryOperatorToString(expr));

            case KindOfLogicalQueryNode.BinaryOperator:
                return(BinaryOperatorToString(expr));

            case KindOfLogicalQueryNode.Relation:
                return(RelationToString(expr));

            case KindOfLogicalQueryNode.Concept:
            case KindOfLogicalQueryNode.QuestionVar:
            case KindOfLogicalQueryNode.Entity:
            case KindOfLogicalQueryNode.LogicalVar:
                return(ConceptToString(expr));

            case KindOfLogicalQueryNode.Value:
                return(ValueToString(expr));

            case KindOfLogicalQueryNode.FuzzyLogicNonNumericSequence:
                return(FuzzyLogicNonNumericSequenceValueToString(expr.FuzzyLogicNonNumericSequenceValue));

            default:
                throw new ArgumentOutOfRangeException(nameof(expr.Kind), expr.Kind, null);
            }
        }
Example #2
0
        private void ProcessUnaryOperator(KindOfOperatorOfLogicalQueryNode kindOfOperator)
        {
            var node = new LogicalQueryNode();

            _lastLogicalQueryNode = node;
            node.Kind             = KindOfLogicalQueryNode.UnaryOperator;

            node.KindOfOperator = kindOfOperator;

            var kind = KindOfOperator.Unknown;

            switch (kindOfOperator)
            {
            case KindOfOperatorOfLogicalQueryNode.Not:
                kind = KindOfOperator.Not;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(kindOfOperator), kindOfOperator, null);
            }

            var priority = OperatorsHelper.GetPriority(kind);

#if DEBUG
            //Log($"priority = {priority}");
#endif

            var intermediateNode = new IntermediateAstNode(node, KindOfIntermediateAstNode.UnaryOperator, priority);

            AstNodesLinker.SetNode(intermediateNode, _nodePoint);

            _state = State.GotOperator;
        }
Example #3
0
        private LogicalQueryNode CreateLogicalQueryNodeByStrongIdentifierValue(StrongIdentifierValue value)
        {
            var node = new LogicalQueryNode();

            var kindOfName = value.KindOfName;

            switch (kindOfName)
            {
            case KindOfName.Concept:
                node.Kind = KindOfLogicalQueryNode.Concept;
                break;

            case KindOfName.Entity:
                node.Kind = KindOfLogicalQueryNode.Entity;
                break;

            case KindOfName.LogicalVar:
                node.Kind = KindOfLogicalQueryNode.LogicalVar;
                break;

            default:
                throw new UnexpectedTokenException(_currToken);
            }

            node.Name = value;

            return(node);
        }
Example #4
0
        private static string ConceptToString(LogicalQueryNode expr)
        {
            var sb = new StringBuilder();

            sb.Append(expr.Name.NameValue);
            sb.Append(AnnotatedItemToString(expr));
            return(sb.ToString());
        }
        private static LogicalQueryNode ConvertLogicalQueryNodeInDefaultWay(LogicalQueryNode source, Dictionary <object, object> convertingContext)
        {
#if DEBUG
            //_gbcLogger.Info("ConvertLogicalQueryNodeInDefaultWay!!!!!");
            //_gbcLogger.Info($"source = {source}");
#endif

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

            if (convertingContext.ContainsKey(source))
            {
                return((LogicalQueryNode)convertingContext[source]);
            }

            var result = new LogicalQueryNode();
            convertingContext[source] = result;

            result.Kind           = source.Kind;
            result.KindOfOperator = source.KindOfOperator;
            result.Name           = source.Name;

            if (source.Left != null)
            {
                result.Left = ConvertLogicalQueryNode(source.Left, convertingContext);
            }

            if (source.Right != null)
            {
                result.Right = ConvertLogicalQueryNode(source.Right, convertingContext);
            }

            if (!source.ParamsList.IsNullOrEmpty())
            {
                var destParametersList = new List <LogicalQueryNode>();

                foreach (var param in source.ParamsList)
                {
                    destParametersList.Add(ConvertLogicalQueryNode(param, convertingContext));
                }

                result.ParamsList = destParametersList;
            }

            result.Value = source.Value;
            result.FuzzyLogicNonNumericSequenceValue = source.FuzzyLogicNonNumericSequenceValue;
            result.IsQuestion = source.IsQuestion;

            FillAnnotationsModalitiesAndSections(source, result, convertingContext);

#if DEBUG
            //_gbcLogger.Info($"result = {result}");
#endif

            return(result);
        }
Example #6
0
        private static string ValueToString(LogicalQueryNode expr)
        {
            var sb    = new StringBuilder();
            var value = expr.Value;

            sb.Append(ToString(value));
            sb.Append(AnnotatedItemToString(expr));

            return(sb.ToString());
        }
Example #7
0
        private static string BinaryOperatorToString(LogicalQueryNode expr)
        {
            var mark = string.Empty;

            switch (expr.KindOfOperator)
            {
            case KindOfOperatorOfLogicalQueryNode.And:
                mark = "&";
                break;

            case KindOfOperatorOfLogicalQueryNode.Or:
                mark = "|";
                break;

            case KindOfOperatorOfLogicalQueryNode.Is:
                mark = "is";
                break;

            case KindOfOperatorOfLogicalQueryNode.IsNot:
                mark = "is not";
                break;

            case KindOfOperatorOfLogicalQueryNode.More:
                mark = ">";
                break;

            case KindOfOperatorOfLogicalQueryNode.MoreOrEqual:
                mark = ">=";
                break;

            case KindOfOperatorOfLogicalQueryNode.Less:
                mark = "<";
                break;

            case KindOfOperatorOfLogicalQueryNode.LessOrEqual:
                mark = "<=";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(expr.KindOfOperator), expr.KindOfOperator, null);
            }

            var sb = new StringBuilder();

            sb.Append(ToString(expr.Left));
            sb.Append($" {mark} ");
            sb.Append(AnnotatedItemToString(expr));
            sb.Append(ToString(expr.Right));

            return(sb.ToString());
        }
Example #8
0
        private void StartProcessingFuzzyLogicNonNumericSequenceValue(StrongIdentifierValue value)
        {
            _fuzzyLogicNonNumericSequenceValue = new FuzzyLogicNonNumericSequenceValue();

            var node = new LogicalQueryNode();

            node.Kind = KindOfLogicalQueryNode.FuzzyLogicNonNumericSequence;

            node.FuzzyLogicNonNumericSequenceValue = _fuzzyLogicNonNumericSequenceValue;

            _fuzzyLogicNonNumericSequenceValue.AddIdentifier(value);

            var intermediateNode = new IntermediateAstNode(node);

            AstNodesLinker.SetNode(intermediateNode, _nodePoint);

            _state = State.GotFuzzyLogicNonNumericSequenceItem;
        }
        private static LogicalQueryNode ConvertUnaryPredicateToFullIsPredicate(LogicalQueryNode source, Dictionary <object, object> convertingContext)
        {
#if DEBUG
            //_gbcLogger.Info($"source = {source}");
            //_gbcLogger.Info($"source.GetHumanizeDbgString() = {source.GetHumanizeDbgString()}");
#endif

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

            if (convertingContext.ContainsKey(source))
            {
                return((LogicalQueryNode)convertingContext[source]);
            }

            var result = new LogicalQueryNode();
            convertingContext[source] = result;

            result.Kind       = KindOfLogicalQueryNode.Relation;
            result.Name       = NameHelper.CreateName("is");
            result.ParamsList = new List <LogicalQueryNode>();

            result.ParamsList.Add(source.ParamsList[0]);

            var superNameNode = new LogicalQueryNode();
            superNameNode.Kind = KindOfLogicalQueryNode.Concept;
            superNameNode.Name = source.Name;

            result.ParamsList.Add(superNameNode);

            var rankNode = new LogicalQueryNode();
            result.ParamsList.Add(rankNode);
            rankNode.Kind  = KindOfLogicalQueryNode.Value;
            rankNode.Value = new LogicalValue(1);

#if DEBUG
            //_gbcLogger.Info($"result = {result}");
            //_gbcLogger.Info($"result.GetHumanizeDbgString() = {result.GetHumanizeDbgString()}");
#endif

            return(result);
        }
        private RuleInstance CreateInheritanceFact(InheritanceItem inheritanceItem)
        {
            var factName = NameHelper.CreateRuleOrFactName();

#if DEBUG
            //Log($"factName = {factName}");
#endif

            var fact = new RuleInstance();
            fact.Kind = KindOfRuleInstance.Fact;
            fact.AppendAnnotations(inheritanceItem);
            fact.Name = factName;
            //fact.KeysOfPrimaryRecords.Add(inheritanceItem.Id);

            var primaryPart = new PrimaryRulePart();
            fact.PrimaryPart   = primaryPart;
            primaryPart.Parent = fact;

            var isRelation = new LogicalQueryNode();
            isRelation.Name       = NameHelper.CreateName("is");
            isRelation.Kind       = KindOfLogicalQueryNode.Relation;
            isRelation.ParamsList = new List <LogicalQueryNode>();

            primaryPart.Expression = isRelation;

            var subItemNode = new LogicalQueryNode();
            isRelation.ParamsList.Add(subItemNode);
            subItemNode.Kind = KindOfLogicalQueryNode.Concept;
            subItemNode.Name = inheritanceItem.SubName;

            var superItemNode = new LogicalQueryNode();
            isRelation.ParamsList.Add(superItemNode);
            superItemNode.Kind = KindOfLogicalQueryNode.Concept;
            superItemNode.Name = inheritanceItem.SuperName;

            var rankNode = new LogicalQueryNode();
            isRelation.ParamsList.Add(rankNode);
            rankNode.Kind  = KindOfLogicalQueryNode.Value;
            rankNode.Value = inheritanceItem.Rank;

            return(fact);
        }
Example #11
0
        private static string RelationToString(LogicalQueryNode expr)
        {
            var sb = new StringBuilder();

            sb.Append($"{expr.Name.NameValue}(");

            var resultParamsList = new List <string>();

            foreach (var param in expr.ParamsList)
            {
                resultParamsList.Add(ToString(param));
            }

            sb.Append(string.Join(",", resultParamsList));

            sb.Append(")");
            sb.Append(AnnotatedItemToString(expr));

            return(sb.ToString());
        }
Example #12
0
        private static string UnaryOperatorToString(LogicalQueryNode expr)
        {
            var mark = string.Empty;

            switch (expr.KindOfOperator)
            {
            case KindOfOperatorOfLogicalQueryNode.Not:
                mark = "not";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(expr.KindOfOperator), expr.KindOfOperator, null);
            }

            var sb = new StringBuilder();

            sb.Append($" {mark} ");
            sb.Append(AnnotatedItemToString(expr));
            sb.Append(ToString(expr.Left));

            return(sb.ToString());
        }
Example #13
0
        private void ProcessGroup()
        {
            var node = new LogicalQueryNode();

            _lastLogicalQueryNode = node;
            node.Kind             = KindOfLogicalQueryNode.Group;

            var parser = new LogicalExpressionParser(_context, true);

            parser.Run();

#if DEBUG
            //Log($"parser.Result = {parser.Result}");
#endif

            node.Left = parser.Result;

            var intermediateNode = new IntermediateAstNode(node);

            AstNodesLinker.SetNode(intermediateNode, _nodePoint);

            _state = State.GotPredicate;
        }
Example #14
0
        private void ProcessPredicate(StrongIdentifierValue name)
        {
            var node = new LogicalQueryNode();

            _lastLogicalQueryNode = node;
            node.Kind             = KindOfLogicalQueryNode.Relation;

            node.Name = name;

            node.ParamsList = new List <LogicalQueryNode>();

            var priority = OperatorsHelper.GetPriority(KindOfOperator.Predicate);

#if DEBUG
            //Log($"priority = {priority}");
#endif

            var intermediateNode = new IntermediateAstNode(node, KindOfIntermediateAstNode.UnaryOperator, priority);

            AstNodesLinker.SetNode(intermediateNode, _nodePoint);

            _state = State.WaitForPredicateParameter;
        }
Example #15
0
 /// <inheritdoc/>
 protected override void OnFinish()
 {
     Result = _nodePoint.BuildExpr <LogicalQueryNode>();
 }
        private static LogicalQueryNode ConvertLogicalQueryNode(LogicalQueryNode source, Dictionary <object, object> convertingContext)
        {
#if DEBUG
            //_gbcLogger.Info($"source = {source}");
#endif

            switch (source.Kind)
            {
            case KindOfLogicalQueryNode.BinaryOperator:
                switch (source.KindOfOperator)
                {
                case KindOfOperatorOfLogicalQueryNode.Is:
                case KindOfOperatorOfLogicalQueryNode.IsNot:
                case KindOfOperatorOfLogicalQueryNode.And:
                case KindOfOperatorOfLogicalQueryNode.Or:
                case KindOfOperatorOfLogicalQueryNode.More:
                case KindOfOperatorOfLogicalQueryNode.MoreOrEqual:
                case KindOfOperatorOfLogicalQueryNode.Less:
                case KindOfOperatorOfLogicalQueryNode.LessOrEqual:
                    return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

                default:
                    throw new ArgumentOutOfRangeException(nameof(source.KindOfOperator), source.KindOfOperator, null);
                }

            case KindOfLogicalQueryNode.UnaryOperator:
                switch (source.KindOfOperator)
                {
                case KindOfOperatorOfLogicalQueryNode.Not:
                    return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

                default:
                    throw new ArgumentOutOfRangeException(nameof(source.KindOfOperator), source.KindOfOperator, null);
                }

            case KindOfLogicalQueryNode.Concept:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.Entity:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.Relation:
                if (source.ParamsList.Count == 1)
                {
                    return(ConvertUnaryPredicateToFullIsPredicate(source, convertingContext));
                }
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.LogicalVar:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.QuestionVar:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.Value:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            case KindOfLogicalQueryNode.FuzzyLogicNonNumericSequence:
                return(ConvertLogicalQueryNodeInDefaultWay(source, convertingContext));

            default:
                throw new ArgumentOutOfRangeException(nameof(source.Kind), source.Kind, null);
            }
        }
Example #17
0
        /// <inheritdoc/>
        protected override void OnRun()
        {
#if DEBUG
            //Log($"_state = {_state}");
            //Log($"_isGroup = {_isGroup}");
            //Log($"_currToken = {_currToken}");
            //Log($"Result = {Result}");
            //Log($"_nodePoint = {_nodePoint}");
#endif

            if (_terminatingTokenKindList.Contains(_currToken.TokenKind))
            {
                _context.Recovery(_currToken);
                Exit();
                return;
            }

            switch (_state)
            {
            case State.Init:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.LogicalVar:
                case TokenKind.Identifier:
                    ProcessWord();
                    break;

                case TokenKind.Entity:
                {
                    var name = NameHelper.CreateName(_currToken.Content);

#if DEBUG
                    //Log($"name = {name}");
#endif

                    var node = new LogicalQueryNode();
                    node.Kind = KindOfLogicalQueryNode.Entity;
                    node.Name = name;

                    var intermediateNode = new IntermediateAstNode(node);

                    AstNodesLinker.SetNode(intermediateNode, _nodePoint);

                    _state = State.GotEntity;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.WaitForPredicateParameter:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Entity:
                case TokenKind.LogicalVar:
                {
                    _context.Recovery(_currToken);

                    var parser = new LogicalExpressionParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif

                    _lastLogicalQueryNode.ParamsList.Add(parser.Result);

                    _state = State.GotPredicateParameter;
                }
                break;

                case TokenKind.Word:
                case TokenKind.Identifier:
                {
                    switch (_currToken.KeyWordTokenKind)
                    {
                    case KeyWordTokenKind.Null:
                    {
                        _context.Recovery(_currToken);

                        var parser = new NullParser(_context);
                        parser.Run();

#if DEBUG
                        //Log($"parser.Result = {parser.Result}");
#endif

                        var node = new LogicalQueryNode();
                        node.Kind  = KindOfLogicalQueryNode.Value;
                        node.Value = parser.Result;

                        _lastLogicalQueryNode.ParamsList.Add(node);

                        _state = State.GotPredicateParameter;
                    }
                    break;

                    default:
                    {
                        var nextToken = _context.GetToken();

                        var terminatingTokenKindList = new List <TokenKind>()
                        {
                            TokenKind.CloseRoundBracket
                        };

                        if (nextToken.TokenKind != TokenKind.OpenRoundBracket)
                        {
                            terminatingTokenKindList.Add(TokenKind.Comma);
                        }

#if DEBUG
                        //if (_currToken.Content == "distance")
                        //{
                        //throw new NotImplementedException();
                        //}
#endif

#if DEBUG
                        //Log($"nextToken = {nextToken}");
#endif

                        _context.Recovery(_currToken);
                        _context.Recovery(nextToken);


                        var parser = new LogicalExpressionParser(_context, terminatingTokenKindList);
                        parser.Run();

#if DEBUG
                        //Log($"parser.Result = {parser.Result}");
                        //if (_currToken.Content == "distance")
                        //{
                        //    //throw new NotImplementedException();
                        //}
#endif

                        _lastLogicalQueryNode.ParamsList.Add(parser.Result);

                        _state = State.GotPredicateParameter;
                    }
                    break;
                    }
                }
                break;


                case TokenKind.Number:
                {
                    _context.Recovery(_currToken);

                    var parser = new NumberParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif
                    var node = new LogicalQueryNode();
                    node.Kind  = KindOfLogicalQueryNode.Value;
                    node.Value = parser.Result;

                    _lastLogicalQueryNode.ParamsList.Add(node);

                    _state = State.GotPredicateParameter;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotPredicateParameter:
                switch (_currToken.TokenKind)
                {
                case TokenKind.CloseRoundBracket:
                    _state = State.GotPredicate;
                    break;

                case TokenKind.Comma:
                    _state = State.WaitForPredicateParameter;
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotPredicate:
                switch (_currToken.TokenKind)
                {
                case TokenKind.And:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.And);
                    break;

                case TokenKind.Or:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Or);
                    break;

                case TokenKind.Comma:
                    _state = State.WaitForPredicateParameter;
                    break;

                case TokenKind.CloseRoundBracket:
                    if (_isGroup)
                    {
                        Exit();
                        break;
                    }
                    throw new UnexpectedTokenException(_currToken);

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotConcept:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                    switch (_currToken.KeyWordTokenKind)
                    {
                    case KeyWordTokenKind.Is:
                        ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Is);
                        break;

                    default:
                        throw new UnexpectedTokenException(_currToken);
                    }
                    break;

                case TokenKind.More:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.More);
                    break;

                case TokenKind.MoreOrEqual:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.MoreOrEqual);
                    break;

                case TokenKind.Less:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Less);
                    break;

                case TokenKind.LessOrEqual:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.LessOrEqual);
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotOperator:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.Identifier:
                    ProcessWord();
                    break;

                case TokenKind.OpenRoundBracket:
                    ProcessGroup();
                    break;

                case TokenKind.Not:
                    ProcessUnaryOperator(KindOfOperatorOfLogicalQueryNode.Not);
                    break;

                case TokenKind.LogicalVar:
                {
                    var value = NameHelper.CreateName(_currToken.Content);

                    ProcessConceptOrQuestionVar(value);
                }
                break;

                case TokenKind.Number:
                {
                    _context.Recovery(_currToken);

                    var parser = new NumberParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif
                    var node = new LogicalQueryNode();
                    node.Kind  = KindOfLogicalQueryNode.Value;
                    node.Value = parser.Result;

                    var intermediateNode = new IntermediateAstNode(node);

                    AstNodesLinker.SetNode(intermediateNode, _nodePoint);

                    _state = State.GotConcept;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotFuzzyLogicNonNumericSequenceItem:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.Identifier:
                {
                    var value = NameHelper.CreateName(_currToken.Content);

#if DEBUG
                    //Log($"value = {value}");
#endif

                    _fuzzyLogicNonNumericSequenceValue.AddIdentifier(value);

#if DEBUG
                    //Log($"_fuzzyLogicNonNumericSequenceValue = {_fuzzyLogicNonNumericSequenceValue}");
#endif
                }
                break;

                case TokenKind.More:
                case TokenKind.MoreOrEqual:
                case TokenKind.Less:
                case TokenKind.LessOrEqual:
                    _context.Recovery(_currToken);
                    _state = State.GotConcept;
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(_state), _state, null);
            }
        }