Exemplo n.º 1
0
        public override void OnGetMessageFromGenerator(string strMessage)
        {
            string queueToMQGeneratorName    = ConfigurationManager.AppSettings["QueueToMQGeneratorName"];
            RabbitProducer <string> producer = new RabbitProducer <string>(queueToMQGeneratorName);

            SendMQMessage message   = JsonConvert.DeserializeObject <SendMQMessage>(strMessage);
            Metaquery     metaquery = new Metaquery(message);

            ResultMQ resultMQ = ResultMQ.HasAnswers;

            // Random the probability of the result
            Random rnd        = new Random();
            int    rndSupport = rnd.Next(100);

            //Probability of  support failure
            if (rndSupport < SupportProbability)
            {
                resultMQ = ResultMQ.SupportFailure;
            }
            else
            {
                //Probability of  confidence failure
                int rndConfidence = rnd.Next(100);
                if (rndConfidence < ConfidenceProbability)
                {
                    resultMQ = ResultMQ.ConfidenceFailure;
                }
            }
            MQResultMessage resultMessage = new MQResultMessage()
            {
                ID = message.ID, Result = resultMQ
            };

            if (resultMQ == ResultMQ.HasAnswers)
            {
                MQAssignmentResultMessage assignmentResultMessage = new MQAssignmentResultMessage()
                {
                    ID              = message.ID,
                    Assignment      = "TEST - Assignment",
                    ConfidenceValue = 0,
                    SupportValue    = 0
                };

                SendMQMessageToSolver(producer, assignmentResultMessage);
            }
            SendMQMessageToSolver(producer, resultMessage);
        }
Exemplo n.º 2
0
        public void TestMQResultMessageToJson()
        {
            MQResultMessage resultMessage = new MQResultMessage()
            {
                ID = 1, Result = ResultMQ.HasAnswers
            };

            string strMessage             = JsonConvert.SerializeObject(resultMessage);
            string strExceptedMessageJson = "{\"Result\":true,\"ID\":1}";

            Assert.AreEqual(strExceptedMessageJson, strMessage);

            MQAssignmentResultMessage assignmentResultMessage = new MQAssignmentResultMessage()
            {
                ID = 1, Assignment = "Any assignment", ConfidenceValue = 0.3m, SupportValue = 0.1m
            };

            strMessage             = JsonConvert.SerializeObject(assignmentResultMessage);
            strExceptedMessageJson = "{\"ID\":1,\"SupportValue\":0.1,\"ConfidenceValue\":0.3,\"Assignment\":\"Any assignment\"}";

            Assert.AreEqual(strExceptedMessageJson, strMessage);
        }
Exemplo n.º 3
0
        //public static void SendMQAssignmentResultMessage(RabbitProducer<string> producer, MQAssignmentResultMessage resultMessage)
        //{
        //	string strMessage = resultMessage.ToJson();
        //	producer.SendMessage(strMessage);
        //}

        public virtual void OnGetMessageFromGenerator(string strMessage)
        {
            string queueToMQGeneratorName    = ConfigurationManager.AppSettings["QueueToMQGeneratorName"];
            RabbitProducer <string> producer = new RabbitProducer <string>(queueToMQGeneratorName);

            SendMQMessage message   = JsonConvert.DeserializeObject <SendMQMessage>(strMessage);
            Metaquery     metaquery = new Metaquery(message);

            List <ResultDemoMq> resultMQList = MetaquerySolverDemoDS.GetResultDemoMq(metaquery.ToString());

            bool     hasResult = resultMQList.Count > 0;
            ResultMQ resultMQ  = ResultMQ.HasAnswers;

            if (!hasResult)
            {
                Random rnd    = new Random();
                int    rndNum = rnd.Next(2);
                if (rndNum == 0)
                {
                    resultMQ = ResultMQ.ConfidenceFailure;
                }
                else
                {
                    resultMQ = ResultMQ.SupportFailure;
                }
            }
            MQResultMessage resultMessage = new MQResultMessage()
            {
                ID = message.ID, Result = resultMQ
            };

            if (hasResult)
            {
                foreach (ResultDemoMq resultDemoMQ in resultMQList)
                {
                    MQAssignmentResultMessage assignmentResultMessage = new MQAssignmentResultMessage()
                    {
                        ID              = message.ID,
                        Assignment      = resultDemoMQ.Assignment,
                        ConfidenceValue = resultDemoMQ.ConfidenceValue.Value,
                        SupportValue    = resultDemoMQ.SupportValue.Value
                    };

                    SendMQMessageToSolver(producer, assignmentResultMessage);

                    //System.Threading.Thread.Sleep(3000);
                }
            }
            SendMQMessageToSolver(producer, resultMessage);

            //System.Threading.Thread.Sleep(3000);

            /*
             * MQResultMessage resultMessage = new MQResultMessage() { ID = 1, Result = true};
             *
             * string strMessage = JsonConvert.SerializeObject(resultMessage);
             * string strResultMessageJson = "{\"ID\":1,\"Result\":true}";
             * Assert.AreEqual(strMessage, strResultMessageJson);
             *
             * MQAssignmentResultMessage assignmentResultMessage = new MQAssignmentResultMessage() { ID = 1,Assignment="Any assignment",ConfidenceValue=0.3f,SupportValue=0.1f };
             * strMessage = JsonConvert.SerializeObject(assignmentResultMessage);
             * strResultMessageJson = "{\"ID\":1,\"SupportValue\":0.1,\"ConfidenceValue\":0.3,\"Assignment\":\"Any assignment\"}";
             */
            //MessageBox.Show("TestEvent, string: " + result.Metaquery);
        }
