public static void StartDBProcess() { List <TblDatabaseManagement> lstDB = DatabaseManagementsDS.GetDBToWork(StatusDB.Received); Metaquery rootMQ = Metaquery.GetRootMQ(); foreach (TblDatabaseManagement db in lstDB) { //create first level Console.WriteLine("start : {0}:{1}:{2}:{3}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond); Console.WriteLine(rootMQ.ToString()); TblMetaquery tblMetaquery = new TblMetaquery() { Arity = rootMQ.Arity, FkDatabaseId = db.Id, FkStatusId = (int)StatusDB.Received, Metaquery = rootMQ.ToString() }; MetaqueryDS.Create(tblMetaquery); DatabaseManagementsDS.UpdateStatus(db, StatusDB.InProcess); } //if at least 1 metaquery created - we can start send it to solver if (lstDB.Count > 0 && IsAutoRunJobs) { ThreadPool.QueueUserWorkItem(delegate { StartSendMQToSolver(); }); } }
public static List <Metaquery> VariableExpand(Metaquery query) { List <Metaquery> mqList = new List <Metaquery>(); Metaquery newMQ; //mqList.AddRange(AddVariable(query.Head, newMQ)); //Check the possibility of adding variable to Head Relation int maxVariablesInRelation = 5; if (query.Head.Arity < maxVariablesInRelation) { newMQ = query.Clone(); newMQ.Head.AddVariable(query.Head.Arity + 1); mqList.Add(newMQ); } //List<int> relationsVarCount = ProcessMQDetails.RelationsVarCount; //foreach (Relation bodyRelation in query.Body.List) for (int i = 0; i < query.Body.Count; i++) { Relation bodyRelation = query.Body[i]; if (bodyRelation.Arity < maxVariablesInRelation) //relationsVarCount[i]) { newMQ = query.Clone(); newMQ.Body[i].AddVariable(bodyRelation.Arity + 1); mqList.Add(newMQ); } //mqList.AddRange(AddVariable(bodyRelation, newMQ)); } return(mqList); }
public void TestMethod_Expand_HeadTo1() { Metaquery metaquery = new Metaquery("R0(1)←R1(1,2)"); List <Metaquery> listResult = metaquery.ExpandHead(); Assert.AreEqual(listResult.Count, 1); Assert.AreEqual(listResult[0].ToString(), "R0(X1,X2)←R1(X1,X2)"); }
public void TestMethod_Expand_BodyVariableRoot() { Metaquery rootMQ = Metaquery.GetRootMQ(); //mtodo List <Metaquery> listResult = rootMQ.ExpandBodyVariable(5); Assert.AreEqual(listResult.Count, 1); Assert.AreEqual(listResult[0].ToString(), "R0(X1)←R1(X1,X2)"); }
public void TestMethod_Expand_BodyRelationRoot() { Metaquery rootMQ = Metaquery.GetRootMQ(); List <Metaquery> listResult = rootMQ.ExpandBodyRelation(); Assert.AreEqual(listResult.Count, 2); Assert.AreEqual(listResult[0].ToString(), "R0(X1)←R1(X1)&R2(X1)"); Assert.AreEqual(listResult[1].ToString(), "R0(X1)←R1(X1)&R2(X2)"); }
public void TestMethod_Expand_MetaqueryLevel3() { Metaquery metaquery = new Metaquery("R(1)←R1(1,2,3)"); List <Metaquery> listResult = metaquery.ExpandHead(); Assert.AreEqual(listResult[0].ToString(), "R0(X1,X2)←R1(X1,X2,X3)"); Assert.AreEqual(listResult[1].ToString(), "R0(X1,X3)←R1(X1,X2,X3)"); Assert.AreEqual(2, listResult.Count); }
public static void StartExpandMQProcess() { List <TblMetaquery> lstMQ = MetaqueryDS.GetMQForExpand(); while (lstMQ.Count > 0) { foreach (TblMetaquery tblMetaquery in lstMQ) { TblDatabaseManagement curDB = tblMetaquery.TblDatabaseManagement; Metaquery metaqueryToExpand = new Metaquery(tblMetaquery.Metaquery); //create first level //int MaxVariablesInRelation = ProcessMQDetails.MaxVariablesInRelation; int maxVariables = curDB.MaxVariablesInRelation; ExpandType expandType = ExpandType.All; if (tblMetaquery.FkResult == (int)ResultMQ.SupportFailure) { expandType = ExpandType.NewRelationOnly; } if (tblMetaquery.FkResult == (int)ResultMQ.ConfidenceFailure) { expandType = ExpandType.InBodyOnly; } List <Metaquery> list = metaqueryToExpand.Expand(maxVariables, expandType); foreach (Metaquery mq in list) { TblMetaquery newTblMetaquery = new TblMetaquery() { Arity = mq.Arity, FkDatabaseId = curDB.Id, FkStatusId = (int)StatusDB.Received, Metaquery = mq.ToString() }; MetaqueryDS.Create(newTblMetaquery); } if (curDB.ForExperiment || tblMetaquery.FkResult != (int)ResultMQ.HasAnswers) { MetaqueryDS.UpdateStatus(tblMetaquery, StatusMQ.ExpandedAndDone); } else { MetaqueryDS.UpdateStatus(tblMetaquery, StatusMQ.Expanded); } } lstMQ = MetaqueryDS.GetMQForExpand(); } if (IsAutoRunJobs) { StartIncreaseDBArity(); } }
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); }
/// <summary> /// Unit test to check the Expand Head - Expand All /// </summary> public void TestMethod_Expand_RootTo3Level() { Metaquery rootMQ = Metaquery.GetRootMQ(); //mtodo List <Metaquery> listResultLevel2 = rootMQ.Expand(5, ExpandType.All); List <Metaquery> listResultLevel3 = new List <Metaquery>(); int[] cnt = new int[3]; for (int i = 0; i < listResultLevel2.Count; i++) { Metaquery metaquery = listResultLevel2[i]; //mtodo var list = metaquery.Expand(5, ExpandType.All); listResultLevel3.AddRange(list); cnt[i] = list.Count; } Assert.AreEqual(cnt[0], 4); Assert.AreEqual(cnt[1], 3); Assert.AreEqual(cnt[2], 4); }
//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); }
public static int StartSendMQToSolver() { List <TblMetaquery> lstMQ = MetaqueryDS.GetMQForSendToSolver(); int count = 0; bool doStartExpandMQProcess = false, doStartIncreaseDBArity = false; while (lstMQ.Count > 0) { RabbitProducer <string> producer = null; foreach (TblMetaquery tblMetaquery in lstMQ) { TblDatabaseManagement curDB = tblMetaquery.TblDatabaseManagement; if (!curDB.ForExperiment) { if (producer == null) { string queueToMQSolverName = ConfigurationManager.AppSettings["QueueToMQSolverName"]; producer = new RabbitProducer <string>(queueToMQSolverName); } Metaquery metaquery = new Metaquery(tblMetaquery.Metaquery); //שליחה לסולבר SendMQMessage message = new SendMQMessage() { ID = tblMetaquery.Id, SupportThreshold = curDB.SupportThreshold, ConfidenceThreshold = curDB.ConfidenceThreshold, Head = metaquery.Head.Variables, Body = metaquery.Body.GetVariables() }; string strMessage = message.ToJson(); //JsonConvert.SerializeObject(message); producer.SendMessage(strMessage); MetaqueryDS.UpdateStatus(tblMetaquery, StatusMQ.WaitingToSolver); } else { RandomMQProbability randomMQProbability = new RandomMQProbability(curDB.SupportProbability.Value, curDB.ConfidenceProbability.Value, curDB.ProbabilityIncreaseByArity ?? 0, tblMetaquery.Arity); tblMetaquery.FkResult = (int)randomMQProbability.GetRandomResultMQ(); StatusMQ statusMQ = (tblMetaquery.IsExpanded || tblMetaquery.Arity == tblMetaquery.TblDatabaseManagement.MaxArity ? StatusMQ.Done : StatusMQ.WaitingToExpand); MetaqueryDS.UpdateStatus(tblMetaquery, statusMQ); if (statusMQ == StatusMQ.WaitingToExpand) { doStartExpandMQProcess = true; } else if (statusMQ == StatusMQ.Done) { doStartIncreaseDBArity = true; } } count++; } lstMQ = MetaqueryDS.GetMQForSendToSolver(); } if (MQGenerator.IsAutoRunJobs) { if (doStartExpandMQProcess) { MQGenerator.StartExpandMQProcess(); } else if (doStartIncreaseDBArity) { MQGenerator.StartIncreaseDBArity(); } } return(count); }
public void TestMethod_Expand_HeadRoot() { Metaquery rootMQ = Metaquery.GetRootMQ(); Assert.AreEqual(rootMQ.ExpandHead().Count, 0); }
public void TestMethodStringConstructor() { Metaquery metaquery = new Metaquery("R0(X1,X2)←R1(X1,X2)&R2(X1,X2)"); Assert.AreEqual(metaquery.ToString(), "R0(X1,X2)←R1(X1,X2)&R2(X1,X2)"); }
public void TestMethodRootMQ() { Metaquery rootMQ = Metaquery.GetRootMQ(); Assert.AreEqual(rootMQ.ToString(), "R0(X1)←R1(X1)"); }