public void Test_Tell_SameBelief_DifferentPerspective() { var kb = new KB(Name.BuildName("Mark")); kb.Tell(Name.BuildName("Likes(Bread)"), Name.BuildName(true), Name.BuildName("Self")); kb.Tell(Name.BuildName("Likes(Bread)"), Name.BuildName(false), Name.BuildName("Jose")); var bs = kb.GetAllBeliefs(); var count = bs.Count(); Assert.AreEqual(2, count); }
public void Test_Self_Property() { var me = (Name)"Ana"; var kb = new KB(me); kb.Tell((Name)"A(B)", Name.SELF_SYMBOL); Assert.AreEqual(kb.AskProperty((Name)"A(B)").Value, me); me = (Name)"John"; kb.SetPerspective(me); Assert.AreEqual(kb.AskProperty((Name)"A(B)").Value, me); kb.Tell((Name)"A(B)", Name.SELF_SYMBOL, (Name)"Ana"); Assert.AreEqual(kb.AskProperty((Name)"A(B)", (Name)"Ana").Value, (Name)"Ana"); }
/// <summary> /// Appraises a set of event strings. /// /// Durring appraisal, the events will be recorded in the asset's autobiographical memory, /// and Property Change Events will update the asset's knowledge about the world, allowing /// the asset to use the new information derived from the events to appraise the correspondent /// emotions. /// </summary> /// <param name="eventNames">A set of string representation of the events to appraise</param> public void AppraiseEvents(IEnumerable <Name> eventNames, Name perspective, IEmotionalState emotionalState, AM am, KB kb) { var appraisalFrame = new InternalAppraisalFrame(); appraisalFrame.Perspective = kb.Perspective; foreach (var n in eventNames) { var evt = am.RecordEvent(n, am.Tick); var propEvt = evt as IPropertyChangedEvent; if (propEvt != null) { var fact = propEvt.Property; var value = Name.BuildName("-"); if (propEvt.NewValue.IsPrimitive) { value = propEvt.NewValue; if (value.ToString() == "-") { var remove = kb.GetAllBeliefs().Where(x => x.Name == fact); kb.removeBelief(fact); } else { kb.Tell(fact, value, perspective); } } else // new value is not grounded { var values = kb.AskPossibleProperties(propEvt.NewValue, perspective, new List <SubstitutionSet>()); if (values.Count() == 1) { kb.Tell(fact, values.FirstOrDefault().Item1.Value, perspective); } else { throw new Exception("Multiple possible values for " + propEvt.NewValue); } } } appraisalFrame.Reset(evt); var componentFrame = appraisalFrame.RequestComponentFrame(m_appraisalDerivator, m_appraisalDerivator.AppraisalWeight); m_appraisalDerivator.Appraisal(kb, evt, componentFrame); UpdateEmotions(appraisalFrame, emotionalState, am); } }
//public static int NumOfPersistentEntries() //{ // return MemoryData().Count(d => // d.Arguments.Length > 2 && (bool) d.Arguments[2] // ); //} public static KB PopulatedTestMemory() { KB kb = new KB((Name)"Me"); foreach (var t in MemoryData()) { Name property = (Name)t.Arguments[0]; Name value = Name.BuildName(t.Arguments[1]); try { //if(t.Arguments.Length>2) // kb.Tell((Name)t.Arguments[0], PrimitiveValue.Cast(t.Arguments[1]),(bool)t.Arguments[2]); //else kb.Tell(property, value); } catch (Exception e) { if (t.Arguments.Length > 2) { Assert.AreEqual(e.GetType(), t.Arguments[2]); } else { Assert.Fail($"An exception was thrown unexpectedly while evaluating {property} = {value}: {e}"); } } } return(kb); }
public void Test_Tell_Pass_Basic_Property_With_ToM() { const string property = "ToM(Mary,ToM(Self,Has(Ball)))"; var kb = new KB((Name)"John"); kb.Tell((Name)property, Name.BuildName(true)); }
public void Test_Tell_Fail_Complext_Property_With_ToM() { const string property = "ToM(Mary,ToM(Self,Has(ToM(Mary,Ball))))"; var kb = new KB((Name)"John"); Assert.Throws <ArgumentException>(() => kb.Tell((Name)property, true)); }
public void Test_Fail_Change_Perspective_To_Invalid_Perspective(string perspective) { var kb = new KB(Name.BuildName("Mark")); kb.Tell(Name.BuildName("IsPerson(Self)"), Name.BuildName(true), Name.BuildName("John(Self)")); Assert.Throws <ArgumentException>(() => kb.SetPerspective(Name.BuildName(perspective))); }
public void Test_Fail_Change_Perspective_Conflict() { var kb = new KB(Name.BuildName("Mark")); kb.Tell(Name.BuildName("IsPerson(Self)"), true, Name.BuildName("John(Self)")); Assert.Throws <ArgumentException>(() => kb.UpdateKBAccordingToNewPerspective(Name.BuildName("John"))); }
public void Test_DynamicProperty_Regist_Fail_ConstantProperties() { var kb = new KB((Name)"Me"); Assert.Throws <ArgumentException>(() => { kb.Tell((Name)"CountSubs(John)", Name.BuildName(3)); kb.RegistDynamicProperty((Name)"CountSubs", "", DummyCount); }); }
public void Test_Tell_Fail_OperatorRegist() { var kb = new KB((Name)"Me"); Assert.Throws <ArgumentException>(() => { kb.RegistDynamicProperty((Name)"Count([x])", ((kb1, p, args, constraints) => null), null); kb.Tell((Name)"Count(John)", 3); }); }
public void Test_Tell_Fail_DynamicProperty_Regist() { var kb = new KB((Name)"Me"); Assert.Throws <ArgumentException>(() => { kb.RegistDynamicProperty((Name)"Count", DummyCount); kb.Tell((Name)"Count(John)", Name.BuildName(3)); }); }
public void Test_Tell_Pass_Add_Self_Belief_and_Change_Perspective_02() { var kb = new KB(Name.BuildName("Mark")); kb.Tell(Name.BuildName("IsPerson(Self)"), true, Name.BuildName("John(Self)")); kb.UpdateKBAccordingToNewPerspective(Name.BuildName("Mary")); Assert.Null(kb.AskProperty(Name.BuildName("IsPerson(Mark)"), Name.BuildName("John(Self)"))); Assert.True((bool?)kb.AskProperty(Name.BuildName("IsPerson(Mary)"), Name.BuildName("John(Self)"))); }
public void ConditionEvaluatorSet_Test_Universal_Fail(params string[] beliefs) { var kb = new KB((Name)"Me"); foreach (var s in beliefs.Select(b => b.Split(':'))) { kb.Tell((Name)s[0], PrimitiveValue.Parse(s[1])); } var set = new ConditionSet(LogicalQuantifier.Universal, TEST_CONDITIONS); Assert.False(set.Evaluate(kb, Name.SELF_SYMBOL, null)); }
public void ConditionEvaluatorSet_Test_Universal_Pass(params string[] beliefs) { var kb = new KB((Name)"Me"); foreach (var s in beliefs.Select(b => b.Split(':'))) { kb.Tell((Name)s[0], (Name)s[1]); } var set = new ConditionSet(LogicalQuantifier.Universal, TEST_CONDITIONS); Assert.True(set.Unify(kb, Name.SELF_SYMBOL, null).Any()); }
//This is a small console program to exemplify the main functionality of the Emotional Decision Making Asset static void Main(string[] args) { //First we construct a new instance of the EmotionalDecisionMakingAsset class var edm = new EmotionalDecisionMakingAsset(); //Then, we have to register an existing knowledge base to the asset so it can check for //beliefs are true var kb = new KB((Name)"John"); kb.Tell((Name)"LikesToFight(SELF)", (Name)"True"); edm.RegisterKnowledgeBase(kb); //create an action rule var actionRule = new ActionRuleDTO { Action = Name.BuildName("Kick"), Priority = Name.BuildName("4"), Target = (Name)"Player" }; //add the reaction rule var id = edm.AddActionRule(actionRule); edm.AddRuleCondition(id, "LikesToFight(SELF) = True"); var actions = edm.Decide(Name.UNIVERSAL_SYMBOL); Console.WriteLine("Decisions: "); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } //this is how you can load the asset from a file Console.WriteLine("Loading From File: "); edm = EmotionalDecisionMakingAsset.LoadFromFile("../../../Examples/EDM-Tutorial/EDMTest.edm"); edm.RegisterKnowledgeBase(kb); actions = edm.Decide(Name.UNIVERSAL_SYMBOL); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } Console.WriteLine("Decisions: "); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } Console.ReadKey(); }
//This is a small console program to exemplify the main functionality of the Social Importance Asset static void Main(string[] args) { var siTarget = "Player"; //First, we load the asset from an existing profile var siAsset = SocialImportanceAsset.CreateInstance(new GAIPS.Rage.AssetStorage()); var rules = siAsset.GetAttributionRules(); //We then register a knowledge base var kb = new KB((Name)"John"); kb.Tell((Name)"IsFriend(Player)", (Name)"False"); siAsset.RegisterKnowledgeBase(kb); Console.WriteLine("The SI attributed to " + siTarget + " is:" + siAsset.GetSocialImportance(siTarget)); Console.ReadKey(); }
//This is a small console program to exemplify the main functionality of the Emotional Appraisal Asset static void Main(string[] args) { var kickEvent = Name.BuildName("Event(Action-End, John, *, *)"); EmotionalAppraisalAsset ea = EmotionalAppraisalAsset.LoadFromFile("../../../Examples/EA-Tutorial/EATest.ea"); //The following lines add an appraisal rule that will make the kickEvent be perceived as undesirable //Normally, these rules should be authored using the AuthoringTool provided with the asset but they can also be added dynamically /* var rule = new AppraisalRuleDTO {EventMatchingTemplate = (Name)"Event(Action-End, [s], *, *)", Desirability = (Name)"4"}; * ea.AddOrUpdateAppraisalRule(rule); */ var am = new AM(); var kb = new KB((Name)"John"); kb.Tell(Name.BuildName("Likes(Mary)"), Name.BuildName("John"), Name.BuildName("SELF")); var emotionalState = new ConcreteEmotionalState(); //Emotions are generated by the appraisal of the events that occur in the game world ea.AppraiseEvents(new[] { kickEvent }, emotionalState, am, kb); Console.WriteLine("\nMood on tick '" + am.Tick + "': " + emotionalState.Mood); Console.WriteLine("Active Emotions: " + string.Concat(emotionalState.GetAllEmotions().Select(e => e.EmotionType + "-" + e.Intensity + " "))); //Each event that is appraised will be stored in the autobiographical memory that was passed as a parameter Console.WriteLine("\nEvents occured so far: " + string.Concat(am.RecallAllEvents().Select(e => "\nId: " + e.Id + " Event: " + e.EventName.ToString()))); //The update function will increase the current tick by 1. Each active emotion will decay to 0 and the mood will slowly return to 0 for (int i = 0; i < 3; i++) { am.Tick++; emotionalState.Decay(am.Tick); Console.WriteLine("\nMood on tick '" + am.Tick + "': " + emotionalState.Mood); Console.WriteLine("Active Emotions: " + string.Concat(emotionalState.GetAllEmotions().Select(e => e.EmotionType + "-" + e.Intensity + " "))); } //The asset can also be loaded from an existing file using the following method: ea = EmotionalAppraisalAsset.LoadFromFile("../../../Examples/EA-Tutorial/EATest.ea"); Console.ReadKey(); }
public void Test_Tell_Pass_Add_With_Perspective(string nativePerspective, string tellPerdicate, string tellPerspective, string queryPerdicate, string queryPerspective) { var kb = new KB(Name.BuildName(nativePerspective)); kb.Tell(Name.BuildName(tellPerdicate), true, Name.BuildName(tellPerspective)); using (var stream = new MemoryStream()) { var formater = new JSONSerializer(); formater.Serialize(stream, kb); stream.Seek(0, SeekOrigin.Begin); Console.WriteLine(new StreamReader(stream).ReadToEnd()); } var r = kb.AskProperty(Name.BuildName(queryPerdicate), Name.BuildName(queryPerspective)) as bool?; Assert.IsTrue(r); }
public void Test_Tell_Pass_Add_Self_Belief_and_Change_Perspective_02() { var kb = new KB(Name.BuildName("Mark")); kb.Tell(Name.BuildName("IsPerson(Self)"), Name.BuildName(true), Name.BuildName("John(Self)")); kb.SetPerspective(Name.BuildName("Mary")); Assert.AreEqual(Name.NIL_STRING, kb.AskProperty(Name.BuildName("IsPerson(Mark)"), Name.BuildName("John(Self)")).Value.ToString()); var n = kb.AskProperty(Name.BuildName("IsPerson(Mary)"), Name.BuildName("John(Self)")); bool b; if (!n.Value.TryConvertToValue(out b)) { Assert.Fail(); } Assert.True(b); }
public void Test_KB_RemoveBelief(string toAdd, string toRemove, int total) { var kb = new KB((Name)"John"); var tell = toAdd.Split(','); foreach (var b in tell) { var value = b.Split('='); kb.Tell((Name)value[0], (Name)value[1]); } var rem = toRemove.Split(','); foreach (var r in rem) { kb.removeBelief((Name)r); } Assert.AreEqual(kb.GetAllBeliefs().Count(), total); }
public void AppraiseEvents(IEnumerable <Name> eventNames) { var APPRAISAL_FRAME = new InternalAppraisalFrame(); foreach (var n in eventNames) { var evtN = n.RemovePerspective(Perspective); var evt = m_am.RecordEvent(evtN, Tick); var propEvt = evt as IPropertyChangedEvent; if (propEvt != null) { var fact = propEvt.Property; var value = (Name)propEvt.NewValue; m_kb.Tell(fact, value.GetPrimitiveValue(), Name.SELF_SYMBOL); } APPRAISAL_FRAME.Reset(evt); var componentFrame = APPRAISAL_FRAME.RequestComponentFrame(m_appraisalDerivator, m_appraisalDerivator.AppraisalWeight); m_appraisalDerivator.Appraisal(this, evt, componentFrame); UpdateEmotions(APPRAISAL_FRAME); } }
//This is a small console program to exemplify the main functionality of the Emotional Decision Making Asset static void Main(string[] args) { //First we construct a new instance of the EmotionalDecisionMakingAsset class var storage = new AssetStorage(); var edm = EmotionalDecisionMakingAsset.CreateInstance(storage); //Then, we have to register an existing knowledge base to the asset so it can check for //beliefs are true var kb = new KB((Name)"John"); kb.Tell((Name)"LikesToFight(SELF)", (Name)"True"); edm.RegisterKnowledgeBase(kb); //create an action rule var actionRule = new ActionRuleDTO { Action = Name.BuildName("Kick"), Priority = Name.BuildName("4"), Target = (Name)"Player" }; //add the reaction rule var id = edm.AddActionRule(actionRule); var rule = edm.GetActionRule(id); edm.AddRuleCondition(id, "LikesToFight(SELF) = True"); var actions = edm.Decide(Name.UNIVERSAL_SYMBOL); var ea = EmotionalAppraisalAsset.CreateInstance(storage); edm.Save(); using (var writer = File.CreateText("D:\\test2.json")) { writer.Write(storage.ToJson()); } string aux2 = File.ReadAllText("D:\\Test2.json"); var storage2 = AssetStorage.FromJson(aux2); using (var writer = File.CreateText("D:\\test3.json")) { writer.Write(storage2.ToJson()); } Console.WriteLine("Decisions: "); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } //this is how you can load the asset from a file Console.WriteLine("Loading From File: "); edm.RegisterKnowledgeBase(kb); actions = edm.Decide(Name.UNIVERSAL_SYMBOL); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } Console.WriteLine("Decisions: "); foreach (var a in actions) { Console.WriteLine(a.Name.ToString() + " p: " + a.Utility); } Console.ReadKey(); }
private static SocialImportanceAsset BuildAsset() { var kb = new KB((Name)"Matt"); #region Set KB kb.Tell((Name)"IsPerson(Matt)", (Name)"true", (Name)"*"); kb.Tell((Name)"IsPerson(Mary)", (Name)"true", (Name)"*"); kb.Tell((Name)"IsPerson(Thomas)", (Name)"true", (Name)"*"); kb.Tell((Name)"IsPerson(Diego)", (Name)"true", (Name)"*"); kb.Tell((Name)"IsPerson(Robot)", (Name)"true", (Name)"Diego"); kb.Tell((Name)"IsOutsider(Diego)", (Name)"true", (Name)"*"); kb.Tell((Name)"IsOutsider(Diego)", (Name)"false", (Name)"Robot"); kb.Tell((Name)"AreFriends(SELF,Mary)", (Name)"true", (Name)"SELF"); kb.Tell((Name)"AreFriends(SELF,Matt)", (Name)"true", (Name)"Mary"); kb.Tell((Name)"AreFriends(SELF,Thomas)", (Name)"true", (Name)"SELF"); kb.Tell((Name)"IsBartender(Matt)", (Name)"true", (Name)"*"); #endregion #region SI DTO especification var siDTO = new SocialImportanceDTO { AttributionRules = new[] { new AttributionRuleDTO() { Target = (Name)"[target]", Value = (Name)"[v]", Conditions = new ConditionSetDTO() { ConditionSet = new [] { "IsPerson([target]) = true", "[target] != Self", "[v] = 20" } } }, new AttributionRuleDTO() { Target = (Name)"[target]", Value = (Name)"-1", Conditions = new ConditionSetDTO() { ConditionSet = new [] { "IsOutsider([target]) = true", "[target] != Self" } } }, new AttributionRuleDTO() { Target = (Name)"[target]", Value = (Name)"15", Conditions = new ConditionSetDTO() { ConditionSet = new [] { "AreFriends(Self,[target]) = true" } } }, new AttributionRuleDTO() { Target = (Name)"[target]", Value = (Name)"10", Conditions = new ConditionSetDTO() { ConditionSet = new [] { "IsClient([target]) = true", "IsBartender(Self) = true", "[target] != Self" } } }, new AttributionRuleDTO() { Target = (Name)"[target]", Value = (Name)"1", Conditions = new ConditionSetDTO() { ConditionSet = new [] { "IsElder([target]) = true", "IsElder(Self) = false" } } } } }; #endregion var si = new SocialImportanceAsset(); si.RegisterKnowledgeBase(kb); return(si); }
public void Test_Tell_Fail_NonConstant_Property(string propertyName) { var kb = new KB((Name)"John"); Assert.Throws <ArgumentException>(() => kb.Tell((Name)propertyName, Name.BuildName(true))); }
public void Test_Fail_Tell_With_Nil_Perspective() { var kb = new KB(Name.BuildName("Mark")); Assert.Throws <ArgumentException>(() => kb.Tell(Name.BuildName("IsPerson(Self)"), Name.BuildName(true), Name.NIL_SYMBOL)); }
private static KB CreateKB() { var kb = new KB((Name)"Agatha"); kb.Tell((Name)"Strength(John)", Name.BuildName(5)); kb.Tell((Name)"Strength(Mary)", Name.BuildName(3)); kb.Tell((Name)"Strength(Leonidas)", Name.BuildName(500)); kb.Tell((Name)"Strength(Goku)", Name.BuildName(9001f)); kb.Tell((Name)"Strength(SuperMan)", Name.BuildName(ulong.MaxValue)); kb.Tell((Name)"Strength(Saitama)", Name.BuildName(float.MaxValue)); kb.Tell((Name)"Race(Saitama)", Name.BuildName("human")); kb.Tell((Name)"Race(Superman)", Name.BuildName("kriptonian")); kb.Tell((Name)"Race(Goku)", Name.BuildName("sayian")); kb.Tell((Name)"Race(Leonidas)", Name.BuildName("human")); kb.Tell((Name)"Race(Mary)", Name.BuildName("human")); kb.Tell((Name)"Race(John)", Name.BuildName("human")); kb.Tell((Name)"Job(Saitama)", Name.BuildName("super-hero")); kb.Tell((Name)"Job(Superman)", Name.BuildName("super-hero")); kb.Tell((Name)"Job(Leonidas)", Name.BuildName("Spartan")); kb.Tell((Name)"AKA(Saitama)", Name.BuildName("One-Punch_Man")); kb.Tell((Name)"AKA(Superman)", Name.BuildName("Clark_Kent")); kb.Tell((Name)"AKA(Goku)", Name.BuildName("Kakarot")); kb.Tell((Name)"Hobby(Saitama)", Name.BuildName("super-hero")); kb.Tell((Name)"Hobby(Goku)", Name.BuildName("training")); kb.Tell((Name)"IsAlive(Leonidas)", Name.BuildName(false)); kb.Tell((Name)"IsAlive(Saitama)", Name.BuildName(true)); kb.Tell((Name)"IsAlive(Superman)", Name.BuildName(true)); kb.Tell((Name)"IsAlive(John)", Name.BuildName(true)); return(kb); }
public void Test_Tell_Fail_Add_Self_To_Universal_Context() { var kb = new KB((Name)"John"); Assert.Throws <InvalidOperationException>(() => kb.Tell((Name)"Likes(Self)", Name.BuildName(true), Name.UNIVERSAL_SYMBOL)); }
public void Test_Tell_Fail_Property_ToM_Transform(string property, string perspective) { var kb = new KB((Name)"John"); Assert.Throws <ArgumentException>(() => kb.Tell((Name)property, Name.BuildName(true), (Name)perspective)); }
public void Test_Tell_Fail_Assert_Perspective(string perspective, Type exceptionType) { var kb = new KB((Name)"John"); Assert.Throws(exceptionType, () => kb.Tell((Name)"Likes(Mary)", Name.BuildName(true), (Name)perspective)); }
public void Test_Tell_Fail_Add_Self_To_Universal() { var kb = new KB(Name.BuildName("Matt")); Assert.Throws <InvalidOperationException>(() => { kb.Tell((Name)"IsPerson(Self)", Name.BuildName(true), Name.UNIVERSAL_SYMBOL); }); }