Esempio n. 1
0
 public static MetaAvmProject Create(MgaProject project)
 {
     return(MetaAvmProject.Create(MgaExtensions.MgaExtensions.GetProjectDirectoryPath(project), project));
 }
Esempio n. 2
0
        /// <summary>
        /// Call this function within a transaction!
        /// </summary>
        /// <param name="outputDirectory"></param>
        /// <param name="project"></param>
        /// <param name="rootDS"></param>
        /// <param name="textWriter"></param>
        /// <returns></returns>
        public static MetaAvmProject Create(
            string outputDirectory,
            MgaProject project            = null,
            MgaModel rootDS               = null,
            GME.CSharp.GMEConsole console = null
            )
        {
            MetaAvmProject avmProj = new MetaAvmProject();

            outputDirectory = Path.GetFullPath(outputDirectory);
            Directory.CreateDirectory(outputDirectory);

            string avmProjFileName = Path.GetFullPath(Path.Combine(outputDirectory, "manifest.project.json"));

            using (new MutexWrapper(avmProjFileName))
            {
                if (File.Exists(avmProjFileName))
                {
                    string sjson = "{}";
                    using (StreamReader reader = new StreamReader(avmProjFileName))
                    {
                        sjson = reader.ReadToEnd();

                        try
                        {
                            avmProj = JsonConvert.DeserializeObject <AVM.DDP.MetaAvmProject>(sjson);
                        }
                        catch (Newtonsoft.Json.JsonReaderException ex)
                        {
                            throw new Exception(string.Format("{0} file is probably malformed. Not a valid json. {1}{2}", Path.GetFullPath(avmProjFileName), Environment.NewLine, ex.Message));
                        }
                    }
                }
            }

            if (console != null)
            {
                avmProj.infoTextWriter  = console.Info;
                avmProj.errorTextWriter = console.Error;
            }

            avmProj.OutputDirectory = outputDirectory;
            avmProj.m_filename      = avmProjFileName;


            avmProj.Project.LastModified = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");

            if (avmProj.Project.Results.UrlHints.Contains("./results/results.metaresults.json") == false)
            {
                avmProj.Project.Results.UrlHints.Add("./results/results.metaresults.json");
            }

            if (avmProj.Project.Requirements.UrlHints.Contains("./requirements/requirements.json") == false)
            {
                avmProj.Project.Requirements.UrlHints.Add("./requirements/requirements.json");
            }

            string reqSourceRepository = string.Empty;
            string reqId   = string.Empty;
            string reqText = string.Empty;

            if (project != null)
            {
                avmProj.Project.CyPhyProjectFileName = MgaExtensions.MgaExtensions.GetProjectName(project);

                CyPhy.RootFolder rf = ISIS.GME.Dsml.CyPhyML.Classes.RootFolder.GetRootFolder(project);

                var topLevelLinks = rf.Children.RequirementsCollection
                                    .SelectMany(x => x.Children.RequirementLinkCollection);

                var defaults   = topLevelLinks.Where(x => x.Name.ToLowerInvariant() == "default");
                var reqDefault = defaults.FirstOrDefault();
                if (defaults.Count() > 1)
                {
                    // more requ found first one will be used
                    string msg = string.Format("More than one Requirement was not found {0} in use.", reqDefault.Impl.AbsPath);
                    Trace.TraceWarning(msg);
                    avmProj.infoTextWriter.WriteLine(msg);
                }

                if (reqDefault != null)
                {
                    reqSourceRepository = reqDefault.Attributes.SourceRepository;
                    reqId   = reqDefault.Attributes.ID;
                    reqText = reqDefault.Attributes.Text;
                }
                else
                {
                    // requ not found
                    string msg = "Requirement was not found.";
                    Trace.TraceWarning(msg);

                    // RESTORE THIS MESSAGE ONCE WE SWITCH TO USING GMELOGGER
                    // avmProj.infoTextWriter.WriteLine(msg);
                }
            }

            avmProj.Project.Requirements.vfLink = reqSourceRepository;
            avmProj.Project.Requirements.id     = reqId;
            avmProj.Project.Requirements.text   = reqText;

            string dirName = Path.Combine(outputDirectory, "requirements");

            if (Directory.Exists(dirName) == false)
            {
                Directory.CreateDirectory(dirName);
                string requFileName = Path.GetFullPath(Path.Combine(dirName, "requirements.json"));

                using (new MutexWrapper(requFileName))
                {
                    if (File.Exists(requFileName) == false)
                    {
                        using (StreamWriter writer = new StreamWriter(requFileName))
                        {
                            // default requirement
                            writer.WriteLine(@"{");
                            writer.WriteLine("    \"name\": \"Undefined\",");
                            writer.WriteLine("    \"children\": []");
                            writer.WriteLine(@"}");
                        }
                    }
                }
            }

            string designSpaceFolder = "design-space";

            dirName = Path.GetFullPath(Path.Combine(outputDirectory, designSpaceFolder));

            Directory.CreateDirectory(dirName);

            if (rootDS != null)
            {
                // export designspace
                var dsProjectJsonLink = Path.Combine(".", designSpaceFolder, rootDS.Name + ".adm").Replace('\\', '/');
                var dsFileName        = Path.Combine(dirName, rootDS.Name + ".adm");

                if (avmProj.Project.DesignSpaceModels.Contains(dsProjectJsonLink) == false)
                {
                    avmProj.Project.DesignSpaceModels.Add(dsProjectJsonLink);
                }
                var designContainer = ISIS.GME.Dsml.CyPhyML.Classes.DesignContainer.Cast(rootDS);
                var design          = CyPhy2DesignInterchange.CyPhy2DesignInterchange.Convert(designContainer);
                using (new MutexWrapper(dsFileName))
                {
                    design.SaveToFile(dsFileName);
                }
            }

            return(avmProj);
        }
