Пример #1
0
        private List <SemanticExceptionItem> CheckObjectSelectionPathInEnvironment(Class selectedClass, IQueryTree osp, Environment ev, out Class cls)
        {
            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();

            cls = selectedClass;

            if (osp != null && cls != null)
            {
                string attributeName = osp.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenValue;
                if (selectedClass.fields.Where(p => p.name == attributeName).Count() == 0)
                {
                    exceptions.Add(new SemanticExceptionItem(osp.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenName, string.Format("Unknown attribute name '{0}'", attributeName), osp.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenLine, osp.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenCol));
                }
                else
                {
                    string newClass = cls.fields.Where(p => p.name == attributeName).Single().type;
                    cls = GetClassFromEnvironment(newClass, ev);

                    if (cls == null)
                    {
                        exceptions.Add(new SemanticExceptionItem(osp.TokenName, string.Format("Unknown type {0}", newClass), osp.TokenLine, osp.TokenCol));
                    }
                    else
                    {
                        IQueryTree tail = osp.ProductionsList.Where(p => p.TokenName == "Path_Tail").Single();
                        exceptions.AddRange(CheckPathTailInEnvironment(cls, tail, ev, out cls));
                    }
                }
            }

            return(exceptions);
        }
Пример #2
0
        protected string GetNumber(IQueryTree numerQueryTree)
        {
            if (numerQueryTree.ProductionsList == null)
            {
                return(numerQueryTree.TokenValue);
            }

            var sb = new StringBuilder();

            numerQueryTree.ProductionsList.ToList().ForEach(q => sb.Append(GetNumber(q)));
            return(sb.ToString());
        }
Пример #3
0
        protected object GetLiteral(IQueryTree literalQueryTree)
        {
            switch (literalQueryTree.TokenName)
            {
            case TokenName.LITERAL:
                return(GetLiteral(literalQueryTree.ProductionsList.Single()));

            case TokenName.NUMBER:
                var IsFloat = literalQueryTree.ProductionsList.Any(q => q.TokenName == TokenName.FLOAT_PRESICION);
                var number  = GetNumber(literalQueryTree);
                return(IsFloat
                        ? (object)Double.Parse(number, NumberStyles.Number, CultureInfo.InvariantCulture)
                        : Int32.Parse(number, NumberStyles.Number, CultureInfo.InvariantCulture));

            case TokenName.STRING_VALUE:
                return(literalQueryTree.TokenValue);

            case TokenName.BOOL_VALUE:
                if (literalQueryTree.TokenValue.ToUpper() == "TRUE")
                {
                    return(true);
                }
                else if (literalQueryTree.TokenValue.ToUpper() == "FALSE")
                {
                    return(false);
                }
                else
                {
                    throw new ApplicationException("Unknown BOOL_VALUE token value: " + literalQueryTree.TokenValue);
                }

            case TokenName.NULL_VALUE:
                return(null);

            default:
                throw new ApplicationException("Unknown LITERAL token: " + literalQueryTree.TokenName);
            }
        }
