BlueSky.CommandExecutionHelper.ExecuteSyntaxEditor C# (CSharp) Method

ExecuteSyntaxEditor() public method

public ExecuteSyntaxEditor ( object param, bool SelectedForDump ) : void
param object
SelectedForDump bool
return void
        public void ExecuteSyntaxEditor(object param, bool SelectedForDump)
        {
            parameter = param;//set Global var.
            selectedForDump = SelectedForDump;//set Global var
            OnPreExecute(param);
            if (!canExecute) return;
            object obj = null;
            string dialogcommandstr = null;
            string HistMenuText = string.Empty;//29Mar2013
            try
            {
                if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: dialog xaml:" + TemplateFileName, LogLevelEnum.Info);
                //here TemplateFileName xaml will have same name as the analysis command function name
                // say- function called frm SynEdtr was 'bsky.my.func()' then in bin\Config\ 
                // dialog xaml, 'bsky.my.func.xaml' and
                // output template file 'bsky.my.func.xml' must exist
                // ie.. func name = xaml name = xml name
                XmlReader xmlr = XmlReader.Create(TemplateFileName);
                xmlr.ReadToFollowing("BSkyCanvas");

                //Following commented code can be used to Add command to "History" menu when executed from Syntax Editor
                //But "Title" from XAML may not same as "text" attr from menu.xml
                //So the same command executed from Syntax Editor and Main App's menu will show duplicate
                ////xmlr.MoveToAttribute("Title");//29Mar2013 For "History" Text
                ////HistMenuText = xmlr.Value;//29Mar2013 For "History" Text
                ////UAMenuCommand uam = (UAMenuCommand)param;
                ////uam.text = HistMenuText;
                ////parameter = uam;//set Global var

                xmlr.MoveToAttribute("CommandString");
                dialogcommandstr = xmlr.Value.Replace(" ", string.Empty).Replace('\"', '\'');
                if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: dialog command string:" + dialogcommandstr, LogLevelEnum.Info);
                xmlr.Close();
                obj = System.Windows.Markup.XamlReader.Load(XmlReader.Create(TemplateFileName));
            }
            catch (Exception ex)
            {
                //18Aug2014 Supressing this message box as we dont need it. But we still pass message in log.
                //MessageBox.Show("XAML missing or has improper format! Could not create template from " + TemplateFileName);
                logService.WriteToLogLevel("SynEdtr:Could not create template from " + TemplateFileName, LogLevelEnum.Error, ex);
                GenerateOutputTablesForNonXAML(param);
                return;
            }
            //obj = GetSessionDialog(); // same dialog cant be used as its child of the another parent in AUAnalysisCommandBase
            element = obj as FrameworkElement;
            window = new BaseOptionWindow();
            window.Template = element;
            element.DataContext = this; // loading vars in left listbox(source)
            ///window.ShowDialog();
            commandwindow = element;
            
            {
                if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: Dialog controls value mapping.", LogLevelEnum.Info);
                ////////////test///////
                //// take two strings and then try to make merged dictionary. remove extra spaces. replace " with '
                //string bksytemplate="bsky.CrossTable(x=c({Rows}),y=c({columns}),layers=c({layers}),datasetname='{%DATASET%}',chisq={chisq})";
                //string bskycommand="bsky.CrossTable(x=c('store','contact'),y='regular',layers=c('gender'),datasetname='Dataset1',chisq=FALSE)";
                //string dialogcommandstr = "bsky.one.sm.t.test(vars=c({SelectedVars}),mu={testValue},conf.level=0.89,datasetname='{%DATASET%}',missing=0)";
                string bskycommand = ((UAMenuCommand)parameter).bskycommand.Replace(" ", string.Empty);//"bsky.one.sm.t.test(vars=c('tg0','tg2','tg3'),mu=30,conf.level=0.89,datasetname='Dataset1',missing=0)";
                if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: Command:" + bskycommand, LogLevelEnum.Info);

                Dictionary<string, string> dialogkeyvalpair = new Dictionary<string, string>();//like: key=mu, val= {testValue}
                Dictionary<string, string> bskycommandkeyvalpair = new Dictionary<string, string>();//like: key=mu, val= 30
                Dictionary<string, string> merged = new Dictionary<string, string>();//like: key=testValue, val = 30

                OutputHelper.getArgumentSetDictionary(dialogcommandstr, dialogkeyvalpair);
                OutputHelper.getArgumentSetDictionary(bskycommand, bskycommandkeyvalpair);
                OutputHelper.MergeTemplateCommandDictionary(dialogkeyvalpair, bskycommandkeyvalpair, merged);

                foreach (KeyValuePair<string, string> pair in merged)
                {
                    if (AdvancedLogging)
                    {
                        string mapping = "Element:" + element.Name + ". Key:" + pair.Key + ". Value:" + pair.Value;
                        logService.WriteToLogLevel("ExtraLogs:\n" + mapping, LogLevelEnum.Info);
                    }
                    if (!pair.Key.Contains("%"))// This should only skip macros(words enclosed within %) and not other formats.
                    {
                        OutputHelper.SetValueFromSynEdt(element, pair.Key, pair.Value); //Filling dialog with values
                    }
                }
            }
            //foreach (Match m in mc)
            //{
            //    //Console.WriteLine(s.Index + " : " + s.ToString());// {SelectedVars} {testValue} {%DATASET%}
            //    if (!m.ToString().Contains("%"))
            //    {
            //        args = OutputHelper.getArgument(bskycommand, m.Index);
            //        uiElementName = m.ToString().Replace('{', ' ').Replace('}', ' ').Trim();
            //        OutputHelper.SetValueFromSynEdt(element, uiElementName, args);
            //    }
            //}
            //OutputHelper.SetValueFromSynEdt(element, "SelectedVars");
            //OutputHelper.SetValueFromSynEdt(element, "testValue");

            //For Chisq check box only
            //FrameworkElement chkElement = element.FindName("chisq") as FrameworkElement;
            if (true)//window.DialogResult.HasValue && window.DialogResult.Value)
            {
                //analytics can be sent from parent function(in SyntaxEditorWindow)
                cmd = new CommandRequest();

                OutputHelper.Reset();
                OutputHelper.UpdateMacro("%DATASET%", UIController.GetActiveDocument().Name);

                ///////////for chisq //// 29Mar2012 ///
                //if ((chkElement != null) && (bool)((chkElement as CheckBox).IsChecked))
                //    OutputHelper.UpdateMacro("%CHISQ%", "chisq");
                /////////////for chisq //// 29Mar2012 ///

                BSkyCanvas canvas = element as BSkyCanvas;
                if (canvas != null && !string.IsNullOrEmpty(canvas.CommandString))
                {
                    UAMenuCommand command = (UAMenuCommand)parameter;
                    cmd.CommandSyntax = command.commandformat;//OutputHelper.GetCommand(canvas.CommandString, element);// can be used for "Paste" for syntax editor
                    if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: Getting DOM", LogLevelEnum.Info);
                    retval = analytics.Execute(cmd); //ExecuteBSkyCommand(true);
                    ExecuteXMLDefinedDialog(cmd.CommandSyntax);//no need to pass parameters here. Just to match func signature
                }
                //16Apr2013 till this point command has already been executed. So now we store this command for "History"
                // moved here from common function ExecuteBSkyCommand b'coz for command batch dialog SaveHistory should execute once.
                //SaveInHistory(); // not sure if needed to be commented
            }

            //OnPostExecute(parameter);

            //Cleanup. Remove Canvas children and make it null. then make Window.templace null
            BSkyCanvas canv = element as BSkyCanvas;
            int count = canv.Children.Count;
            canv.Children.RemoveRange(0, count);
            window.Template = null;
        }

