1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
7 | using HeuristicLab.Services.Optimization.Web.Helpers;
|
---|
8 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
9 | using System.Web.Security;
|
---|
10 | using Mvc3TestApplication.Binders;
|
---|
11 | using HeuristicLab.Services.Optimization.ControllerService.Parsers;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Services.Optimization.Web.Controllers
|
---|
14 | {
|
---|
15 | [Authorize(Roles = "Web User")]
|
---|
16 | public class ExperimentController : Controller
|
---|
17 | {
|
---|
18 | private ControllerServiceHelper ControllerService {
|
---|
19 | get {
|
---|
20 | var helper = new ControllerServiceHelper(Session["pw"] as string);
|
---|
21 | if (!helper.Valid) {
|
---|
22 | Response.Redirect("/Account/Logon?ReturnUrl=%2fExperiment");
|
---|
23 | }
|
---|
24 | return helper;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | //
|
---|
29 | // GET: /Experiment/
|
---|
30 |
|
---|
31 | public ActionResult Index()
|
---|
32 | {
|
---|
33 | var model = new ExperimentViewModel();
|
---|
34 | var scenarios = ControllerService.withControllerService<IEnumerable<string>>((service) => {
|
---|
35 | return service.GetOptimizationScenarioNames();
|
---|
36 | });
|
---|
37 | var experiments = ControllerService.withControllerService<IEnumerable<string>>((service) => {
|
---|
38 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
39 | return service.GetExperiments(u);
|
---|
40 | });
|
---|
41 | foreach (var experiment in experiments)
|
---|
42 | model.Experiments.Add(experiment);
|
---|
43 | foreach (var scenario in scenarios)
|
---|
44 | model.Scenarios.Add(scenario);
|
---|
45 |
|
---|
46 | Session["experiment"] = new Experiment();
|
---|
47 |
|
---|
48 | return View(model);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ActionResult New() {
|
---|
52 | return Index();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public Experiment Experiment { get { return Session["experiment"] as Experiment; } set { Session["experiment"] = value;} }
|
---|
56 |
|
---|
57 | [HttpPost]
|
---|
58 | public JsonResult Save(ExperimentModel model) {
|
---|
59 | var experiment = new Experiment() { Name = model.Name };
|
---|
60 | var stack = new Stack<Node>();
|
---|
61 |
|
---|
62 | foreach (var node in model.Experiment.children)
|
---|
63 | stack.Push(node);
|
---|
64 |
|
---|
65 | while (stack.Count > 0) {
|
---|
66 | var currentNode = stack.Pop();
|
---|
67 | // add children to stack
|
---|
68 | if (currentNode.children != null)
|
---|
69 | foreach (var node in currentNode.children)
|
---|
70 | stack.Push(node);
|
---|
71 |
|
---|
72 | // TODO: Do we really need a tree of scenarios? Or is it a simple list?
|
---|
73 | // push currentNode to experiment
|
---|
74 | if (currentNode.title == "Experiment")
|
---|
75 | continue;
|
---|
76 | experiment.Algorithm.Add(new Algorithm() { Name = currentNode.title });
|
---|
77 | }
|
---|
78 |
|
---|
79 | var ok = ControllerService.withControllerService<bool>((service) => {
|
---|
80 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
81 | return service.SaveExperiment(u, experiment);
|
---|
82 | });
|
---|
83 |
|
---|
84 | if (ok)
|
---|
85 | return Json(new { success = true, data = model.Name });
|
---|
86 | return Json(new { success = false, error = "Experiment already exists!" });
|
---|
87 | }
|
---|
88 |
|
---|
89 | public ActionResult Edit() {
|
---|
90 | var model = new ExperimentViewModel();
|
---|
91 | var experiments = ControllerService.withControllerService<IEnumerable<string>>((service) => {
|
---|
92 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
93 | return service.GetExperiments(u);
|
---|
94 | });
|
---|
95 | foreach (var experiment in experiments)
|
---|
96 | model.Experiments.Add(experiment);
|
---|
97 | return View(model);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public ActionResult DeleteExperiment(string experiment) {
|
---|
101 | ControllerService.withControllerService<bool>((service) => {
|
---|
102 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
103 | return service.DeleteExperiment(u, experiment);
|
---|
104 | });
|
---|
105 | return RedirectToAction("Edit");
|
---|
106 | }
|
---|
107 |
|
---|
108 | [HttpPost]
|
---|
109 | public JsonResult SaveExperiment([ExperimentJson] Experiment experiment) {
|
---|
110 | return Json(
|
---|
111 | new {
|
---|
112 | Success = ControllerService.withControllerService<bool>(service => {
|
---|
113 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
114 | return service.SaveExperiment(u, experiment);
|
---|
115 | })
|
---|
116 | });
|
---|
117 | }
|
---|
118 |
|
---|
119 | private JsonResult ComplexExperimentCase(string scenario) {
|
---|
120 | // complex case, find children of experiment
|
---|
121 | var model = new GetParametersModel() { Type = "Complex" };
|
---|
122 | model.Subnames = new List<string>();
|
---|
123 |
|
---|
124 | var experiment = ControllerService.withControllerService<Experiment>(service => {
|
---|
125 | User user = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
126 | return service.GetExperimentByName(user, scenario);
|
---|
127 | });
|
---|
128 |
|
---|
129 | // TODO: Make a tree out of subnames!!
|
---|
130 | if (experiment != null)
|
---|
131 | foreach (var algo in experiment.Algorithm)
|
---|
132 | model.Subnames.Add(algo.Name);
|
---|
133 |
|
---|
134 | return Json(model, JsonRequestBehavior.AllowGet);
|
---|
135 | }
|
---|
136 |
|
---|
137 | [HttpGet]
|
---|
138 | public JsonResult GetParameters(string scenario, string context, int index) {
|
---|
139 | if (context == "Experiment" || context == null) {
|
---|
140 | var optimizationScenario = ControllerService.withControllerService<OptimizationScenario>(service => {
|
---|
141 | return service.GetOptimizationScenarioByName(scenario);
|
---|
142 | });
|
---|
143 |
|
---|
144 | if (optimizationScenario != null) {
|
---|
145 | var model = new GetParametersModel() { Type = "Basic" };
|
---|
146 | model.Algorithm = optimizationScenario.FirstAlgorithm;
|
---|
147 | model.Algorithm.Mapper = optimizationScenario.Id;
|
---|
148 | var json = Json(model, JsonRequestBehavior.AllowGet);
|
---|
149 | return json;
|
---|
150 | }
|
---|
151 | else {
|
---|
152 | // complex case, find children of experiment
|
---|
153 | var model = new GetParametersModel() { Type = "Complex" };
|
---|
154 | model.Subnames = new List<string>();
|
---|
155 |
|
---|
156 | var experiment = ControllerService.withControllerService<Experiment>(service => {
|
---|
157 | User user = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
158 | return service.GetExperimentByName(user, scenario);
|
---|
159 | });
|
---|
160 |
|
---|
161 | // TODO: Make a tree out of subnames!!
|
---|
162 | if (experiment != null)
|
---|
163 | foreach (var algo in experiment.Algorithm)
|
---|
164 | model.Subnames.Add(algo.Name);
|
---|
165 |
|
---|
166 | return Json(model, JsonRequestBehavior.AllowGet);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | var experiment = ControllerService.withControllerService<Experiment>(service => {
|
---|
171 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
172 | return service.GetExperimentByName(u, context);
|
---|
173 | });
|
---|
174 |
|
---|
175 | // if child algorithm is a complex algorithm, return as such
|
---|
176 | if (experiment.Algorithm[index].ChildAlgorithms.Count > 0) {
|
---|
177 | var model = new GetParametersModel() { Type = "Complex" };
|
---|
178 | model.Subnames = new List<string>();
|
---|
179 | foreach (var algo in experiment.Algorithm[index].ChildAlgorithms)
|
---|
180 | model.Subnames.Add(algo.Name);
|
---|
181 | return Json(model, JsonRequestBehavior.AllowGet);
|
---|
182 | }
|
---|
183 | else {
|
---|
184 | // otherwise we have the algorithms just below it
|
---|
185 | var model = new GetParametersModel() { Type = "Basic" };
|
---|
186 | model.Algorithm = experiment.Algorithm[index];
|
---|
187 | model.Algorithm.Mapper = experiment.Algorithm[index].Name;
|
---|
188 | var json = Json(model, JsonRequestBehavior.AllowGet);
|
---|
189 | return json;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|