Пример #4
0
        internal override DTOQueryResult Execute(IQueryTree queryTree)
        {
            switch (queryTree.TokenName)
            {
            case TokenName.STATEMENT:
                DTOQueryResult qr     = null;
                DTOQueryResult lastQr = null;
                foreach (var subTree in queryTree.ProductionsList.Where(p => p.TokenName != TokenName.SEMICOLON))
                {
                    var res = Execute(subTree);
                    if (qr == null)
                    {
                        lastQr = qr = res;
                    }
                    else
                    {
                        lastQr.NextResult = res;
                        lastQr            = lastQr.NextDTOResult;
                    }
                }
                return(qr);

            case TokenName.SYSTEM_OPERATION:
                return(Execute(queryTree.ProductionsList[0]));

            case TokenName.GET_SYSTEM_INFO:
                var sb            = new StringBuilder();
                var sw            = new StringWriter(sb);
                var xmlSerializer = new XmlSerializer(typeof(SystemInfo));
                xmlSerializer.Serialize(sw, _systemInfo);
                return(new DTOQueryResult()
                {
                    NextResult = null,
                    QueryResults = null,
                    QueryResultType = ResultType.SystemInfo,
                    StringOutput = sb.ToString()
                });

            case TokenName.CREATE_DATABASE:
                try
                {
                    var dbName = queryTree.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;
                    var did    = _storage.CreateDatabase(new DatabaseParameters(dbName, _settingsManager));
                    _log(string.Format("new database created as {0}", did), MessageLevel.Info);
                    var sb2            = new StringBuilder();
                    var sw2            = new StringWriter(sb2);
                    var xmlSerializer2 = new XmlSerializer(typeof(DatabaseInfo));
                    var db             = _storage.GetDatabase(did);
                    xmlSerializer2.Serialize(sw2,
                                             new DatabaseInfo()
                    {
                        Name    = db.Name,
                        Classes = db.Schema.Classes.Select(c => new DatabaseClass
                        {
                            Name      = c.Value.Name,
                            Interface = c.Value.Interface,
                            Fields    =
                                db.Schema.Properties.Values.Where(p => p.ParentClassId == c.Value.ClassId.Id)
                                .Select(f => new Field {
                                Name = f.Name, Type = f.Type
                            })
                                .ToList(),
                            Methods =
                                db.Schema.Methods.ContainsKey(c.Key)
                                            ? db.Schema.Methods[c.Key]
                                            : new List <string>()
                        }).ToList()
                    });
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.DatabaseInfo,
                        StringOutput = sb2.ToString()
                    });
                }
                catch (Exception ex)
                {
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.StringResult,
                        StringOutput = "Error during database creation: " + ex.ToString()
                    });
                }

            case TokenName.CLASS_DECLARATION:
            case TokenName.INTERFACE_DECLARATION:
                if (_database == null)
                {
                    _log("Database is required!", MessageLevel.Error);
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.StringResult,
                        StringOutput = "Error ocured while class creation"
                    });
                }
                bool isClass      = queryTree.TokenName == TokenName.CLASS_DECLARATION;
                var  desc         = isClass ? "Class" : "Interface";
                var  newClassName =
                    (queryTree.TokenName == TokenName.CLASS_DECLARATION
                            ? queryTree.ProductionsList.Single(t => t.TokenName == TokenName.CLASS_NAME)
                            : queryTree)
                    .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;

                if (_database.Schema.Classes.Any(c => c.Value.Name.ToUpper() == newClassName.ToUpper()))
                {
                    return new DTOQueryResult()
                           {
                               NextResult      = null,
                               QueryResults    = null,
                               QueryResultType = ResultType.StringResult,
                               StringOutput    = desc + " with name: " + newClassName + " arleady exists!"
                           }
                }
                ;
                var classId = new ClassId
                {
                    Name = newClassName,
                    Id   = (_database.Schema.Classes.Max(d => (long?)d.Key.Id) ?? 0) + 1
                };
                var classDef = new Class
                {
                    ClassId = classId,
                    Name    = newClassName
                };
                foreach (var attr in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.ATTRIBUTE_DEC_STM)
                         )
                {
                    var attrName = (isClass
                            ? attr.ProductionsList.Single(t => t.TokenName == TokenName.ATTRIBUTE_NAME)
                            : attr)
                                   .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;

                    var typeName =
                        attr.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE)
                        .ProductionsList.Single();
                    var propertyId = new PropertyId
                    {
                        Id            = 1 + _database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0,
                        Name          = attrName,
                        ParentClassId = classId.Id
                    };
                    _database.Schema.Properties.TryAdd(propertyId, new Property
                    {
                        ParentClassId = classId.Id,
                        Name          = attrName,
                        PropertyId    = propertyId,
                        Type          = typeName.TokenValue,
                        IsValueType   = typeName.TokenName != TokenName.NAME
                    });
                }
                _database.Schema.Methods.TryAdd(classId, new List <string>());
                foreach (var meth in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.METHOD_DEC_STM))
                {
                    var methName = (isClass
                            ? meth.ProductionsList.Single(t => t.TokenName == TokenName.METHOD_NAME)
                            : meth)
                                   .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;
                    _database.Schema.Methods[classId].Add(methName);
                }
                foreach (var rel in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.RELATION_DEC_STM))
                {
                    var    attrName = rel.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;
                    string typeName =
                        rel.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE)
                        .ProductionsList.Single()
                        .TokenValue;
                    var propertyId = new PropertyId
                    {
                        Id            = 1 + (_database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0),
                        Name          = attrName,
                        ParentClassId = classId.Id
                    };
                    _database.Schema.Properties.TryAdd(propertyId, new Property
                    {
                        ParentClassId = classId.Id,
                        Name          = attrName,
                        PropertyId    = propertyId,
                        Type          = typeName,
                        IsValueType   = false
                    });
                }
                if (!_database.Schema.Classes.TryAdd(classId, classDef))
                {
                    _log("Could not define new " + desc.ToLower(), MessageLevel.Error);
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.StringResult,
                        StringOutput = "Error ocured while class creation"
                    });
                }
                _storage.SaveSchema(_database.Schema);
                _log("Defined new class: " + newClassName, MessageLevel.QueryExecution);
                return(new DTOQueryResult()
                {
                    NextResult = null,
                    QueryResults = null,
                    QueryResultType = ResultType.StringResult,
                    StringOutput = "New " + desc.ToLower() + ": " + newClassName + " created."
                });

            case TokenName.DROP:
                if (_database == null)
                {
                    _log("Database is required!", MessageLevel.Error);
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.StringResult,
                        StringOutput = "Error ocured while class droping"
                    });
                }

                var className = queryTree.ProductionsList.Single(t => t.TokenName == TokenName.CLASS_NAME)
                                .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue;

                Class classToDrop = GetClass(className);
                Class dropedClass;

                if (!_database.Schema.Classes.TryRemove(classToDrop.ClassId, out dropedClass))
                {
                    _log("Could not define new", MessageLevel.Error);
                    return(new DTOQueryResult()
                    {
                        NextResult = null,
                        QueryResults = null,
                        QueryResultType = ResultType.StringResult,
                        StringOutput = "Error ocured while class droping"
                    });
                }
                _storage.SaveSchema(_database.Schema);

                _log("Droped class: " + dropedClass.Name, MessageLevel.QueryExecution);
                return(new DTOQueryResult()
                {
                    NextResult = null,
                    QueryResults = null,
                    QueryResultType = ResultType.StringResult,
                    StringOutput = "Class:" + dropedClass.Name + " droped."
                });

            case TokenName.NEW_OBJECT:
                return(base.Execute(queryTree));

            default:
                //if (_doOnDataServers != null)
                //    _doOnDataServers(queryTree);
                return(base.Execute(queryTree));
            }
        }
