Beispiel #1
0
        /// <summary>
        /// Gets the target proto role player.
        /// </summary>
        /// <param name="partition">Partition.</param>
        /// <returns>Target proto role player.</returns>
        public ModelProtoRolePlayer GetTargetRolePlayer(Partition partition)
        {
            DomainDataDirectory  domainDataDirectory = partition.Store.DomainDataDirectory;
            ModelProtoRolePlayer protoRolePlayer     = rolePlayers[1];
            DomainRoleInfo       domainRoleInfo      = domainDataDirectory.GetDomainRole(protoRolePlayer.DomainRoleId);

            if (domainRoleInfo.IsSource)
            {
                protoRolePlayer = rolePlayers[0];
            }
            return(protoRolePlayer);
        }
        private void InitDictionaries(Partition partition)
        {
            bNeedsInitialization = false;

            this.embeddingMapping         = new Dictionary <Guid, List <ModelProtoElement> >();
            this.referencingMapping       = new Dictionary <Guid, List <ModelProtoLink> >();
            this.referencingMappingTarget = new Dictionary <Guid, List <ModelProtoLink> >();
            this.elementMapping           = new Dictionary <Guid, ModelProtoElement>();

            foreach (ModelProtoElement element in this.ProtoElements)
            {
                embeddingMapping.Add(element.ElementId, new List <ModelProtoElement>());
                referencingMapping.Add(element.ElementId, new List <ModelProtoLink>());
                referencingMappingTarget.Add(element.ElementId, new List <ModelProtoLink>());
                elementMapping.Add(element.ElementId, element);
            }
            foreach (ModelProtoLink link in this.ProtoEmbeddingLinks)
            {
                ModelProtoRolePlayer rolePlayer = link.GetSourceRolePlayer(partition);
                ModelProtoElement    p          = GetElementById(rolePlayer.RolePlayerId);
                if (p != null)
                {
                    embeddingMapping[p.ElementId].Add(
                        GetElementById(link.GetTargetRolePlayer(partition).RolePlayerId));
                }
            }

            foreach (ModelProtoLink link in this.ProtoReferenceLinks)
            {
                ModelProtoRolePlayer sourceRP = link.GetSourceRolePlayer(partition);
                ModelProtoElement    p        = GetElementById(sourceRP.RolePlayerId);
                if (p != null)
                {
                    referencingMapping[p.ElementId].Add(link);
                }

                ModelProtoRolePlayer targetRP = link.GetTargetRolePlayer(partition);
                p = GetElementById(targetRP.RolePlayerId);
                if (p != null)
                {
                    referencingMappingTarget[p.ElementId].Add(link);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Adds a proto link to the current element.
        /// </summary>
        /// <param name="protoLink">Proto link representation of the element link that is to be added.</param>
        /// <param name="groupMerger">
        /// Group merger class used to track id mapping, merge errors/warnings and
        /// postprocess merging by rebuilding reference relationships.
        /// </param>
        public virtual void ModelMerge(ModelProtoLink protoLink, ModelProtoGroupMerger groupMerger)
        {
            if (protoLink.IsTargetIncludedSubmodel && !String.IsNullOrEmpty(protoLink.DomainFilePath))
            {
                // TODO ...

                /*
                 * string file = protoLink.DomainFilePath;
                 * IParentModelElement parent = this.GetDomainModelServices().ElementParentProvider.GetParentModelElement(this);
                 * if (parent == null)
                 *  throw new System.ArgumentNullException("Parent of element " + this.ToString() + " can not be null");
                 *
                 * string path = parent.DomainFilePath;
                 * string vModellDirectory = new System.IO.FileInfo(path).DirectoryName;
                 * string curPath = vModellDirectory + System.IO.Path.DirectorySeparatorChar + path;
                 *
                 * // load model
                 * using (Transaction transaction = this.Store.TransactionManager.BeginTransaction("Set referenced model"))
                 * {
                 *  // TODO load.
                 *  /*
                 *  global::Tum.VModellXT.VModellXTDocumentData data = global::Tum.VModellXT.VModellXTDocumentData.Instance as global::Tum.VModellXT.VModellXTDocumentData;
                 *  global::Tum.VModellXT.VModell referenceModel = data.ModelContextVModellXT.LoadInternal(file) as global::Tum.VModellXT.VModell;
                 *  model.VModell = referenceModel;
                 */
                /*
                 *  transaction.Commit();
                 * }
                 *
                 * return;
                 */
                return;
            }

            DomainRelationshipInfo linkDomainInfo = null;

            if (protoLink != null)
            {
                linkDomainInfo = this.Partition.DomainDataDirectory.GetDomainRelationship(protoLink.DomainClassId);
            }
            else
            {
                // try getting the linkDomainInfo from name
                DomainClassInfo elementDomainInfo = this.GetDomainClass();
                foreach (DomainRoleInfo info in elementDomainInfo.AllDomainRolesPlayed)
                {
                    if (info.IsSource)
                    {
                        if (!this.Store.DomainDataAdvDirectory.IsEmbeddingRelationship(info.DomainRelationship.Id) &&
                            !this.Store.DomainDataAdvDirectory.IsAbstractRelationship(info.DomainRelationship.Id))
                        {
                            if (protoLink.Name == info.DomainRelationship.Name && linkDomainInfo == null)
                            {
                                linkDomainInfo = this.Partition.DomainDataDirectory.GetDomainRelationship(info.DomainRelationship.Id);
                            }
                        }
                    }
                }
            }

            if (linkDomainInfo == null)
            {
                groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeElementLinkDomainTypeMissingId,
                                                                         ModelValidationViolationType.Error, "Element link can not be created as the corresponding domain type is missing."));
                return;
            }

            ReferenceRelationshipAdvancedInfo advancedInfo = this.Store.DomainDataAdvDirectory.GetRelationshipInfo(linkDomainInfo.Id) as ReferenceRelationshipAdvancedInfo;

            if (advancedInfo == null)
            {
                throw new InvalidOperationException("Relationship advanced info not found for " + linkDomainInfo.Name);
            }

            // see if this element is taking part in this role
            bool bTakesPart = false;

            ModelProtoRolePlayer sourceRolePlayer   = protoLink.GetSourceRolePlayer(this.Store.DefaultPartition);
            ModelProtoElement    sourceProtoElement = groupMerger.GetElementById(sourceRolePlayer.RolePlayerId);

            System.Guid mappedSourceIdTP = System.Guid.Empty;
            if (sourceProtoElement != null)
            {
                mappedSourceIdTP = groupMerger.GetIdMapping(sourceRolePlayer.RolePlayerId);
                if (mappedSourceIdTP == this.Id)
                {
                    bTakesPart = true;
                }
            }

            if (advancedInfo.PropagatesCopyOnDeniedElement)
            {
                if (!bTakesPart && mappedSourceIdTP == System.Guid.Empty)
                {
                    if (this.Id == sourceRolePlayer.RolePlayerId)
                    {
                        bTakesPart = true;
                    }
                }
            }

            if (bTakesPart)
            {
                bool bExists = true;
                if (this.Store.ElementDirectory.FindElement(protoLink.ElementId) == null)
                {
                    bExists = false;
                }

                if (bExists && groupMerger.ProtoGroup.Operation == ModelProtoGroupOperation.Move)
                {
                    groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeElementLinkExistsOnMoveId,
                                                                             ModelValidationViolationType.Error, "Element link exists although the operation = Move."));
                }

                #region Target
                // see if target element was copied
                ModelProtoRolePlayer targetRolePlayer   = protoLink.GetTargetRolePlayer(this.Store.DefaultPartition);
                ModelProtoElement    targetProtoElement = groupMerger.GetElementById(targetRolePlayer.RolePlayerId);
                Guid mappedTargetId = Guid.Empty;
                if (targetProtoElement != null)
                {
                    mappedTargetId = groupMerger.GetIdMapping(targetRolePlayer.RolePlayerId);
                }

                if (advancedInfo.PropagatesCopyOnDeniedElement)
                {
                    if (mappedTargetId == System.Guid.Empty)
                    {
                        // try creating relationship to existing element
                        mappedTargetId = targetRolePlayer.RolePlayerId;
                    }
                }

                if (mappedTargetId == System.Guid.Empty)
                {
                    // log warning
                    groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeLinkElementNotCopiedId,
                                                                             ModelValidationViolationType.Error, "Referenced model element was not copied. Relationship: " + linkDomainInfo.Name));
                }
                else
                {
                    ModelElement targetElement = this.Store.ElementDirectory.FindElement(mappedTargetId);
                    if (targetElement == null)
                    {
                        // log error
                        groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeLinkElementNotFoundId,
                                                                                 ModelValidationViolationType.Error, "Referenced model element was not found. Relationship: " + linkDomainInfo.Name));
                    }
                    else
                    {
                        bool bContinue = true;

                        // check cardinalities, so we don't violate them by additing a new relationship
                        if (advancedInfo.SourceRoleMultiplicity == Multiplicity.One || advancedInfo.SourceRoleMultiplicity == Multiplicity.ZeroOne)
                        {
                            if (DomainRoleInfo.GetLinkedElement(this, advancedInfo.SourceRoleId) != null)
                            {
                                // log warning
                                groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeLinkCreationViolatesMultiplicityId,
                                                                                         ModelValidationViolationType.Error, "Can not create relationship because one already exists. Relationship: " + linkDomainInfo.Name));

                                bContinue = false;
                            }
                        }

                        if (advancedInfo.TargetRoleMultiplicity == Multiplicity.One || advancedInfo.TargetRoleMultiplicity == Multiplicity.ZeroOne)
                        {
                            if (DomainRoleInfo.GetLinkedElement(this, advancedInfo.TargetRoleId) != null)
                            {
                                // log warning
                                groupMerger.MergeResult.AddMessage(new ValidationMessage(ModelValidationMessageIds.ModelMergeLinkCreationViolatesMultiplicityId,
                                                                                         ModelValidationViolationType.Error, "Can not create relationship because one already exists. Relationship: " + linkDomainInfo.Name));

                                bContinue = false;
                            }
                        }

                        if (bContinue)
                        {
                            // create property assignments
                            PropertyAssignment[] propertyAssignemnts = protoLink.GetPropertyAssignments(this.Store.DefaultPartition,
                                                                                                        this.GetDomainModelServices().ElementIdProvider.GenerateNewKey());

                            // create role assignments
                            RoleAssignment[] roleAssignments = new RoleAssignment[2];
                            roleAssignments[0] = new RoleAssignment(advancedInfo.SourceRoleId, this);
                            roleAssignments[1] = new RoleAssignment(advancedInfo.TargetRoleId, targetElement);

                            // create new relationship
                            this.Store.ElementFactory.CreateElementLink(linkDomainInfo, propertyAssignemnts, roleAssignments);
                        }
                    }
                }
                #endregion
            }
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="protoGroup">Proto group to paste/move.</param>
        /// <param name="rootElement">Element to execute paste/move on.</param>
        public ModelProtoGroupMerger(ModelProtoGroup protoGroup, ModelElement rootElement)
        {
            this.protoGroup       = protoGroup;
            this.validationResult = new ValidationResult();

            this.idMapping                = new Dictionary <Guid, Guid>();
            this.embeddingMapping         = new Dictionary <Guid, List <ModelProtoElement> >();
            this.referencingMapping       = new Dictionary <Guid, List <ModelProtoLink> >();
            this.referencingMappingTarget = new Dictionary <Guid, List <ModelProtoLink> >();
            this.elementMapping           = new Dictionary <Guid, ModelProtoElement>();

            if (protoGroup.Operation == ModelProtoGroupOperation.Move)
            {
                // move element
                foreach (ModelProtoElement protoElement in protoGroup.ProtoRootElements)
                {
                    (rootElement as IModelMergeElements).ModelMove(protoElement, this);
                }
                return;
            }

            foreach (ModelProtoElement element in protoGroup.ProtoElements)
            {
                idMapping.Add(element.ElementId, Guid.Empty);
                embeddingMapping.Add(element.ElementId, new List <ModelProtoElement>());
                referencingMapping.Add(element.ElementId, new List <ModelProtoLink>());
                referencingMappingTarget.Add(element.ElementId, new List <ModelProtoLink>());
                elementMapping.Add(element.ElementId, element);
            }
            foreach (ModelProtoLink link in protoGroup.ProtoEmbeddingLinks)
            {
                ModelProtoRolePlayer rolePlayer = link.GetSourceRolePlayer(rootElement.Store.DefaultPartition);
                ModelProtoElement    p          = GetElementById(rolePlayer.RolePlayerId);
                if (p != null)
                {
                    embeddingMapping[p.ElementId].Add(
                        GetElementById(link.GetTargetRolePlayer(rootElement.Store.DefaultPartition).RolePlayerId));
                }
            }

            foreach (ModelProtoLink link in protoGroup.ProtoReferenceLinks)
            {
                ModelProtoRolePlayer sourceRP = link.GetSourceRolePlayer(rootElement.Store.DefaultPartition);
                ModelProtoElement    p        = GetElementById(sourceRP.RolePlayerId);
                if (p != null)
                {
                    referencingMapping[p.ElementId].Add(link);
                }

                ModelProtoRolePlayer targetRP = link.GetTargetRolePlayer(rootElement.Store.DefaultPartition);
                p = GetElementById(targetRP.RolePlayerId);
                if (p != null)
                {
                    referencingMappingTarget[p.ElementId].Add(link);
                }
            }

            // start merging
            foreach (ModelProtoElement protoElement in protoGroup.ProtoRootElements)
            {
                (rootElement as IModelMergeElements).ModelMerge(protoElement, this, true);
            }

            // process reference relationships
            foreach (ModelProtoLink link in protoGroup.ProtoReferenceLinks)
            {
                ModelProtoRolePlayer sourceRP = link.GetSourceRolePlayer(rootElement.Store.DefaultPartition);
                if (this.idMapping.ContainsKey(sourceRP.RolePlayerId))
                {
                    System.Guid id = this.idMapping[sourceRP.RolePlayerId];
                    if (id != Guid.Empty)
                    {
                        ModelElement copiedModelElement = rootElement.Store.ElementDirectory.FindElement(id);
                        if (copiedModelElement != null)
                        {
                            if (copiedModelElement is IModelMergeElements)
                            {
                                (copiedModelElement as IModelMergeElements).ModelMerge(link, this);
                            }
                        }
                        continue;
                    }
                }

                System.Guid  elementId    = sourceRP.RolePlayerId;
                ModelElement modelElement = rootElement.Store.ElementDirectory.FindElement(elementId);
                if (modelElement != null)
                {
                    if (modelElement is IModelMergeElements)
                    {
                        (modelElement as IModelMergeElements).ModelMerge(link, this);
                    }
                }
            }

            // finalize merging
            foreach (ModelProtoElement protoElement in protoGroup.ProtoElements)
            {
                Guid id = Guid.Empty;
                try
                {
                    id = GetIdMapping(protoElement.ElementId);
                }
                catch
                {
                    id = Guid.Empty;
                    continue;
                }

                /*
                 * if (id == Guid.Empty)
                 * {
                 *  ModelElement m = rootElement.Store.ElementDirectory.FindElement(id);
                 *  if (m != null)
                 *      id = m.Id;
                 *  else
                 *      continue;
                 * }*/

                ModelElement m = rootElement.Store.ElementDirectory.FindElement(id);
                if (m != null)
                {
                    (m as IModelMergeElements).ModelFinalize(protoElement, this);
                }
            }

            // finalize merging
            foreach (ModelProtoElement protoElement in protoGroup.ProtoRootElements)
            {
                (rootElement as IModelMergeElements).ModelFinalizeMerge(protoElement, this);
            }
        }