Exemple #1
0
        private async void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            decimal maxRuntime = numericUpDown1.Value * 1000;

            //Clean GUI and Errors
            CompleteErrorList.Clear();
            label1.Text   = "";
            fileValiditys = "";
            allocationsToolStripMenuItem.Enabled = false;

            string source = comboBox1.Text;

            if (source != null)
            {
                //Download file stored on internet
                string    destination = Path.GetFileName(source);
                WebClient webclient   = new WebClient();
                webclient.DownloadFile(source, destination);

                //Initialise Configuration Data
                string fullConfigPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + destination;
                ProcessConfig(fullConfigPath);
                ConfigurationData ConfData = new ConfigurationData(aConfiguration)
                {
                    AlgorithmMaxRuntime = maxRuntime
                };

                //Setup Local WCF Services
                LocalALG1WebService.ServiceClient LocalALG1 = new LocalALG1WebService.ServiceClient();
                LocalALG1.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                LocalALG2WebService.ServiceClient LocalALG2 = new LocalALG2WebService.ServiceClient();
                LocalALG2.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                LocalALG3WebService.ServiceClient LocalALG3 = new LocalALG3WebService.ServiceClient();
                LocalALG3.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                //Setup AWS WCF Services
                AwsALG1WebService.ServiceClient AwsALG1 = new AwsALG1WebService.ServiceClient();
                AwsALG1.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                AwsALG2WebService.ServiceClient AwsALG2 = new AwsALG2WebService.ServiceClient();
                AwsALG2.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                AwsALG3WebService.ServiceClient AwsALG3 = new AwsALG3WebService.ServiceClient();
                AwsALG3.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);


                //Loading Bar setup
                int interval = 5 + ((Convert.ToInt32(numericUpDown1.Value) - 1) * 10);
                while (progressBar1.Value < 100)
                {
                    Thread.Sleep(interval);
                    progressBar1.Increment(1);
                }


                //Receive results from Web Services
                List <string[]> returnedStrings = new List <string[]>
                {
                    //await LocalALG1.GetAllocationsAsync(ConfData),
                    //await LocalALG2.GetAllocationsAsync(ConfData),
                    //await LocalALG3.GetAllocationsAsync(ConfData),
                    await AwsALG1.GetAllocationsAsync(ConfData),
                    await AwsALG2.GetAllocationsAsync(ConfData),
                    await AwsALG3.GetAllocationsAsync(ConfData)
                };

                //Close service clients
                LocalALG1.Close();
                LocalALG2.Close();
                LocalALG3.Close();
                AwsALG1.Close();
                AwsALG2.Close();
                AwsALG3.Close();

                //Create Allocations from returned strings
                List <Allocation> returnedAllocations = new List <Allocation>();
                endpointAdresses.Clear();

                foreach (string[] stringArray in returnedStrings)
                {
                    List <string> stringList = stringArray.ToList();

                    endpointAdresses.Add(stringList[0]);
                    stringList.RemoveAt(0);

                    foreach (string s in stringList)
                    {
                        List <string> matrixList = s.Split('\n').ToList();
                        Allocation    allocation = new Allocation(matrixList);
                        returnedAllocations.Add(allocation);
                    }
                }

                //Display endpoint address's in popup
                EndpointForm endpointForm = new EndpointForm();
                endpointForm.Show();

                //Determine best set of allocations
                double            energy;
                double            bestEnergy      = double.MaxValue;
                List <Allocation> bestAllocations = new List <Allocation>();
                int allocationIDCounter           = 1;

                foreach (Allocation al in returnedAllocations)
                {
                    if (al.ValidateAllocation() == false)
                    {
                        returnedAllocations.Remove(al);
                    }
                    al.CalculateTime(aConfiguration);
                    energy = al.CalculateEnergy(aConfiguration);

                    if (energy < bestEnergy)
                    {
                        //Reset Allocation ID's
                        allocationIDCounter = 1;
                        al.ID = allocationIDCounter;
                        allocationIDCounter++;

                        bestAllocations.Clear();
                        bestAllocations.Add(al);
                        bestEnergy = energy;
                    }
                    else if (energy == bestEnergy)
                    {
                        if (!bestAllocations.Contains(al))
                        {
                            al.ID = allocationIDCounter;
                            allocationIDCounter++;
                            bestAllocations.Add(al);
                        }
                    }
                }


                //Update GUI
                label1.Text += fileValiditys;
                int idCounter = 1;

                //Show best allocations on GUI
                foreach (Allocation al in bestAllocations)
                {
                    label1.Text += al.ToString() + "\n\n";
                    idCounter++;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Determines if a .tan file is valid and parses the data to an instance of TaskAllocation
        /// </summary>
        /// <param name="path">A string containg the filepath of a .tan file</param>
        /// <param name="anAllocation">An instance of TaskAllocation that is populated with data from the .tan file being parsed</param>
        /// <returns>A bool depending on if the file is valid and error free.</returns>
        public static bool TryParse(string path, out TaskAllocations anAllocation)
        {
            anAllocation = new TaskAllocations(path);
            List <String> ParsingErrorList = new List <string>();

            anAllocation.SetOfAllocations = new List <Allocation>();

            Regex seperatorRegex = new Regex(seperatorPattern);

            //Begin parsing TAN file
            StreamReader file = new StreamReader(path);

            while (!file.EndOfStream)
            {
                string          line           = file.ReadLine();
                MatchCollection seperatorMatch = seperatorRegex.Matches(line);

                if (line.Length == 0)
                {
                }
                else if (line.Contains(comment))
                {
                    if (line.StartsWith(comment))
                    {
                        //Comment Found
                    }
                    else
                    {
                        ParsingErrorList.Add(commentLineError + line);
                    }
                }
                else if (seperatorMatch.Count == 0)
                {
                    ParsingErrorList.Add(invalidSeperatorError + line);
                }
                else if (line.Contains(anAllocation.configPathKey))
                {
                    string[] substrings = line.Split(delimiter);

                    //Check Line is valid and only contains expected number of arguements
                    if (substrings[0] == anAllocation.configPathKey && substrings.Count() == 2)
                    {
                        string configPath = substrings[1].Replace(doubleQuote, emptySpace);
                        anAllocation.ConfigPath = configPath;
                    }
                    else
                    {
                        ParsingErrorList.Add(invalidLineError + line);
                    }
                }
                else if (line.Contains(anAllocation.tasksKey))
                {
                    string[] substrings = line.Split(delimiter);
                    int      input      = ToInt32(substrings[1]);

                    if (input == -1)
                    {
                        ParsingErrorList.Add(stringToIntError + line);
                    }
                    else
                    {
                        anAllocation.Tasks = input;
                    }
                }
                else if (line.Contains(anAllocation.processorsKey))
                {
                    string[] substrings = line.Split(delimiter);

                    int input = ToInt32(substrings[1]);
                    if (input == -1)
                    {
                        string error = stringToIntError + line;
                        ParsingErrorList.Add(error);
                    }
                    else
                    {
                        anAllocation.Processors = input;
                    }
                }
                else if (line.Contains(anAllocation.allocationsKey))
                {
                    string[] substrings = line.Split(delimiter);
                    int      input      = ToInt32(substrings[1]);
                    if (input == -1)
                    {
                        string error = stringToIntError + line;
                        ParsingErrorList.Add(error);
                    }
                    else
                    {
                        anAllocation.NumAllocations = input;
                    }
                }
                else if (line.Contains(anAllocation.allocationIDKey))
                {
                    string[] substrings = line.Split(delimiter);
                    int      id         = ToInt32(substrings[1]);
                    if (id == -1)
                    {
                        string error = stringToIntError + line;
                        ParsingErrorList.Add(error);
                    }

                    List <String> allocationMatrix = new List <string>();

                    while (line.Length != 0)
                    {
                        line = file.ReadLine();

                        if (line != emptySpace && line != null)
                        {
                            allocationMatrix.Add(line);
                        }
                        else
                        {
                            break;
                        }
                    }

                    Allocation aAllocation = new Allocation(id, allocationMatrix);
                    anAllocation.SetOfAllocations.Add(aAllocation);
                }
                else
                {
                    string error = invalidLineError + line;
                    ParsingErrorList.Add(error);
                }
            }
            file.Close();

            //Add parsing errors to instance error list
            anAllocation.AllocationErrorList.AddRange(ParsingErrorList);

            //Check validity of file
            anAllocation.isValid = (anAllocation.Validate() && ParsingErrorList.Count == 0);

            return(ParsingErrorList.Count == 0);
        }