public void GetRulesForWorlds_GivenWorlds_ExpectRuleSetfor2Worlds() { // Arrange... var jsonWorlds = new List <string>(); jsonWorlds.Add("{'Name':'Bob', 'Age':'40', 'Year': '2040'}"); jsonWorlds.Add("{'Name':'Bob', 'Age':'30', 'Year': '2030'}"); var sut = new DymeInferenceEvaluator(_worldReader, _worldAnalyser); var expected = new List <IEvaluatable>(); expected.Add(If.When(ItsAFact.That("Year").Is("2040")).Then(ItsAFact.That("Age").Is("40"))); expected.Add(If.When(ItsAFact.That("Year").Is("2030")).Then(ItsAFact.That("Age").Is("30"))); expected.Add(If.When(ItsAFact.That("Year").Is("2040")).Then(ItsAFact.That("Name").Is("Bob"))); expected.Add(If.When(ItsAFact.That("Year").Is("2030")).Then(ItsAFact.That("Name").Is("Bob"))); expected.Add(If.When(ItsAFact.That("Age").Is("40")).Then(ItsAFact.That("Name").Is("Bob"))); expected.Add(If.When(ItsAFact.That("Age").Is("30")).Then(ItsAFact.That("Name").Is("Bob"))); expected.Add(If.When(ItsAFact.That("Age").Is("40")).Then(ItsAFact.That("Year").Is("2040"))); expected.Add(If.When(ItsAFact.That("Age").Is("30")).Then(ItsAFact.That("Year").Is("2030"))); // Act... var results = sut.GetRulesForWorlds(jsonWorlds, InferenceMethod.Optimistic); // Assert... CollectionAssert.AreEquivalent(expected, results); }
public void CreateRule_GivenWorlds_ExpectSimpleImply() { // Arrange... var world1 = "{'Name':'Bob', 'Age':'40'}"; var sut = new DymeInferenceEvaluator(_worldReader, _worldAnalyser); var expected = new List <IEvaluatable>(); expected.Add(If .When(ItsAFact.That("Name").Is("Bob")) .Then(ItsAFact.That("Age").Is("40")) ); expected.Add(If .When(ItsAFact.That("Age").Is("40")) .Then(ItsAFact.That("Name").Is("Bob")) ); // Act... var results = sut.GetRulesForWorlds(new List <string>() { world1 }, InferenceMethod.Optimistic); // Assert... Assert.AreEqual(expected, results); }
public void CreateRules_GivenWorlds_ExpectConjunctions() { // Arrange... var jsonWorlds = new List <string>(); jsonWorlds.Add("{'planet':'Earth', 'sky':'blue', 'ground': 'soft', 'cat': 'InCharge'}"); jsonWorlds.Add("{'planet':'Pluto', 'sky':'blue', 'ground': 'soft', 'cat': 'grumpy'}"); var input = new List <Proposition>(); input.Add(ItsAFact.That("sky").Is("blue") as Proposition); input.Add(ItsAFact.That("ground").Is("soft") as Proposition); input.Add(ItsAFact.That("cat").Is("InCharge") as Proposition); var expected = new List <IEvaluatable>(); expected.Add(All.Of(ItsAFact.That("sky").Is("blue")).And(ItsAFact.That("ground").Is("soft")).IsTrue()); var sut = new DymeInferenceEvaluator(_worldReader, _worldAnalyser); // Act... var results = sut.AsConjunctionGetAllFactsThatRepeatWhenOtherFactsRepeat(jsonWorlds, input); // Assert... CollectionAssert.AreEquivalent(expected, results); }
public void ImplicationEquals_Given2EqualImplications_ExpectEqual() { // Arrange... var compareItem1 = If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("weather").Is("sunny")); var compareItem2 = If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("weather").Is("sunny")); // Assert... Assert.AreEqual(compareItem1, compareItem2); }
public void ConjunctionEquals_Given2EqualConjunctionsCreatedByDifferentMeans_ExpectEqual() { // Arrange... var parser = new EasyRuleDymeRuleConverter(); var compareItem1 = parser.ConvertEasyRuleToDymeRule("(sky) IS (blue) AND (weather) IS (sunny)"); var compareItem2 = All.Of(ItsAFact.That("sky").Is("blue")).And(ItsAFact.That("weather").Is("sunny")).IsTrue(); // Assert... Assert.AreEqual(compareItem1, compareItem2); }
public void ImplicationEquals_Given2EqualImplicationsCreatedByDifferentMeans_ExpectEqual() { // Arrange... var parser = new EasyRuleDymeRuleConverter(); var compareItem1 = parser.ConvertEasyRuleToDymeRule("IF (sky) IS (blue) THEN (weather) IS (sunny)"); var compareItem2 = If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("weather").Is("sunny")); // Assert... Assert.AreEqual(compareItem1, compareItem2); }
public void EasyRuleDymeParser_GivenDymeRules_ExpectEasyRules() { // Arrange... var sut = new EasyRuleDymeRuleConverter(); var expectEasyRule = "IF (Age) IS (18) THEN (Movie) IS (Visible)"; var inputDymeRule = If.When(ItsAFact.That("Age").Is("18")).Then(ItsAFact.That("Movie").Is("Visible")); // Act... var result = sut.ConvertDymeRuleToEasyRule(inputDymeRule); Assert.AreEqual(expectEasyRule, result); }
public void ImplicationEquals_GivenEqualImplicationsListDistinctified_Expect1Item() { // Arrange... var parser = new EasyRuleDymeRuleConverter(); var compareItemList = new List <IEvaluatable>(); compareItemList.Add(parser.ConvertEasyRuleToDymeRule("IF (sky) IS (blue) THEN (weather) IS (sunny)")); compareItemList.Add(If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("weather").Is("sunny"))); // Act... compareItemList = compareItemList.Distinct().ToList(); // Assert... Assert.AreEqual(1, compareItemList.Count()); }
public void ConvertArgumentsToJunction_Given2Arguments_ExpectJunction() { // Arrange... var arguments = new List <IEvaluatable>(); arguments.Add(ItsAFact.That("sky").Is("blue")); //var expected = new List<IEvaluatable>(); var expected = All.Of(ItsAFact.That("sky").Is("blue")).IsTrue(); // Act... var result = ImplicationHelper.ConvertArgumentsToJunction(arguments, Junction.AND); // Assert... Assert.AreEqual(expected, result); }
public void MergeImplications_GivenImplicationsWithSameAntecedent_ExpectMergedImplication2() { // Arrange... var input = new List <IEvaluatable>(); input.Add(If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("ground").Is("soft"))); input.Add(If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("ground").Is("hard"))); var expected = new List <IEvaluatable>(); expected.Add(If.When(ItsAFact.That("sky").Is("blue")).Then(Any.Of(ItsAFact.That("ground").Is("soft")).Or(ItsAFact.That("ground").Is("hard")).IsTrue())); // Act... var results = ImplicationHelper.MergeImplications(input); // Assert... CollectionAssert.AreEquivalent(expected, results); }
public void ConjunctionEquals_Given2EqualConjunctionsWith3ArgumentsCreatedByDifferentMeans_ExpectSimilarHashes() { // Arrange... var parser = new EasyRuleDymeRuleConverter(); var compareItem1 = parser.ConvertEasyRuleToDymeRule("(sky) IS (blue) AND (weather) IS (sunny) AND (water) IS (warm)"); var compareItem2 = All.Of( ItsAFact.That("sky").Is("blue")) .And(ItsAFact.That("weather").Is("sunny")) .And(ItsAFact.That("water").Is("warm")) .IsTrue(); // Act... var result1 = compareItem1.GetHashCode(); var result2 = compareItem2.GetHashCode(); // Assert... Assert.AreEqual(result1, result2); }
public void EasyRuleDymeParser_GivenBothJunctiveDymeRules_ExpectEasyRules() { // Arrange... var sut = new EasyRuleDymeRuleConverter(); var expectEasyRule = "IF (age) IS (10) OR (genre) IS (cartoon) THEN (movie) IS (visible) AND (there) NOT (violence)"; var inputDymeRule = If.When(Any.Of (ItsAFact.That("age").Is("10")) .Or (ItsAFact.That("genre").Is("cartoon")) .IsTrue()) .Then(All.Of (ItsAFact.That("movie").Is("visible")) .And (ItsAFact.That("there").IsNot("violence")) .IsTrue()); // Act... var result = sut.ConvertDymeRuleToEasyRule(inputDymeRule); Assert.AreEqual(expectEasyRule, result); }
public void PessimisticWorld_GivenWorlds2_ExpectRuleSet() { // Arrange... var jsonWorlds = new List <string>(); jsonWorlds.Add("{'planet':'Earth', 'sky':'blue', 'ground': 'soft', 'cat': 'InCharge'}"); jsonWorlds.Add("{'planet':'Venus', 'sky':'yellow', 'ground': 'hard', 'cat': 'InCharge'}"); jsonWorlds.Add("{'planet':'Mars', 'sky':'red', 'ground': 'soft', 'cat': 'InCharge'}"); jsonWorlds.Add("{'planet':'Pluto', 'sky':'blue', 'ground': 'soft', 'cat': 'InCharge'}"); var sut = new DymeInferenceEvaluator(_worldReader, _worldAnalyser); var expected = new List <IEvaluatable>(); expected.Add(If.When(ItsAFact.That("sky").Is("blue")).Then(ItsAFact.That("ground").Is("soft"))); // Act... var results = sut.GetRulesForWorlds(jsonWorlds, InferenceMethod.Pessimistic); // Assert... CollectionAssert.AreEquivalent(expected, results); }
private IEnumerable <IEvaluatable> GetCartesianImplicationsFromWorld <WorldType>(WorldType world) { var allFactsFromWorld = _worldAnalyser.GetAttributesAndValues(world).Select(attVal => ItsAFact.That(attVal.Key).Is(attVal.Value)); var factPairs = CreateCartesianPairs(allFactsFromWorld); var implications = factPairs.Select(factPair => If.When(factPair.Item1).Then(factPair.Item2)); return(implications); }