// Kiem tra 2 menh de co chua bien menh de doi ngau nhau khong // true : co bien menh de doi ngau nhau // false : khong co bien menh de doi ngau nhau public static Boolean AntithesisLiteral(_Clause p, _Clause q) { Boolean result = false; if (p == null || q == null) { return(result); } if (p.mArrLiteral == null || q.mArrLiteral == null) { return(result); } for (int i = 0; i < p.mArrLiteral.Length && !result; ++i) { for (int j = 0; j < q.mArrLiteral.Length && !result; ++j) { if (_Literal.Antithesis(p.mArrLiteral[i], q.mArrLiteral[j]) == true) { result = true; } } } return(result); }
// Chuyen chuoi ket luan thanh KB public static _KB ConclusionToKB(String str) { // Chua kiem tra tinh hop le cua chuoi str _KB result = new _KB(); str = str.Replace("|", ","); String[] strClause = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); // Phan tich thanh cac chuoi menh de result.mArrClause = new _Clause[strClause.Length]; // Them cac menh de vao KB for (int i = 0; i < strClause.Length; ++i) { _Clause clause = new _Clause(); // Tu cac chuoi menh de, phan tich thanh cac chuoi bien menh de String[] strLiteral = strClause[i].Split(new String[] { "&" }, StringSplitOptions.RemoveEmptyEntries); // Them cac bien menh de vao menh de clause for (int j = 0; j < strLiteral.Length; ++j) { _Literal literal = new _Literal(strLiteral[j]); clause.AddLiteral(!literal); } result.mArrClause[i] = clause; } return(result); }
// Chuyen chuoi thanh KB public void StringToKB(String str) { // Chua kiem tra tinh hop le cua str // Phan tich thanh cac chuoi menh de String[] strClause = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); mArrClause = new _Clause[strClause.Length]; for (int i = 0; i < strClause.Length; ++i) { mArrClause[i] = new _Clause(strClause[i]); } }
// Ham tao sao chep public _Clause(_Clause p) { if (p == null) { mArrLiteral = null; return; } mArrLiteral = new _Literal[p.mArrLiteral.Length]; for (int i = 0; i < mArrLiteral.Length; ++i) { mArrLiteral[i] = new _Literal(p.mArrLiteral[i]); } }
// Ham dung sao chep public _KB(_KB p) { if (p == null) { mArrClause = null; return; } mArrClause = new _Clause[p.mArrClause.Length]; for (int i = 0; i < mArrClause.Length; ++i) { mArrClause[i] = new _Clause(p.mArrClause[i]); } }
// Them menh de vao KB public void AddClause(_Clause p) { if (p == null) { return; } if (mArrClause == null) { mArrClause = new _Clause[1]; } else { Array.Resize(ref mArrClause, mArrClause.Length + 1); } mArrClause[mArrClause.Length - 1] = new _Clause(p); }
static void Main(string[] args) { _Clause x = new _Clause("p"); _Clause y = new _Clause("~p|v"); int i; int j; _KB kb = new _KB(); kb.AddClause(x); kb.AddClause(new _Clause("v")); kb.AddClause(y); Console.WriteLine(kb.FindAntithesisClause(out i, out j)); Console.WriteLine(i + " " + j); /* * _RobinsonAlgorithm x = new _RobinsonAlgorithm(); * x.ReadFile("..\\..\\RobinsonInput.inp"); * */ /* * _Literal t = new _Literal("~literal"); * _Clause clause = new _Clause("~p|q|r"); * * _KB kb = new _KB(); * _KB kb1 = new _KB("p|~q"); * kb.ResultToKB("m&s, p , ~s & k"); * kb.AddClause(clause); * kb1.AddClause(clause); * * _KB kq = kb; * kq = kb + kb1; * * Console.WriteLine("_Literal t = " + !t); * Console.WriteLine("_Clause clause = " + clause); * Console.WriteLine("_KB kb = " + kb); * Console.WriteLine("_KB kb1 = " + kb1); * Console.WriteLine("_KB kq=kb + kb1 = " + kq); * Console.WriteLine("_KB kb = " + kb); */ }
// Kiem tra 2 menh de co doi ngau nhau khong // true : doi ngau nhau // false : khong doi ngau nhau public static Boolean Antithesis(_Clause p, _Clause q) { Boolean result = false; if (p == null || q == null) { return(result); } if (p.mArrLiteral == null || q.mArrLiteral == null) { return(result); } if (p.mArrLiteral.Length != 1 || q.mArrLiteral.Length != 1) { return(result); } result = _Literal.Antithesis(p.mArrLiteral[0], q.mArrLiteral[0]); return(result); }