Пример #5
0
        private List <SemanticExceptionItem> CheckClauseExpressionInEnvironment(Class selectedClass, IQueryTree clause, Environment ev)
        {
            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();
            Class cls = selectedClass;

            foreach (IQueryTree qt in clause.ProductionsList)
            {
                if (qt.TokenName == "Clause")
                {
                    foreach (IQueryTree q in qt.ProductionsList.Where(p => p.TokenValue == string.Empty))
                    {
                        if (q.TokenName == "Expression_Factor")
                        {
                            exceptions.AddRange(CheckExpressionFactorInEnvironment(cls, q, ev, out cls));
                        }
                        else if (q.TokenName == "Collection_Source")
                        {
                            exceptions.AddRange(analize(q.ProductionsList.Where(p => p.TokenName == "Get_Expression").Single(), ev));
                            exceptions.AddRange(CheckExpressionFactorInEnvironment(cls, q.ProductionsList.Where(p => p.TokenName == "Expression_Factor").Single(), ev, out cls));
                        }
                    }
                }
                else if (qt.TokenName == "Clause_Tail")
                {
                    exceptions.AddRange(CheckClauseExpressionInEnvironment(cls, qt.ProductionsList.Where(p => p.TokenName == "Clause_Expr").Single(), ev));
                }
            }

            return(exceptions);
        }
Пример #6
0
        private List <SemanticExceptionItem> CheckExpressionFactorInEnvironment(Class selectedClass, IQueryTree query, Environment ev, out Class cls)
        {
            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();

            cls = selectedClass;
            IQueryTree qt = query.ProductionsList.Where(p => p.TokenValue == string.Empty).Single();

            switch (qt.TokenName)
            {
            case "Expression_Atom":
                IQueryTree atom = qt.ProductionsList.First();
                switch (atom.TokenName)
                {
                case "NAME":
                    if (!CheckClassForField(cls, atom.TokenValue))
                    {
                        exceptions.Add(new SemanticExceptionItem(atom.TokenName, string.Format("Unknown field '{0}'", atom.TokenValue), atom.TokenLine, atom.TokenCol));
                    }
                    else
                    {
                        cls = GetClassFromEnvironment(selectedClass.fields.Where(p => p.name == atom.TokenValue).Single().type, ev);
                    }
                    break;

                case "Literal":
                    IQueryTree constant = atom.ProductionsList.First();
                    switch (constant.TokenName)
                    {
                    case "STRING_VALUE":
                        cls = GetClassFromEnvironment("String", ev);
                        break;

                    case "INTEGER_VALUE":
                        cls = GetClassFromEnvironment("Int", ev);
                        break;

                    case "FLOAT_VALUE":
                        cls = GetClassFromEnvironment("Float", ev);
                        break;

                    case "BOOL_VALUE":
                        cls = GetClassFromEnvironment("Bool", ev);
                        break;
                    }
                    break;

                default:
                    break;
                }
                break;

            case "Object_Selection_Path":
                exceptions.AddRange(CheckObjectSelectionPathInEnvironment(cls, qt, ev, out cls));
                break;

            case "Binary_Expr":
                foreach (IQueryTree q in qt.ProductionsList)
                {
                    if (q.TokenName == "Expression_Factor")
                    {
                        exceptions.AddRange(CheckExpressionFactorInEnvironment(cls, q, ev, out cls));
                    }
                    else if (q.TokenName == "Right_Expr")
                    {
                        exceptions.AddRange(CheckExpressionFactorInEnvironment(cls, q.ProductionsList.Where(p => p.TokenName == "Expression_Factor").Single(), ev, out cls));
                    }
                }
                break;

            default:
                break;
            }

            return(exceptions);
        }
