public void TestParseResultSet()
 {
     string xmlContent = "<?xml version='1.0' encoding='utf-16'?>" +
                             "<BioWFML>" +
                             "  <ExecutionResult>" +
                             "    <Collection>" +
                             "      <Item>" +
                             "        <Property name='Similarity'>1</Property>" +
                             "        <Property name='MatchPattern'>QUT.Bio.BioPatML.Patterns.Motif</Property>" +
                             "        <Property name='Matches'>5</Property>" +
                             "        <Property name='Impact'>0.9</Property>" +
                             "        <Property name='SubMatchNumber'>0</Property>" +
                             "        <Property name='Length'>5</Property>" +
                             "        <Property name='Strand'>1</Property>" +
                             "        <Property name='Start'>1321</Property>" +
                             "        <Property name='End'>1325</Property>" +
                             "        <Property name='CenterPosition'>1323</Property>" +
                             "      </Item>" +
                             "      <Item>" +
                             "        <Property name='Similarity'>1</Property>" +
                             "       <Property name='MatchPattern'>QUT.Bio.BioPatML.Patterns.Motif</Property>" +
                             "        <Property name='Matches'>5</Property>" +
                             "        <Property name='Impact'>0.9</Property>" +
                             "        <Property name='SubMatchNumber'>0</Property>" +
                             "        <Property name='Length'>5</Property>" +
                             "        <Property name='Strand'>1</Property>" +
                             "        <Property name='Start'>15038</Property>" +
                             "        <Property name='End'>15042</Property>" +
                             "        <Property name='CenterPosition'>15040</Property>" +
                             "      </Item>" +
                             "    </Collection>" +
                             "  </ExecutionResult>" +
                             "</BioWFML>";
     BioWFMLResultManager resultManager = new BioWFMLResultManager();
     ResultCollections collection = resultManager.ReadBioWFML(xmlContent);
 }
        private void MenuItem1_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;

            if (menu != null)
            {
                switch (menu.Header.ToString())
                {
                    case "Run Workflow":
                        {
                            try
                            {
                                if (CheckHasWorkflow())
                                {
                                    runnerPanel = new Panels.JobRunner();
                                    runnerPanel.Show();

                                    tridentService = new TridentEmulatorSvc.WorkflowServiceClient();
                                    bw = new BackgroundWorker();
                                    bw.DoWork += delegate(object s, DoWorkEventArgs args)
                                    {
                                        Dispatcher pbDispatcher = runnerPanel.StatusTxt.Dispatcher;
                                        UpdateStatusBarProgressTextDelegate update = new UpdateStatusBarProgressTextDelegate(UpdateStatusBarProgressText);
                                        

                                        Object[] arguments = args.Argument as Object[];
                                        string xmlContent = arguments[0] as string;
                                        bool done = false; // a flag to determine the state of the search process
                                        Guid jobId = new Guid();
                                        Guid workflowId = new Guid();

                                        tridentService.SaveWorkFlowCompleted += delegate (object source, TridentEmulatorSvc.SaveWorkFlowCompletedEventArgs e2)
                                        {
                                            
                                            workflowId = e2.Result;
                                            pbDispatcher.BeginInvoke(update, "Workflow Saved!");
                                            done = true;
                                        };

                                        pbDispatcher.BeginInvoke(update, "Saving Workflow...");
                                        tridentService.SaveWorkFlowAsync(xmlContent);

                                        while (!done) { System.Threading.Thread.Sleep(1000); }

                                        done = false;

                                        tridentService.ExecuteWorkflowCompleted += delegate(object sender2, TridentEmulatorSvc.ExecuteWorkflowCompletedEventArgs e3)
                                        {
                                            jobId = e3.Result;
                                            done = true;
                                        };

                                        pbDispatcher.BeginInvoke(update, "Queuing job " + workflowId + "");
                                        tridentService.ExecuteWorkflowAsync("BioPlaygroundWF", workflowId);

                                        bool isCheckingStatus = false;
                                        string jobStatus = String.Empty;
                                        while (!done) { System.Threading.Thread.Sleep(1000); }

                                        done = false;

                                        tridentService.GetJobStatusCompleted += delegate(object sender3, TridentEmulatorSvc.GetJobStatusCompletedEventArgs e4)
                                        {
                                            jobStatus = e4.Result;
                                            pbDispatcher.BeginInvoke(update, jobStatus + "...");

                                            System.Diagnostics.Debug.WriteLine("Job Status : > " + jobStatus);
                                            if (jobStatus.ToLower().Equals("completed"))
                                            {   
                                                done = true;
                                            }
                                            else if (jobStatus.ToLower().Equals("aborted"))
                                            {
                                                //this.ReportException(new Exception("Task run fail!"));
                                                done = true;
                                            }
                                            isCheckingStatus = false;
                                         };


                                        while (!done) 
                                        {
                                            System.Threading.Thread.Sleep(2000);
                                            if (!isCheckingStatus)
                                            {
                                                if (done && jobStatus == "completed" )
                                                { 
                                                    pbDispatcher.BeginInvoke(update, "Job execution success!"); 
                                                    System.Threading.Thread.Sleep(2000);
                                                    break;
                                                } 

                                                else
                                                    if
                                                (done && jobStatus == "aborted")
                                                    {
                                                        pbDispatcher.BeginInvoke(update, "Job execution fail, please check your workflow or parameters.");
                                                        System.Threading.Thread.Sleep(2000);
                                                        break;
                                                    }
                                                
                                                tridentService.GetJobStatusAsync("BioPlaygroundWF", jobId);
                                                isCheckingStatus = true;
                                            }
                                        }

                                       
                                        if (jobStatus == "completed") 
                                        {
                                            pbDispatcher.BeginInvoke(update, "Scanning for data output.");
                                            done = false; 
                                        }

                                        System.Threading.Thread.Sleep(1500);
  
                                        List<string> BioWFResultML = new List<string>();
                                        BioWFResultML.Add(jobStatus); 
                                       
                                        tridentService.GetJobOuputCompleted += delegate(object sender4, TridentEmulatorSvc.GetJobOuputCompletedEventArgs e5)
                                        {
                                            if (e5.Result != null)
                                            {
                                                pbDispatcher.BeginInvoke(update, "downloading data output...");
                                                BioWFResultML = e5.Result.ToList();
                                                args.Result = BioWFResultML[0]; //for now...
                                            }
                                            else
                                            {
                                                pbDispatcher.BeginInvoke(update, "No data output...");
                                            }

                                            System.Threading.Thread.Sleep(2500);
                                            done = true;
                                        };

                                        if (jobStatus.CompareTo("Completed") == 0)
                                        {
                                            done = false;
                                            tridentService.GetJobOuputAsync("BioPlaygroundWF", jobId);
                                        }
                                        else
                                            args.Result = BioWFResultML[0]; //try

                                        while (!done) { System.Threading.Thread.Sleep(1000); }
                                
                                    };

                                    Object[] parameters = new Object[1];
                                    parameters[0] = wfManager.GetWorkflowXML();

                                    
                                    try
                                    {
                                        bw.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
                                        {
                                            string resultString = args.Result as string;
                                            if (resultString.CompareTo("Aborted") == 0)
                                                ReportException(new Exception("Task Run Fail, please check your workflow or parameters."));
                                            else
                                            {
                                                //Inform the parent about the task completion
                                                runnerPanel.CloseMe();
                                                BioWFMLResultManager resultComponent = new BioWFMLResultManager();
                                                OnHasNewResults(new ResultSetEventArgs(resultComponent.ReadBioWFML(args.Result as string)));
                                            }
                                        };
                                        //Run the bot
                                        bw.RunWorkerAsync(parameters);
                                    }
                                    catch (Exception eee)
                                    {
                                        ReportException(eee);
                                        runnerPanel.CloseMe();
                                    }
  
                                }
                                else
                                {
                                    MessageBox.Show("No workflow found!");
                                }
                            }
                            catch (Exception ex)
                            {
                                ReportException(ex);
                                runnerPanel.CloseMe();
                            }
                            
                            break;
                        }

                    case "Save Workflow":
                        {
                            try
                            {
                                //MessageBox.Show("Ooops not supported yet, perhaps try running it directly?");
                                MessageBox.Show(wfManager.GetWorkflowXML());
                                
                            }
                            catch (Exception ee)
                            {
                                ReportException(ee);
                            }

                            break;
                        }
                }


            }
        }