//------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        /*
         * Methods:
         */

        /// <summary>
        /// Updates the model associated with that sensor and resets the sensor option data.
        /// </summary>
        /// <param name="model">The new model to associate to the sensor.</param>
        public void UpdateModel(DiscreteSensorModel model)
        {
            _model = model;
            _options.Clear();
            for (int i = 0; i < model.OptionFlags.Count; i++)
            {
                _options.Add(new DiscreteSensorOption(model.OptionFlags[i], model.OptionParams[i]));
            }
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        /*
         * Constructor:
         */

        /// <summary>
        /// Builds an association between a sensor and a model. Finally, initialises the sensor option data.
        /// </summary>
        /// <param name="sensorName">The name of the sensor.</param>
        /// <param name="model">The model to which it will be associated.</param>
        public DiscreteSensorData(string sensorName, DiscreteSensorModel model)
            : this()
        {
            this.SensorName = sensorName;
            _model          = model;
            _watch          = Stopwatch.StartNew();
            _options        = new List <DiscreteSensorOption>();
            for (int i = 0; i < model.OptionFlags.Count; i++)
            {
                _options.Add(new DiscreteSensorOption(model.OptionFlags[i], model.OptionParams[i]));
            }
        }
예제 #3
0
        //------------------------------------------------------------------------------------------------

        private void ParseDiscreteSensorModel(XmlNode node)
        {
            DiscreteSensorModel sensorBelief = new DiscreteSensorModel(node.Attributes["name"].InnerText);
            //Load options if there are some:
            XmlNode options = node.SelectSingleNode("options");

            if (options != null)
            {
                foreach (XmlNode n in options.ChildNodes)
                {
                    string name  = n.Attributes["name"].InnerText.ToLower();
                    string param = n.InnerText;
                    if (name == "tempo-specificity")
                    {
                        sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_TEMPO_SPECIFICITY, Convert.ToDouble(param));
                    }
                    else if (name == "tempo-fusion")
                    {
                        sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_TEMPO_FUSION, Convert.ToDouble(param));
                    }
                    else if (name == "variation")
                    {
                        sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_VARIATION, Convert.ToDouble(param));
                    }
                    else
                    {
                        throw new InvalidBeliefModelException("The option \"{0}\" does not exist!");
                    }
                }
            }
            //Load each point:
            XmlNodeList points = node.SelectNodes("point");

            if (points == null)
            {
                throw new InvalidBeliefModelException("A sensor model should contain points!");
            }
            List <DiscreteSensorFocalBelief> focals = new List <DiscreteSensorFocalBelief>();

            foreach (XmlNode point in points)
            {
                XmlNode value = point.SelectSingleNode("value");
                if (value == null)
                {
                    throw new InvalidBeliefModelException("Every point in a model should contain a sensor measure value under \"value\"!");
                }
                double      measure = Convert.ToDouble(value.InnerText);
                XmlNodeList masses  = point.SelectNodes("mass");
                if (masses == null)
                {
                    throw new InvalidBeliefModelException("Every point in a model should contain masses!");
                }
                foreach (XmlNode mass in masses)
                {
                    //Create the focal point:
                    double massValue             = Convert.ToDouble(mass.InnerText);
                    DiscreteSensorFocalPoint sfp = new DiscreteSensorFocalPoint(measure, massValue);
                    //Create the element and add it if necessary:
                    DiscreteElement           e   = new DiscreteElement(this._refList, mass.Attributes["set"].InnerText.Split(' '));
                    DiscreteSensorFocalBelief sfb = new DiscreteSensorFocalBelief(e);
                    if (focals.Contains(sfb))
                    {
                        int index = focals.IndexOf(sfb);
                        focals[index].AddPoint(sfp);
                    }
                    else
                    {
                        focals.Add(sfb);
                        focals[focals.Count - 1].AddPoint(sfp);
                    }
                }
            }
            foreach (DiscreteSensorFocalBelief sfb in focals)
            {
                sfb.Sort();
                sensorBelief.AddFocal(sfb);
            }
            //Add the model:
            _discreteSensorModels.Add(sensorBelief);
        }
