public void DescribeInstancesNoRelationNoAttributeTest() { var cnlSentences = new List <string>() { "John is a man.", "John has-friend Martha.", "Mark is a man.", "Mark has-nickname equal-to 'Maro'." }; var feClient = new CogniPySvr(); feClient.LoadCnlFromString(string.Join("\r\n", cnlSentences), true, true); var result = feClient.DescribeInstances("a man"); Assert.AreEqual("Martha", result["John"].RelatedInstances["has-friend"].First()); Assert.AreEqual(0, result["John"].AttributeValues.Count); Assert.AreEqual("Maro", (string)result["Mark"].AttributeValues["has-nickname"].First()); Assert.AreEqual(0, result["Mark"].RelatedInstances.Count); }
public void MaterializationTest(bool materialize) { var cnlSentences = new List <string>() { "John is a man.", "Every man is a human-being." }; var feClient = new CogniPySvr(); feClient.LoadCnlFromString(string.Join("\r\n", cnlSentences), true, materialize: materialize); //var res = feClient.SparqlQuery("SELECT * WHERE {?x ?y ?z}"); var result = feClient.ToCNL(true, true); Assert.IsNotNull(result); if (materialize) { Assert.IsTrue(result.Contains("John is a human-being.")); } else { Assert.IsFalse(result.Contains("John is a human-being.")); } }
public void KnowledgeInsertAnnotationsTest(bool includeImplicit, bool removeTrivials) { var cnlSentences = new List <string>() { "Data-Location-Form is a data-location-form.", "Annotations: _Data-Location-Form Instance: description 'A data location form.'@en.", "Comment: This comment should not be returned as CNL statement." }; var feClient = new CogniPySvr(); feClient.LoadCnlFromString(string.Join("\r\n", cnlSentences), true, true); var toInsert = new List <string>() { "Operational-Risk is a thing.", "Annotations: _Operational-Risk Instance: network-description 'Network of operational risk.'@en." }; feClient.KnowledgeInsert(string.Join("\r\n", toInsert), true, true); var annots = feClient.GetAnnotationsForSignature(new List <string>() { "Operational-Risk" }); Assert.IsTrue(annots.Where(a => a.Property == "network-description").Count() > 0); //Check for number of Annotations: blocks, should be exactly 1 var toCnl = feClient.ToCNL(includeImplicit, true); Assert.AreEqual(1, toCnl.Split(' ').Where(tok => tok.Contains("Annotations:")).Count()); var toCnlList = feClient.ToCNLList(includeImplicit, removeTrivials, true); Assert.AreEqual(1, toCnlList.Count(s => s.Contains("Annotations:"))); }
public void ToCNLListTest(bool includeImplicit, bool removeTrivials = true) { var cnlSentences = new List <string>() { "Every location-form is a form.", "Every data-location-form is a location-form.", "Data-Location-Form is a data-location-form.", "If a form is a data-location-form then the form is-a-form-of-type Special-Form.", "Annotations:\r\n_Operational-Risk Instance: network-description 'Network of operational risk.'@en\r\n.", }; var feClient = new CogniPySvr(); feClient.LoadCnlFromString(string.Join("\r\n", cnlSentences.Union <string>(new List <string>() { "Comment: This comment should not be returned." })), true, true); var sentencesReturned = feClient.ToCNLList(includeImplicit, removeTrivials, true); Assert.AreEqual(sentencesReturned.Count, sentencesReturned.Distinct().ToList().Count, "There are duplicate senetences!"); if (includeImplicit) { if (removeTrivials) { Assert.AreEqual(9, sentencesReturned.Count); } else { Assert.AreEqual(14, sentencesReturned.Count); } } else { CollectionAssert.AreEquivalent(cnlSentences, sentencesReturned); } }
public void RuleDebuggerTest() { var cnlSentences = new List <string>() { "John is a man.", "Mary is a woman.", "John has-friend Mary.", "Mary has-name equal-to 'Mary'.", "If a man has-friend a woman and the woman has-name equal-to 'Mary' then the man is an happy-man." }; var feClient = new CogniPySvr(); var ruleId = feClient.GetStatementId(cnlSentences[4]); feClient.SetDebugListener((statementId, elements) => { Assert.AreEqual(ruleId, statementId); Assert.AreEqual(2, elements.Count()); Assert.AreEqual("John", elements[0].Value.ToString()); Assert.AreEqual("man", elements[0].Name); Assert.AreEqual("Mary", elements[1].Value.ToString()); Assert.AreEqual("woman", elements[1].Name); }, (s, c) => Tuple.Create(s, c)); feClient.LoadCnlFromString(string.Join("\r\n", cnlSentences), true, true); }
public static void EntryPoint(string[] args) { Stream inputStream = Console.OpenStandardInput(); Stream outputStream = Console.OpenStandardOutput(); var reader = new StreamReader(inputStream); var writer = new StreamWriter(outputStream); Dictionary <string, CogniPySvr> clients = new Dictionary <string, CogniPySvr>(); while (true) { var cmd = reader.ReadLine(); if (cmd == "@create") { var uid = Guid.NewGuid().ToString(); var fe = new CogniPySvr(); clients.Add(uid, fe); writer.Write(uid); } else if (cmd == "@delete") { var uid = reader.ReadLine(); clients.Remove(uid); writer.Write("@deleted"); } else if (cmd == null || cmd == "@exit") { break; } else { try { var uid = reader.ReadLine(); var fe = clients[uid]; StringBuilder sb = new StringBuilder(); while (true) { var line = reader.ReadLine(); if (line == "\0") { break; } sb.Append(line); } JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.Indented, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); var parms = serializer.Deserialize <object[]>(new JsonTextReader(new StringReader(sb.ToString()))); object ret = null; try { var method = fe.GetType().GetMethod(cmd); var ptp = method.GetParameters(); var cps = new List <object>(); for (var idx = 0; idx < ptp.Length; idx++) { object cp; if (parms[idx] is JToken) { cp = (parms[idx] as JToken).ToObject(ptp[idx].ParameterType); } else { cp = parms[idx]; } cps.Add(cp); } ret = method.Invoke(fe, cps.ToArray()); } catch (AmbiguousMatchException) { ret = fe.GetType().InvokeMember(cmd, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, fe, parms); } writer.WriteLine("@result"); serializer.Serialize(new JsonTextWriter(writer), ret); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } writer.WriteLine("@exception"); var w = new JsonTextWriter(writer); JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.Indented, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); serializer.Serialize(w, new object[] { ex.GetType().Name, ex }); } } writer.WriteLine(); writer.WriteLine("\0"); writer.Flush(); } }