private static void GenerateAssemblies(MgaFolder componentAssemblyFolder, MgaModel assembly, int level, MgaModel leaf) { var elementsOnEachLevel = 2; var numberOfLevels = 6; if (level <= numberOfLevels) { System.Console.Out.WriteLine(level); MgaFolder componentAssemblyChildFolder = componentAssemblyFolder.CreateFolder(componentAssemblyFolder.Project.RootMeta.RootFolder.DefinedFolderByName["ComponentAssemblies", true]); componentAssemblyChildFolder.Name = string.Format("ComponentAssembly Child folder {0}", level); for (int i = 0; i < elementsOnEachLevel; i++) { var childAssembly = componentAssemblyChildFolder.CreateRootObject(assembly.Meta) as MgaModel; childAssembly.Name = string.Format("ChildAssembly {0} {1}", level, i); var cref = assembly.CreateChildObject((assembly.Meta as GME.MGA.Meta.MgaMetaModel).RoleByName["ComponentRef"]) as MgaReference; cref.Name = string.Format("ComponentRef {0} {1}", level, i); if (level < numberOfLevels) { cref.Referred = childAssembly as MgaFCO; GenerateAssemblies(componentAssemblyFolder, childAssembly, level + 1, leaf); } else { cref.Referred = leaf as MgaFCO; } } } }
private static MgaFCO makeFCO(MgaModel parent, string role) { MgaFCO newFCO = parent.CreateChildObject((parent.Meta as MgaMetaModel).RoleByName[role]); newFCO.Name = role; return(newFCO); }
/// <summary> /// Given a container, find all ConnectorComposition connections, and make /// new connections between the new "standalone" ports that have been created. /// </summary> /// <param name="container"></param> private void ExpandConnectorCompositionChildren(MgaModel container) { Logger.WriteDebug("ExpandConnectorCompositionChildren: {0}", container.AbsPath); // Find PortComposition role for this parent type GME.MGA.Meta.MgaMetaRole role = null; foreach (GME.MGA.Meta.MgaMetaRole roleItem in (container.Meta as GME.MGA.Meta.MgaMetaModel).Roles) { if (roleItem.Name == "PortComposition") { role = roleItem; break; } } // For each ConnectorComposition, create new connections between the new "standalone" ports. // Since we did depth-first recursion in modifying the connectors, they should all be "expanded" // and ready to go. foreach (MgaSimpleConnection connectorComposition in container.GetChildrenOfKind("ConnectorComposition")) { MgaModel connector1 = connectorComposition.Src as MgaModel; MgaModel connector2 = connectorComposition.Dst as MgaModel; var portsConn1 = ConnectorToStandalonePortMap[connector1]; var portsConn2 = ConnectorToStandalonePortMap[connector2]; // For each port, find the analogue from the other connector. foreach (var port1 in portsConn1) { var kindPort1 = port1.SourcePort.MetaBase.Name; // Try to match by role & kind. var port2 = portsConn2.FirstOrDefault(p2 => p2.SourcePortRole == port1.SourcePortRole && p2.SourcePort.MetaBase.Name == kindPort1); // Nominal match case failed. Try alternatives. if (port2 == null) { // If we failed to match by the methods above, we'll try another method. // Try to see if each port's kind is unique to its connector (e.g.: They are the only ModelicaConnectors in their parent Connectors). // If so, then we will go ahead and match them, but yield a warning. Boolean port1KindIsUnique = portsConn1.Where(pc => kindPort1 == pc.SourcePort.MetaBase.Name).Count() == 1; var port2KindMatches = portsConn2.Where(pc => kindPort1 == pc.SourcePort.MetaBase.Name); Boolean port2KindIsUnique = port2KindMatches.Count() == 1; if (port1KindIsUnique && port2KindIsUnique) { // Match anyway based on unique kinds. port2 = port2KindMatches.First(); Logger.WriteWarning("Non-name match: " + "Port {0} in Connector {1} and Port {2} in Connector {3}", TraceabilityExtensions.ToMgaHyperLink(port1.SourcePort, Traceability), TraceabilityExtensions.ToMgaHyperLink(port1.SourceConnector, Traceability), TraceabilityExtensions.ToMgaHyperLink(port2.SourcePort, Traceability), TraceabilityExtensions.ToMgaHyperLink(port2.SourceConnector, Traceability)); } else { // Kinds were not unique, so we can't guess. Logger.WriteWarning("NO MATCH found for Port {0} of Connector {1} with any Port within Connector {2}", TraceabilityExtensions.ToMgaHyperLink(port1.SourcePort, Traceability), TraceabilityExtensions.ToMgaHyperLink(port1.SourceConnector, Traceability), TraceabilityExtensions.ToMgaHyperLink(connector2, Traceability)); continue; } } var conn = container.CreateChildObject(role) as MgaSimpleConnection; conn.SetSrc(EmptyArray, port1.StandalonePort); conn.SetDst(EmptyArray, port2.StandalonePort); } } }
private static MgaFCO makeFCO(MgaModel parent, string role) { MgaFCO newFCO = parent.CreateChildObject((parent.Meta as MgaMetaModel).RoleByName[role]); newFCO.Name = role; return newFCO; }
/// <summary> /// Given a container, find all ConnectorComposition connections, and make /// new connections between the new "standalone" ports that have been created. /// </summary> /// <param name="container"></param> private void ExpandConnectorCompositionChildren(MgaModel container) { Logger.WriteDebug("ExpandConnectorCompositionChildren: {0}", container.AbsPath); // Find PortComposition role for this parent type GME.MGA.Meta.MgaMetaRole role = null; foreach (GME.MGA.Meta.MgaMetaRole roleItem in (container.Meta as GME.MGA.Meta.MgaMetaModel).Roles) { if (roleItem.Name == "PortComposition") { role = roleItem; break; } } // For each ConnectorComposition, create new connections between the new "standalone" ports. // Since we did depth-first recursion in modifying the connectors, they should all be "expanded" // and ready to go. foreach (MgaSimpleConnection connectorComposition in container.GetChildrenOfKind("ConnectorComposition")) { MgaModel connector1 = connectorComposition.Src as MgaModel; MgaModel connector2 = connectorComposition.Dst as MgaModel; var portsConn1 = ConnectorToStandalonePortMap[connector1]; var portsConn2 = ConnectorToStandalonePortMap[connector2]; // For each port, find the analogue from the other connector. foreach (var port1 in portsConn1) { var kindPort1 = port1.SourcePort.MetaBase.Name; // Try to match by role & kind. var port2 = portsConn2.FirstOrDefault(p2 => p2.SourcePortRole == port1.SourcePortRole && p2.SourcePort.MetaBase.Name == kindPort1); // Nominal match case failed. Try alternatives. if (port2 == null) { // If we failed to match by the methods above, we'll try another method. // Try to see if each port's kind is unique to its connector (e.g.: They are the only ModelicaConnectors in their parent Connectors). // If so, then we will go ahead and match them, but yield a warning. Boolean port1KindIsUnique = portsConn1.Where(pc => kindPort1 == pc.SourcePort.MetaBase.Name).Count() == 1; var port2KindMatches = portsConn2.Where(pc => kindPort1 == pc.SourcePort.MetaBase.Name); Boolean port2KindIsUnique = port2KindMatches.Count() == 1; if (port1KindIsUnique && port2KindIsUnique) { // Match anyway based on unique kinds. port2 = port2KindMatches.First(); Logger.WriteWarning("Non-name match: " + "Port {0} in Connector {1} and Port {2} in Connector {3}", GmeConsoleHelper.ToMgaHyperLink(port1.SourcePort, Traceability), GmeConsoleHelper.ToMgaHyperLink(port1.SourceConnector, Traceability), GmeConsoleHelper.ToMgaHyperLink(port2.SourcePort, Traceability), GmeConsoleHelper.ToMgaHyperLink(port2.SourceConnector, Traceability)); } else { // Kinds were not unique, so we can't guess. Logger.WriteWarning("NO MATCH found for Port {0} of Connector {1} with any Port within Connector {2}", GmeConsoleHelper.ToMgaHyperLink(port1.SourcePort, Traceability), GmeConsoleHelper.ToMgaHyperLink(port1.SourceConnector, Traceability), GmeConsoleHelper.ToMgaHyperLink(connector2, Traceability)); continue; } } var conn = container.CreateChildObject(role) as MgaSimpleConnection; conn.SetSrc(EmptyArray, port1.StandalonePort); conn.SetDst(EmptyArray, port2.StandalonePort); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parent"></param> /// <param name="metaRef">meta ref of the new object</param> /// <param name="roleMetaRef">meta ref of the role (if the parent is a model)</param> /// <returns></returns> public static T CreateObject <T>( ISIS.GME.Common.Interfaces.Container parent, int metaRef, int roleMetaRef = 0) where T : ISIS.GME.Common.Classes.Base, new() { Contract.Requires(parent != null); T result = new T(); if (parent.Impl is MgaModel) { MgaModel model = parent.Impl as MgaModel; MgaMetaRole role = null; try { // try to use user defined role role = (model.MetaBase as MgaMetaModel). Roles. Cast <MgaMetaRole>(). FirstOrDefault(x => x.MetaRef == roleMetaRef); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Role was not found in the container."); sb.AppendLine("Paradigm violation."); sb.AppendFormat("Container type: {0} Requested role: {1}", parent.Kind, result.GetType().Name); throw new Exception(sb.ToString(), ex); } try { IMgaFCO fco = model.CreateChildObject(role); result.Impl = fco as IMgaObject; return(result); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("New element could not be created."); sb.AppendFormat("Container type: {0} Requested role: {1}", parent.Kind, result.GetType().Name); throw new Exception(sb.ToString(), ex); } } else if (parent.Impl is MgaFolder) { try { MgaFolder folder = parent.Impl as MgaFolder; MgaMetaFolder item = folder.MetaFolder. LegalChildFolders. Cast <MgaMetaFolder>(). FirstOrDefault(x => x.MetaRef == metaRef); if (item != null) { // create new folder MgaFolder f = folder.CreateFolder(item); result.Impl = f as IMgaObject; return(result); } else { MgaMetaFCO itemFco = folder.MetaFolder. LegalRootObjects. Cast <MgaMetaFCO>(). FirstOrDefault(x => x.MetaRef == metaRef); if (itemFco != null) { IMgaFCO f = folder.CreateRootObject(itemFco); result.Impl = f as IMgaObject; return(result); } } } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("New element could not be created in folder."); sb.AppendFormat("Container type: {0} Requested child type: {1}", parent.Kind, result.GetType().Name); throw new Exception(sb.ToString(), ex); } } return(null); }