コード例 #1
0
        public override IType check(Context context)
        {
            CategoryDeclaration cd = context.getRegisteredDeclaration <CategoryDeclaration>(this.type.GetTypeName());

            if (cd == null)
            {
                throw new SyntaxError("Unknown category " + this.type.GetTypeName());
            }
            checkFirstHomonym(context, cd);
            cd.checkConstructorContext(context);
            if (copyFrom != null)
            {
                IType cft = copyFrom.check(context);
                if (!(cft is CategoryType) && (cft != DocumentType.Instance))
                {
                    throw new SyntaxError("Cannot copy from " + cft.GetTypeName());
                }
            }
            if (arguments != null)
            {
                foreach (Argument argument in arguments)
                {
                    if (!cd.hasAttribute(context, argument.GetName()))
                    {
                        throw new SyntaxError("\"" + argument.GetName() +
                                              "\" is not an attribute of " + type.GetTypeName());
                    }
                    argument.check(context);
                }
            }
            return(((CategoryType)cd.GetIType(context)).AsMutable(context, type.Mutable));
        }
コード例 #2
0
        public override void ToDialect(CodeWriter writer)
        {
            Context             context = writer.getContext();
            CategoryDeclaration cd      = context.getRegisteredDeclaration <CategoryDeclaration>(this.type.GetTypeName());

            if (cd == null)
            {
                throw new SyntaxError("Unknown category " + this.type.GetTypeName());
            }
            checkFirstHomonym(context, cd);
            switch (writer.getDialect())
            {
            case Dialect.E:
                ToEDialect(writer);
                break;

            case Dialect.O:
                ToODialect(writer);
                break;

            case Dialect.M:
                ToMDialect(writer);
                break;
            }
        }
コード例 #3
0
        public IType inferCommonCategoryType(Context context, CategoryType type1, CategoryType type2, bool trySwap)
        {
            CategoryDeclaration decl1 = context.getRegisteredDeclaration <CategoryDeclaration>(type1.GetTypeName());

            if (decl1.getDerivedFrom() != null)
            {
                foreach (String name in decl1.getDerivedFrom())
                {
                    CategoryType parentType = new CategoryType(name);
                    if (parentType.isAssignableFrom(context, type2))
                    {
                        return(parentType);
                    }
                }
                // climb up the tree
                foreach (String name in decl1.getDerivedFrom())
                {
                    CategoryType parentType = new CategoryType(name);
                    IType        commonType = inferCommonBaseType(context, parentType, type2);
                    if (commonType != null)
                    {
                        return(commonType);
                    }
                }
            }
            if (trySwap)
            {
                return(inferCommonCategoryType(context, type2, type1, false));
            }
            else
            {
                return(null);
            }
        }
コード例 #4
0
 public IType checkAttribute(Context context, CategoryDeclaration decl, String name)
 {
     if (decl.Storable && "dbId" == name)
     {
         return(AnyType.Instance);
     }
     else if (decl.hasAttribute(context, name))
     {
         AttributeDeclaration ad = context.getRegisteredDeclaration <AttributeDeclaration>(name);
         if (ad == null)
         {
             throw new SyntaxError("Unknown atttribute:" + name);
         }
         else
         {
             return(ad.GetIType(context));
         }
     }
     else if ("text" == name)
     {
         return(TextType.Instance);
     }
     else if (decl.hasMethod(context, name))
     {
         IMethodDeclaration method = decl.getMemberMethods(context, name).GetFirst();
         return(new MethodType(method));
     }
     else
     {
         throw new SyntaxError("No attribute:" + name + " in category:" + GetTypeName());
     }
 }
コード例 #5
0
        public IInstance newInstance(Context context, IStored stored)
        {
            CategoryDeclaration decl = context.getRegisteredDeclaration <CategoryDeclaration>(this.GetTypeName());
            IInstance           inst = decl.newInstance(context, stored);

            inst.setMutable(this.Mutable);
            return(inst);
        }
