Ejemplo n.º 1
0
        private void AddLink(Association association, ObjectPlusPlus linkedObject, object qualifier, int counter)
        {
            if (counter < 1)
            {
                return;
            }
            if (linkedObject == null)
            {
                return;
            }
            if (association == null)
            {
                return;
            }
            if (!LegalAssociations.Any(x => x.Role.Equals(association.Role)))
            {
                throw new Exception("Association is not legal");
            }
            if (this.GetType() != association.StartClassifier)
            {
                throw new Exception("This object type is not correct");
            }
            if (linkedObject.GetType() != association.EndClassifier)
            {
                throw new Exception("Linked object type is not correct");
            }
            Dictionary <object, ObjectPlusPlus> roleLinks;

            if (Links.ContainsKey(association.Role))
            {
                roleLinks = Links[association.Role];
            }
            else
            {
                roleLinks = new Dictionary <object, ObjectPlusPlus>();
                Links.Add(association.Role, roleLinks);
            }
            if (!roleLinks.ContainsKey(linkedObject))
            {
                Association reverseAssociation = association.GetReversedAssociation();
                switch (association.EndMultiplicityLimit)
                {
                case -1:
                    roleLinks.Add(qualifier, linkedObject);
                    if (reverseAssociation != null)
                    {
                        linkedObject.AddLink(reverseAssociation, this, this, counter - 1);
                    }
                    break;

                case 0:
                    if (roleLinks.Count != 0)
                    {
                        throw new Exception("Multiplicity limit has been reached");
                    }
                    roleLinks.Add(qualifier, linkedObject);
                    linkedObject.AddLink(reverseAssociation, this, this, counter - 1);
                    break;

                default:
                    if (roleLinks.Count >= association.EndMultiplicityLimit)
                    {
                        throw new Exception("Multiplicity limit has been reached");
                    }
                    roleLinks.Add(qualifier, linkedObject);
                    linkedObject.AddLink(reverseAssociation, this, this, counter - 1);
                    break;
                }
            }
        }