using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Services.Optimization.ControllerService.Interfaces; using HeuristicLab.Optimization; using HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Problems.TravelingSalesman; using HeuristicLab; using System.Reflection; using HeuristicLab.Services.Optimization.ControllerService.Model; using HeuristicLab.Core; using System.Collections; using HeuristicLab.Clients.Hive; using System.Threading; using HeuristicLab.Data; using System.IO; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using HeuristicLab.Services.Optimization.ControllerService.Azure; using System.Data; namespace HeuristicLab.Services.Optimization.ControllerService { public class MockupScenarioManager : IScenarioManager { private static List jobs; private static Dictionary> jobResults; private static IDataAccessLayer dal = DataAccessLayerProvider.GetLayer(); private Dictionary mappers = new Dictionary(); private static object lockable = new object(); static MockupScenarioManager() { jobs = new List() { new Model.Job() { DateCreated = DateTime.Now, Id = "1", Name="Webscheduled TSP", Resource="TESTGROUP", State=JobState.Finished }, new Model.Job() { DateCreated = DateTime.Now, Id = "2", Name="Webscheduled TSP 2", Resource="TESTGROUP", State=JobState.Calculating } }; jobResults = new Dictionary>(); jobResults["1"] = new List() { new Model.Run() { Id = "Webscheduled TSP Run 1", Name = "Webscheduled TSP Run 1", Results = new List() { new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "AbsoluteDifferenceBestKnownToBest", Value = 0 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "BestKnownQuality", Value = 1600 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "BestQuality", Value = 1600 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "CurrentAverageQuality", Value = 1600 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "CurrentBestQuality", Value = 1600 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "CurrentWorstQuality", Value = 1600 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "Evaluated Solutions", Value = 99100 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "Generations", Value = 1000 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalValue(){ Name = "RelativeDifferenceBestKnownToBest", Value = 0 }, Type = ParameterType.Decimal }, new Parameter() { Value = new HeuristicLab.Services.Optimization.ControllerService.Model.DecimalMatrix { Name = "Qualities", RowNames = new string[] { "CurrentBestQuality", "CurrentAverageQuality", "CurrentWorstQuality", "BestQuality", "BestKnownQuality" }, Value = new double[][] { // Best Quality new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600}, // Average new double[]{6000, 5000, 5650, 3750, 4500, 4050, 3450, 3300, 2550, 2775, 2400}, // Worst Quality new double[]{8000, 7200, 7350, 5000, 6000, 5400, 4600, 4400, 3400, 3700, 3200}, // Best Quality new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600}, // Best Known Quality new double[]{4000, 3900, 3950, 2500, 3000, 2700, 2300, 2200, 1700, 1850, 1600}, } }, Type = ParameterType.Decimal } } } }; } public MockupScenarioManager() { } public string DispatchScenario(Model.User user, Model.OptimizationScenario scenario, JobExecutionDetails details) { // does nothing return "guid"; } public IList GetJobs(User user) { return jobs; } public Model.Job GetJob(User user, string id) { return (from j in jobs where j.Id == id select j).FirstOrDefault(); } public bool DeleteJob(User user, string id) { jobs.RemoveAll(j => j.Id == id); return true; } public IList GetJobResults(User user, string id) { return jobResults[id]; } private static string AssemblyDirectory { get { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); } } private IScenarioMapper CompileMapper(string scenarioMapper) { // http://stackoverflow.com/questions/3188882/compile-and-run-dynamic-code-without-generating-exe using (var csCodeProvider = new Microsoft.CSharp.CSharpCodeProvider()) { var cp = new System.CodeDom.Compiler.CompilerParameters() { GenerateInMemory = true }; var referencedPaths = Directory.GetFiles(AssemblyDirectory, "*.dll"); foreach (var loadedAssembly in referencedPaths) { cp.ReferencedAssemblies.Add(loadedAssembly); } cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); var res = csCodeProvider.CompileAssemblyFromSource( cp, scenarioMapper ); foreach (var error in res.Errors) { Console.WriteLine(error); } var firstMapper = res.CompiledAssembly.GetTypes().Where(p => typeof(IScenarioMapper).IsAssignableFrom(p)).FirstOrDefault(); return Activator.CreateInstance(firstMapper) as IScenarioMapper; } } public bool AddScenario(User user, string scenarioName, string scenarioXml, string scenarioMapper) { // create scenario mapper var mapper = CompileMapper(scenarioMapper); if (mapper == null) return false; // insert into table & blob store var scenDao = dal.ScenarioDao; var blobDao = dal.BlobDao; Guid scenarioXmlGuid = Guid.NewGuid(); Guid scenarioMapperGuid = Guid.NewGuid(); string scenarioXmlId = scenarioName + "_" + scenarioXmlGuid.ToString(); string scenarioMapperId = scenarioName + "_" + scenarioMapperGuid.ToString(); if (!blobDao.Add(new StringEntry() { Key = scenarioXmlId, Text = scenarioXml })) return false; if (!blobDao.Add(new StringEntry() { Key = scenarioMapperId, Text = scenarioMapper })) return false; if (!scenDao.Add(new ScenarioEntity(scenarioName, scenarioXmlId, scenarioMapperId))) return false; // everything stored in the DB -> add mapper to dictionary if (!mappers.ContainsKey(scenarioName)) { lock (lockable) { if (!mappers.ContainsKey(scenarioName)) mappers[scenarioName] = mapper; } } return true; } public bool DeleteScenario(User user, string scenarioName) { // delete from table & blob store var scenarioDao = dal.ScenarioDao; var blobDao = dal.BlobDao; var entity = scenarioDao.FindByName(scenarioName); if (entity == null) return false; blobDao.DeleteByKey(entity.Mapper); blobDao.DeleteByKey(entity.Scenario); scenarioDao.DeleteByName(scenarioName); return true; } public string SaveExperiment(User user, Model.Experiment experiment) { throw new NotImplementedException(); } public IEnumerable GetExperimentNames(User user) { throw new NotImplementedException(); } public bool DeleteExperiment(User user, string experiment) { throw new NotImplementedException(); } public Model.Task GetTaskData(User u, string jobId, string taskId) { throw new NotImplementedException(); } public Model.Job GetTasks(User u, string jobId) { throw new NotImplementedException(); } public Model.Experiment GetExperimentByName(User user, string scenario) { throw new NotImplementedException(); } public bool DispatchExperiment(User user, Model.Experiment exp, JobExecutionDetails details) { throw new NotImplementedException(); } string IScenarioManager.SaveExperiment(User user, Model.Experiment experiment) { throw new NotImplementedException(); } public IEnumerable GetExperiments(User user, bool namesOnly = false) { throw new NotImplementedException(); } public Model.Experiment GetExperimentById(User user, string nodeId) { throw new NotImplementedException(); } } }