コード例 #6
0
        public Context newInstanceContext(IInstance instance, bool isChild)
        {
            InstanceContext     context = initInstanceContext(new InstanceContext(instance), isChild);
            CategoryDeclaration decl    = context.getDeclaration();

            decl.processAnnotations(context, true);
            return(context);
        }
コード例 #7
0
        public void processCategory(Context context, CategoryDeclaration declaration)
        {
            AnnotationProcessor processor = AnnotationProcessor.forName(name);

            if (processor != null)
            {
                processor.ProcessCategory(this, context, declaration);
            }
        }
コード例 #8
0
 public override void ProcessCategory(Annotation annotation, Context context, CategoryDeclaration declaration)
 {
     if (declaration.IsAWidget(context))
     {
         doProcessCategory(annotation, context, declaration);
     }
     else
     {
         throw new SyntaxError("WidgetField is only applicable to widgets");
     }
 }
コード例 #9
0
        public void testCategory1Attribute()
        {
            String              statement = "define Person as category with attribute id ";
            ETestParser         parser    = new ETestParser(statement, false);
            CategoryDeclaration cd        = parser.parse_category_declaration();

            Assert.IsNotNull(cd);
            Assert.AreEqual("Person", cd.GetName());
            Assert.IsNull(cd.getDerivedFrom());
            Assert.IsNotNull(cd.GetLocalAttributes());
            Assert.IsTrue(cd.GetLocalAttributes().Contains("id"));
        }
コード例 #10
0
 void checkFirstHomonym(Context context, CategoryDeclaration decl)
 {
     if (xchecked)
     {
         return;
     }
     if (arguments != null && arguments.Count > 0)
     {
         checkFirstHomonym(context, decl, arguments[0]);
     }
     xchecked = true;
 }
コード例 #11
0
        public void parsesCategory1Attribute()
        {
            String              statement = "category Person ( id );";
            OTestParser         parser    = new OTestParser(statement);
            CategoryDeclaration cd        = parser.parse_category_declaration();

            Assert.IsNotNull(cd);
            Assert.AreEqual("Person", cd.GetName());
            Assert.IsNull(cd.getDerivedFrom());
            Assert.IsNotNull(cd.GetLocalAttributes());
            Assert.IsTrue(cd.GetLocalAttributes().Contains("id"));
        }
コード例 #12
0
        public void parsesCategory2DerivedNoAttribute()
        {
            String              statement = "category Entrepreneur extends Person, Company;";
            OTestParser         parser    = new OTestParser(statement);
            CategoryDeclaration cd        = parser.parse_category_declaration();

            Assert.IsNotNull(cd);
            Assert.AreEqual("Entrepreneur", cd.GetName());
            Assert.IsNotNull(cd.getDerivedFrom());
            Assert.IsTrue(cd.getDerivedFrom().Contains("Person"));
            Assert.IsTrue(cd.getDerivedFrom().Contains("Company"));
            Assert.IsNull(cd.GetLocalAttributes());
        }
コード例 #13
0
        public void parsesCategory1Derived1Attribute()
        {
            String              statement = "category Employee( company ) extends Person ;";
            OTestParser         parser    = new OTestParser(statement);
            CategoryDeclaration cd        = parser.parse_category_declaration();

            Assert.IsNotNull(cd);
            Assert.AreEqual("Employee", cd.GetName());
            Assert.IsNotNull(cd.getDerivedFrom());
            Assert.IsTrue(cd.getDerivedFrom().Contains("Person"));
            Assert.IsNotNull(cd.GetLocalAttributes());
            Assert.IsTrue(cd.GetLocalAttributes().Contains("company"));
        }
コード例 #14
0
        public void testCategory2DerivedNoAttribute()
        {
            String              statement = "define Entrepreneur as Person and Company";
            ETestParser         parser    = new ETestParser(statement, false);
            CategoryDeclaration cd        = parser.parse_category_declaration();

            Assert.IsNotNull(cd);
            Assert.AreEqual("Entrepreneur", cd.GetName());
            Assert.IsNotNull(cd.getDerivedFrom());
            Assert.IsTrue(cd.getDerivedFrom().Contains("Person"));
            Assert.IsTrue(cd.getDerivedFrom().Contains("Company"));
            Assert.IsNull(cd.GetLocalAttributes());
        }
