private WorkspaceModel RestoreModel(WorkspaceModel workspaceModel)
        {
            foreach (var paramValue in workspaceModel.ParametersValues)
            {
                paramValue.AllParameters = GetParametersSelectList(paramValue.ID);
            }

            return workspaceModel;
        }
        public ActionResult LoadWorkspace(int id)
        {
            try
            {
                var workspace = db.Workspaces.Single(w => w.Id == id);
                if (workspace != null)
                {
                    var workspaceModel = new WorkspaceModel()
                    {
                        ID = workspace.Id,
                        IsComplete = workspace.IsComplete,
                        Name = workspace.Name
                    };

                    // ReSharper disable once RedundantAnonymousTypePropertyName
                    var paramValues = from p in workspace.ParamValues
                                      select new { Parameter = db.Parameters.Single(par => par.Id == p.ParameterId), Value = p.Value };
                    workspaceModel.ParametersValues = (from pv in paramValues
                        select
                            new ParameterModel
                            {
                                Description = pv.Parameter.Description,
                                ID = pv.Parameter.Id,
                                ParamName = pv.Parameter.Name,
                                Unit = pv.Parameter.Unit,
                                Value = pv.Value,
                                AllParameters = GetParametersSelectList(pv.Parameter.Id)
                            }).ToList();

                    if (!string.IsNullOrEmpty(workspace.OutputInterval))
                    {
                        workspaceModel.Result = new ResultModel
                        {
                            IsAggregated = false,
                            IsFullWorkspace = workspace.IsComplete,
                            IsSelectedForAggregation =false,
                            OutputInterval = workspace.OutputInterval,
                            OutputLinguistic = workspace.OutputLinguistic,
                            WorkspaceID = workspace.Id,
                            WorkspaceName = workspace.Name
                        };
                    }

                    ModelState.Clear();
                    return PartialView("_Workspace", workspaceModel);
                }
                else
                {
                    return new HttpStatusCodeResult(HttpStatusCode.NotFound);
                }
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
        public ActionResult SaveWorkspace(WorkspaceModel workspace)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var updatedWorkspace = new Workspace
                    {
                        Id = workspace.ID,
                        IsComplete = workspace.IsComplete,
                        Name = workspace.Name
                    };

                    db.Workspaces.AddOrUpdate(updatedWorkspace);
                    db.SaveChanges();

                    // update parameters
                    var existingValues = from pv in db.ParameterValues where pv.WorkspaceId == workspace.ID select pv;
                    foreach (var value in existingValues)
                    {
                        db.ParameterValues.Remove(value);
                    }

                    db.SaveChanges();
                    foreach (var paramValue in workspace.ParametersValues)
                    {
                        db.ParameterValues.AddOrUpdate(
                            new ParamValue
                            {
                                ParameterId = paramValue.ID,
                                Value = paramValue.Value,
                                WorkspaceId = updatedWorkspace.Id
                            });
                    }

                    db.SaveChanges();

                    if (workspace.Result != null)
                    {
                        updatedWorkspace.OutputInterval = workspace.Result.OutputInterval;
                        updatedWorkspace.OutputLinguistic = workspace.Result.OutputLinguistic;
                        db.Workspaces.AddOrUpdate(updatedWorkspace);
                        db.SaveChanges();

                    }

                    return LoadWorkspace(updatedWorkspace.Id);
                }
                catch (Exception ex)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message);
                }
            }

            // if didn't pass validation
            return PartialView("_Workspace", RestoreModel(workspace));
        }
        public ActionResult Infer(WorkspaceModel workspace)
        {
            try
            {
                List<FuzzyVariable> inputVariables = GetInputVariables();
                FuzzyVariable outputVariable = GetOutputVariable();
                RulesList ruleDefinitions = GetRulesDefinitions();

                List<string> includedVariables = GetIncludedVariables(workspace.ParametersValues);
                var decisionMakerT1 = new DecisionMakerType1(inputVariables, outputVariable, ruleDefinitions, includedVariables);

                // DataProvider.DataProvider.SaveInputVariables(decisionMakerT1.SerializableParameters);
                var decisionMakerT2 = new DecisionMakerType2(decisionMakerT1);

                var learningDataSet = DataProvider.DataProvider.GetLearningDataSet();
                decisionMakerT2.Initialize(learningDataSet);

               // var decisionMakerProperties = db.DecisionMakerProperties.Single(d => true);
               // var decisionMakerT2 = new DecisionMakerType2(decisionMakerProperties.ParametersXml, outputVariable, ruleDefinitions, inputVariables);
                Dictionary<string, double> vector = GetInputVector(workspace.ParametersValues);
                var result = decisionMakerT2.Infer(vector);
                var linguisticTerms = DecisionMakerType2.IntervalToLinguistic(result);

                ResultModel resultModel = ResultToModel(result, workspace.IsComplete, workspace.ID, workspace.Name, string.Join(", ", linguisticTerms));
                return this.PartialView("_Result", resultModel);
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message);
            }
        }