/// <summary>
        /// No GUI and interactive elements are allowed within this function.
        /// </summary>
        /// <param name="parameters">Main parameters for this run and GUI configuration.</param>
        /// <returns>Result of the run, which contains a success flag.</returns>
        public IInterpreterResult Main(IInterpreterMainParameters parameters)
        {
            this.mainParameters = parameters;

            //this.runtime = new Queue<Tuple<string, TimeSpan>>();
            //this.runtime.Clear();
            this.result = new InterpreterResult()
            {
                Success = true
            };

            try
            {
                BOMVisitor visitor = new BOMVisitor()
                {
                    Logger = Logger,
                    Traceability = this.result.Traceability
                };

                String nameFCO = null;
                int designQuantity = 1;
                List<String> listSupplierAffinity = null;
                MgaGateway.PerformInTransaction(delegate
                {
                    // Call Elaborator
                    var elaboratorSuccess = this.CallElaborator(this.mainParameters.Project,
                                                                this.mainParameters.CurrentFCO,
                                                                this.mainParameters.SelectedFCOs,
                                                                this.mainParameters.StartModeParam);
                    this.UpdateSuccess("Elaborator", elaboratorSuccess);

                    // Parse design
                    var tb = CyPhyClasses.TestBench.Cast(parameters.CurrentFCO);
                    visitor.visit(tb);

                    nameFCO = this.mainParameters.CurrentFCO.Name;

                    var propDesignQuantity = tb.Children
                                               .PropertyCollection
                                               .FirstOrDefault(p => p.Name == "design_quantity");
                    if (propDesignQuantity != null)
                    {
                        int val;
                        if (int.TryParse(propDesignQuantity.Attributes.Value, out val))
                        {
                            designQuantity = val;
                        }
                        else
                        {
                            Logger.WriteWarning("The property <a href=\"mga:{0}\">{1}</a> has a non-integer value of \"{2}\". Setting to default of 1.",
                                                propDesignQuantity.ID,
                                                propDesignQuantity.Name,
                                                propDesignQuantity.Attributes.Value);
                        }
                    }
                    else
                    {
                        Logger.WriteWarning("No property named \"design_quantity\" found. Assuming quantity of 1.");
                    }

                    var metricPartCostPerDesign = tb.Children
                                                    .MetricCollection
                                                    .FirstOrDefault(m => m.Name == "part_cost_per_design");
                    if (metricPartCostPerDesign == null)
                    {
                        var manifest = AVM.DDP.MetaTBManifest.OpenForUpdate(this.mainParameters.OutputDirectory);
                        var metric = new AVM.DDP.MetaTBManifest.Metric()
                        {
                            Name = "part_cost_per_design",
                            DisplayedName = "Part Cost Per Design",
                            Unit = "USD",
                            GMEID = null
                        };
                        if (manifest.Metrics == null)
                        {
                            manifest.Metrics = new List<AVM.DDP.MetaTBManifest.Metric>();
                        }
                        manifest.Metrics.Add(metric);

                        manifest.Serialize(this.mainParameters.OutputDirectory);
                    }

                    var propSupplierAffinity = tb.Children
                                                 .PropertyCollection
                                                 .FirstOrDefault(p => p.Name == "supplier_affinity");
                    if (propSupplierAffinity != null &&
                        !String.IsNullOrWhiteSpace(propSupplierAffinity.Attributes.Value))
                    {
                        listSupplierAffinity = propSupplierAffinity.Attributes
                                                                   .Value
                                                                   .Split(',')
                                                                   .Select(s => s.Trim())
                                                                   .ToList();
                    }
                },
                transactiontype_enum.TRANSACTION_NON_NESTED,
                abort: true);

                // Serialize BOM to file
                var bom = visitor.bom;
                var filenameBom = nameFCO + "_bom.json";
                var pathBom = Path.Combine(this.mainParameters.OutputDirectory, 
                                           filenameBom);
                using (StreamWriter outfile = new StreamWriter(pathBom))
                {
                    outfile.Write(bom.Serialize());
                }

                // Create CostEstimationRequest
                var request = new MfgBom.CostEstimation.CostEstimationRequest()
                {
                    bom = bom,
                    design_quantity = designQuantity,
                    supplier_affinity = listSupplierAffinity
                };
                var filenameRequest = nameFCO + "_cost_estimate_request.json";
                var pathRequest = Path.Combine(this.mainParameters.OutputDirectory,
                                               filenameRequest);
                using (StreamWriter outfile = new StreamWriter(pathRequest))
                {
                    outfile.Write(request.Serialize());
                }

                var tbManifest = AVM.DDP.MetaTBManifest.OpenForUpdate(this.mainParameters.OutputDirectory);
                tbManifest.AddArtifact(filenameBom, "CyPhy2MfgBom::BOM");
                tbManifest.AddArtifact(filenameRequest, "CyPhy2MfgBom::CostEstimationRequest");
                tbManifest.Serialize(this.mainParameters.OutputDirectory);
                
                using (var batRunBomCostAnalysis = new StreamWriter(Path.Combine(this.mainParameters.OutputDirectory, 
                                                                                 "runBomCostAnalysis.bat")))
                {
                    batRunBomCostAnalysis.Write(CyPhy2MfgBom.Properties.Resources.runBomCostAnalysis);
                }
                this.result.RunCommand = "runBomCostAnalysis.bat";
                

                if (this.result.Success)
                {
                    Logger.WriteInfo("Generated files are here: <a href=\"file:///{0}\" target=\"_blank\">{0}</a>", this.mainParameters.OutputDirectory);
                    Logger.WriteInfo("CyPhy2MfgBom has finished. [SUCCESS: {0}, Labels: {1}]", this.result.Success, this.result.Labels);
                }
                else
                {
                    Logger.WriteError("CyPhy2MfgBom failed! See error messages above.");
                }
            }
            catch (Exception ex)
            {
                Logger.WriteError("Exception: {0}<br> {1}", ex.Message, ex.StackTrace);
            }
            return this.result;
        }