コード例 #15
0
        private void LoadCategoryDeclarations(XDocument doc)
        {
            lock (_lockConfigurationFile)
            {
                // Validate input...
                if (doc == null)
                {
                    throw new ArgumentNullException("doc");
                }

                foreach (XElement categoryDeclaration in doc.Descendants(CATEGORY_DECLARATION))
                {
                    CategoryDeclaration cd = new CategoryDeclaration();
                    cd.Id          = int.Parse(categoryDeclaration.Attribute(XName.Get(ID)).Value);
                    cd.Name        = categoryDeclaration.Attribute(XName.Get(NAME)).Value;
                    cd.Description = categoryDeclaration.Element(XName.Get(CATEGORY_DESCRIPTION)).Value;
                    cd.Rules       = new Dictionary <int, IRuleDeclaration>();

                    foreach (XElement ruleDeclaration in categoryDeclaration.Descendants(XName.Get(RULE_DECLARATION)))
                    {
                        RuleDeclaration rd = new RuleDeclaration();
                        rd.ParentDeclaration = cd;
                        rd.Id          = int.Parse(ruleDeclaration.Attribute(XName.Get(ID)).Value);
                        rd.Name        = ruleDeclaration.Attribute(XName.Get(NAME)).Value;
                        rd.Description = ruleDeclaration.Descendants(XName.Get(RULE_DESCRIPTION)).Single().Value;
                        rd.Severity    = RuleSeverityMapper.String2RuleSeverity(ruleDeclaration.Descendants(XName.Get(RULE_SEVERITY)).Single().Value);
                        rd.Expression  = ruleDeclaration.Descendants(XName.Get(RULE_EXPRESSION)).Single().Value;

                        IEnumerable <XElement> languages = ruleDeclaration.Descendants(XName.Get("Language"));
                        foreach (XElement l in languages)
                        {
                            int    id   = int.Parse(l.Attribute(XName.Get("RefId")).Value);
                            string name = l.Attribute(XName.Get("RefName")).Value;
                            rd.Languages.Add(id, Manager.Declarations.Languages[id]);
                        }

                        cd.Rules.Add(rd.Id, rd);

                        // Update 'Rule Declaration' statistics counters...
                        ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.RuleDeclarations);
                    }

                    Manager.Declarations.Categories.Add(cd.Id, cd);

                    // Update 'Category Declaration' statistics counters...
                    ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.CategoryDeclarations);
                }
            }
        }
コード例 #16
0
 private IValue ConvertCSharpValueToIValue(Context context, CategoryDeclaration decl, Object value)
 {
     if (DataStore.Instance.GetDbIdType().IsInstanceOfType(value))
     {
         value = DataStore.Instance.FetchUnique(value);
     }
     if (value is IStored)
     {
         return(decl.newInstance(context, (IStored)value));
     }
     else
     {
         return(base.ConvertCSharpValueToIValue(context, value));
     }
 }
コード例 #17
0
        private void DoProcessCategory(Annotation annotation, Context context, CategoryDeclaration declaration)
        {
            IWidgetDeclaration widget     = declaration.AsWidget();
            Object             value      = annotation.GetDefaultArgument();
            PropertyMap        properties = CheckProperties(annotation, context, value);

            if (properties != null)
            {
                widget.SetProperties(properties);
                Annotation widgetField = FindWidgetPropertiesFieldAnnotation(context, declaration);
                if (widgetField != null)
                {
                    OverrideWidgetFieldType(context, widgetField, new PropertiesType(properties));
                }
            }
        }
