Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Controllers/OptimizationController.cs @ 9166

Last change on this file since 9166 was 9166, checked in by fschoepp, 11 years ago

#1888:

  • Model: OptimizationScenario may be a tree of algorithms (and problems)
  • Model: Renamed InputParameters to ProblemParameters (as they are the parameters of a problem)
  • Model: Added JobExecutionDetails which contain Repetitions + Group (resource to use)
  • ScenarioParser parses the new XML scenario files
  • Website + Model: You are now able to add/remove rows from a table (no JavaScript involved yet)
  • Website + Controller: Added repetitions (enables batch jobs) and group (resource to use) to OaaS which will be used by the controller to schedule the job
  • Website: Updated templates to use new model structure
  • Website + Scenarios: Added the new algorithm Benchmark Algorithm
  • Controller: Added a singleton to make the (Azure/Mockup)-DAL exchangeable
  • Controller: Added mockup classes for DAL + IScenarioManager
  • Website/Result Page: Line Diagrams will be added via JavaScript, crawling their data using AJAX
  • Website: Most configuration parameters can be set in the ServiceDefinition directly
  • Added a mockup for the Membership classes: These can be used if no network connection is available or if other parts of the app shall be tested
  • Scenarios: Updated TSP mappings to new xsd
File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using System.ServiceModel;
7using HeuristicLab.Services.Optimization.ControllerService;
8using HeuristicLab.Services.Optimization.ControllerService.Model;
9using System.ServiceModel.Description;
10using System.Web.Security;
11using HeuristicLab.Services.Optimization.Web.Models;
12using HeuristicLab.Services.Optimization.ControllerService.General;
13
14namespace HeuristicLab.Services.Optimization.Web.Controllers
15{
16    [Authorize(Roles="Web User")]
17    public class OptimizationController : Controller
18    {
19      public static readonly string DEFAULT_CONTROLLER_ENDPOINT = Configuration.ControllerEndpointName;
20
21      public delegate T ControllerServiceDelegate<T>(IControllerService service);
22      public delegate void ControllerServiceDelegate(IControllerService service);
23
24      public T withControllerService<T>(ControllerServiceDelegate<T> del) {
25        if (Session["pw"] == null)
26          Response.Redirect("/Account/Logon");
27
28        using (var cf = new ChannelFactory<IControllerService>(DEFAULT_CONTROLLER_ENDPOINT)) {
29          var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
30          credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
31          credentialBehaviour.UserName.Password = Session["pw"] as string;
32          var controllerProxy = cf.CreateChannel();
33          return del(controllerProxy);
34        }
35      }
36
37      public void withControllerService(ControllerServiceDelegate del) {
38        if (Session["pw"] == null)
39          Response.Redirect("/Account/Logon");
40
41        using (var cf = new ChannelFactory<IControllerService>(DEFAULT_CONTROLLER_ENDPOINT)) {         
42          var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
43          credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
44          credentialBehaviour.UserName.Password = Session["pw"] as string;         
45          var controllerProxy = cf.CreateChannel();
46          del(controllerProxy);
47        }
48      }
49        //
50        // GET: /Optimization/       
51
52        public ActionResult Index()
53        {
54          var optModel = withControllerService<OptimizationModel>((service) => {
55            OptimizationModel model = new OptimizationModel();
56            model.Scenarios = service.GetOptimizationScenarios();
57            User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
58            model.Jobs = service.GetJobs(u);
59            return model;
60          });
61          return View(optModel);
62        }
63
64        public ActionResult ProblemParameters(string scenario) {
65          var optScenario = withControllerService<OptimizationScenario>((service) => {
66            return service.GetOptimizationScenarioByName(scenario);
67          });
68          Session["scenario"] = optScenario;
69          if (optScenario.FirstAlgorithm.Problem == null)
70            return RedirectToAction("AlgorithmParameters");
71          return View(optScenario.FirstAlgorithm.Problem.Parameters);
72        }
73
74        public ActionResult ProblemParameters2() {                   
75          return View("ProblemParameters", (Session["scenario"] as OptimizationScenario).FirstAlgorithm.Problem.Parameters);
76        }
77
78        public ActionResult JobDetails(string jobId) {
79          Job job = withControllerService<Job>((service) => {
80            User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
81            return service.GetJob(u, jobId);
82          });
83          var runs = withControllerService<IList<Run>>((service) => {
84            User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
85            return service.GetJobResults(u, jobId);
86          });
87          JobDetailsModel jdm = new JobDetailsModel() { Job = job, Runs = runs };
88          Session["jobDetails"] = jdm;
89          return View(jdm);
90        }
91
92        [HttpPost]
93        public ActionResult JobDetails(Job job) {
94          withControllerService((service) => {
95            User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
96            service.DeleteJob(u, job.Id);
97          });
98          return RedirectToAction("Index");
99        }
100
101        [HttpPost]
102        public ActionResult ProblemParameters(ProblemParameters parameters) {
103          if (ModelState.IsValid) {
104            return RedirectToAction("AlgorithmParameters", parameters);
105          }
106          return View(parameters);       
107        }
108
109        public ActionResult AlgorithmParameters() {
110          return View((Session["scenario"] as OptimizationScenario).FirstAlgorithm.Parameters);
111        }
112
113        [HttpPost]
114        public ActionResult AlgorithmParameters(AlgorithmParameters parameters) {
115          if (ModelState.IsValid) {
116            return RedirectToAction("ScheduleJob");
117          }
118          return View(parameters);
119        }
120
121        public ActionResult ScheduleJob() {
122          return View(Session["scenario"]);
123        }
124
125        [HttpPost]
126        public ActionResult ScheduleJob(ScheduleJobModel jobModel) {
127          using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
128            var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
129            credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
130            credentialBehaviour.UserName.Password = Session["pw"] as string;
131            var controllerProxy = cf.CreateChannel();
132            User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
133            var scenario = Session["scenario"] as OptimizationScenario;
134            var details = new JobExecutionDetails() { Group = jobModel.Group, JobTitle = jobModel.Name, Repititions = jobModel.Repetitions };
135            controllerProxy.ScheduleOptimizationScenario(u, scenario, details);
136          }
137          return RedirectToAction("Index");
138        }
139        // TODO: Add unique identifier for algorithm / input parameters
140        public ActionResult AddRow(int id, string paramName) {
141          var os = Session["scenario"] as OptimizationScenario;
142          var parameter = os.FirstAlgorithm.Problem.Parameters.FindByName(paramName);
143          bool inputParam = true;
144          if (parameter == null) {
145            parameter = os.FirstAlgorithm.Parameters.FindByName(paramName);
146            inputParam = false;
147          }
148          var dm = parameter.Value as DecimalMatrix;
149          dm.CopyRow(id);
150          return inputParam ? View("ProblemParameters", os.FirstAlgorithm.Problem.Parameters) : View("AlgorithmParameters", os.FirstAlgorithm.Parameters);
151        }
152
153        public ActionResult DeleteRow(int id, string paramName) {
154          var os = Session["scenario"] as OptimizationScenario;
155          var parameter = os.FirstAlgorithm.Problem.Parameters.FindByName(paramName);
156          bool inputParam = true;
157          if (parameter == null) {
158            parameter = os.FirstAlgorithm.Parameters.FindByName(paramName);
159            inputParam = false;
160          }
161          var dm = parameter.Value as DecimalMatrix;
162          dm.DeleteRow(id);
163          return inputParam ? View("ProblemParameters", os.FirstAlgorithm.Problem.Parameters) : View("AlgorithmParameters", os.FirstAlgorithm.Parameters);
164        }
165    }
166}
Note: See TracBrowser for help on using the repository browser.