Exemplo n.º 4
0
        public static void OnGetMessageFromSolver(string strMessage)
        {
            MQMessage message = null;

            if (strMessage.Contains("Result"))            //strMessage is MQResultMessage
            {
                message = JsonConvert.DeserializeObject <MQResultMessage>(strMessage);
            }
            else             //strMessage is MQAssignmentResultMessage
            {
                message = JsonConvert.DeserializeObject <MQAssignmentResultMessage>(strMessage);
            }

            int          metaqueryID  = message.ID;
            TblMetaquery tblMetaquery = MetaqueryDS.GetByID(metaqueryID);

            try
            {
                if (tblMetaquery == null)
                {
                    throw new UnknownMetaqueryException(string.Format("There is an unknown metaquery id sent from solver. message id: {0}. MQ Solver message :", metaqueryID, message.ToString()));
                }

                if (message is MQResultMessage)
                {
                    MQResultMessage resultMessage = message as MQResultMessage;
                    tblMetaquery.FkResult = (int)resultMessage.Result;
                    StatusMQ statusMQ = (tblMetaquery.IsExpanded || tblMetaquery.Arity == tblMetaquery.TblDatabaseManagement.MaxArity ? StatusMQ.Done : StatusMQ.WaitingToExpand);
                    MetaqueryDS.UpdateStatus(tblMetaquery, statusMQ);
                    bool execSendMail = bool.Parse(ConfigurationManager.AppSettings["ExecSendMail"]);
                    if (execSendMail)
                    {
                        MQGeneratorMail.SendResultMail(tblMetaquery);
                    }

                    if (MQGenerator.IsAutoRunJobs)
                    {
                        if (statusMQ == StatusMQ.WaitingToExpand)
                        {
                            MQGenerator.StartExpandMQProcess();
                        }
                        else if (statusMQ == StatusMQ.Done)
                        {
                            MQGenerator.StartIncreaseDBArity();
                        }
                    }
                }
                else if (message is MQAssignmentResultMessage)
                {
                    MQAssignmentResultMessage assignmentResultMessage = message as MQAssignmentResultMessage;
                    TblMetaqueriesResult      tblMetaqueriesResult    = new TblMetaqueriesResult()
                    {
                        FkMetaqueryId   = metaqueryID,
                        Assignment      = assignmentResultMessage.Assignment,
                        ConfidenceValue = assignmentResultMessage.ConfidenceValue,
                        SupportValue    = assignmentResultMessage.SupportValue
                    };
                    MetaqueryResultDS.Create(tblMetaqueriesResult);

                    bool execSendMail = bool.Parse(ConfigurationManager.AppSettings["ExecSendMail"]);
                    if (execSendMail)
                    {
                        MQGeneratorMail.SendAssignmentMail(tblMetaqueriesResult, tblMetaquery.Metaquery);
                    }
                    if (MQGenerator.IsAutoRunJobs && !tblMetaquery.IsExpanded)
                    {
                        MQGenerator.StartExpandMQProcess();
                    }
                }
                else
                {
                    throw new UnknownMetaqueryException("There is an unknown metaquery sent from solver. MQ Solver message :" + message.ToString());
                }
            }
            catch (Exception ex)
            {
                MQGeneratorMail.SendExceptionMail(ex);
            }
        }