Пример #7
0
        private List <SemanticExceptionItem> CheckPathTailInEnvironment(Class selectedClass, IQueryTree tail, Environment ev, out Class cls)
        {
            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();

            cls = selectedClass;

            var firstElem = tail.ProductionsList.First();

            switch (firstElem.TokenName)
            {
            case "NAME":
                if (selectedClass.fields.Where(p => p.name == tail.TokenValue).Count() != 1)
                {
                    exceptions.Add(new SemanticExceptionItem(tail.TokenName, string.Format("Unknown attribute name '{0}'", tail.TokenValue), tail.TokenLine, tail.TokenCol));
                }
                else
                {
                    cls = GetClassFromEnvironment(selectedClass.fields.Where(p => p.name == tail.TokenValue).Single().type, ev);
                }
                break;

            case "Executable_Method":
                string methodName = firstElem.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenValue;
                if (selectedClass.methods.Where(p => p.name == methodName).Count() == 0)
                {
                    exceptions.Add(new SemanticExceptionItem(firstElem.TokenName, string.Format("Unknown method name '{0}'", methodName), firstElem.ProductionsList.First().TokenLine, firstElem.ProductionsList.First().TokenCol));
                }

                /*
                 *  UWAGA: uproszczenie!
                 *      brak sprawdzania argumentów dla wywoływanych metod
                 *      jeżeli istnieje więcej, niż jedna metoda o identycznej nazwie, to uznaję, że zwracanym typem
                 *      przez jej wszystkie implmentacje jest typ, który wystąpi jako pierwszy w modelu
                 */
                else
                {
                    cls = GetClassFromEnvironment(selectedClass.methods.Where(p => p.name == methodName).First().returnedType, ev);
                    if (tail.ProductionsList.Where(p => p.TokenName == "Path_Tail").Count() == 1)
                    {
                        exceptions.AddRange(CheckPathTailInEnvironment(cls, tail.ProductionsList.Where(p => p.TokenName == "Path_Tail").Single(), ev, out cls));
                    }
                }
                break;

            case "Object_Selection_Path":
                exceptions.AddRange(CheckObjectSelectionPathInEnvironment(selectedClass, firstElem, ev, out cls));
                break;

            default:
                break;
            }

            return(exceptions);
        }
Пример #8
0
        private List <SemanticExceptionItem> CheckGetListItemsInEnvironment(Class selectedClass, IQueryTree qt, Environment ev)
        {
            if (selectedClass == null)
            {
                throw new Exception("Unexcpected operation!");
            }

            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();
            Class newClass = null;

            if (qt == null)
            {
                exceptions.Add(new SemanticExceptionItem(qt.TokenName, "Get list can not be empty", qt.TokenLine, qt.TokenCol));
            }
            else
            {
                IQueryTree gli = qt.ProductionsList.First();

                switch (gli.TokenName)
                {
                case "NAME":
                    if (selectedClass.fields.Where(p => p.name == gli.TokenValue).Count() != 1)
                    {
                        exceptions.Add(new SemanticExceptionItem(gli.TokenName, string.Format("Unknown attribute name '{0}'", gli.TokenValue), gli.TokenLine, gli.TokenCol));
                    }
                    else
                    {
                        string newCls = selectedClass.fields.Where(p => p.name == gli.TokenValue).Single().type;
                        newClass = GetClassFromEnvironment(newCls, ev);
                    }
                    break;

                case "Object_Selection_Path":
                    exceptions.AddRange(CheckObjectSelectionPathInEnvironment(selectedClass, gli, ev, out newClass));
                    break;

                case "Agregation_Function":
                    if (qt.ProductionsList[2].TokenName == "NAME")
                    {
                        if (selectedClass.fields.Where(p => p.name == qt.ProductionsList[2].TokenValue).Count() == 0)
                        {
                            exceptions.Add(new SemanticExceptionItem(qt.ProductionsList[2].TokenName, string.Format("Unknown attribute name '{0}'", qt.ProductionsList[2].TokenValue), qt.ProductionsList[2].TokenLine, qt.ProductionsList[2].TokenCol));
                        }
                        else
                        {
                            string newCls = selectedClass.fields.Where(p => p.name == gli.TokenValue).Single().type;
                            newClass = GetClassFromEnvironment(newCls, ev);
                        }
                    }
                    else if (qt.ProductionsList[2].TokenName == "Object_Selection_Path")
                    {
                        exceptions.AddRange(CheckObjectSelectionPathInEnvironment(selectedClass, qt.ProductionsList[2], ev, out newClass));
                    }
                    else
                    {
                        exceptions.Add(new SemanticExceptionItem(qt.ProductionsList[2].TokenName, "Unknown token", qt.ProductionsList[2].TokenLine, qt.ProductionsList[2].TokenCol));
                    }
                    break;

                case "MUL":
                    break;

                case "COUNT":
                    break;

                default:
                    break;
                }

                if (qt.ProductionsList.Where(p => p.TokenName == "Get_List_Item").Count() == 1)
                {
                    exceptions.AddRange(CheckGetListItemsInEnvironment(newClass, qt.ProductionsList.Where(p => p.TokenName == "Get_List_Item").Single(), ev));
                }
            }

            return(exceptions);
        }
