コード例 #1
0
        Relation GetOrCreateRelation(ParsedInRelationExpression rel, Dictionary <string, KAOSTools.Core.Entity> declarations)
        {
            dynamic identifierOrName = rel.Relation;

            Relation type;

            if (identifierOrName is NameExpression)
            {
                type = model.Relations().SingleOrDefault(t => t.Name == identifierOrName.Value);
            }
            else if (identifierOrName is IdentifierExpression)
            {
                type = model.Relations().SingleOrDefault(t => t.Identifier == identifierOrName.Value);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (type == null)
            {
                if (identifierOrName is NameExpression)
                {
                    type = new Relation(model)
                    {
                        Name = identifierOrName.Value, Implicit = true
                    };
                }
                else if (identifierOrName is IdentifierExpression)
                {
                    type = new Relation(model)
                    {
                        Identifier = identifierOrName.Value, Implicit = true
                    };
                }

                foreach (var arg in rel.Variables)
                {
                    type.Links.Add(new Link(model)
                    {
                        Target = declarations[arg]
                    });
                }

                model.Add(type);
            }
            else
            {
                // Check that types matches
                // TODO make this shit more robust. In the case of two links to a same entity, this
                // check will fail...
                foreach (var arg in rel.Variables)
                {
                    if (type.Links.Count(x => x.Target == declarations[arg]) == 0)
                    {
                        throw new BuilderException("Relation and formal spec are incompatible.",
                                                   rel.Filename, rel.Line, rel.Col);
                    }
                }
            }


            return(type);
        }