public static void OnEmbeddingRelationshipAdded(EmbeddingRelationship embeddingRelationship) { #region Check Paramaters if (embeddingRelationship.Source == null) { throw new ArgumentNullException("embeddingRelationship.Source"); } if (embeddingRelationship.Target == null) { throw new ArgumentNullException("embeddingRelationship.Target"); } if (!(embeddingRelationship.Source.RolePlayer is DomainClass)) { throw new ArgumentNullException("embeddingRelationship.Source.RolePlayer needs to be DomainClass"); } if (!(embeddingRelationship.Target.RolePlayer is DomainClass)) { throw new ArgumentNullException("embeddingRelationship.Target.RolePlayer needs to be DomainClass"); } #endregion // add serialization info SerializedDomainClass child; if ((embeddingRelationship.Target.RolePlayer as DomainClass).SerializedDomainClass == null) { child = new SerializedDomainClass(embeddingRelationship.Store); child.DomainClass = embeddingRelationship.Target.RolePlayer as DomainClass; (embeddingRelationship.Target.RolePlayer as DomainClass).ModelContext.SerializationModel.Children.Add(child); SerializationHelper.AddSerializationDomainProperties(embeddingRelationship.Store, embeddingRelationship.Target.RolePlayer); } else { child = (embeddingRelationship.Target.RolePlayer as DomainClass).SerializedDomainClass; } // Add properties and id attribute and set serialization form for embedding relationship. SerializedEmbeddingRelationship embChild = new SerializedEmbeddingRelationship(embeddingRelationship.Store); embChild.EmbeddingRelationship = embeddingRelationship; embChild.IsInFullSerialization = false; embChild.SerializationName = embeddingRelationship.SerializationName; SerializationHelper.AddSerializationDomainProperties(embeddingRelationship.Store, embeddingRelationship); embeddingRelationship.ModelContext.SerializationModel.Children.Add(embChild); embChild.Children.Add(child); // Add connection between roleplayers, to reflect it onto the serialization model. SerializationClass sourceSerializationClass = (embeddingRelationship.Source.RolePlayer as DomainClass).SerializedDomainClass; sourceSerializationClass.Children.Add(embChild); // update derived roles SerializationHelper.UpdateDerivedElementsSerializationDomainRoles(embeddingRelationship.Source.RolePlayer); }
/// <summary> /// Updaates serialized domain properties (add/remove) to reflect the existing domain properties onto the serialization model. /// </summary> /// <param name="store">Store containing the domain model.</param> /// <param name="serializationElement">Element to add or remove the serialized domain properties to.</param> /// <param name="domainElement">Element to get the domain properties from.</param> public static void UpdateSerializationDomainProperties(Store store, SerializationClass serializationClass, AttributedDomainElement domainElement) { if (serializationClass == null || domainElement == null) { return; } //if (!domainElement.ParentModelContext.MetaModel.IsTopMost) // return; // update serialization properties on the current level: --> add missing UpdateSerializationDomainPropertiesOnCurrentLevel(store, domainElement); List <SerializedDomainProperty> serializedAttributePropertyToAdd = new List <SerializedDomainProperty>(); List <SerializedDomainProperty> serializedElementPropertyToAdd = new List <SerializedDomainProperty>(); AttributedDomainElement element = domainElement; while (element != null) { foreach (DomainProperty domainProperty in element.Properties) { if (domainProperty.SerializedDomainProperty != null) { if (domainProperty.SerializedDomainProperty.SerializationRepresentationType == SerializationRepresentationType.Attribute) { serializedAttributePropertyToAdd.Add(domainProperty.SerializedDomainProperty); } else if (domainProperty.SerializedDomainProperty.SerializationRepresentationType == SerializationRepresentationType.Element) { serializedElementPropertyToAdd.Add(domainProperty.SerializedDomainProperty); } } } element = element.BaseElement; } // 1. add missing foreach (SerializedDomainProperty p in serializedAttributePropertyToAdd) { if (!serializationClass.Attributes.Contains(p)) { serializationClass.Attributes.Add(p); } } foreach (SerializedDomainProperty p in serializedElementPropertyToAdd) { if (!serializationClass.Children.Contains(p)) { serializationClass.Children.Add(p); } } // 2. remove unneeded ones for (int i = serializationClass.Attributes.Count - 1; i >= 0; i--) { if (serializationClass.Attributes[i] is SerializedDomainProperty) { if (!serializedAttributePropertyToAdd.Contains(serializationClass.Attributes[i] as SerializedDomainProperty)) { serializationClass.Attributes.RemoveAt(i); } } } for (int i = serializationClass.Children.Count - 1; i >= 0; i--) { if (serializationClass.Children[i] is SerializedDomainProperty) { if (!serializedElementPropertyToAdd.Contains(serializationClass.Children[i] as SerializedDomainProperty)) { serializationClass.Children.RemoveAt(i); } } } }
public static void OnReferenceRelationshipAdded(ReferenceRelationship referenceRelationship) { #region Check Paramaters if (referenceRelationship.Source == null) { throw new ArgumentNullException("referenceRelationship.Source"); } if (referenceRelationship.Target == null) { throw new ArgumentNullException("referenceRelationship.Target"); } if (!(referenceRelationship.Source.RolePlayer is DomainClass)) { throw new ArgumentNullException("referenceRelationship.Source.RolePlayer needs to be DomainClass"); } if (!(referenceRelationship.Target.RolePlayer is DomainClass) && !(referenceRelationship.Target.RolePlayer is ReferenceRelationship)) { throw new ArgumentNullException("referenceRelationship.Target.RolePlayer needs to be DomainClass or ReferenceRelationship"); } #endregion // Add properties and id attribute and set serialization form. SerializedReferenceRelationship child = new SerializedReferenceRelationship(referenceRelationship.Store); child.ReferenceRelationship = referenceRelationship; if (referenceRelationship.NeedsFullSerialization()) { child.IsInFullSerialization = true; } else { child.IsInFullSerialization = false; } child.SerializationName = referenceRelationship.SerializationName; SerializationHelper.AddSerializationDomainProperties(referenceRelationship.Store, referenceRelationship); referenceRelationship.ModelContext.SerializationModel.Children.Add(child); SerializationClass domainClassSource = (referenceRelationship.Source.RolePlayer as DomainClass).SerializedDomainClass; domainClassSource.Children.Add(child); // Add roles players. SerializedDomainRole roleSource = new SerializedDomainRole(referenceRelationship.Store); roleSource.DomainRole = referenceRelationship.Source; roleSource.SerializationAttributeName = referenceRelationship.SerializationAttributeName; roleSource.SerializationName = referenceRelationship.SerializationSourceName; child.Children.Add(roleSource); SerializedDomainRole roleTarget = new SerializedDomainRole(referenceRelationship.Store); roleTarget.DomainRole = referenceRelationship.Target; roleTarget.SerializationAttributeName = referenceRelationship.SerializationAttributeName; roleTarget.SerializationName = referenceRelationship.SerializationTargetName; child.Children.Add(roleTarget); child.SerializedDomainRoles.Add(roleSource); child.SerializedDomainRoles.Add(roleTarget); // update derived roles SerializationHelper.UpdateDerivedElementsSerializationDomainRoles(referenceRelationship.Source.RolePlayer); }
public static void PostProcessModelLoad(MetaModel model) { // package and custom editor GUIDs if (model.PackageGuid == null || model.PackageGuid == Guid.Empty) { model.PackageGuid = Guid.NewGuid(); } if (model.CustomExtensionGuid == null || model.CustomExtensionGuid == Guid.Empty) { model.CustomExtensionGuid = Guid.NewGuid(); } #region relationship targets fixup ReadOnlyCollection <DomainRelationship> rels = model.AllRelationships; foreach (DomainRelationship rel in rels) { if (rel.Target.RolePlayer == null) { ReferenceRelationship referenceRelationship = rel as ReferenceRelationship; if (referenceRelationship != null) { if (referenceRelationship.ReferenceRSNode != null) { referenceRelationship.ReferenceRSNode.Delete(); } if (referenceRelationship.SerializedReferenceRelationship != null) { referenceRelationship.SerializedReferenceRelationship.Delete(); } } EmbeddingRelationship embeddingRelationship = rel as EmbeddingRelationship; if (embeddingRelationship != null) { if (embeddingRelationship.EmbeddingRSNode != null) { embeddingRelationship.EmbeddingRSNode.Delete(); } if (embeddingRelationship.SerializedEmbeddingRelationship != null) { embeddingRelationship.SerializedEmbeddingRelationship.Delete(); } } rel.Delete(); } } #endregion #region inconsistent serialization elements foreach (BaseModelContext context in model.ModelContexts) { if (context is LibraryModelContext) { LibraryModelContext lib = context as LibraryModelContext; if (lib.SerializationModel != null) { for (int i = lib.SerializationModel.Children.Count - 1; i >= 0; i--) { SerializationClass c = lib.SerializationModel.Children[i]; if (c is SerializedDomainClass) { SerializedDomainClass s = c as SerializedDomainClass; if (s.DomainClass == null) { s.Delete(); } continue; } else if (c is SerializedEmbeddingRelationship) { SerializedEmbeddingRelationship s = c as SerializedEmbeddingRelationship; if (s.EmbeddingRelationship == null) { s.Delete(); } continue; } else if (c is SerializedReferenceRelationship) { SerializedReferenceRelationship s = c as SerializedReferenceRelationship; if (s.ReferenceRelationship == null) { s.Delete(); } continue; } // element has not been deleted, see if its properties are ok for (int y = c.Properties.Count - 1; y >= 0; y--) { if (c.Properties[y] == null) { c.Properties[y].Delete(); } } } } } } #endregion #region derived classes serialization items fixup if (model.MetaModelLibraries.Count > 0) { ReadOnlyCollection <ModelElement> elements = model.Store.ElementDirectory.FindElements(DomainClassReferencesBaseClass.DomainClassId); foreach (ModelElement m in elements) { DomainClassReferencesBaseClass con = m as DomainClassReferencesBaseClass; if (con != null) { if (con.BaseClass != null) { if (con.BaseClass.ModelContext.MetaModel != model) { foreach (DomainClass derivedClass in con.BaseClass.DerivedClasses) { FixUpDerivedClasses(derivedClass, model); } } } } } ReadOnlyCollection <ModelElement> elementsCon = model.Store.ElementDirectory.FindElements(DomainRelationshipReferencesBaseRelationship.DomainClassId); foreach (ModelElement m in elementsCon) { DomainRelationshipReferencesBaseRelationship con = m as DomainRelationshipReferencesBaseRelationship; if (con != null) { if (con.BaseRelationship != null) { if (con.BaseRelationship.ModelContext.MetaModel != model) { foreach (DomainRelationship derivedClass in con.BaseRelationship.DerivedRelationships) { FixUpDerivedRelationships(derivedClass, model); } } } } } } #endregion #region check if model contains all required elements // property grid editors if (model.PropertyGridEditors.Count == 0) { FixUpPropertyGridEditors(model); } // domain types if (model.DomainTypes.Count == 0) { FixUpDomainTypes(model); } // model context if (model.ModelContexts.Count == 0) { FixUpModelContext(model); } // validation if (model.Validation == null) { model.Validation = new Validation(model.Store); } if (model.View == null) { model.View = new View(model.Store); } if (model.View.ModelTree == null) { model.View.ModelTree = new ModelTree(model.Store); } foreach (BaseModelContext mContext in model.ModelContexts) { if (mContext is LibraryModelContext) { LibraryModelContext m = mContext as LibraryModelContext; if (m.DiagramClasses.Count == 0 && m is ModelContext) { DesignerDiagramClass ddC = new DesignerDiagramClass(model.Store); ddC.Name = "DesignerDiagram"; ddC.Title = "Designer"; m.DiagramClasses.Add(ddC); } if (m.ViewContext == null) { m.ViewContext = new ViewContext(model.Store); m.ViewContext.DomainModelTreeView = new DomainModelTreeView(model.Store); m.ViewContext.DiagramView = new DiagramView(model.Store); model.View.ViewContexts.Add(m.ViewContext); FixUpDomainModelTreeView(m); FixUpDiagramView(m); } if (m.ViewContext.DiagramView == null || m.ViewContext.DomainModelTreeView == null) { if (m.ViewContext.DomainModelTreeView == null) { m.ViewContext.DomainModelTreeView = new DomainModelTreeView(model.Store); FixUpDomainModelTreeView(m); } if (m.ViewContext.DiagramView == null) { m.ViewContext.DiagramView = new DiagramView(model.Store); FixUpDiagramView(m); } } // diagram class view for designer diagram if (m.ViewContext.DiagramView.DiagramClassViews.Count == 0 && m is ModelContext) { DiagramClassView vm = new DiagramClassView(model.Store); vm.IsExpanded = true; foreach (DiagramClass d in m.DiagramClasses) { if (d is DesignerDiagramClass) { vm.DiagramClass = d; break; } } m.ViewContext.DiagramView.DiagramClassViews.Add(vm); } // serialization if (m.SerializationModel == null) { m.SerializationModel = new SerializationModel(model.Store); } // serialized domain model if (m is ModelContext) { if (m.SerializationModel.SerializedDomainModel == null) { FixUpSerializedDomainModel(m as ModelContext); } } } } #endregion // view ids. if (model.View != null) { if (model.View.ModelTreeId == null || model.View.ModelTreeId == Guid.Empty) { model.View.ModelTreeId = Guid.NewGuid(); } if (model.View.DependenciesViewId == null || model.View.DependenciesViewId == Guid.Empty) { model.View.DependenciesViewId = Guid.NewGuid(); } if (model.View.ErrorListId == null || model.View.ErrorListId == Guid.Empty) { model.View.ErrorListId = Guid.NewGuid(); } if (model.View.PropertyGridId == null || model.View.PropertyGridId == Guid.Empty) { model.View.PropertyGridId = Guid.NewGuid(); } if (model.View.SearchId == null || model.View.SearchId == Guid.Empty) { model.View.SearchId = Guid.NewGuid(); } if (model.View.SearchResultId == null || model.View.SearchResultId == Guid.Empty) { model.View.SearchResultId = Guid.NewGuid(); } if (model.View.PluginWindowId == null || model.View.PluginWindowId == Guid.Empty) { model.View.PluginWindowId = Guid.NewGuid(); } } }
/// <summary> /// Updaates serialized domain properties (add/remove) to reflect the existing domain properties onto the serialization model. /// </summary> /// <param name="store">Store containing the domain model.</param> /// <param name="serializationElement">Element to add or remove the serialized domain properties to.</param> /// <param name="domainElement">Element to get the domain properties from.</param> public static void UpdateSerializationDomainProperties(Store store, SerializationClass serializationClass, AttributedDomainElement domainElement) { if (serializationClass == null || domainElement == null) return; //if (!domainElement.ParentModelContext.MetaModel.IsTopMost) // return; // update serialization properties on the current level: --> add missing UpdateSerializationDomainPropertiesOnCurrentLevel(store, domainElement); List<SerializedDomainProperty> serializedAttributePropertyToAdd = new List<SerializedDomainProperty>(); List<SerializedDomainProperty> serializedElementPropertyToAdd = new List<SerializedDomainProperty>(); AttributedDomainElement element = domainElement; while (element != null) { foreach (DomainProperty domainProperty in element.Properties) { if (domainProperty.SerializedDomainProperty != null) { if (domainProperty.SerializedDomainProperty.SerializationRepresentationType == SerializationRepresentationType.Attribute) serializedAttributePropertyToAdd.Add(domainProperty.SerializedDomainProperty); else if( domainProperty.SerializedDomainProperty.SerializationRepresentationType == SerializationRepresentationType.Element ) serializedElementPropertyToAdd.Add(domainProperty.SerializedDomainProperty); } } element = element.BaseElement; } // 1. add missing foreach (SerializedDomainProperty p in serializedAttributePropertyToAdd) if(!serializationClass.Attributes.Contains(p)) serializationClass.Attributes.Add(p); foreach (SerializedDomainProperty p in serializedElementPropertyToAdd) if (!serializationClass.Children.Contains(p)) serializationClass.Children.Add(p); // 2. remove unneeded ones for(int i = serializationClass.Attributes.Count-1; i>=0; i-- ) if( serializationClass.Attributes[i] is SerializedDomainProperty) if(!serializedAttributePropertyToAdd.Contains(serializationClass.Attributes[i] as SerializedDomainProperty)) serializationClass.Attributes.RemoveAt(i); for (int i = serializationClass.Children.Count - 1; i >= 0; i--) if (serializationClass.Children[i] is SerializedDomainProperty) if (!serializedElementPropertyToAdd.Contains(serializationClass.Children[i] as SerializedDomainProperty)) serializationClass.Children.RemoveAt(i); }