Esempio n. 1
0
        public override void Localize(IList<IMeasure> input, ref Location currLocation, ref bool unknown)
        {
            using (var uow = ModelHelper.GetUnitOfWork())
            {
                // currLocation may be updated by other threads
                if (!unknown)
                {
                    Log.Debug("Trying to retrieve Location " + currLocation.Name);
                    currLocation = uow.GetLocationByName(currLocation.Name);
                    Log.Debug(currLocation == null ? "Nothing Retrieved " : "Retrieved " + currLocation.Name);
                }

                // currLocation may be null if updated by others threads
                //it could also be unknown
                // proximity bias
                if (currLocation != null && !unknown && currLocation.TestInput(input) >= 0)
                {
                        currLocation.UpdateStats(input);
                }
                else
                {
                    String oldLocationName = null;
                    if (currLocation != null)
                    {
                        oldLocationName = currLocation.Name;
                    }
                    if (!unknown)
                    {
                        currLocation = null;
                    }

                    Log.Debug("Trying to retrieve all locations");
                    var locations = uow.GetAll<Location>();
                    Log.Debug("All locations retrieved");

                    if (locations.Count > 0)
                    {
                        foreach (var location in locations
                            .Where(location => oldLocationName == null || !location.Name.Equals(oldLocationName)))
                        {
                            if (location.TestInput(input) >= 0)
                            {
                                unknown = false;
                                currLocation = location;

                                currLocation.StartActivities();

                                currLocation.SetUpCurrentTimeStats();

                                location.UpdateStats(input);
                                break;
                            }
                        }
                    }

                    if (unknown)
                    {
                        Debug.Assert(currLocation != null, "currLocation != null");
                        if (currLocation.TestInput(input) >= 0)
                        {
                            currLocation.UpdateStats(input);
                        }
                        else
                        {
                            currLocation = null;
                        }
                    }
                    if (currLocation == null)
                    {
                        currLocation = ModelHelper.EntitiesFactory
                                                  .CreateLocation(LocationLocalizer.Unknown, input);
                        unknown = true;
                    }
                }
                uow.Commit();
            }
        }
Esempio n. 2
0
        public override void Localize(IList<IMeasure> input, ref Location currLocation, ref bool unknown)
        {
            using (var uow = ModelHelper.GetUnitOfWork())
            {
                // currLocation may be updated by other threads
                if (!unknown)
                {
                    Log.Debug("Trying to retrieve Location " + currLocation.Name);
                    currLocation = uow.GetLocationByName(currLocation.Name);
                    Log.Debug(currLocation == null ? "Nothing Retrieved " : "Retrieved " + currLocation.Name);

                    // Learning phase
                    if (currLocation != null && currLocation.GetObservations() <= LearningTresh)
                    {
                        Log.Debug("Learning Phase "+ currLocation.Name + ": step = " + currLocation.GetObservations());
                        currLocation.UpdateStats(input);
                        uow.Commit();
                        return;
                    }
                }

                Log.Debug("Trying to retrieve all locations");
                var locations = uow.GetAll<Location>();
                Log.Debug("All locations retrieved");

                Location candidateLocation = null;
                if (locations.Count > 0)
                {
                    double min = Double.MaxValue;
                    foreach (var location in locations)
                    {
                        double curr = location.TestInput(input);
                        if (curr.CompareTo(0D) >= 0 && curr.CompareTo(min) < 0)
                        {
                            candidateLocation = location;
                            min = curr;
                        }
                    }
                }

                if (candidateLocation != null)
                {
                    unknown = false;
                    if (currLocation == null || !currLocation.Name.Equals(candidateLocation.Name))
                    {
                        currLocation = candidateLocation;
                        currLocation.StartActivities();
                        currLocation.SetUpCurrentTimeStats();
                    }
                    Debug.Assert(currLocation.Name.Equals(candidateLocation.Name),
                                 "currLocation.Name.Equals(candidateLocation.Name)");
                    currLocation.UpdateStats(input);
                }
                else
                {
                    if (unknown)
                    {
                        Debug.Assert(currLocation != null, "currLocation != null");
                        if (currLocation.TestInput(input).CompareTo(0D) >= 0)
                        {
                            currLocation.UpdateStats(input);
                        }
                        else
                        {
                            unknown = false;
                        }
                    }

                    if (!unknown)
                    {
                        currLocation = ModelHelper.EntitiesFactory
                                                  .CreateLocation(LocationLocalizer.Unknown, input);
                        unknown = true;
                    }
                }
                uow.Commit();
            }
        }