Пример #9
0
        private List <SemanticExceptionItem> analize(IQueryTree queryTree, Environment environment)
        {
            List <SemanticExceptionItem> exceptions = new List <SemanticExceptionItem>();

            string      label;
            int         value;
            string      className;
            string      parentClassName;
            Environment innerSection;

            switch (queryTree.TokenName)
            {
            case "Target":
            case "TermBlockTail":
            case "Attributes_Block_Tail":
            case "Loop":
                exceptions.AddRange(analize(queryTree.ProductionsList.Single(), environment));
                break;

            case "Block_Expr":
            case "Expression":
                if (queryTree.ProductionsList.Where(p => p.TokenValue == string.Empty).Count() == 1)
                {
                    exceptions.AddRange(analize(queryTree.ProductionsList.Where(p => p.TokenValue == string.Empty).Single(), environment));
                }
                break;

            case "Term_Block":
            case "Expr":
            case "Get_Expression":
                foreach (QueryTree qt in queryTree.ProductionsList)
                {
                    exceptions.AddRange(analize(qt, environment));
                }
                break;

            case "COMMIT":
            case "ROLLBACK":
            case "Transaction_Expr":
                break;

            case "Goto_Expr":
                label = queryTree.ProductionsList.Where(p => p.TokenName == "Label_Name").Single().ProductionsList.Single().TokenValue;
                if (!CheckEnvironmentForLabel(label, environment))
                {
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, string.Format("Unknown label '{0}'", label), queryTree.TokenLine, queryTree.TokenCol));
                }
                break;

            case "Label_Statement":
                label = queryTree.ProductionsList.Where(p => p.TokenName == "Label_Name").Single().ProductionsList.Single().TokenValue;
                IQueryTree labelQuery = queryTree.ProductionsList.Where(p => p.TokenName == "Label_Name").Single().ProductionsList.Single();
                switch (environment.section)
                {
                case Section.Class_Declaration:
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, "Label declaration is forbidden", queryTree.TokenLine, queryTree.TokenCol));
                    break;

                case Section.Method_Declaration:
                case Section.For_Statement:
                case Section.Foreach_Statement:
                case Section.If_Statement:
                case Section.Target:
                case Section.Try_Catch:
                case Section.While_Statement:
                case Section.Parallel:
                    if (environment.labelList.Contains(label))
                    {
                        exceptions.Add(new SemanticExceptionItem(labelQuery.TokenName, string.Format("Duplicated label name '{0}'", label), labelQuery.TokenLine, labelQuery.TokenCol));
                    }
                    else
                    {
                        environment.labelList.Add(label);
                    }
                    break;

                default:
                    throw new Exception(string.Format("Unexpected section '{0}'", environment.section.ToString()));
                }
                break;

            case "Parallel_Statement":
                if (Int32.Parse(queryTree.ProductionsList.Where(p => p.TokenName == "INTEGER_VALUE").Single().TokenValue) <= 0)
                {
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, "Parallel number must be positive", queryTree.TokenLine, queryTree.TokenCol));
                }
                innerSection = new Environment {
                    parent    = environment,
                    section   = Section.Parallel,
                    labelList = new List <string>(),
                    fields    = new Dictionary <string, string>(),
                    classList = new List <Class>()
                };
                exceptions.AddRange(analize(queryTree.ProductionsList.Where(p => p.TokenName == "Term_Block").Single(), innerSection));
                break;

            case "Class_Declaration":
                exceptions.AddRange(analize(queryTree.ProductionsList.Where(p => p.TokenName == "Class_Header").Single(), environment));
                break;

            case "Class_Header":
                className = queryTree.ProductionsList.Where(p => p.TokenName == "NAME").Single().ProductionsList.Single().TokenValue;
                if (CheckEnvironmentForClass(className, environment))
                {
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, string.Format("Duplicated class name '{0}'", className), queryTree.TokenLine, queryTree.TokenCol));
                }
                if (queryTree.ProductionsList.Where(p => p.TokenName == "Base_Class").Count() > 0)
                {
                    parentClassName = queryTree.ProductionsList.Where(p => p.TokenName == "Base_Class").Single().ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenValue;
                    if (!CheckEnvironmentForClass(parentClassName, environment))
                    {
                        exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, string.Format("Unknown type '{0}'", parentClassName), queryTree.TokenLine, queryTree.TokenCol));
                    }
                }
                break;

            case "Get_Factor":
                foreach (QueryTree qt in queryTree.ProductionsList.Where(p => (p.ProductionsList == null ? 0 : p.ProductionsList.Count) > 0))
                {
                    exceptions.AddRange(analize(qt, environment));
                }
                break;

            case "Element_Cardinality":
                if (Int32.Parse(queryTree.ProductionsList.Where(p => p.TokenName == "Cardinality_Expr").Single().ProductionsList.Where(p => p.TokenName == "INTEGER_VALUE").Single().TokenValue) <= 0)
                {
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, "Number of objects must be positive", queryTree.TokenLine, queryTree.TokenCol));
                }
                break;

            case "Get_Statement":
                if (queryTree.ProductionsList.Where(p => p.TokenName == "Element_Cardinality").Count() > 0)
                {
                    exceptions.AddRange(analize(queryTree.ProductionsList.Where(p => p.TokenName == "Element_Cardinality").Single(), environment));
                }

                className = queryTree.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenValue;
                if (!CheckEnvironmentForClass(className, environment))
                {
                    exceptions.Add(new SemanticExceptionItem(queryTree.TokenName, string.Format("Unknown class name '{0}'", className), queryTree.TokenLine, queryTree.TokenCol));
                }
                else
                {
                    Class selectedClass = GetClassFromEnvironment(className, environment);
                    if (selectedClass != null)
                    {
                        IQueryTree getListItems = queryTree.ProductionsList.Where(p => p.TokenName == "Get_List").Single().ProductionsList.Where(p => p.TokenName == "Get_List_Item").Single();
                        exceptions.AddRange(CheckGetListItemsInEnvironment(selectedClass, getListItems, environment));
                        if (queryTree.ProductionsList.Where(p => p.TokenName == "Where_Clause").Count() > 0)
                        {
                            exceptions.AddRange(CheckClauseExpressionInEnvironment(selectedClass, queryTree.ProductionsList.Where(p => p.TokenName == "Where_Clause").Single().ProductionsList.Where(p => p.TokenName == "Clause_Expr").Single(), environment));
                        }
                    }
                }
                break;

            case "Foreach_Statement":
                innerSection = new Environment
                {
                    parent    = environment,
                    section   = Section.Foreach_Statement,
                    labelList = new List <string>(),
                    fields    = new Dictionary <string, string>(),
                    classList = new List <Class>()
                };
                IQueryTree forachHead = queryTree.ProductionsList.Where(p => p.TokenName == "Foreach_Head").Single();
                if (CheckEnvironmentForClass(forachHead.ProductionsList.Where(p => p.TokenName == "Type").Single().ProductionsList.First().TokenValue, environment))
                {
                    string typeName = forachHead.ProductionsList.Where(p => p.TokenName == "Type").Single().ProductionsList.First().TokenValue;
                    string classN   = forachHead.ProductionsList.Where(p => p.TokenName == "NAME").Single().TokenValue;
                    innerSection.fields.Add(classN, typeName);
                }

                IQueryTree collSource = forachHead.ProductionsList.Where(p => p.TokenName == "Collection_Source").Single();
                foreach (QueryTree q in collSource.ProductionsList)
                {
                    if (q.TokenName == "Get_Expression")
                    {
                        exceptions.AddRange(analize(q, environment));
                    }
                    else if (q.TokenName == "Expression_Factor")
                    {
                        IQueryTree exItem = q.ProductionsList.Where(p => p.TokenValue != string.Empty).First();
                        switch (exItem.TokenName)
                        {
                        case "Expression_Atom":
                            if (!CheckEnvironmentForFields(exItem.ProductionsList.First().TokenValue, environment))
                            {
                                exceptions.Add(new SemanticExceptionItem(exItem.ProductionsList.First().TokenName, String.Format("Incorrect data collection: {}", exItem.ProductionsList.First().TokenValue), exItem.ProductionsList.First().TokenLine, exItem.ProductionsList.First().TokenCol));
                            }
                            break;

                        case "Object_Selection_Path":
                            Class returnedClass;
                            if (CheckEnvironmentForClass(exItem.ProductionsList.First().TokenValue, environment))
                            {
                                Class newClass = GetClassFromEnvironment(exItem.ProductionsList.First().TokenValue, environment);
                                exceptions.AddRange(CheckPathTailInEnvironment(newClass, exItem.ProductionsList.Where(p => p.TokenName == "Path_Tail").Single(), environment, out returnedClass));
                            }
                            else if (CheckEnvironmentForFields(exItem.ProductionsList.First().TokenValue, environment))
                            {
                                string fieldType = environment.fields[exItem.ProductionsList.First().TokenValue];
                                Class  newClass  = GetClassFromEnvironment(fieldType, environment);
                                exceptions.AddRange(CheckPathTailInEnvironment(newClass, exItem.ProductionsList.Where(p => p.TokenName == "Path_Tail").Single(), environment, out returnedClass));
                            }
                            break;

                        case "Binary_Expr":
                            exceptions.Add(new SemanticExceptionItem(exItem.ProductionsList.First().TokenName, String.Format("Incorrect data collection: {}", exItem.ProductionsList.First().TokenValue), q.ProductionsList.First().TokenLine, q.ProductionsList.First().TokenCol + 1));
                            break;
                        }
                    }
                }

                if (queryTree.ProductionsList.Where(p => p.TokenName == "Term_Block").Count() == 1)
                {
                    IQueryTree term = queryTree.ProductionsList.Where(p => p.TokenName == "Term_Block").Single();
                    exceptions.AddRange(analize(term, innerSection));
                }
                break;


            default:
                break;
            }

            return(exceptions);
        }