コード例 #18
0
 public bool isDerivedFrom(Context context, CategoryDeclaration decl, CategoryType other)
 {
     if (decl.getDerivedFrom() == null)
     {
         return(false);
     }
     foreach (String derived in decl.getDerivedFrom())
     {
         CategoryType ct = new CategoryType(derived);
         if (ct.Equals(other) || ct.isDerivedFrom(context, other))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #19
0
 private Annotation FindWidgetPropertiesFieldAnnotation(Context context, CategoryDeclaration declaration)
 {
     foreach (Annotation a in declaration.GetAllAnnotations(context))
     {
         if ("@WidgetField" != a.Name)
         {
             continue;
         }
         Object value = a.GetArgument("isProperties");
         if (value is BooleanLiteral && ((BooleanValue)((BooleanLiteral)value).getValue()).Value)
         {
             return(a);
         }
     }
     return(null);
 }
コード例 #20
0
        public bool isDerivedFromAnonymous(Context context, CategoryDeclaration thisDecl, CategoryDeclaration otherDecl)
        {
            // an anonymous category extends 1 and only 1 category
            String baseName = otherDecl.getDerivedFrom()[0];

            // check we derive from root category (if not extending 'any')
            if (!"any".Equals(baseName) && !thisDecl.isDerivedFrom(context, new CategoryType(baseName)))
            {
                return(false);
            }
            foreach (String attribute in otherDecl.GetAllAttributes(context))
            {
                if (!thisDecl.hasAttribute(context, attribute))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #21
0
 public bool isDerivedFromAnonymous(Context context, CategoryDeclaration thisDecl, CategoryType other)
 {
     if (!other.isAnonymous())
     {
         return(false);
     }
     try
     {
         IDeclaration otherDecl = other.getDeclaration(context);
         if (otherDecl is CategoryDeclaration)
         {
             return(isDerivedFromAnonymous(context, thisDecl, (CategoryDeclaration)otherDecl));
         }
     }
     catch (SyntaxError)
     {
     }
     return(false); // TODO
 }
コード例 #22
0
        private void doProcessCategory(Annotation annotation, Context context, CategoryDeclaration declaration)
        {
            Object fieldName = annotation.GetArgument("name");
            Object fieldType = annotation.GetArgument("type");

            if (!(fieldName is TextLiteral))
            {
                throw new SyntaxError("WidgetField requires a Text value for argument 'name'");
            }
            else if (!(fieldType is TypeLiteral || fieldType is TypeExpression))
            {
                throw new SyntaxError("WidgetField requires a Type value for argument 'type'");
            }
            else
            {
                String name = ((TextLiteral)fieldName).ToString();
                IType  type = fieldType is TypeLiteral ? ((TypeLiteral)fieldType).getType() : ((TypeExpression)fieldType).getType();
                context.registerValue(new Variable(name.Substring(1, name.Length - 2), type), false);
            }
        }
コード例 #23
0
        public override IType check(Context context)
        {
            if (!(predicate is IPredicateExpression))
            {
                throw new SyntaxError("Filtering expression must be a predicate !");
            }
            if (type != null)
            {
                CategoryDeclaration decl = context.getRegisteredDeclaration <CategoryDeclaration>(type.GetTypeName());
                if (decl == null)
                {
                    throw new SyntaxError("Unknown category: " + type.GetTypeName().ToString());
                }
                context = context.newInstanceContext((CategoryType)decl.GetIType(context), true);
            }
            IType filterType = predicate.check(context);

            if (filterType != BooleanType.Instance)
            {
                throw new SyntaxError("Filtering expression must return a boolean !");
            }
            if (include != null)
            {
                foreach (string name in include)
                {
                    if (context.getRegisteredDeclaration <AttributeDeclaration>(name) == null)
                    {
                        throw new SyntaxError("Unknown attribute: " + name);
                    }
                }
            }
            if (type != null)
            {
                return(type);
            }
            else
            {
                return(AnyType.Instance);
            }
        }
コード例 #24
0
        public override IType check(Context context)
        {
            IType type = this.type;

            if (type == null)
            {
                type = AnyType.Instance;
            }
            else
            {
                CategoryDeclaration decl = context.getRegisteredDeclaration <CategoryDeclaration>(type.GetTypeName());
                if (decl == null)
                {
                    throw new SyntaxError("Unknown category: " + type.GetTypeName().ToString());
                }
            }
            checkPredicate(context);
            checkInclude(context);
            checkOrderBy(context);
            checkSlice(context);
            return(new CursorType(type));
        }
コード例 #25
0
        public override IValue interpretReference(Context context)
        {
            // resolve parent to keep clarity
            IExpression parent   = resolveParent(context);
            IValue      instance = parent.interpret(context);

            if (instance == null || instance == NullValue.Instance)
            {
                throw new NullReferenceError();
            }
            else if (instance is IInstance)
            {
                CategoryDeclaration  category = ((IInstance)instance).getDeclaration();
                MethodDeclarationMap methods  = category.getMemberMethods(context, name);
                IMethodDeclaration   method   = methods.GetFirst();             // TODO check prototype
                return(new ClosureValue(context.newInstanceContext((IInstance)instance, true), new MethodType(method)));
            }
            else
            {
                throw new SyntaxError("Should never get here!");
            }
        }
コード例 #26
0
 void checkFirstHomonym(Context context, CategoryDeclaration decl, Argument argument)
 {
     if (argument.Parameter == null)
     {
         IExpression exp = argument.getExpression();
         // when coming from UnresolvedCall, could be an homonym
         string name = null;
         if (exp is UnresolvedIdentifier)
         {
             name = ((UnresolvedIdentifier)exp).getName();
         }
         else if (exp is InstanceExpression)
         {
             name = ((InstanceExpression)exp).getName();
         }
         if (name != null && decl.hasAttribute(context, name))
         {
             // convert expression to name to avoid translation issues
             argument.Parameter  = new AttributeParameter(name);
             argument.Expression = null;
         }
     }
 }
コード例 #27
0
        public override Comparer <IValue> getComparer(Context context, IExpression key, bool descending)
        {
            if (key == null)
            {
                key = new UnresolvedIdentifier("key", Dialect.E);
            }
            IDeclaration d = getDeclaration(context);

            if (d is CategoryDeclaration)
            {
                CategoryDeclaration decl = (CategoryDeclaration)d;
                if (decl.hasAttribute(context, key.ToString()))
                {
                    return(new AttributeComparer(context, key.ToString(), descending));
                }
                else if (decl.hasMethod(context, key.ToString()))
                {
                    return(new CategoryMethodComparer(context, key.ToString(), descending));
                }
                else if (globalMethodExists(context, key.ToString())) // TODO support 2 args
                {
                    return(new GlobalMethodComparer(context, key.ToString(), descending, this));
                }
                else if (key is ArrowExpression)
                {
                    return(((ArrowExpression)key).GetComparer(context, this, descending));
                }
                else
                {
                    return(new ExpressionComparer(context, key, descending));
                }
            }
            else
            {
                throw new Exception("Unsupported!");
            }
        }
コード例 #28
0
        public override bool isMoreSpecificThan(Context context, IType other)
        {
            if (other is NullType || other is AnyType || other is MissingType)
            {
                return(true);
            }
            if (!(other is CategoryType))
            {
                return(false);
            }
            CategoryType otherCat = (CategoryType)other;

            if (otherCat.isAnonymous())
            {
                return(true);
            }
            CategoryDeclaration thisDecl = context.getRegisteredDeclaration <CategoryDeclaration>(this.GetTypeName());

            if (thisDecl.isDerivedFrom(context, otherCat))
            {
                return(true);
            }
            return(false);
        }
コード例 #29
0
 public abstract void ProcessCategory(Annotation annotation, Context context, CategoryDeclaration declaration);
コード例 #30
0
        public override IValue interpret(Context context)
        {
            CategoryDeclaration cd = context.getRegisteredDeclaration <CategoryDeclaration>(this.type.GetTypeName());

            if (cd == null)
            {
                throw new SyntaxError("Unknown category " + this.type.GetTypeName());
            }
            checkFirstHomonym(context, cd);
            IInstance instance = type.newInstance(context);

            instance.setMutable(true);
            try
            {
                if (copyFrom != null)
                {
                    Object copyObj = copyFrom.interpret(context);
                    if (copyObj is IInstance)
                    {
                        IInstance copyInstance = (IInstance)copyObj;
                        foreach (String name in copyInstance.GetMemberNames())
                        {
                            if (name == "dbId")
                            {
                                continue;
                            }
                            else if (cd.hasAttribute(context, name))
                            {
                                IValue value = copyInstance.GetMemberValue(context, name, false);
                                if (value != null && value.IsMutable() && !this.type.Mutable)
                                {
                                    throw new NotMutableError();
                                }
                                instance.SetMemberValue(context, name, value);
                            }
                        }
                    }
                    else if (copyObj is DocumentValue)
                    {
                        DocumentValue copyDoc = (DocumentValue)copyObj;
                        foreach (String name in copyDoc.GetMemberNames())
                        {
                            if (name == "dbId")
                            {
                                continue;
                            }
                            else if (cd.hasAttribute(context, name))
                            {
                                IValue value = copyDoc.GetMemberValue(context, name, false);
                                if (value != null && value.IsMutable() && !this.type.Mutable)
                                {
                                    throw new NotMutableError();
                                }
                                // TODO convert to attribute type, see Java version
                                instance.SetMemberValue(context, name, value);
                            }
                        }
                    }
                }
                if (arguments != null)
                {
                    foreach (Argument argument in arguments)
                    {
                        IValue value = argument.getExpression().interpret(context);
                        if (value != null && value.IsMutable() && !this.type.Mutable)
                        {
                            throw new NotMutableError();
                        }
                        instance.SetMemberValue(context, argument.GetName(), value);
                    }
                }
            }
            finally
            {
                instance.setMutable(this.type.Mutable);
            }
            return(instance);
        }
コード例 #31
0
        private void LoadCategoryDeclarations(XDocument doc)
        {
            lock (_lockConfigurationFile)
              {
            // Validate input...
            if (doc == null)
              throw new ArgumentNullException("doc");

            foreach (XElement categoryDeclaration in doc.Descendants(CATEGORY_DECLARATION))
            {
              CategoryDeclaration cd = new CategoryDeclaration();
              cd.Id = int.Parse(categoryDeclaration.Attribute(XName.Get(ID)).Value);
              cd.Name = categoryDeclaration.Attribute(XName.Get(NAME)).Value;
              cd.Description = categoryDeclaration.Element(XName.Get(CATEGORY_DESCRIPTION)).Value;
              cd.Rules = new Dictionary<int, IRuleDeclaration>();

              foreach (XElement ruleDeclaration in categoryDeclaration.Descendants(XName.Get(RULE_DECLARATION)))
              {
            RuleDeclaration rd = new RuleDeclaration();
            rd.ParentDeclaration = cd;
            rd.Id = int.Parse(ruleDeclaration.Attribute(XName.Get(ID)).Value);
            rd.Name = ruleDeclaration.Attribute(XName.Get(NAME)).Value;
            rd.Description = ruleDeclaration.Descendants(XName.Get(RULE_DESCRIPTION)).Single().Value;
            rd.Severity = RuleSeverityMapper.String2RuleSeverity(ruleDeclaration.Descendants(XName.Get(RULE_SEVERITY)).Single().Value);
            rd.Expression = ruleDeclaration.Descendants(XName.Get(RULE_EXPRESSION)).Single().Value;

            IEnumerable<XElement> languages = ruleDeclaration.Descendants(XName.Get("Language"));
            foreach (XElement l in languages)
            {
              int id = int.Parse(l.Attribute(XName.Get("RefId")).Value);
              string name = l.Attribute(XName.Get("RefName")).Value;
              rd.Languages.Add(id, Manager.Declarations.Languages[id]);
            }

            cd.Rules.Add(rd.Id, rd);

            // Update 'Rule Declaration' statistics counters...
            ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.RuleDeclarations);
              }

              Manager.Declarations.Categories.Add(cd.Id, cd);

              // Update 'Category Declaration' statistics counters...
              ProxyHome.Instance.RetrieveStatisticsProxy(ConfigKeyKeeper.Instance.AccessKey).IncrementCounter(CounterIds.CategoryDeclarations);
            }
              }
        }