Exemple #2
0
        private IInterpreterResult _Main(IInterpreterMainParameters parameters)
        {
            this.mainParameters = parameters;

            //this.runtime = new Queue<Tuple<string, TimeSpan>>();
            //this.runtime.Clear();
            this.result = new InterpreterResult()
            {
                Success = true
            };

            try
            {
                BOMVisitor visitor = new BOMVisitor()
                {
                    Logger       = Logger,
                    Traceability = this.result.Traceability
                };

                String        nameFCO              = null;
                int           designQuantity       = 1;
                List <String> listSupplierAffinity = null;
                MgaGateway.PerformInTransaction(delegate
                {
                    // Call Elaborator
                    var elaboratorSuccess = this.CallElaborator(this.mainParameters.Project,
                                                                this.mainParameters.CurrentFCO,
                                                                this.mainParameters.SelectedFCOs,
                                                                this.mainParameters.StartModeParam);
                    this.UpdateSuccess("Elaborator", elaboratorSuccess);

                    // Parse design
                    var tb = CyPhyClasses.TestBench.Cast(parameters.CurrentFCO);
                    visitor.visit(tb);

                    nameFCO = this.mainParameters.CurrentFCO.Name;

                    var propDesignQuantity = tb.Children
                                             .PropertyCollection
                                             .FirstOrDefault(p => p.Name == "design_quantity");
                    if (propDesignQuantity != null)
                    {
                        int val;
                        if (int.TryParse(propDesignQuantity.Attributes.Value, out val))
                        {
                            designQuantity = val;
                        }
                        else
                        {
                            Logger.WriteWarning("The property <a href=\"mga:{0}\">{1}</a> has a non-integer value of \"{2}\". Setting to default of 1.",
                                                propDesignQuantity.ID,
                                                propDesignQuantity.Name,
                                                propDesignQuantity.Attributes.Value);
                        }
                    }
                    else
                    {
                        Logger.WriteWarning("No property named \"design_quantity\" found. Assuming quantity of 1.");
                    }

                    var metricPartCostPerDesign = tb.Children
                                                  .MetricCollection
                                                  .FirstOrDefault(m => m.Name == "part_cost_per_design");
                    if (metricPartCostPerDesign == null)
                    {
                        var manifest = AVM.DDP.MetaTBManifest.OpenForUpdate(this.mainParameters.OutputDirectory);
                        var metric   = new AVM.DDP.MetaTBManifest.Metric()
                        {
                            Name          = "part_cost_per_design",
                            DisplayedName = "Part Cost Per Design",
                            Unit          = "USD",
                            GMEID         = null
                        };
                        if (manifest.Metrics == null)
                        {
                            manifest.Metrics = new List <AVM.DDP.MetaTBManifest.Metric>();
                        }
                        manifest.Metrics.Add(metric);

                        manifest.Serialize(this.mainParameters.OutputDirectory);
                    }

                    var propSupplierAffinity = tb.Children
                                               .PropertyCollection
                                               .FirstOrDefault(p => p.Name == "supplier_affinity");
                    if (propSupplierAffinity != null &&
                        !String.IsNullOrWhiteSpace(propSupplierAffinity.Attributes.Value))
                    {
                        listSupplierAffinity = propSupplierAffinity.Attributes
                                               .Value
                                               .Split(',')
                                               .Select(s => s.Trim())
                                               .ToList();
                    }
                },
                                                transactiontype_enum.TRANSACTION_NON_NESTED,
                                                abort: true);

                // Serialize BOM to file
                var bom         = visitor.bom;
                var filenameBom = nameFCO + "_bom.json";
                var pathBom     = Path.Combine(this.mainParameters.OutputDirectory,
                                               filenameBom);
                using (StreamWriter outfile = new StreamWriter(pathBom))
                {
                    outfile.Write(bom.Serialize());
                }

                // Create CostEstimationRequest
                var request = new MfgBom.CostEstimation.CostEstimationRequest()
                {
                    bom               = bom,
                    design_quantity   = designQuantity,
                    supplier_affinity = listSupplierAffinity
                };
                var filenameRequest = nameFCO + "_cost_estimate_request.json";
                var pathRequest     = Path.Combine(this.mainParameters.OutputDirectory,
                                                   filenameRequest);
                using (StreamWriter outfile = new StreamWriter(pathRequest))
                {
                    outfile.Write(request.Serialize());
                }

                var tbManifest = AVM.DDP.MetaTBManifest.OpenForUpdate(this.mainParameters.OutputDirectory);
                tbManifest.AddArtifact(filenameBom, "CyPhy2MfgBom::BOM");
                tbManifest.AddArtifact(filenameRequest, "CyPhy2MfgBom::CostEstimationRequest");
                tbManifest.Serialize(this.mainParameters.OutputDirectory);

                using (var batRunBomCostAnalysis = new StreamWriter(Path.Combine(this.mainParameters.OutputDirectory,
                                                                                 "runBomCostAnalysis.bat")))
                {
                    batRunBomCostAnalysis.Write(CyPhy2MfgBom.Properties.Resources.runBomCostAnalysis);
                }
                this.result.RunCommand = "runBomCostAnalysis.bat";


                if (this.result.Success)
                {
                    Logger.WriteInfo("Generated files are here: <a href=\"file:///{0}\" target=\"_blank\">{0}</a>", this.mainParameters.OutputDirectory);
                    Logger.WriteInfo("CyPhy2MfgBom has finished. [SUCCESS: {0}, Labels: {1}]", this.result.Success, this.result.Labels);
                }
                else
                {
                    Logger.WriteError("CyPhy2MfgBom failed! See error messages above.");
                }
            }
            catch (Exception ex)
            {
                Logger.WriteError("Exception: {0}<br> {1}", ex.Message, ex.StackTrace);
            }
            return(this.result);
        }