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");
            }
        }
        public void TestSimulationContract()
        {
            var simContract = AspenSimulationContract.Get("testSim1"); //testSim1 is the first in Simulations
            var data        = new Byte[] { 0XAA };                     // Just a BS holder value in place of the aspen.bkp file
            //""abs_ht_wash"":5.0,
            var json       = @"{""P_abs_top"":16.0,""abs_ht_mid"":10.7,""abs_ic_dT"":-11.3,""P_sol_pump"":30.0,""lean_load"":0.178,""P_regen_top"":21.2,""cond_T_regen"":121.0,""ht_regen"":13.0,""slv_cool_01"":130.0,""lr_rich_T"":207.0,""reg_dia_in"":17.37,""input_s[0,0]"":134.4,""input_s[0,1]"":16.0,""input_s[0,2]"":959553.0,""input_s[0,3]"":0.071,""input_s[0,4]"":0.0,""input_s[0,5]"":0.0,""input_s[0,6]"":0.211,""input_s[0,7]"":0.0,""input_s[0,8]"":0.0,""input_s[0,9]"":0.0,""input_s[0,10]"":0.0,""input_s[0,11]"":0.0,""input_s[0,12]"":0.0,""input_s[0,13]"":0.0,""input_s[0,14]"":0.0,""input_s[0,15]"":0.718,""input_s[1,0]"":126.0,""input_s[1,1]"":30.0,""input_s[1,2]"":4319500.4116,""input_s[1,3]"":0.66207726067,""input_s[1,4]"":0.28374739743,""input_s[1,5]"":0.0,""input_s[1,6]"":0.0541753419,""input_s[1,7]"":0.0,""input_s[1,8]"":0.0,""input_s[1,9]"":0.0,""input_s[1,10]"":0.0,""input_s[1,11]"":0.0,""input_s[1,12]"":0.0,""input_s[1,13]"":0.0,""input_s[1,14]"":0.0,""input_s[1,15]"":0.0,""input_s[2,0]"":120.0,""input_s[2,1]"":30.0,""input_s[2,2]"":10000.0,""input_s[2,3]"":1.0,""input_s[2,4]"":0.0,""input_s[2,5]"":0.0,""input_s[2,6]"":0.0,""input_s[2,7]"":0.0,""input_s[2,8]"":0.0,""input_s[2,9]"":0.0,""input_s[2,10]"":0.0,""input_s[2,11]"":0.0,""input_s[2,12]"":0.0,""input_s[2,13]"":0.0,""input_s[2,14]"":0.0,""input_s[2,15]"":0.0,""eq_par[0,0]"":0.7996,""eq_par[0,1]"":-8094.81,""eq_par[0,2]"":0.0,""eq_par[0,3]"":-0.007484,""eq_par[1,0]"":98.566,""eq_par[1,1]"":1353.8,""eq_par[1,2]"":-14.3043,""eq_par[1,3]"":0.0,""eq_par[2,0]"":216.049,""eq_par[2,1]"":-12431.7,""eq_par[2,2]"":-35.4819,""eq_par[2,3]"":0.0,""eq_par[3,0]"":1.282562,""eq_par[3,1]"":-3456.179,""eq_par[3,2]"":0.0,""eq_par[3,3]"":0.0,""eq_par[4,0]"":132.899,""eq_par[4,1]"":-13445.9,""eq_par[4,2]"":-22.4773,""eq_par[4,3]"":0.0}";
            var initialize = false;

            Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json);


            simContract.UpdateAspenBackup(data);  // Set the aspen.bkp file to BS value (really this should've already been set when the sim was created)

            // Producer: Produces jobs to be run from simulations
            IJobProducerContract producer = simContract.NewJob(Guid.NewGuid(), initialize); // Ask the AspenSimulationContract to create a new job based on the selected simulation

            producer.Submit();                                                              //Basically just sets state to submit
            int jobId = producer.Id;

            // Consumer: Pops submitted jobs off the 'queue' and runs them
            IJobConsumerContract consumer = new AspenJobConsumerContract()
            {
                Id = jobId
            };
            IProcess process = consumer.Setup();                                                         //Setup the job (change state, copy setup files)  Returns SinterProcessContract

            process.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Sets the dir in the function to MyDocs, but doesn't actually chdir
            //process.Input = inputData.ToArray<string>(); //The varaible inputs are a file?  I actually didn't expect that.

            process.Input = inputData;

            consumer.Running();                         //Does some checking and changes state to running.  Run will actually be done outside contract
            consumer.Success();                         //Changes state to success. (Other option is error)

            Byte[] d2 = consumer.GetSimulationBackup(); //Final checks to make sure that all kinda worked.
            Assert.IsTrue(data.SequenceEqual(d2));
            process = consumer.Process;
            Assert.IsTrue(inputData.SequenceEqual(process.Input));
        }
示例#3
0
        public bool UpdateStagedInputFile(string name, string inputName, Stream data)
        {
            Debug.WriteLine("UpdateStagedInputFile: " + name, this.GetType());
            //Simulation sim = GetSimulation(name);
            OutgoingWebResponseContext ctx = WebOperationContext.Current.OutgoingResponse;
            var content_type = WebOperationContext.Current.IncomingRequest.ContentType;

            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                data.CopyTo(ms);
                bytes = ms.ToArray();
            }

            //StreamReader sr = new StreamReader(data);
            //string s = sr.ReadToEnd();
            //byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
            Debug.WriteLine("content_type " + content_type, this.GetType());

            // TODO: Validate Backup File
            var  contract = AspenSimulationContract.Get(name);
            bool success  = false;

            try
            {
                success = contract.UpdateInput(inputName, bytes, content_type);
            }
            catch (ArgumentException ex)
            {
                string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}",
                                           name, inputName, ex.Message);
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.AUTHORIZATION_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.Unauthorized);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}, {3}",
                                           name, inputName, ex.Message, ex.StackTrace);
                Debug.WriteLine(msg, this.GetType().Name);
                if (ex.InnerException != null)
                {
                    Debug.WriteLine("InnerException: " + ex.InnerException.Message, this.GetType().Name);
                    Debug.WriteLine(ex.InnerException.StackTrace, this.GetType().Name);
                }
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            //sim = GetSimulation(name);
            ctx.ContentType = content_type;
            ctx.StatusCode  = System.Net.HttpStatusCode.OK;

            //return new MemoryStream(bytes);
            return(success);
        }
        /*  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);
        }
示例#5
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 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);
        }