public bool Validate(string nameOrID)
        {
            Guid simulationId = Guid.Empty;
            bool isGuid       = Guid.TryParse(nameOrID, out simulationId);

            bool all = SimulationQueryParameters.GetRecurse(false);
            ISimulationProducerContract contract = null;

            try
            {
                contract = ProducerSimulationContract.Get(nameOrID);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Get Simulation {0}: {1} {2}",
                                           nameOrID, ex.Message, ex.StackTrace.ToString());
                if (ex.InnerException != null)
                {
                    msg += String.Format("    InnerException:    {0} {1}",
                                         ex.InnerException.Message, ex.InnerException.StackTrace.ToString());
                }
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }
            return(isGuid ? contract.Validate() : contract.ValidateAll());
        }
        public void TestSimulationProducerContract()
        {
            var app = ApplicationProducerContract.Get("AspenPlus");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "AspenPlus");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open("mea.bkp", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("aspenfile", data, "plain/text");
            data = null;
            using (var fstream = File.Open("mea-sinter.txt", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");
        }
        public void TestSimulationProducerConsumerContracts()
        {
            var simulation_name = "TestSimulationProducerConsumerContracts";
            var input_name      = "configuration";
            ISimulationProducerContract contract = AspenSimulationContract.Create(simulation_name, "ACM");

            using (var container = new Turbine.Data.TurbineModelContainer())
            {
                var obj   = container.Simulations.Single <Turbine.Data.Simulation>(s => s.Name == simulation_name);
                var input = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Assert.IsNull(input.Hash);
                Assert.IsNull(input.Content);
                Debug.WriteLine("SimulationStagedInput: " + input.Name, this.GetType().Name);

                input_name = "aclm";
                input      = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Assert.IsNull(input.Hash);
                Assert.IsNull(input.Content);
                Debug.WriteLine("SimulationStagedInput: " + input.Name, this.GetType().Name);
            }

            input_name = "configuration";
            contract   = AspenSimulationContract.Get(simulation_name);
            byte[] data     = Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            var    provider = System.Security.Cryptography.MD5CryptoServiceProvider.Create();

            byte[] hash = provider.ComputeHash(data);
            var    sb   = new StringBuilder();

            foreach (byte b in hash)
            {
                sb.Append(b.ToString("X2"));
            }
            string hval = sb.ToString();

            var  content_type = "plain/text";
            bool success      = contract.UpdateInput(input_name, data, content_type);

            using (var container = new Turbine.Data.TurbineModelContainer())
            {
                var obj   = container.Simulations.OrderByDescending(q => q.Create).First <Turbine.Data.Simulation>(s => s.Name == simulation_name);
                var input = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Debug.WriteLine(String.Format("SimulationStagedInput: {0}, {1}, {2}", input.Name, input.Hash, Encoding.UTF8.GetString(input.Content)),
                                this.GetType().Name);
                Assert.AreEqual(input.Hash, hval);
                Assert.AreEqual(Encoding.UTF8.GetString(input.Content), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            }
        }
示例#4
0
        public Simulation UpdateSimulation(string name, Simulation sim)
        {
            ISimulationProducerContract contract = null;

            if (sim == null)
            {
                string msg = String.Format("No Application specified in Simulation {0}", name);
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            string appname = sim.Application;

            try
            {
                contract = Turbine.Lite.Web.Resources.Contracts.ProducerSimulationContract.Create(name, appname);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Create Simulation {0}: {1} {2}",
                                           name, ex.Message, ex.StackTrace.ToString());
                if (ex.InnerException != null)
                {
                    msg += String.Format("    InnerException:    {0} {1}",
                                         ex.InnerException.Message, ex.InnerException.StackTrace.ToString());
                }
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            sim = DataMarshal.GetSimulation(name);

            return(sim);
        }
        public void TestDBJobQueue()
        {
            ISimulationProducerContract simulation_contract = Utility.CreateExcelSimulation("exceltest.xlsm", "exceltest-sinter.json");

            var producerJob = simulation_contract.NewJob(Guid.NewGuid(), false, false);

            producerJob.Submit();

            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            //Assert.IsNull(contract.Queue);
            contract.Register();

            IJobQueue queue       = new Turbine.Consumer.Data.Contract.DBJobQueue(new string[] { "excel" });
            var       consumerJob = queue.GetNext();

            Assert.IsTrue(consumerJob.ApplicationName == "excel",
                          String.Format("Wrong Application {0}", consumerJob.ApplicationName));
        }
        public string CreateSimulation(string appname)
        {
            string name = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["simulation"];

            if (string.IsNullOrEmpty(name))
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.QUERYPARAM_ERROR,
                    "Required parameter \"simulation\":  Provide unique name for factory method."
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }

            ISimulationProducerContract contract = null;

            try
            {
                contract = AspenSimulationContract.Create(name, appname);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Create Simulation {0}: {1} {2}",
                                           name, ex.Message, ex.StackTrace.ToString());
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            //Simulation sim = DataMarshal.GetSimulation(name);
            var uri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri;

            return(new Uri(uri, String.Format("../simulation/{0}", name)).ToString());
        }
        public void TestWrongParameter()
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open("exceltest.xlsm", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("wrongparameter", data, "plain/text");
        }
        internal static ISimulationProducerContract CreateExcelSimulation(String spreadsheet, String sinter)
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open(spreadsheet, FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("spreadsheet", data, "plain/text");
            data = null;
            using (var fstream = File.Open(sinter, FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");

            return(simulation_contract);
        }
        /*  AppendJobs
         *
         * Each job request has a "Simulation" and "Input"
         *
         */
        public List <int> AppendJobs(string sessionID, System.IO.Stream data)
        {
            Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name);
            Object name       = "";
            Object inputs     = "";
            Object initialize = false;
            Object reset      = true;

            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();
            List <Dictionary <string, Object> > jobsInputList =
                Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json);
            List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >();
            List <string> simulationList = new List <string>();
            List <bool>   initializeList = new List <bool>();
            List <bool>   resetList      = new List <bool>();

            foreach (var inputData in jobsInputList)
            {
                if (!inputData.TryGetValue("Simulation", out name))
                {
                    Debug.WriteLine("Missing simulation name", this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Simulation is required"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                if (!inputData.TryGetValue("Input", out inputs))
                {
                    Debug.WriteLine("Missing input data", this.GetType().Name);
                    throw new ArgumentException("No Input data name");
                }

                if (!inputData.TryGetValue("Initialize", out initialize))
                {
                    initialize = false;
                }
                if (!inputData.TryGetValue("Reset", out reset))
                {
                    reset = true;
                }

                Newtonsoft.Json.Linq.JObject inputsD = null;
                try
                {
                    inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Input is required to be a dictionary"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                Dictionary <String, Object> dd = new Dictionary <string, object>();
                foreach (var v in inputsD)
                {
                    Debug.WriteLine("VALUE: " + v);
                    dd[v.Key] = v.Value;
                }
                jobDictList.Add(dd);
                simulationList.Add(String.Format("{0}", name));
                try
                {
                    initializeList.Add((bool)initialize);
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key initialize is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                try
                {
                    resetList.Add((bool)reset);
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Reset is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
            }

            List <int> jobList = new List <int>();

            // Formatting complete
            for (int i = 0; i < jobDictList.Count; i++)
            {
                ISimulationProducerContract contract = null;
                string simulationName          = simulationList[i];
                Dictionary <String, Object> dd = jobDictList[i];

                try
                {
                    contract = AspenSimulationContract.Get(simulationName);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to get Simulation {0}: {1}",
                                               name, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                IJobProducerContract jobContract;
                try
                {
                    jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i]);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to create new job {0}: {1}",
                                               i, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                jobContract.Process.Input = dd;
                jobList.Add(jobContract.Id);
            }

            return(jobList);
        }
示例#10
0
        public Stream CreateJob(Stream data)
        {
            Debug.WriteLine("create new job", this.GetType().Name);
            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();

            Debug.WriteLine("JSON: " + json, this.GetType().Name);
            Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json);
            Object name = "";
            Guid   sessionID;

            if (!inputData.TryGetValue("Session", out name))
            {
                Debug.WriteLine("Missing Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Session is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }
            try
            {
                sessionID = Guid.Parse((string)name);
            }
            catch (Exception)
            {
                Debug.WriteLine("Bad Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Bad Session ID, must be GUID"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("Simulation", out name))
            {
                Debug.WriteLine("Missing simulation name", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Simulation is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }


            Object tmp = false;

            if (!inputData.TryGetValue("initialize", out tmp))
            {
                tmp = false;
            }
            bool initialize;

            try
            {
                initialize = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key initialize is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("reset", out tmp))
            {
                tmp = true;
            }
            bool reset;

            try
            {
                reset = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key reset is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            Debug.WriteLine("Retrieve Simulation Contract: " + name, this.GetType().Name);
            ISimulationProducerContract contract = null;

            try
            {
                contract = AspenSimulationContract.Get((string)name);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to get Simulation {0}: {1}",
                                           name, ex.StackTrace.ToString());
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Object inputs;

            if (!inputData.TryGetValue("Inputs", out inputs))
            {
                Debug.WriteLine("Missing input data", this.GetType().Name);
                throw new ArgumentException("No Input data name");
            }
            Debug.WriteLine("Found Inputs", this.GetType().Name);
            Newtonsoft.Json.Linq.JObject inputsD = null;
            try
            {
                inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Inputs is required to be a dictionary"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Debug.WriteLine("Cast Inputs", this.GetType().Name);

            Dictionary <String, Object> dd = new Dictionary <string, object>();

            foreach (var v in inputsD)
            {
                Debug.WriteLine("VALUE: " + v);
                dd[v.Key] = v.Value;
            }
            var jobContract = contract.NewJob(sessionID, initialize, reset);

            jobContract.Process.Input = dd;

            var d = DataMarshal.GetJobDict(jobContract.Id, true);

            json = Newtonsoft.Json.JsonConvert.SerializeObject(d);
            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
        public void TestSessionProducerConsumerContracts()
        {
            // producers
            ISessionProducerContract producer = new AspenSessionProducerContract();
            Guid session_id = producer.Create();

            Assert.IsNotNull(session_id);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("testX", "AspenPlus");

            var bytes = File.ReadAllBytes("mea-sinter.txt");

            Assert.IsNotNull(bytes);
            Assert.IsTrue(bytes.Length > 0);
            System.Threading.Thread.Sleep(100);
            simulation_contract.UpdateInput("configuration", bytes, "plain/text");

            bytes = File.ReadAllBytes("mea.bkp");
            Assert.IsNotNull(bytes);
            Assert.IsTrue(bytes.Length > 0);
            System.Threading.Thread.Sleep(100);
            simulation_contract.UpdateInput("aspenfile", bytes, "plain/text");

            IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            contract.Register();

            IJobConsumerContract job = contract.Queue.GetNext();

            Assert.IsNull(job);
            job_producer_contract.Submit();
            job = contract.Queue.GetNext();
            Assert.IsNotNull(job);
            Assert.AreEqual(job.Id, job_producer_contract.Id);


            job.Setup();

            SimpleFile backup = null;
            SimpleFile config = null;

            foreach (var f in job.GetSimulationInputFiles())
            {
                if (f.name == "configuration")
                {
                    config = f;
                }
                else
                {
                    backup = f;
                }
            }

            Assert.AreEqual(config.name, "configuration");

            String filename = SinterHelperFunctions
                              .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content));

            Assert.AreEqual(backup.name, "aspenfile");


            // NEED TO SET INPUT Before Setting to Run
            //j
            try
            {
                job.Running();
                Assert.Fail("Job.Process.Input Must be set before setting job to run");
            }
            catch (InvalidStateChangeException) {}
            job.Process.Input = new Dictionary <string, Object>();
            job.Running();
            job.Success();

            job = contract.Queue.GetNext();
            Assert.IsNull(job);
        }
        public void TestJobProducerConsumerContractsSetupFailUnRegisterConsumer()
        {
            // Create test Simulation
            TestSimulationProducerContract();

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Get("test");
            ISessionProducerContract    producer            = new AspenSessionProducerContract();
            Guid session_id = producer.Create();
            IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            // consumers

            /*
             * IJobQueue queue = AppUtility.GetJobQueue();
             * IJobConsumerContract job = queue.GetNext();
             * Assert.IsNull(job);
             * job_producer_contract.Submit();
             * job = queue.GetNext();
             * Assert.IsNotNull(job);
             */
            // NHibernate
            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            Assert.IsNull(contract.Queue);
            contract.Register();
            Assert.IsNotNull(contract.Queue);
            Assert.IsNull(contract.Queue.GetNext());  // nothing on queue

            job_producer_contract.Submit();
            IJobConsumerContract job = contract.Queue.GetNext();

            Assert.IsNotNull(job);
            contract.UnRegister();

            job.Setup();

            SimpleFile backup = null;
            SimpleFile config = null;

            foreach (var f in job.GetSimulationInputFiles())
            {
                if (f.name == "configuration")
                {
                    config = f;
                }
                else
                {
                    backup = f;
                }
            }

            Assert.AreEqual(config.name, "configuration");

            String filename = SinterHelperFunctions
                              .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content));

            Assert.AreEqual(backup.name, "");

            //Assert(,
            //IJobConsumerContract consumer = AppUtility.GetJobConsumerContract(id);
        }
        /// <summary>
        /// Copy Session to a new session with the desired job state.
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public Dictionary <string, string> Copy(Guid sessionId, string state)
        {
            ISimulationProducerContract contract = null;

            List <int> jobList = new List <int>();

            Guid   sessionIdNew = Guid.NewGuid();
            string owner        = Container.GetAppContext().UserName;

            using (var db = new ProducerContext())
            {
                User user    = db.Users.Single <User>(s => s.Name == owner);
                var  session = new Session()
                {
                    Id     = sessionIdNew,
                    User   = user,
                    Create = DateTime.UtcNow
                };
                db.Sessions.Add(session);

                db.SaveChanges();
            }


            using (var db = new ProducerContext())
            {
                var sessionJobs = db.Jobs.Where(j => j.SessionId == sessionId).Where(j => j.State == state);

                Debug.WriteLine("Simulation Name: " + sessionJobs.First().Simulation.Name, "ProducerSessionContract.Copy");

                contract = ProducerSimulationContract.Get(sessionJobs.First().Simulation.Name);


                foreach (var job in sessionJobs)
                {
                    Debug.WriteLine("job: " + job.Count.ToString(), "ProducerSessionContract.Copy");
                    var input = Newtonsoft.Json.Linq.JObject.Parse(job.Process.Input);

                    Dictionary <String, Object> dd = new Dictionary <string, object>();
                    foreach (var v in input)
                    {
                        Debug.WriteLine("VALUE: " + v);
                        Debug.WriteLine("Key: " + v.Key + ", Value: " + v.Value);
                        dd[v.Key] = v.Value;
                    }

                    Debug.WriteLine("Creating jobContract", "ProducerSessionContract.Copy");
                    IJobProducerContract jobContract;
                    jobContract = contract.NewJob(sessionIdNew, job.Initialize, job.Reset, job.Visible);
                    jobContract.Process.Input = dd;

                    Debug.WriteLine("Adding jobContract Count to jobList", "ProducerSessionContract.Copy");
                    jobList.Add(jobContract.Id);
                }
            }

            var d = new Dictionary <string, string>();

            d.Add("NewSessionGuid", sessionIdNew.ToString());
            d.Add("NewJobIds", string.Join(",", jobList.ToArray()));

            return(d);
        }
示例#14
0
        /*
         *
         * /// <summary>
         * ///
         * /// </summary>
         * /// <param name="sessionID"></param>
         * /// <returns></returns>
         * public int KillSession(string sessionID)
         * {
         *  ISessionProducerContract contract = new AspenSessionProducerContract();
         *  int total = 0;
         *
         *  try
         *  {
         *      total += contract.Cancel(new Guid(sessionID));
         *      total += contract.Terminate(new Guid(sessionID));
         *  }
         *  catch (AuthorizationError ex)
         *  {
         *      var detail = new ErrorDetail(
         *          ErrorDetail.Codes.AUTHORIZATION_ERROR,
         *          ex.Message
         *      );
         *      throw new WebFaultException<ErrorDetail>(detail, System.Net.HttpStatusCode.Unauthorized);
         *  }
         *  return total;
         * }
         */

        /// <summary>
        /// Append new Jobs to session, in 'Create' state.
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="data">list of job requests  [ {"Simulation":.., "Input":..., "Initialize":..., "Reset":..., } ]</param>
        /// <returns></returns>
        public List <int> AppendJobs(string sessionID, System.IO.Stream data)
        {
            Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name);
            Object name       = "";
            Object inputs     = "";
            Object initialize = false;
            Object reset      = true;
            Object visible    = false;
            Object jobId      = "";
            Object consumerId = "";

            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();
            List <Dictionary <string, Object> > jobsInputList =
                Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json);
            List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >();
            List <string> simulationList = new List <string>();
            List <bool>   initializeList = new List <bool>();
            List <bool>   resetList      = new List <bool>();
            List <bool>   visibleList    = new List <bool>();
            List <Guid>   jobIdList      = new List <Guid>();
            List <Guid>   consumerIdList = new List <Guid>();

            foreach (var inputData in jobsInputList)
            {
                if (!inputData.TryGetValue("Simulation", out name))
                {
                    Debug.WriteLine("Missing simulation name", this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Simulation is required"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                if (!inputData.TryGetValue("Input", out inputs))
                {
                    Debug.WriteLine("Missing input data", this.GetType().Name);
                    throw new ArgumentException("No Input data name");
                }

                if (!inputData.TryGetValue("Initialize", out initialize))
                {
                    initialize = false;
                }
                if (!inputData.TryGetValue("Reset", out reset))
                {
                    reset = true;
                }
                if (!inputData.TryGetValue("Visible", out visible))
                {
                    visible = false;
                }

                if (inputData.TryGetValue("Guid", out jobId))
                {
                    try
                    {
                        jobIdList.Add(Guid.Parse((string)jobId));
                    }
                    catch (InvalidCastException)
                    {
                        var detail = new ErrorDetail(
                            ErrorDetail.Codes.DATAFORMAT_ERROR,
                            "Key jobId is required to be a string/GUID"
                            );
                        throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                    }
                    catch (FormatException)
                    {
                        var detail = new ErrorDetail(
                            ErrorDetail.Codes.DATAFORMAT_ERROR,
                            String.Format("Key jobId {0} is required to be a formatted as a GUID", jobId)
                            );
                        throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    jobIdList.Add(Guid.Empty);
                }

                if (inputData.TryGetValue("ConsumerId", out consumerId))
                {
                    try
                    {
                        consumerIdList.Add(Guid.Parse((string)consumerId));
                    }
                    catch (InvalidCastException)
                    {
                        var detail = new ErrorDetail(
                            ErrorDetail.Codes.DATAFORMAT_ERROR,
                            "Key ConsumerId is required to be a string/GUID"
                            );
                        throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                    }
                    catch (FormatException)
                    {
                        var detail = new ErrorDetail(
                            ErrorDetail.Codes.DATAFORMAT_ERROR,
                            String.Format("Key ConsumerId {0} is required to be a formatted as a GUID", consumerId)
                            );
                        throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    consumerIdList.Add(Guid.Empty);
                }

                Newtonsoft.Json.Linq.JObject inputsD = null;
                try
                {
                    inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Input is required to be a dictionary"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                Dictionary <String, Object> dd = new Dictionary <string, object>();
                foreach (var v in inputsD)
                {
                    //Debug.WriteLine("VALUE: " + v);
                    dd[v.Key] = v.Value;
                }
                jobDictList.Add(dd);
                simulationList.Add(String.Format("{0}", name));
                try
                {
                    Debug.WriteLine("initialize String: " + initialize.ToString(), "SessionResource.AppendJobs");
                    Debug.WriteLine("initialize Type: " + initialize.GetType().ToString(), "SessionResource.AppendJobs");
                    initializeList.Add(Convert.ToBoolean(initialize));
                }
                catch (InvalidCastException e)
                {
                    Debug.WriteLine("Error: " + e.Message, "SessionResource.AppendJobs");
                    Debug.WriteLine("StackTrace: " + e.StackTrace, "SessionResource.AppendJobs");
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key initialize is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                try
                {
                    resetList.Add(Convert.ToBoolean(reset));
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Reset is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                try
                {
                    visibleList.Add(Convert.ToBoolean(visible));
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Visible is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
            }

            List <int> jobList = new List <int>();

            // Formatting complete
            for (int i = 0; i < jobDictList.Count; i++)
            {
                ISimulationProducerContract contract = null;
                string simulationName          = simulationList[i];
                Dictionary <String, Object> dd = jobDictList[i];

                try
                {
                    contract = ProducerSimulationContract.Get(simulationName);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to get Simulation {0}: {1}",
                                               name, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                if (contract == null)
                {
                    string msg = String.Format("Failed to create new job {0}: No Simulation {1}",
                                               i, simulationName);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.NotFound);
                }

                IJobProducerContract jobContract;
                try
                {
                    jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i], visibleList[i], jobIdList[i], consumerIdList[i]);
                    jobContract.Process.Input = dd;
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to create new job {0}: {1} {2}",
                                               i, ex, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                jobList.Add(jobContract.Id);
            }

            return(jobList);
        }
        /// <summary>
        /// PUT: Update Simulation
        /// </summary>
        /// <param name="name">Simulation name</param>
        /// <param name="sim">Simulation Resource Representation</param>
        /// <returns>Updated Simulation</returns>
        public Simulation UpdateSimulation(string nameOrID, Simulation sim)
        {
            Guid simulationId = Guid.Empty;
            bool isGuid       = Guid.TryParse(nameOrID, out simulationId);

            ISimulationProducerContract contract = null;

            if (sim == null || sim.Name == null)
            {
                Debug.WriteLine(String.Format("Sim Name: {0}, ID: {1}, app: {2}", sim.Name, sim.Id.ToString(), sim.Application)
                                , this.GetType().Name);
                string msg = String.Format("No Application specified in Simulation {0}", nameOrID);
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            if (!isGuid && !nameOrID.Equals(sim.Name))
            {
                string msg = String.Format("No URL Resource Name {0} doesn't match Simulation.Name {1}", nameOrID, sim.Name);
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            string appname = sim.Application;

            try
            {
                contract = ProducerSimulationContract.Create(nameOrID, sim.Name, appname);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Create Simulation {0}: {1} {2}",
                                           nameOrID, ex.Message, ex.StackTrace.ToString());
                if (ex.InnerException != null)
                {
                    msg += String.Format("    InnerException:    {0} {1}",
                                         ex.InnerException.Message, ex.InnerException.StackTrace.ToString());
                }
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            sim = DataMarshal.GetSimulation(nameOrID, isGuid);

            return(sim);
        }
        public void TestExcelSpreadsheet1()
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open(@"exceltest.xlsm", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("spreadsheet", data, "plain/text");
            data = null;
            using (var fstream = File.Open("exceltest-sinter.json", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");
            System.Threading.Thread.Sleep(10);

            var guid = Guid.NewGuid();
            var job_producer_contract = simulation_contract.NewJob(guid, false, false);

            job_producer_contract.Process.Input = new Dictionary <string, Object>()
            {
            };
            job_producer_contract.Submit();
            System.Threading.Thread.Sleep(10);

            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            Assert.IsNull(contract.Queue);
            contract.Register();

            Turbine.Consumer.Contract.SinterConsumer consumer = new Turbine.Consumer.Excel.ExcelSinterConsumer();

            //Utility.CheckBaseDirectory();

            consumer.Run();

            int jobID = job_producer_contract.Id;

            Debug.WriteLine("Job GUID" + guid.ToString(), GetType().Name);
            Dictionary <string, Object> json = null;

            using (Turbine.Data.TurbineModelContainer container = new Turbine.Data.TurbineModelContainer())
            {
                Turbine.Data.Job entity = container.Jobs.Single(s => s.Id == job_producer_contract.Id);
                json = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(entity.Process.Output);
            }

            string output = Newtonsoft.Json.JsonConvert.SerializeObject(json);

            Debug.WriteLine("OUTPUT: " + output, GetType().Name);
        }