Esempio n. 1
0
        public void TestJsonSerialization()
        {
            string     errorMessage      = null;
            string     xmlInputFilePath  = @"C:\Projects\NCHRP 08-103\DEA-Example-20-depots.xml";
            string     xmlOutputFilePath = @"C:\Projects\NCHRP 08-103\DEA-Example-20-depots-3.xml";
            DEAContext context           = DEAContext.CreateFromXmlFile(xmlInputFilePath, out errorMessage);

            Assert.IsNotNull(context, errorMessage);
            if (context != null)
            {
                string json = context.ToJsonString(out errorMessage);
                Assert.IsNotNull(json, errorMessage);
                if (json != null)
                {
                    DEAContext context2 = DEAContext.CreateFromJsonString(json, out errorMessage);
                    Assert.IsNotNull(context2, errorMessage);
                    if (context2 != null)
                    {
                        bool ok = context2.SaveToXmlFile(xmlOutputFilePath, out errorMessage);
                        Assert.IsTrue(ok, errorMessage);
                    }
                }
            }
        }
Esempio n. 2
0
        public void TestJsonSerializationInRequestMode()
        {
            string errorMessage   = null;
            string warningMessage = null;

            string csvFilePath = @"C:\Projects\NCHRP 08-103\DEA-Example-20-depots.csv";
            string xmlFilePath = @"C:\Projects\NCHRP 08-103\DEA-Example-20-depots-6.xml";


            bool       ok      = true;
            DEAContext context = new DEAContext("DEAContext", "Depot");

            ok = context.AddVariable("STOCK", "I", 1.0, out errorMessage);
            if (ok)
            {
                ok = context.AddVariable("WAGES", "I", 1.0, out errorMessage);
            }
            if (ok)
            {
                ok = context.AddVariable("ISSUES", "O", 1.0, out errorMessage);
            }
            if (ok)
            {
                ok = context.AddVariable("RECEIPTS", "O", 1.0, out errorMessage);
            }
            if (ok)
            {
                ok = context.AddVariable("REQS", "O", 1.0, out errorMessage);
            }

            if (ok)
            {
                ok = context.UploadCsvFile(csvFilePath, out warningMessage, out errorMessage);
            }

            if (ok)
            {
                string json = context.ToJsonString(out errorMessage);
                Assert.IsNotNull(json, errorMessage);
                if (json != null)
                {
                    DEAContext context2 = DEAContext.CreateFromJsonString(json, out errorMessage);
                    Assert.IsNotNull(context2, errorMessage);
                    ok = (context2 != null);

                    if (ok)
                    {
                        ok = context2.RunDEA(out errorMessage);
                    }

                    if (ok)
                    {
                        ok = context2.AddCostConstraint("STOCK", 27.0, out errorMessage);
                        if (ok)
                        {
                            ok = context2.AddCostConstraint("WAGES", 40, out errorMessage);
                        }
                    }

                    if (ok)
                    {
                        ok = context2.ApplyCostConstraintsToProjectSelection(out errorMessage);
                    }


                    if (ok)
                    {
                        ok = context2.SaveToXmlFile(xmlFilePath, out errorMessage);
                    }

                    Assert.IsTrue(ok, errorMessage);
                }
            }
        }
Esempio n. 3
0
        public Stream RunDEAnalysis(Stream request)
        {
            bool         ok           = true;
            string       errorMessage = null;
            DEAContext   context      = null;
            string       jsonRequest  = null;
            string       jsonResponse = null;
            const string fn           = "RunDEAnalysis";

            LOG.DebugFormat("{0} - started", fn);

            UriTemplateMatch utm = WebOperationContext.Current.IncomingRequest.UriTemplateMatch;

            LOG.Debug(utm.RequestUri.OriginalString);

            try
            {
                using (StreamReader reader = new StreamReader(request))
                {
                    jsonRequest = reader.ReadToEnd();
                }

                LOG.DebugFormat("Request: {0}", jsonRequest);

                context = DEAContext.CreateFromJsonString(jsonRequest, out errorMessage);
                ok      = (context != null);
                if (ok)
                {
                    ok = context.RunDEA(out errorMessage);
                    if (ok && context.ConstraintsSet)
                    {
                        ok = context.ApplyCostConstraintsToProjectSelection(out errorMessage);
                    }
                    if (ok)
                    {
                        jsonResponse = context.ToJsonString(out errorMessage);
                        ok           = (jsonResponse != null);
                    }
                }
            }
            catch (Exception ex)
            {
                ok           = false;
                errorMessage = ex.Message;
            }

            DEAResponse response = new DEAResponse();

            response.OK           = ok;
            response.errorMessage = errorMessage;
            response.context      = context;

            jsonResponse = JsonConvert.SerializeObject(response, Formatting.Indented);

            LOG.DebugFormat("Response: {0}", jsonResponse);
            Stream ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(s: jsonResponse));

            LOG.DebugFormat("{0} - ended", fn);

            return(ms);
        }