Esempio n. 1
0
        public static string MatchPattern(string str)
        {
            string ScriptParameter = "";

            try
            {
                str = str.TrimEnd('?', '.', '!');
                str = str.Replace(" 'S", "'S");
                str = str.Replace(" 's", "'s");
                DataSet dataset = new DataSet();
                //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                //command.Parameters.AddWithValue("@Name", str);
                dataset = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern_match WHERE `match`='" + str.Replace("'", "''") + "'");

                if (dataset.Tables[0].Rows.Count > 0)
                {
                    //Since we have a match, lets execute the scripts
                    OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), "", "Jabber");
                    return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                }
                else
                {
                    //Replace Words with place holders and retry the pattern match
                    //example  "Please turn the main light on" becomes "Please turn the [OBJECT] [STATE]"

                    //Step 1: Break the Input into an Array to Query the Words for DB matches
                    str = str.ToUpper();

                    string[] words = str.Split(' ');

                    DataSet dsObjects = new DataSet();
                    foreach (String word in words)
                    {
                        dsObjects = OSAE.Common.ObjectNamesStartingWith(word.Replace("'S", ""));
                        foreach (DataRow dr in dsObjects.Tables[0].Rows)
                        {
                            if (str.IndexOf(dr["object_name"].ToString().ToUpper()) > -1)
                            //return "Found " + dr["object_name"].ToString();
                            {
                                str = str.Replace(dr["object_name"].ToString().ToUpper(), "[OBJECT]");
                                if (ScriptParameter.Length > 1)
                                {
                                    ScriptParameter = ScriptParameter + ",";
                                }
                                ScriptParameter += dr["object_name"].ToString();
                                //Determine if the Object is Possessive, which would be followed by a Property
                                if (str.ToUpper().IndexOf("[OBJECT]'S") > -1)
                                {
                                    //Here We have found our Possessive Object, so we need to look for an appropriate property afterwards
                                    //So we are going to retrieve a property list and compare it to the start of theremainder of the string

                                    DataSet dsProperties = new DataSet();
                                    dsProperties = OSAEObjectPropertyManager.ObjectPropertyListGet(dr["object_name"].ToString());
                                    foreach (DataRow drProperty in dsProperties.Tables[0].Rows)
                                    {
                                        //Here we need to break the string into words to avoid partial matches
                                        int    objectStartLoc = str.ToUpper().IndexOf("[OBJECT]'S");
                                        string strNewSearch   = str.Substring(objectStartLoc + 11);
                                        if (strNewSearch.ToUpper().IndexOf(drProperty["property_name"].ToString().ToUpper()) > -1)
                                        {
                                            str = str.Replace("[OBJECT]'S " + drProperty["property_name"].ToString().ToUpper(), "[OBJECT]'S [PROPERTY]");
                                            //str = str.Replace(drState["state_label"].ToString().ToUpper(), "[STATE]");
                                            ScriptParameter += "," + drProperty["property_name"].ToString();
                                        }
                                    }
                                }

                                //Here We have found our Object, so we need to look for an appropriate state afterwards
                                //So we are going to retrieve a state list and compare it to the remainder of the string
                                DataSet dsStates = new DataSet();
                                dsStates = OSAEObjectStateManager.ObjectStateListGet(dr["object_name"].ToString());
                                foreach (DataRow drState in dsStates.Tables[0].Rows)
                                {
                                    //Here we need to break the string into words to avoid partial matches
                                    string   replacementString = "";
                                    string[] wordArray         = str.Split(new Char[] { ' ' });
                                    foreach (string w in wordArray)
                                    {
                                        if (replacementString.Length > 1)
                                        {
                                            replacementString = replacementString + " ";
                                        }
                                        if (drState["state_label"].ToString().ToUpper() == w || drState["state_name"].ToString().ToUpper() == w)
                                        {
                                            replacementString = replacementString + "[STATE]";
                                            //str = str.Replace(drState["state_label"].ToString().ToUpper(), "[STATE]");
                                            ScriptParameter += "," + drState["state_name"].ToString();
                                        }
                                        else
                                        {
                                            replacementString = replacementString + w;
                                        }
                                    }
                                    //Now that we have replaced the Object and State, Lets check for a match again
                                    //DataSet dataset = new DataSet();
                                    //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                                    //command.Parameters.AddWithValue("@Name", str);
                                    //dataset = OSAESql.RunQuery(command);
                                    replacementString = replacementString.Replace(" 'S", "'S");
                                    dataset           = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern_match WHERE `match`='" + replacementString.Replace("'", "''") + "'");
                                    if (dataset.Tables[0].Rows.Count > 0)
                                    {
                                        //return dataset.Tables[0].Rows[0]["pattern"].ToString();
                                        //Since we have a match, lets execute the scripts
                                        OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), ScriptParameter, "Jabber");
                                        return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                                    }
                                    //break;
                                }
                                //break;
                            }
                        }
                    }
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - MatchPattern error: " + ex.Message, true);
                return(string.Empty);
            }
        }
        public static string MatchPattern(string str)
        {
            string ScriptParameter = "";

            try
            {
                DataSet dataset = new DataSet();
                //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                //command.Parameters.AddWithValue("@Name", str);
                dataset = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern WHERE `match`='" + str + "'");

                if (dataset.Tables[0].Rows.Count > 0)
                {
                    //Since we have a match, lets execute the scripts
                    OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), "", "Jabber");
                    return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                }
                else
                {
                    //Replace Words with place holders and retry the pattern match
                    //example  "Please turn the main light on" becomes "Please turn the [OBJECT] [STATE]"

                    //Step 1: Break the Input into an Array to Query the Words for DB matches
                    str = str.ToUpper();
                    string[] words = str.Split(' ');

                    DataSet dsObjects = new DataSet();
                    foreach (String word in words)
                    {
                        dsObjects = OSAE.Common.ObjectNamesStartingWith(word);
                        foreach (DataRow dr in dsObjects.Tables[0].Rows)
                        {
                            if (str.IndexOf(dr["object_name"].ToString()) > -1)
                            //return "Found " + dr["object_name"].ToString();
                            {
                                str              = str.Replace(dr["object_name"].ToString(), "[OBJECT]");
                                ScriptParameter += dr["object_name"].ToString();
                                //Here We have found our Object, so we need to look for an appropriate state afterwards
                                //So we are going to retrieve a state list and compare it to the remainder of the string

                                DataSet dsStates = new DataSet();
                                dsStates = OSAEObjectStateManager.ObjectStateListGet(dr["object_name"].ToString());
                                foreach (DataRow drState in dsStates.Tables[0].Rows)
                                {
                                    if (str.IndexOf(drState["state_label"].ToString().ToUpper()) > 0)
                                    {
                                        str              = str.Replace(drState["state_label"].ToString().ToUpper(), "[STATE]");
                                        ScriptParameter += ", " + drState["state_label"].ToString();

                                        //Now that we have replaced the Object and State, Lets check for a match again
                                        //DataSet dataset = new DataSet();
                                        //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                                        //command.Parameters.AddWithValue("@Name", str);
                                        //dataset = OSAESql.RunQuery(command);
                                        dataset = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern WHERE `match`='" + str + "'");
                                        if (dataset.Tables[0].Rows.Count > 0)
                                        {
                                            //return dataset.Tables[0].Rows[0]["pattern"].ToString();
                                            //Since we have a match, lets execute the scripts
                                            OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), ScriptParameter, "Jabber");
                                            return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                                        }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    return(string.Empty);
                    //return string.Empty;
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - MatchPattern error: " + ex.Message, true);
                return(string.Empty);
            }
        }
 public void SetState(string state)
 {
     OSAEObjectStateManager.ObjectStateSet(Name, state, "API");
     State = OSAEObjectStateManager.GetObjectStateValue(Name);
 }