Esempio n. 3
0
        /// <summary>
        /// Call this function within a transaction!
        /// </summary>
        /// <param name="outputDirectory"></param>
        /// <param name="project"></param>
        /// <param name="rootDS"></param>
        /// <param name="textWriter"></param>
        /// <returns></returns>
        public static MetaAvmProject Create(
            string outputDirectory,
            MgaProject project = null,
            MgaModel rootDS = null,
            GME.CSharp.GMEConsole console = null
            )
        {
            MetaAvmProject avmProj = new MetaAvmProject();

            string currentDir = Environment.CurrentDirectory;

            Directory.SetCurrentDirectory(outputDirectory);

            string avmProjFileName = Path.GetFullPath(Path.Combine(outputDirectory, "manifest.project.json"));

            if (File.Exists(avmProjFileName))
            {
                string sjson = "{}";
                using (StreamReader reader = new StreamReader(avmProjFileName))
                {
                    sjson = reader.ReadToEnd();
                    avmProj = JsonConvert.DeserializeObject<AVM.DDP.MetaAvmProject>(sjson);
                }
            }

            if (console != null)
            {
                avmProj.infoTextWriter = console.Info;
                avmProj.errorTextWriter = console.Error;
            }

            avmProj.m_filename = avmProjFileName;

            avmProj.Project.LastModified = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");

            if (avmProj.Project.Results.UrlHints.Contains("./results/results.metaresults.json") == false)
            {
                avmProj.Project.Results.UrlHints.Add("./results/results.metaresults.json");
            }

            if (avmProj.Project.Requirements.UrlHints.Contains("./requirements/requirements.json") == false)
            {
                avmProj.Project.Requirements.UrlHints.Add("./requirements/requirements.json");
            }

            string reqSourceRepository = string.Empty;
            string reqId = string.Empty;
            string reqText = string.Empty;

            if (project != null)
            {
                CyPhy.RootFolder rf = ISIS.GME.Dsml.CyPhyML.Classes.RootFolder.GetRootFolder(project);

                var topLevelLinks = rf.Children.RequirementsCollection
                    .SelectMany(x => x.Children.RequirementLinkCollection);

                var defaults = topLevelLinks.Where(x => x.Name.ToLowerInvariant() == "default");
                var reqDefault = defaults.FirstOrDefault();
                if (defaults.Count() > 1)
                {
                    // more requ found first one will be used
                    string msg = string.Format("More than one Requirement was not found {0} in use.", reqDefault.Impl.AbsPath);
                    Trace.TraceWarning(msg);
                    avmProj.infoTextWriter.WriteLine(msg);

                }

                if (reqDefault != null)
                {
                    reqSourceRepository = reqDefault.Attributes.SourceRepository;
                    reqId = reqDefault.Attributes.ID;
                    reqText = reqDefault.Attributes.Text;
                }
                else
                {
                    // requ not found
                    string msg = "Requirement was not found.";
                    Trace.TraceWarning(msg);
                    avmProj.infoTextWriter.WriteLine(msg);
                }
            }

            avmProj.Project.Requirements.vfLink = reqSourceRepository;
            avmProj.Project.Requirements.id = reqId;
            avmProj.Project.Requirements.text = reqText;

            string dirName = Path.GetFullPath(Path.Combine(outputDirectory, "requirements"));

            if (Directory.Exists(dirName) == false)
            {
                Directory.CreateDirectory(dirName);
                string requFileName = Path.GetFullPath(Path.Combine(dirName, "requirements.json"));

                if (File.Exists(requFileName) == false)
                {
                    using (StreamWriter writer = new StreamWriter(requFileName))
                    {
                        // default requirement
                        writer.WriteLine(@"{");
                        writer.WriteLine("    \"name\": \"Undefined\",");
                        writer.WriteLine("    \"children\": []");
                        writer.WriteLine(@"}");
                    }
                }
            }

            string designSpaceFolder = "design-space";
            dirName = Path.GetFullPath(Path.Combine(outputDirectory, designSpaceFolder));

            if (Directory.Exists(dirName) == false)
            {
                Directory.CreateDirectory(dirName);
            }

            if (rootDS != null)
            {
                // export designspace
                var dsProjectJsonLink = Path.Combine(".", designSpaceFolder, rootDS.Name + ".metadesign.json").Replace('\\', '/');
                var dsFileName = Path.Combine(dirName, rootDS.Name + ".metadesign.json");

                if (avmProj.Project.DesignSpaceModels.Contains(dsProjectJsonLink) == false)
                {
                    avmProj.Project.DesignSpaceModels.Add(dsProjectJsonLink);
                }
                var designContainer = ISIS.GME.Dsml.CyPhyML.Classes.DesignContainer.Cast(rootDS);
                var design = CyPhy2DesignInterchange.CyPhy2DesignInterchange.Convert(designContainer);
                string d = design.SerializeToFile(Path.GetFullPath(dsFileName));
            }

            Directory.SetCurrentDirectory(currentDir);
            return avmProj;
        }