Usage Example

        //For Painting Output window for BSKyFormated object. And/Or to refresh datagrid for non-analytics commands like sort, compute
        private void RefreshOutputORDataset(string objectname, CommandRequest cmd, string originalCommand, OutputWindow ow)
        {
            UAMenuCommand uamc = new UAMenuCommand();
            cmd.CommandSyntax = "is.null(" + objectname + "$BSkySplit)";//$BSkySplit or $split in return structure
            if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: Executing : " + cmd.CommandSyntax, LogLevelEnum.Info);
            // is it just a list(non Bsky) or its a list that contains tables (user tables or Bsky Stat result tables)
            bool isNonBSkyList = false;
            object isNonBSkyListstr = analytics.ExecuteR(cmd, true, false);
            if (isNonBSkyListstr != null && isNonBSkyListstr.ToString().ToLower().Equals("true"))
            {
                isNonBSkyList = true;
            }
            if (isNonBSkyList)
            {
                string ewmessage = "This Object cannot be formatted using BSKyFormat. BSkyFormat can be used on Array, Matrix, Data Frame and BSky List objects only.";
                SendErrorToOutput(ewmessage, ow);//03Jul2013
                return;
            }
            if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: $BSkySplit Result (false means non-bsky list): " + isNonBSkyList, LogLevelEnum.Info);

            cmd.CommandSyntax = objectname + "$uasummary[[7]]";
            if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: Executing : " + cmd.CommandSyntax, LogLevelEnum.Info);

            string bskyfunctioncall = (string)analytics.ExecuteR(cmd, true, false);//actual call with values
            if (bskyfunctioncall == null)
            {
                bskyfunctioncall = ""; //24Apr2014 This is when no Dataset is open. And Syntax editor is open.Not returning, instead putting blank.
            }
            if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: $uasummary[[7]] Result : " + bskyfunctioncall, LogLevelEnum.Info);
            string bskyfunctionname = "";
            if (bskyfunctioncall.Length > 0)
            {
                if (bskyfunctioncall.Contains("("))
                    bskyfunctionname = bskyfunctioncall.Substring(0, bskyfunctioncall.IndexOf('(')).Trim();
                else
                    bskyfunctionname = bskyfunctioncall;
            }
            uamc.commandformat = objectname;// object that stores the result of analysis
            uamc.bskycommand = bskyfunctioncall.Replace('\"', '\'');// actual BSkyFunction call. " quotes replaced by '
            uamc.commandoutputformat = bskyfunctionname.Length > 0 ? string.Format(@"{0}", BSkyAppData.BSkyDataDirConfigBkSlash) + bskyfunctionname + ".xml" : "";//23Apr2015 
            uamc.commandtemplate = bskyfunctionname.Length > 0 ? string.Format(@"{0}", BSkyAppData.BSkyDataDirConfigBkSlash) + bskyfunctionname + ".xaml" : "";//23Apr2015 

            uamc.commandtype = originalCommand;
            CommandExecutionHelper auacb = new CommandExecutionHelper();
            if (AdvancedLogging) logService.WriteToLogLevel("ExtraLogs: CommandExecutionHelper: " , LogLevelEnum.Info);
            auacb.ExecuteSyntaxEditor(uamc, saveoutput.IsChecked == true ? true : false);//10JAn2013 edit
            auacb = null;

        }