private static bool ValidateTransition(NavigationDiagram navigationConfiguration, List<Dialog> dialogs) { var invalidTransitions = from d in dialogs from s in d.States from t in s.Transitions let n = Transition.GetLink(s.State, t.To.State) where n == null || n.Key != t.Key select t; return invalidTransitions.FirstOrDefault() == null; }
private static bool ValidateNavigation(NavigationDiagram navigationConfiguration, List<Dialog> dialogs) { var unconvertedNavigations = from s in navigationConfiguration.States from su in s.Successors let c = GetStateWrappersCanNavigateTo(dialogs, s, su).Count() where (c > 0 && c < GetStateWrappers(dialogs, s).Count()) || (c == 0 && dialogs.Where(d => d.Initial == su).FirstOrDefault() == null) || (c == 0 && Transition.GetLink(s, su).CanNavigateBack) select s; var unconvertedInitialStates = from s in navigationConfiguration.States where s.Initial && dialogs.Where(d => d.Initial == s).FirstOrDefault() == null select s; return unconvertedNavigations.FirstOrDefault() == null && unconvertedInitialStates.FirstOrDefault() == null; }
public List<Dialog> Convert(NavigationDiagram navigationDiagram) { Initials = new HashSet<State>(navigationDiagram.States.Where(s => s.Initial)); for (int i = 0; i < 3; i++) { AssignedStates.Clear(); RemovedTransitions.Clear(); Dialogs.Clear(); AssignStates(navigationDiagram); List<Transition> removed = RemovedTransitions.ToList(); TransitionCreated = true; while (TransitionCreated) { TransitionCreated = false; RestoreNavigations(navigationDiagram); RemovedTransitions.AddRange(removed); } CleanDialogs(); CleanStates(); Initials = new HashSet<State>(Dialogs.Select(d => d.Initial)); } return Dialogs; }
private void AssignStates(NavigationDiagram navigationDiagram) { int assignedCount = 0; while (AssignedStates.Count != navigationDiagram.States.Count) { CreateNavigations(GetNextStates(navigationDiagram).ToList()); if (assignedCount == AssignedStates.Count) { RemoveNavigation(navigationDiagram); } assignedCount = AssignedStates.Count; } }
private void RestoreNavigations(NavigationDiagram navigationDiagram) { foreach (Transition navigation in RemovedTransitions.Reverse<Transition>()) { AssignedStates.Clear(); AssignedStates.AddRange(navigationDiagram.States); AssignedStates.Remove(navigation.Successor); RemovedTransitions.Remove(navigation); AssignStates(navigationDiagram); } AssignedStates.Clear(); AssignStates(navigationDiagram); }
private void RemoveNavigation(NavigationDiagram navigationDiagram) { var removableNavigations = from s in navigationDiagram.States let p = ( from sp in GetPredecessors(s) where !AssignedStates.Contains(sp) select sp ) where !AssignedStates.Contains(s) orderby Initials.Contains(s) descending, p.Count(), s.Predecessors.Count descending, s.Order select new { state = s, unassignedPredecessors = p }; var navigation = removableNavigations.First(); RemovedTransitions.Add(Transition.GetLink(navigation.unassignedPredecessors.First(), navigation.state)); }
private IEnumerable<State> GetNextStates(NavigationDiagram navigationDiagram) { return from s in navigationDiagram.States let c = ( from sp in GetPredecessors(s) where !AssignedStates.Contains(sp) select sp ).Count() where !AssignedStates.Contains(s) && c == 0 select s; }
public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, NavigationDiagram modelRoot, string modelFileName, NavigationLanguageDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue) { #region Check Parameters if (serializationResult == null) throw new global::System.ArgumentNullException("serializationResult"); if (string.IsNullOrEmpty(modelFileName)) throw new global::System.ArgumentNullException("modelFileName"); if (diagram == null) throw new global::System.ArgumentNullException("diagram"); if (string.IsNullOrEmpty(diagramFileName)) throw new global::System.ArgumentNullException("diagramFileName"); #endregion if (serializationResult.Failed) return; // Save the model file first using (global::System.IO.MemoryStream modelFileContent = this.InternalSaveModel(serializationResult, modelRoot, modelFileName, encoding, writeOptionalPropertiesWithDefaultValue)) { if (serializationResult.Failed) return; using (global::System.IO.MemoryStream diagramFileContent = this.InternalSaveDiagram(serializationResult, diagram, diagramFileName, encoding, writeOptionalPropertiesWithDefaultValue)) { if (!serializationResult.Failed) { // Only write the contents if there's no error encountered during serialization. if (modelFileContent != null) { using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(modelFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None)) { using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding)) { writer.Write(modelFileContent.ToArray()); } } } if (diagramFileContent != null) { using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None)) { using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding)) { writer.Write(diagramFileContent.ToArray()); } } } } } } }
/// <summary> /// Saves the given diagram to the given file, with default encoding (UTF-8). /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">NavigationDiagram instance to be saved.</param> /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param> /// <param name="diagram">NavigationLanguageDiagram to be saved.</param> /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param> /// <param name="writeOptionalPropertiesWithDefaultValue">Whether optional properties with default value will be saved.</param> public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, NavigationDiagram modelRoot, string modelFileName, NavigationLanguageDiagram diagram, string diagramFileName, bool writeOptionalPropertiesWithDefaultValue) { this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, writeOptionalPropertiesWithDefaultValue); }
/// <summary> /// Saves the given diagram to the given file, with default encoding (UTF-8), and optional properties with default value will not /// be written out. /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">NavigationDiagram instance to be saved.</param> /// <param name="modelFileName">Name of the file in which the CanonicalSampleRoot instance will be saved.</param> /// <param name="diagram">NavigationLanguageDiagram to be saved.</param> /// <param name="diagramFileName">Name of the file in which the diagram will be saved.</param> public virtual void SaveModelAndDiagram(DslModeling::SerializationResult serializationResult, NavigationDiagram modelRoot, string modelFileName, NavigationLanguageDiagram diagram, string diagramFileName) { this.SaveModelAndDiagram(serializationResult, modelRoot, modelFileName, diagram, diagramFileName, global::System.Text.Encoding.UTF8, false); }
/// <summary> /// Helper method to create and initialize a new NavigationDiagram. /// </summary> internal protected virtual NavigationDiagram CreateModelHelper(DslModeling::Partition modelPartition) { NavigationDiagram model = new NavigationDiagram(modelPartition); return model; }
internal global::System.IO.MemoryStream InternalSaveModel(DslModeling::SerializationResult serializationResult, NavigationDiagram modelRoot, string fileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue) { #region Check Parameters global::System.Diagnostics.Debug.Assert(serializationResult != null); global::System.Diagnostics.Debug.Assert(modelRoot != null); global::System.Diagnostics.Debug.Assert(!serializationResult.Failed); #endregion serializationResult.Encoding = encoding; DslModeling::DomainXmlSerializerDirectory directory = this.GetDirectory(modelRoot.Store); global::System.IO.MemoryStream newFileContent = new global::System.IO.MemoryStream(); DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(directory, fileName, serializationResult); this.InitializeSerializationContext(modelRoot.Partition, serializationContext, false); // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here. serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue; global::System.Xml.XmlWriterSettings settings = NavigationLanguageSerializationHelper.Instance.CreateXmlWriterSettings(serializationContext, false, encoding); using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(newFileContent, settings)) { this.WriteRootElement(serializationContext, modelRoot, writer); } return newFileContent; }
/// <summary> /// Saves the given model root to the given file, with default encoding (UTF-8), and optional properties with default value will not /// be written out. /// </summary> /// <param name="serializationResult">Stores serialization result from the save operation.</param> /// <param name="modelRoot">NavigationDiagram instance to be saved.</param> /// <param name="fileName">Name of the file in which the NavigationDiagram instance will be saved.</param> public virtual void SaveModel(DslModeling::SerializationResult serializationResult, NavigationDiagram modelRoot, string fileName) { this.SaveModel(serializationResult, modelRoot, fileName, global::System.Text.Encoding.UTF8, false); }
private static void WriteChildElements(DslModeling::SerializationContext serializationContext, NavigationDiagram element, global::System.Xml.XmlWriter writer) { // NavigationDiagramHasStates global::System.Collections.ObjectModel.ReadOnlyCollection<NavigationDiagramHasStates> allNavigationDiagramHasStatesInstances = NavigationDiagramHasStates.GetLinksToStates(element); if (!serializationContext.Result.Failed && allNavigationDiagramHasStatesInstances.Count > 0) { writer.WriteStartElement("states"); global::System.Type typeofNavigationDiagramHasStates = typeof(NavigationDiagramHasStates); foreach (NavigationDiagramHasStates eachNavigationDiagramHasStatesInstance in allNavigationDiagramHasStatesInstances) { if (serializationContext.Result.Failed) break; if (eachNavigationDiagramHasStatesInstance.GetType() != typeofNavigationDiagramHasStates) { // Derived relationships will be serialized in full-form. DslModeling::DomainClassXmlSerializer derivedRelSerializer = serializationContext.Directory.GetSerializer(eachNavigationDiagramHasStatesInstance.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(derivedRelSerializer != null, "Cannot find serializer for " + eachNavigationDiagramHasStatesInstance.GetDomainClass().Name + "!"); derivedRelSerializer.Write(serializationContext, eachNavigationDiagramHasStatesInstance, writer); } else { // No need to serialize the relationship itself, just serialize the role-player directly. DslModeling::ModelElement targetElement = eachNavigationDiagramHasStatesInstance.Element; DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer(targetElement.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(targetSerializer != null, "Cannot find serializer for " + targetElement.GetDomainClass().Name + "!"); targetSerializer.Write(serializationContext, targetElement, writer); } } writer.WriteEndElement(); } }
/// <summary> /// Reads all instances of relationship NavigationDiagramHasStates. /// </summary> /// <remarks> /// The caller will position the reader at the open tag of the first XML element inside the relationship tag, so it can be /// either the first instance, or a bogus tag. This method will deserialize all instances and ignore all bogus tags. When the /// method returns, the reader will be positioned at the end tag of the relationship (or EOF if somehow that happens). /// </remarks> /// <param name="serializationContext">Serialization context.</param> /// <param name="element">In-memory NavigationDiagram instance that will get the deserialized data.</param> /// <param name="reader">XmlReader to read serialized data from.</param> private static void ReadNavigationDiagramHasStatesInstances(DslModeling::SerializationContext serializationContext, NavigationDiagram element, global::System.Xml.XmlReader reader) { while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { DslModeling::DomainClassXmlSerializer newStateOfNavigationDiagramHasStatesSerializer = serializationContext.Directory.GetSerializer(State.DomainClassId); global::System.Diagnostics.Debug.Assert(newStateOfNavigationDiagramHasStatesSerializer != null, "Cannot find serializer for State!"); State newStateOfNavigationDiagramHasStates = newStateOfNavigationDiagramHasStatesSerializer.TryCreateInstance(serializationContext, reader, element.Partition) as State; if (newStateOfNavigationDiagramHasStates != null) { element.States.Add(newStateOfNavigationDiagramHasStates); DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer (newStateOfNavigationDiagramHasStates.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert (targetSerializer != null, "Cannot find serializer for " + newStateOfNavigationDiagramHasStates.GetDomainClass().Name + "!"); targetSerializer.Read(serializationContext, newStateOfNavigationDiagramHasStates, reader); } else { global::System.Type typeofNavigationDiagramHasStates = typeof(NavigationDiagramHasStates); DslModeling::DomainRelationshipXmlSerializer newNavigationDiagramHasStatesSerializer = serializationContext.Directory.GetSerializer(NavigationDiagramHasStates.DomainClassId) as DslModeling::DomainRelationshipXmlSerializer; global::System.Diagnostics.Debug.Assert(newNavigationDiagramHasStatesSerializer != null, "Cannot find serializer for NavigationDiagramHasStates!"); NavigationDiagramHasStates newNavigationDiagramHasStates = newNavigationDiagramHasStatesSerializer.TryCreateInstance (serializationContext, reader, element.Partition) as NavigationDiagramHasStates; if (newNavigationDiagramHasStates != null) { if (newNavigationDiagramHasStates.GetType() == typeofNavigationDiagramHasStates) { // The relationship should be serialized in short-form. NavigationLanguageSerializationBehaviorSerializationMessages.ExpectingShortFormRelationship(serializationContext, reader, typeof(NavigationDiagramHasStates)); } DslModeling::DomainRoleInfo.SetRolePlayer (newNavigationDiagramHasStates, NavigationDiagramHasStates.NavigationDiagramDomainRoleId, element); DslModeling::DomainClassXmlSerializer targetSerializer = serializationContext.Directory.GetSerializer (newNavigationDiagramHasStates.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert (targetSerializer != null, "Cannot find serializer for " + newNavigationDiagramHasStates.GetDomainClass().Name + "!"); targetSerializer.Read(serializationContext, newNavigationDiagramHasStates, reader); } else { // Unknown element, skip DslModeling::SerializationUtilities.Skip(reader); } } } }
/// <summary> /// This method deserializes all child model elements. /// </summary> /// <remarks> /// The caller will position the reader at the open tag of the first child XML element to deserialized. /// This method will read as many child elements as it can. It returns under three circumstances: /// 1) When an unknown child XML element is encountered. In this case, this method will position the reader at the /// open tag of the unknown element. This implies that if the first child XML element is unknown, this method /// should return immediately and do nothing. /// 2) When all child XML elemnets are read. In this case, the reader will be positioned at the end tag of the parent element. /// 3) EOF. /// </remarks> /// <param name="serializationContext">Serialization context.</param> /// <param name="reader">XmlReader to read serialized data from.</param> /// <param name="element">In-memory NavigationDiagram instance that will get the deserialized data.</param> private static void ReadChildElements(DslModeling::SerializationContext serializationContext, NavigationDiagram element, global::System.Xml.XmlReader reader) { if (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element) { if (string.Compare(reader.LocalName, "states", global::System.StringComparison.CurrentCulture) == 0) { if (reader.IsEmptyElement) { // No instance of this relationship, just skip DslModeling::SerializationUtilities.Skip(reader); } else { DslModeling::SerializationUtilities.SkipToFirstChild(reader); // Skip the open tag of <states> ReadNavigationDiagramHasStatesInstances(serializationContext, element, reader); DslModeling::SerializationUtilities.Skip(reader); // Skip the close tag of </states> } } } }