Пример #10
0
 private string GetClassName(IQueryTree queryTree)
 {
     return(queryTree.ProductionsList.Single().TokenValue);
 }
Пример #11
0
        internal virtual DTOQueryResult Execute(IQueryTree queryTree)
        {
            switch (queryTree.TokenName)
            {
            case TokenName.NEW_OBJECT:
                var className   = GetClassName(queryTree.ProductionsList.Single(q => q.TokenName == TokenName.CLASS_NAME));
                var objectClass = GetClass(className);
                if (objectClass == null)
                {
                    return new DTOQueryResult
                           {
                               NextResult      = null,
                               QueryResultType = ResultType.StringResult,
                               StringOutput    = "Unknown class: " + className
                           }
                }
                ;
                var propeteries =
                    _database.Schema.Properties.Select(p => p.Value)
                    .Where(p => p.ParentClassId == objectClass.ClassId.Id)
                    .ToList();
                var oid     = new Oid(Guid.NewGuid(), _database.DatabaseId.Dli);
                var toStore = new Storable {
                    Oid = oid
                };

                var attr =
                    queryTree.ProductionsList.SingleOrDefault(
                        q => q.TokenName == TokenName.OBJECT_INITIALIZATION_ATTRIBUTES_LIST);
                if (attr != null)
                {
                    foreach (
                        var attrToSet in
                        attr.ProductionsList.Where(q => q.TokenName == TokenName.OBJECT_INITIALIZATION_ELEMENT))
                    {
                        var field =
                            attrToSet.ProductionsList.Single(q => q.TokenName == TokenName.ATTRIBUTE_NAME)
                            .ProductionsList.Single()
                            .TokenValue;
                        var property = propeteries.SingleOrDefault(p => p.Name == field);
                        if (property == null)
                        {
                            return new DTOQueryResult
                                   {
                                       NextResult      = null,
                                       QueryResultType = ResultType.StringResult,
                                       StringOutput    = "Unknown field: " + field
                                   }
                        }
                        ;
                        var literal = attrToSet.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.LITERAL);
                        if (literal != null)
                        {
                            toStore.Properties.Add(property, GetLiteral(literal));
                        }
                    }
                }

                _storage.Save(_database.DatabaseId, toStore);
                _log("new object saved with id: " + oid, MessageLevel.QueryExecution);
                return(new DTOQueryResult
                {
                    NextResult = null,
                    QueryResultType = ResultType.StringResult,
                    StringOutput = "new object saved with id: " + oid
                });

            case TokenName.GET:
                var get_stm = queryTree.ProductionsList.Where(q => q.TokenName == TokenName.GET_STM);
                if (get_stm.Count() == 1)
                {
                    return(Execute(get_stm.Single()));
                }
                goto default;

            case TokenName.GET_STM:
                var classNameToGet =
                    GetClassName(
                        queryTree.ProductionsList.Single(q => q.TokenName == TokenName.GET_HEADER)
                        .ProductionsList.Single(q => q.TokenName == TokenName.CLASS_NAME));
                var classToGet = GetClass(classNameToGet);
                if (classToGet == null)
                {
                    return new DTOQueryResult
                           {
                               NextResult      = null,
                               QueryResultType = ResultType.StringResult,
                               StringOutput    = "Unknown class: " + classNameToGet
                           }
                }
                ;
                var objs = _storage.GetAll(_database.DatabaseId);
                objs = objs.Where(s => s.Properties.All(p => p.Key.ParentClassId == classToGet.ClassId.Id));
                var seachCriteria = queryTree.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.WHERE_CLAUSE);
                if (seachCriteria != null)
                {
                    var wc = BuildWhereCriteria(seachCriteria);

                    objs = objs.Where(s => wc.All(f => f(s)));
                }

                var deref = queryTree.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.K_DEREF);
                if (deref != null)
                {
                    return new DTOQueryResult
                           {
                               NextResult      = null,
                               QueryResultType = ResultType.Default,
                               StringOutput    = ToXml(objs, classToGet).OuterXml
                           }
                }
                ;

                return(new DTOQueryResult
                {
                    NextResult = null,
                    QueryResultType = ResultType.ReferencesOnly,
                    QueryResults = objs.Select(o => o.Oid).ToList()
                });

            default:
                return(new DTOQueryResult
                {
                    NextResult = null,
                    QueryResultType = ResultType.StringResult,
                    StringOutput = "Your query is correct. The execution is not ready yet"
                });
            }
        }
