Exemple #1
0
 public bool Equals(OclProgramSource other)
 {
     return(ContextId.Equals(other.ContextId) &&
            DeviceId.Equals(other.DeviceId) &&
            Source == other.Source &&
            Options == other.Options);
 }
Exemple #2
0
        /// <summary>
        /// Try to find a scenario for the target context. On failure, a new
        /// scenario is created if the creation is allowed, otherwise the
        /// predicted scenario is returned.
        /// </summary>
        /// <returns>A prediction, or null in case of error.</returns>
        public PredictionArgs Prediction()
        {
            Context        context    = null;
            PredictionArgs prediction = null;

            lock (_localizerLock)
            {
                if (ContextId != null)
                {
                    try
                    {
                        context = _contextService.GetById(ContextId);
                        if (context == null)
                        {
                            // notify error
                            if (LocalizerErrorNotification != null)
                            {
                                var args = new LocalizerErrorNotificationEventArgs(
                                    LocalizerErrorNotificationCode.UnknownContext);
                                ThreadPool.QueueUserWorkItem(new WaitCallback(DoNotification), args);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (LocalizerErrorNotification != null)
                        {
                            // notify error
                            var args = new LocalizerErrorNotificationEventArgs(
                                LocalizerErrorNotificationCode.DatabaseError, ex);
                            ThreadPool.QueueUserWorkItem(new WaitCallback(DoNotification), args);
                        }
                    }
                }

                if (context != null)
                {
                    // context found in the database

                    prediction = new PredictionArgs
                    {
                        Success = false
                    };

                    IEnumerable <ContextPreference> pref = null;
                    for (var attempts = this.Retries + 1; attempts > 0; attempts--)
                    {
                        prediction.PredictedScenario = GetScenarioForCurrentPosition(out pref);
                        if (prediction.PredictedScenario == null)
                        {
                            // retry, eventually..
                            continue;
                        }

                        if (ContextId.Equals(prediction.PredictedScenario.ContextId.ToString()))
                        {
                            // correct prediction
                            _scenarioService.IncreaseAccuracy(prediction.PredictedScenario);
                            prediction.Success = true;
                            break;
                        }

                        // wrong prediction
                        _scenarioService.DecreaseAccuracy(prediction.PredictedScenario);
                    }

                    if (!prediction.Success && CreationAllowed)
                    {
                        // create a new scenario for the requested context
                        prediction.PredictedScenario = new Scenario
                        {
                            ContextId    = new ObjectId(ContextId),
                            CreationTime = DateTime.Now
                        };

                        try
                        {
                            _scenarioService.Create(prediction.PredictedScenario);
                        }
                        catch (Exception ex)
                        {
                            if (LocalizerErrorNotification != null)
                            {
                                // notify error
                                var args = new LocalizerErrorNotificationEventArgs(
                                    LocalizerErrorNotificationCode.DatabaseError, ex);
                                ThreadPool.QueueUserWorkItem(new WaitCallback(DoNotification), args);
                            }
                            prediction = null;
                        }
                    }
                } // end if(context != null)
            }     // unlock

            return(prediction);
        }