public void Main(MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, ComponentStartMode startMode) { // TODO: Add your interpreter code GMEConsole.Out.WriteLine("Running Cyber Component Exporter..."); //// Get RootFolder //IMgaFolder rootFolder = project.RootFolder; //GMEConsole.Out.WriteLine(rootFolder.Name); // To use the domain-specific API: // Create another project with the same name as the paradigm name // Copy the paradigm .mga file to the directory containing the new project // In the new project, install the GME DSMLGenerator NuGet package (search for DSMLGenerator) // Add a Reference in this project to the other project // Add "using [ParadigmName] = ISIS.GME.Dsml.[ParadigmName].Classes.Interfaces;" to the top of this file // if (currentobj.Meta.Name == "KindName") // [ParadigmName].[KindName] dsCurrentObj = ISIS.GME.Dsml.[ParadigmName].Classes.[KindName].Cast(currentobj); // Adapted from CyPhyComponentExporter.cs //#region Prompt for Output Path //string startupPath = Path.GetDirectoryName(project.ProjectConnStr.Substring("MGA=".Length)); // Make sure Cyber output paths exist. String s_outPath = "."; if (!Directory.Exists(s_outPath + "\\Cyber")) { Directory.CreateDirectory(s_outPath + "\\Cyber"); } //#endregion GMEConsole.Info.WriteLine("Beginning Export..."); string filename = project.ProjectConnStr; if (filename.Length > 4) { filename = filename.Substring(4); } else { GMEConsole.Info.WriteLine("Invalid MGA connection string for project. Can not determine file name. Bailing out..."); throw new System.Exception("Invalid MGA connection string."); } HashSet<Cyber.ModelicaComponent> cyberComponentSet = null; if (currentobj != null) { cyberComponentSet = ComponentLister.getCyberComponentSet(currentobj); } else if (selectedobjs.Count > 0) { cyberComponentSet = new HashSet<Cyber.ModelicaComponent>(); foreach (MgaFCO mf in selectedobjs) { cyberComponentSet.UnionWith(ComponentLister.getCyberComponentSet(mf)); } } else { cyberComponentSet = ComponentLister.getCyberComponentSet(project.RootFolder); } // Debug foreach (Cyber.ModelicaComponent cyberComponent in cyberComponentSet) { GMEConsole.Info.WriteLine(""); GMEConsole.Info.Write("Processing Component ... " + cyberComponent.Path); AVMComponentBuilder avmComponentBuilder = new AVMComponentBuilder(); avmComponentBuilder.GMEConsole = GMEConsole; avmComponentBuilder.createAVMCyberModel(cyberComponent, filename); String s_outFilePath = String.Format("{0}\\{1}.component.acm", s_outPath, META2AVM_Util.UtilFuncs.Safeify(cyberComponent.Name)); META2AVM_Util.UtilFuncs.SerializeAvmComponent(avmComponentBuilder.getAVMComponent(), s_outFilePath); GMEConsole.Info.Write("... Exported " + cyberComponent.Path + " to " + s_outFilePath); } try { string[] path_parts = filename.Split('\\'); string filename_part = path_parts[path_parts.Length - 1]; path_parts[path_parts.Length - 1] = "Cyber"; string path_part = System.IO.Path.Combine(path_parts); //GMEConsole.Info.Write("Copying " + filename + " to " + System.IO.Path.Combine(path_part, filename_part) ); GMEConsole.Info.Write("Copying " + filename_part + " to " + System.IO.Path.Combine("Cyber", filename_part)); // copy the project file to the Cyber subdirectory System.IO.File.Copy(filename_part, System.IO.Path.Combine("Cyber", filename_part), true); } catch (Exception err_copy_model_file) { GMEConsole.Error.Write("Error copying model file to Cyber subdirectory: " + err_copy_model_file.Message); return; } // Try to create the corresponding XML file InvokeUDMCopy invokeUdmCopy = new InvokeUDMCopy(); if (invokeUdmCopy.GenerateXML(project, filename) == true) { GMEConsole.Out.WriteLine("Finished XML generation."); } else { GMEConsole.Out.WriteLine("XML not generated."); } GMEConsole.Info.WriteLine("Cyber Component Exporter finished!"); }
public static int Main(String[] args) { if (args.Length < 1) { Console.Out.WriteLine("Usage: <program> CyberFile.mga [-f] [outputDirectory]"); Environment.Exit(1); } MgaProject mgaProject = null; string outputDirectory = "."; bool flat = false; String mgaFilePath = null; for (int ix = 0; ix < args.Length; ++ix) { if (args[ix] == "-f") { flat = true; } else if (mgaProject == null) { mgaProject = CyberModelUtil.MgaUtil.GetProject(args[ix]); mgaFilePath = args[ix]; } else { outputDirectory = args[ix]; } } if (mgaProject == null) { Console.Out.WriteLine("Usage: <program> CyberFile.mga [-f] [outputDirectory]"); Environment.Exit(1); } MgaGateway mgaGateway = new MgaGateway(mgaProject); mgaProject.CreateTerritoryWithoutSink(out mgaGateway.territory); bool bExceptionOccurred = false; mgaGateway.PerformInTransaction(delegate { try { Cyber.RootFolder cyberRootFolder = ISIS.GME.Common.Utils.CreateObject<CyberClasses.RootFolder>(mgaProject.RootFolder); HashSet<Cyber.ModelicaComponent> cyberComponentSet = ComponentLister.getCyberComponentSet(cyberRootFolder); //ParallelOptions options = new ParallelOptions(); //options.MaxDegreeOfParallelism = 4; //var ruleChecker = new DesignConsistencyChecker.Framework.Checker(null, mgaProject); //ruleChecker.RegisterRuleDll("DesignConsistencyChecker.dll"); //var uniqueNameRule = ruleChecker.GetRegisteredRules.FirstOrDefault(x => x.Name == "UniquePPMNames"); int i_Counter = 1; foreach (var cyberComponent in cyberComponentSet) { try { //if (uniqueNameRule != null) //{ // var feedBacks = uniqueNameRule.Check((MgaFCO)cyberComponent.Impl).ToList(); // if (feedBacks.Any(x => x.FeedbackType == FeedbackTypes.Error)) // { // foreach (var fb in feedBacks.Where(x => x.FeedbackType == FeedbackTypes.Error)) // { // Console.Error.WriteLine("Rule {0} failed: {1}", uniqueNameRule.Name, fb.Message); // } // continue; // } //} //foreach (Cyber.ModelicaComponent cyberComponent in cyberComponentList) { AVMComponentBuilder avmComponentBuilder = new AVMComponentBuilder(); avmComponentBuilder.createAVMCyberModel(cyberComponent, mgaFilePath); avm.Component avmComponent = avmComponentBuilder.getAVMComponent(); string componentPath = null; if (flat) { componentPath = outputDirectory; } else { componentPath = cyberComponent.Path; componentPath = outputDirectory + "/" + componentPath.Substring(componentPath.IndexOf("/")); } Directory.CreateDirectory(componentPath); // delete below //if (!flat) //{ // Directory.CreateDirectory(componentPath + "/images"); // //Directory.CreateDirectory( componentPath + "/Cyber" ); // Directory.CreateDirectory(componentPath + "/doc"); // Directory.CreateDirectory(componentPath + "/CAD"); //} String s_outFilePath = String.Format("{0}/{1}.component.acm", componentPath, META2AVM_Util.UtilFuncs.Safeify(cyberComponent.Name)); META2AVM_Util.UtilFuncs.SerializeAvmComponent(avmComponent, s_outFilePath); //StreamWriter streamWriter = new StreamWriter(s_outFilePath); //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(avm.Component), META2AVM_Util.UtilFuncs.getAVMClasses()); //serializer.Serialize(streamWriter, avmComponent); //streamWriter.Close(); Console.Out.WriteLine(string.Format("({0}/{1}) {2}", i_Counter++, cyberComponentSet.Count, META2AVM_Util.UtilFuncs.Safeify(cyberComponent.Name))); //} } catch (Exception ex) { Console.Error.WriteLine("Exception: {0}", ex.Message.ToString()); Console.Error.WriteLine("Stack: {0}", ex.StackTrace.ToString()); bExceptionOccurred = true; } } } catch (Exception ex) { Console.Error.WriteLine("Exception: {0}", ex.Message.ToString()); Console.Error.WriteLine("Stack: {0}", ex.StackTrace.ToString()); bExceptionOccurred = true; } }); mgaProject.Close(); if (bExceptionOccurred) return -1; return 0; }