예제 #4
0
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        #region Private load methods

        /*
         * Private methods
         */
        private void LoadDiscreteSensorModel(string path)
        {
            //Load the options if there are some:
            string optionsFileName           = path + Path.DirectorySeparatorChar + "options";
            DiscreteSensorModel sensorBelief = new DiscreteSensorModel(new DirectoryInfo(path).Name);

            //Load files one by one:
            foreach (string filename in Directory.GetFiles(path))
            {
                //Avoid temp files:
                if (filename[filename.Length - 1] == '~')
                {
                    continue;
                }

                using (StreamReader file = new StreamReader(filename))
                {
                    string[] lines = file.ReadToEnd().Split('\n');
                    //Options file:
                    if (filename == optionsFileName)
                    {
                        try
                        {
                            int nbOptions = Convert.ToInt32(lines[0].Split(' ')[0]);
                            if (nbOptions <= 0)
                            {
                                throw new InvalidBeliefModelException("You're seriously declaring 0 options with an options file?!");
                            }
                            for (int i = 0; i < nbOptions; i++)
                            {
                                string[] words = lines[i + 1].Split(' ');
                                words[0] = words[0].ToLower();
                                if (words[0] == "tempo-specificity")
                                {
                                    sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_TEMPO_SPECIFICITY, Convert.ToDouble(words[1]));
                                }
                                else if (words[0] == "tempo-fusion")
                                {
                                    sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_TEMPO_FUSION, Convert.ToDouble(words[1]));
                                }
                                else if (words[0] == "variation")
                                {
                                    sensorBelief.AddOption(DiscreteSensorOption.Option_Flags.OP_VARIATION, Convert.ToDouble(words[1]));
                                }
                                else
                                {
                                    throw new InvalidBeliefModelException("The option \"{0}\" does not exist!");
                                }
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            throw new InvalidBeliefModelException(String.Format("The number of options indicated in \"{0}\" does not correspond to the rest of the file!", filename));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidBeliefModelException(String.Format("The numbers in the file \"{0}\" are not in the proper format or at the wrong place!", filename));
                        }
                        //Any other file:
                    }
                    else
                    {
                        try
                        {
                            int nbAtoms = Convert.ToInt32(lines[0].Split(' ')[0]);
                            if (nbAtoms <= 0)
                            {
                                throw new InvalidBeliefModelException("There must be at least one atom to build an Element!");
                            }
                            string[] atoms = new string[nbAtoms];
                            for (int i = 0; i < nbAtoms; i++)
                            {
                                atoms[i] = lines[i + 1];
                            }
                            DiscreteSensorFocalBelief fb = new DiscreteSensorFocalBelief(new DiscreteElement(_refList, atoms));
                            //Add the points:
                            int nbPoints = Convert.ToInt32(lines[nbAtoms + 1].Split(' ')[0]);
                            if (nbPoints < 1)
                            {
                                throw new InvalidBeliefModelException("There must be at least one point to get mass from!");
                            }
                            for (int i = 0; i < nbPoints; i++)
                            {
                                string[] values = lines[nbAtoms + 2 + i].Split(' ');
                                fb.AddPoint(new DiscreteSensorFocalPoint(Convert.ToDouble(values[0]), Convert.ToDouble(values[1])));
                            }
                            fb.Sort();
                            sensorBelief.AddFocal(fb);
                        }
                        catch (IndexOutOfRangeException)
                        {
                            throw new InvalidBeliefModelException(String.Format("The model contained in file \"{0}\" is not formatted properly!", filename));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidBeliefModelException(String.Format("The numbers in the file \"{0}\" are not in the proper format or at the wrong place!", filename));
                        }
                    }
                }
            }
            _discreteSensorModels.Add(sensorBelief);
        }