Пример #12
0
        private List <Func <IStorable, bool> > BuildWhereCriteria(IQueryTree queryTree)
        {
            switch (queryTree.TokenName)
            {
            case TokenName.WHERE_CLAUSE:
                var ret = new List <Func <IStorable, bool> >();
                foreach (var subTree in queryTree.ProductionsList)
                {
                    BuildWhereCriteria(subTree).ForEach(ret.Add);
                }
                return(ret);

            case TokenName.AND:
            case TokenName.OR:
            case TokenName.K_WHERE:
                return(new List <Func <IStorable, bool> >());

            case TokenName.AND_OR_CLAUSE:
            case TokenName.CLAUSE:
                var ret2 = new List <Func <IStorable, bool> >();
                foreach (var subTree in queryTree.ProductionsList)
                {
                    BuildWhereCriteria(subTree).ForEach(ret2.Add);
                }
                return(ret2);

            case TokenName.WHERE_OPERATION:
                var wv =
                    queryTree.ProductionsList.Single(q => q.TokenName == TokenName.WHERE_VALUE);
                var leftLiteral = wv.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.LITERAL);
                var leftField   = wv.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.NAME);
                if (leftLiteral == null & leftField == null)
                {
                    return(new List <Func <IStorable, bool> >()); //TODO!
                }
                var wt =
                    queryTree.ProductionsList.Single(q => q.TokenName == TokenName.WHERE_TAIL);
                var wo = wt.ProductionsList.Single(q => q.TokenName == TokenName.WHERE_OPERATOR);
                if (wo.ProductionsList.Count > 1)
                {
                    throw new ApplicationException(
                              "No support for other tokens then IS_NULL, IS_NOT_NULL and COMPARISON_OPERATOR");
                }
                var param = Expression.Parameter(typeof(IStorable));
                if (wo.ProductionsList.Single().TokenName == TokenName.IS_NULL)
                {
                    if (leftLiteral != null)
                    {
                        var left   = Expression.Constant(GetLiteral(leftLiteral));
                        var clasue = Expression.Equal(left, Expression.Constant(null));

                        return
                            (new List <Func <IStorable, bool> >(new[]
                                                                { Expression.Lambda <Func <IStorable, bool> >(clasue, param).Compile() }));
                    }
                    else
                    {
                        return(new List <Func <IStorable, bool> >(new Func <IStorable, bool>[]
                        {
                            storable =>
                            {
                                var f = storable.Properties.Where(p => p.Key.Name == leftField.TokenValue);
                                return !f.Any() ||
                                f.Single().Value == null;
                            }
                        }));
                    }
                }
                if (wo.ProductionsList.Single().TokenName == TokenName.IS_NOT_NULL)
                {
                    if (leftLiteral != null)
                    {
                        var left   = Expression.Constant(GetLiteral(leftLiteral));
                        var clasue = Expression.NotEqual(left, Expression.Constant(null));

                        return
                            (new List <Func <IStorable, bool> >(new[]
                                                                { Expression.Lambda <Func <IStorable, bool> >(clasue, param).Compile() }));
                    }
                    else
                    {
                        return(new List <Func <IStorable, bool> >(new Func <IStorable, bool>[]
                        {
                            storable =>
                            {
                                var f = storable.Properties.Where(p => p.Key.Name == leftField.TokenValue);
                                return f.Count() == 1 &&
                                f.Single().Value != null;
                            }
                        }));
                    }
                }
                if (wo.ProductionsList.Single().TokenName == TokenName.COMPARISON_OPERATOR)
                {
                    var rightwv      = wt.ProductionsList.Single(q => q.TokenName == TokenName.WHERE_VALUE);
                    var rightLiteral = rightwv.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.LITERAL);
                    var rightField   = rightwv.ProductionsList.SingleOrDefault(q => q.TokenName == TokenName.NAME);

                    if (leftLiteral != null && rightLiteral != null)
                    {
                        var left   = Expression.Constant(GetLiteral(leftLiteral));
                        var right  = Expression.Constant(GetLiteral(rightLiteral));
                        var clasue = GetComparationExpression(wo.ProductionsList.Single().ProductionsList.Single().TokenName, left,
                                                              right);

                        return
                            (new List <Func <IStorable, bool> >(new[]
                                                                { Expression.Lambda <Func <IStorable, bool> >(clasue, param).Compile() }));
                    }
                    else
                    {
                        return(new List <Func <IStorable, bool> >(new Func <IStorable, bool>[]
                        {
                            storable =>
                            {
                                Expression left;
                                Expression right;
                                if (leftField != null)
                                {
                                    var f =
                                        storable.Properties.Where(p => p.Key.Name == leftField.TokenValue);
                                    if (!f.Any())
                                    {
                                        return false;
                                    }
                                    var prop = f.Single();
                                    left = Expression.Constant(prop.Value, prop.Key.DotNetType);
                                }
                                else
                                {
                                    left = Expression.Constant(GetLiteral(leftLiteral));
                                }
                                if (rightField != null)
                                {
                                    var f =
                                        storable.Properties.Where(p => p.Key.Name == rightField.TokenValue);
                                    if (!f.Any())
                                    {
                                        return false;
                                    }
                                    var prop = f.Single();
                                    right = Expression.Constant(prop.Value, prop.Key.DotNetType);
                                }
                                else
                                {
                                    right = Expression.Constant(GetLiteral(rightLiteral));
                                }
                                var clasue = GetComparationExpression(
                                    wo.ProductionsList.Single().ProductionsList.Single().TokenName, left, right);

                                return
                                Expression.Lambda <Func <IStorable, bool> >(clasue, param)
                                .Compile()(storable);
                            }
                        }));
                    }
                }
                else
                {
                    throw new ApplicationException(
                              "Unknown WHERE_OPERATION token: " + wo.ProductionsList.Single().TokenName);
                }

            default:
                throw new ApplicationException("Unknown WHERE_CLAUSE token: " + queryTree.TokenName);
            }
        }