예제 #1
0
        private IEnumerable <DynamicPropertyResult> GetEmotionsForEntity(IEmotionalState state,
                                                                         Name emotionName, IQueryable kb, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (emotionName.IsVariable)
            {
                foreach (var emotion in state.GetAllEmotions())
                {
                    var sub = new Substitution(emotionName, new ComplexValue((Name)emotion.EmotionType));
                    foreach (var c in constraints)
                    {
                        if (c.Conflicts(sub))
                        {
                            continue;
                        }

                        var newConstraints = new SubstitutionSet(c);
                        newConstraints.AddSubstitution(sub);
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(emotion.Intensity)), newConstraints));
                    }
                }
            }
            else
            {
                foreach (var resultPair in kb.AskPossibleProperties(emotionName, perspective, constraints))
                {
                    string emotionKey = resultPair.Item1.Value.ToString();
                    var    emotion    = state.GetEmotionsByType(emotionKey).OrderByDescending(e => e.Intensity).FirstOrDefault();
                    float  value      = emotion?.Intensity ?? 0;
                    foreach (var c in resultPair.Item2)
                    {
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(value)), c));
                    }
                }
            }
        }
예제 #2
0
        public void MakeGround_GroundedName(string n1, string var, string sub, string expectedResult, Refactorization r)
        {
            String result = string.Empty;

            if (r == Refactorization.New)
            {
                var        name       = new SimpleName(n1);
                SimpleName nameResult = null;
                Dictionary <string, SimpleName> subs = new Dictionary <string, SimpleName>();
                subs[var] = new SimpleName(sub);
                for (long i = 0; i < reps; i++)
                {
                    nameResult = SimpleWFN.MakeGround(name, subs);
                }
                result = nameResult.ToString();
            }
            else if (r == Refactorization.Current)
            {
                var             name       = Name.BuildName(n1);
                Name            nameResult = null;
                SubstitutionSet subSet     = new SubstitutionSet();
                subSet.AddSubstitution(new Substitution("[x]/J(I)"));
                for (long i = 0; i < reps; i++)
                {
                    nameResult = name.MakeGround(subSet);
                }
            }
            Assert.AreEqual(expectedResult, result);
        }
예제 #3
0
        private IEnumerable <Pair <PrimitiveValue, SubstitutionSet> > GetEmotionsForEntity(IEmotionalState state,
                                                                                           Name emotionName, KB kb, Name perspective, IEnumerable <SubstitutionSet> constraints)
        {
            if (emotionName.IsVariable)
            {
                foreach (var emotion in state.GetAllEmotions())
                {
                    var sub = new Substitution(emotionName, (Name)emotion.EmotionType);
                    foreach (var c in constraints)
                    {
                        if (c.Conflicts(sub))
                        {
                            continue;
                        }

                        var newConstraints = new SubstitutionSet(c);
                        newConstraints.AddSubstitution(sub);
                        yield return(Tuples.Create((PrimitiveValue)emotion.Intensity, newConstraints));
                    }
                }
            }
            else
            {
                foreach (var resultPair in kb.AskPossibleProperties(emotionName, perspective, constraints))
                {
                    string         emotionKey = resultPair.Item1.ToString();
                    var            emotion    = state.GetEmotionsByType(emotionKey).OrderByDescending(e => e.Intensity).FirstOrDefault();
                    PrimitiveValue value      = emotion == null ? 0 : emotion.Intensity;
                    foreach (var c in resultPair.Item2)
                    {
                        yield return(Tuples.Create(value, c));
                    }
                }
            }
        }
예제 #4
0
        public void AddSubstitution_V1_Pass(params string[] substitutions)
        {
            var s = new SubstitutionSet();

            foreach (var s2 in substitutions.Select(s1 => new Substitution(s1)))
            {
                s.AddSubstitution(s2);
            }
        }
예제 #5
0
        private IEnumerable <DynamicPropertyResult> IsAgentPropertyCalculator(IQueryContext context, Name x)
        {
            if (context.Perspective != Name.SELF_SYMBOL)
            {
                yield break;
            }

            if (x.IsVariable)
            {
                var otherAgentsSubstitutions = m_otherAgents.Keys.Append(CharacterName).Select(n => new Substitution(x, new ComplexValue(n)));

                foreach (var s in otherAgentsSubstitutions)
                {
                    foreach (var set in context.Constraints)
                    {
                        if (set.Conflicts(s))
                        {
                            continue;
                        }

                        var r = new SubstitutionSet(set);
                        r.AddSubstitution(s);
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(true)), r));
                    }
                }

                yield break;
            }

            if (m_otherAgents.ContainsKey(x) || x == context.Queryable.Perspective)
            {
                if (context.Constraints.Count() == 0)
                {
                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(true)), new SubstitutionSet()));
                }
                else
                {
                    foreach (var set in context.Constraints)
                    {
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(true)), set));
                    }
                }
            }
            else
            {
                foreach (var set in context.Constraints)
                {
                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(false)), set));
                }
            }
        }
예제 #6
0
        private IEnumerable <Pair <PrimitiveValue, SubstitutionSet> > EventAgePropertyCalculator(KB kb, Name perspective, IDictionary <string, Name> args, IEnumerable <SubstitutionSet> constraints)
        {
            if (!perspective.Match(Name.SELF_SYMBOL))
            {
                yield break;
            }

            Name idName = args["id"];

            if (idName.IsVariable)
            {
                foreach (var record in m_registry.Values)
                {
                    var idSub = new Substitution(idName, Name.BuildName(record.Id));
                    foreach (var c in constraints)
                    {
                        if (c.Conflicts(idSub))
                        {
                            continue;
                        }

                        var newSet = new SubstitutionSet(c);
                        newSet.AddSubstitution(idSub);

                        var value = Tick - record.Timestamp;
                        yield return(Tuples.Create((PrimitiveValue)value, newSet));
                    }
                }
                yield break;
            }

            foreach (var pair in kb.AskPossibleProperties(idName, perspective, constraints))
            {
                var idValue = pair.Item1;
                if (!idValue.TypeCode.IsUnsignedNumeric())
                {
                    continue;
                }

                var record = m_registry[idValue];
                var value  = (PrimitiveValue)(Tick - record.Timestamp);
                foreach (var c in pair.Item2)
                {
                    yield return(Tuples.Create(value, c));
                }
            }
        }
예제 #7
0
        private IEnumerable <DynamicPropertyResult> EventAgePropertyCalculator(IQueryContext context, Name id)
        {
            if (id.IsVariable)
            {
                foreach (var record in m_registry.Values)
                {
                    var idSub = new Substitution(id, new ComplexValue(Name.BuildName(record.Id)));
                    foreach (var c in context.Constraints)
                    {
                        if (c.Conflicts(idSub))
                        {
                            continue;
                        }

                        var newSet = new SubstitutionSet(c);
                        newSet.AddSubstitution(idSub);

                        var value = Tick - record.Timestamp;
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(value)), newSet));
                    }
                }
                yield break;
            }

            foreach (var pair in context.AskPossibleProperties(id))
            {
                uint idValue;
                if (!pair.Item1.Value.TryConvertToValue(out idValue))
                {
                    continue;
                }

                var record = m_registry[idValue];
                var value  = (Tick - record.Timestamp);
                foreach (var c in pair.Item2)
                {
                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(value)), c));
                }
            }
        }
예제 #8
0
        public void MakeGround_GroundedName(string n1, string var, string sub, string expectedResult, Refactorization r)
        {
            String result = string.Empty;

            if (r == Refactorization.New)
            {
                var name = new SimpleName(n1);
                SimpleName nameResult = null;
                Dictionary<string, SimpleName> subs = new Dictionary<string, SimpleName>();
                subs[var] = new SimpleName(sub);
                for (long i = 0; i < reps; i++)
                {
                    nameResult = SimpleWFN.MakeGround(name, subs);
                }
                result = nameResult.ToString();
            }
            else if (r == Refactorization.Current)
            {
                var name = Name.BuildName(n1);
                Name nameResult = null;
                SubstitutionSet subSet = new SubstitutionSet();
                subSet.AddSubstitution(new Substitution("[x]/J(I)"));
                for (long i = 0; i < reps; i++)
                {
                    nameResult = name.MakeGround(subSet);
                }
            }
            Assert.AreEqual(expectedResult, result);
        }
 public void AddSubstitution_V1_Pass(params string[] substitutions)
 {
     var s = new SubstitutionSet();
     foreach (var s2 in substitutions.Select(s1 => new Substitution(s1)))
         s.AddSubstitution(s2);
 }
예제 #10
0
        public float VolitionValue(Name step, Name targ, Name mode, KB m_Kb)
        {
            if (m_Kb.Perspective == targ)
            {
                return(-1);
            }

            var targetSub = new Substitution(Target, new ComplexValue(targ));

            var constraints = new SubstitutionSet();

            constraints.AddSubstitution(targetSub);
            float total = Single.NegativeInfinity;



            // List<SubstitutionSet> resultingConstraints = new List<SubstitutionSet>();

            if (step == this.Steps.FirstOrDefault())
            {
                var resultingConstraints = StartingConditions.FirstOrDefault()?.Unify(m_Kb, m_Kb.Perspective, new[] { constraints }).ToList();

                if (StartingConditions.Count() > 1)
                {
                    int counter = 0;
                    foreach (var c in StartingConditions) // For instance SI([x]) >= 40
                    {
                        if (counter == 0)
                        {
                            continue;
                        }
                        resultingConstraints = c.Unify(m_Kb, m_Kb.Perspective, resultingConstraints).ToList(); // Whats the sub here [x]/John
                    }
                }



                if (!resultingConstraints.Any())
                {
                    return(total);
                }


                foreach (var res in resultingConstraints)
                {
                    if (resultingConstraints.Any())                      // Assuming all Starting COnditions match lets go into the Influence Rules
                    {
                        foreach (var constraint in resultingConstraints) //  var condition = c.ToString();

                        {
                            var contraintVolitionValue = .0f;

                            // var certainty = res.FindMinimumCertainty();  // How do I ask SI(John) >= 40 and get its certainty

                            //total += certainty;


                            List <InfluenceRule> influenceRuleList;

                            if (mode.IsUniversal)
                            {
                                influenceRuleList = this.InfluenceRules;
                            }
                            else
                            {
                                influenceRuleList = this.InfluenceRules.FindAll(x => x.Mode == mode);
                            }

                            foreach (var inf in influenceRuleList)
                            {
                                var toSum = inf.EvaluateInfluenceRule(m_Kb, constraint);

                                contraintVolitionValue += toSum;
                            }

                            if (contraintVolitionValue > total)
                            {
                                total = contraintVolitionValue;
                            }
                        }
                    }
                }

                //What if the step is beyond the first one, we should not consider Starting Conditions, or any conditions at all, only the influence rules
            }
            else

            {
                var volVal = .0f;

                List <InfluenceRule> influenceRuleList;

                if (mode.IsUniversal)
                {
                    influenceRuleList = this.InfluenceRules;
                }
                else
                {
                    influenceRuleList = this.InfluenceRules.FindAll(x => x.Mode == mode);
                }


                foreach (var inf in influenceRuleList)
                {
                    var toSum = inf.EvaluateInfluenceRule(m_Kb, constraints);

                    volVal += toSum;
                }


                if (volVal > total)
                {
                    total = volVal;
                }
            }



            return(total);
        }
예제 #11
0
            public IEnumerable <Pair <T, SubstitutionSet> > Unify(Stack <Name> stack, SubstitutionSet binding)
            {
                if (stack.Count == 0)                 //End stack
                {
                    if (m_hasValue)
                    {
                        yield return(Tuples.Create(m_value, binding));
                    }
                    yield break;
                }

                TreeNode nodeToEvaluate;
                Name     term = stack.Pop();

                if (!term.IsVariable)
                {
                    int numOfTerms = term.NumberOfTerms;
                    if (numOfTerms == 1)
                    {
                        var  selectedNodes = Enumerable.Empty <TreeNode>();
                        Name key           = term;
                        if (key.IsUniversal)
                        {
                            if (m_universal != null)
                            {
                                selectedNodes = selectedNodes.Append(m_universal);
                            }

                            selectedNodes = selectedNodes.Union(GetNextLevel().SelectMany(p => p.Item2));
                        }
                        else
                        {
                            if (m_nextSymbol != null && m_nextSymbol.TryGetValue(key, out nodeToEvaluate))
                            {
                                selectedNodes = selectedNodes.Append(nodeToEvaluate);
                            }

                            if (m_universal != null)
                            {
                                selectedNodes = selectedNodes.Append(m_universal);
                            }
                        }

                        foreach (var pair in selectedNodes.SelectMany(n => n.Unify(stack, binding)))
                        {
                            yield return(pair);
                        }
                    }
                    else
                    {
                        if (m_nextComposed != null && m_nextComposed.TryGetValue(numOfTerms, out nodeToEvaluate))
                        {
                            using (var it = term.GetTerms().Reverse().GetEnumerator())
                            {
                                while (it.MoveNext())
                                {
                                    stack.Push(it.Current);
                                }
                            }

                            foreach (var pair in nodeToEvaluate.Unify(stack, binding))
                            {
                                yield return(pair);
                            }

                            for (int i = 0; i < term.NumberOfTerms; i++)
                            {
                                stack.Pop();
                            }
                        }

                        if (m_universal != null)
                        {
                            foreach (var pair in m_universal.Unify(stack, binding))
                            {
                                yield return(pair);
                            }
                        }
                    }

                    if (m_nextVariable != null)
                    {
                        //Find bindings with stored variables
                        foreach (var pair in m_nextVariable)
                        {
                            var sub = new Substitution(pair.Key, new ComplexValue(term));
                            if (binding.Conflicts(sub))
                            {
                                continue;
                            }

                            var set = new SubstitutionSet(binding);
                            set.AddSubstitution(sub);
                            foreach (var r in pair.Value.Unify(stack, set))
                            {
                                yield return(r);
                            }
                        }
                    }
                }
                else
                {
                    //Find bindings
                    var nextLevel = GetNextLevel();
                    foreach (var pair in nextLevel)
                    {
                        SubstitutionSet set = binding;
                        if (!pair.Item1.IsVariable || pair.Item1 != term)
                        {
                            //Very odd trick to make certainty work
                            var sub = new Substitution(term, new ComplexValue(pair.Item1, 1));
                            if (binding.Conflicts(sub))
                            {
                                continue;
                            }

                            set = new SubstitutionSet(set);
                            set.AddSubstitution(sub);
                        }

                        foreach (var node in pair.Item2)
                        {
                            foreach (var r in node.Unify(stack, set))
                            {
                                yield return(r);
                            }
                        }
                    }
                }

                stack.Push(term);
            }
예제 #12
0
        public IEnumerable <DynamicPropertyResult> VolitionPropertyCalculator(IQueryContext context, Name socialMoveName, Name Step, Name Target, Name Mode)
        {
            Dictionary <SubstitutionSet, Name> ret = new Dictionary <SubstitutionSet, Name>();

            var  stringVolition = "";
            var  possibleSE     = new List <SocialExchange>();
            bool SEConstraint   = false;


            if (context.Perspective != Name.SELF_SYMBOL)
            {
                yield break;
            }

            if (this.m_kB.Perspective == Target)
            {
                yield break;
            }

            List <Name> possibleSEs = new List <Name>();

            if (socialMoveName.IsVariable)
            {
                foreach (var s in context.AskPossibleProperties(socialMoveName))
                {
                    possibleSEs.Add(s.Item1.Value);
                }

                foreach (var se in this.m_SocialExchanges)
                {
                    possibleSEs.Add(se.Name);
                }
            }
            else if (socialMoveName.IsUniversal)
            {
                foreach (var se in this.m_SocialExchanges)
                {
                    possibleSEs.Add(se.Name);
                }
            }

            else
            {
                possibleSEs.Add(socialMoveName);
            }

            List <Name> possibleTargets = new List <Name>();

            if (Target.IsVariable)
            {
                foreach (var s in context.AskPossibleProperties(Target))
                {
                    possibleTargets.Add(s.Item1.Value);
                }
            }
            else
            {
                possibleTargets.Add(Target);
            }



            List <Name> possibleModes = new List <Name>();

            if (Mode.IsVariable)
            {
                foreach (var s in context.AskPossibleProperties(Mode))
                {
                    possibleModes.Add(s.Item1.Value);
                }
            }

            else
            {
                possibleModes.Add(Mode);
            }

            foreach (var seName in possibleSEs)
            {
                if (!m_SocialExchanges.Exists(x => x.Name == seName))
                {
                    continue;
                }


                foreach (var targ in possibleTargets)
                {
                    var actualStep = FilterStep(seName, targ);

                    if (actualStep.ToString() == "-")
                    {
                        continue;
                    }

                    foreach (var seMode in possibleModes)
                    {
                        var volValue = CalculateSocialMoveVolition(seName, actualStep, targ, seMode);


                        if (volValue != Single.NegativeInfinity)
                        {
                            var subSet = new SubstitutionSet();

                            if (socialMoveName.IsVariable)
                            {
                                var sub = new Substitution(socialMoveName, new ComplexValue(seName, 1));
                                subSet.AddSubstitution(sub);
                            }


                            if (Step.IsVariable)
                            {
                                var sub = new Substitution(Step, new ComplexValue(actualStep, 1));
                                subSet.AddSubstitution(sub);
                            }


                            if (Target.IsVariable)
                            {
                                var sub = new Substitution(Target, new ComplexValue(targ, 1));
                                subSet.AddSubstitution(sub);
                            }

                            if (Mode.IsVariable)
                            {
                                var sub = new Substitution(Mode, new ComplexValue(seMode, 1));
                                subSet.AddSubstitution(sub);
                            }

                            if (context.Constraints.Count() > 0)
                            {
                                foreach (var c in context.Constraints)
                                {
                                    if (c.Conflicts(subSet))
                                    {
                                        continue;
                                    }

                                    var newcontext = new SubstitutionSet();

                                    newcontext.AddSubstitutions(c);

                                    newcontext.AddSubstitutions(subSet);

                                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(volValue)), newcontext));
                                }
                            }

                            else
                            {
                                yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(volValue)), subSet));
                            }
                        }
                    }
                }
            }
        }
예제 #13
0
		private static IEnumerable<DynamicPropertyResult> HasLiteralPropertyCalculator(IQueryContext context, Name x, Name y)
		{
			if (!(y.IsVariable || y.IsComposed))
				return Enumerable.Empty<DynamicPropertyResult>();

			List<DynamicPropertyResult> results = ObjectPool<List<DynamicPropertyResult>>.GetObject();
			try
			{
				IEnumerable<Name> candidates;
				if (y.IsVariable)
				{
					candidates = context.Constraints.Select(s => s[y]).Where(n => n != null);
				}
				else if (!y.IsGrounded)
				{
					candidates = context.Constraints.Select(y.MakeGround).Where(c => c.IsConstant);
				}
				else
				{
					candidates = new[] { y };
				}

				foreach (var c in candidates)
				{
					foreach (var t in c.GetTerms())
					{
						if (x.IsVariable)
						{
							var sub = new Substitution(x,new ComplexValue(t));
							foreach (var g in context.Constraints)
							{
								if(g.Conflicts(sub))
									continue;

								var s = new SubstitutionSet(g);
								s.AddSubstitution(sub);
								results.Add(new DynamicPropertyResult(new ComplexValue(Name.BuildName(true)),s));
							}
						}
						else
						{
							foreach (var x2 in context.AskPossibleProperties(x))
							{
                                if (x2.Item1.Value == t)
								{
									var r = Name.BuildName(true);
									foreach (var s in x2.Item2)
									{
										results.Add(new DynamicPropertyResult(new ComplexValue(r), s));
									}
								}
							}
						}
					}
				}

				return results.ToArray();
			}
			finally
			{
				results.Clear();
				ObjectPool<List<DynamicPropertyResult>>.Recycle(results);
			}
		}
예제 #14
0
        private IEnumerable<DynamicPropertyResult> EventAgePropertyCalculator(IQueryContext context, Name id)
        {
            if(!context.Perspective.Match(Name.SELF_SYMBOL))
                yield break;

            if (id.IsVariable)
            {
                foreach (var record in m_registry.Values)
                {
                    var idSub = new Substitution(id, Name.BuildName(record.Id));
                    foreach (var c in context.Constraints)
                    {
                        if (c.Conflicts(idSub))
                            continue;

                        var newSet = new SubstitutionSet(c);
                        newSet.AddSubstitution(idSub);

                        var value = Tick - record.Timestamp;
                        yield return new DynamicPropertyResult(Name.BuildName(value), newSet);
                    }
                }
                yield break;
            }

            foreach (var pair in context.AskPossibleProperties(id))
            {
                uint idValue;
                if(!pair.Item1.TryConvertToValue(out idValue))
                    continue;

                var record = m_registry[idValue];
                var value = (Tick - record.Timestamp);
                foreach (var c in pair.Item2)
                    yield return new DynamicPropertyResult(Name.BuildName(value), c);
            }
        }
예제 #15
0
        private static bool FindSubst(Name n1, Name n2, bool allowPartialTerms, SubstitutionSet bindings)
        {
            n1 = n1.MakeGround(bindings);
            n2 = n2.MakeGround(bindings);
            var t = GetTerms(n1, n2, allowPartialTerms);
            if (t == null)
                return false;

            foreach (var p in t)
            {
                Substitution candidate = null;
                bool isVar1 = p.Item1.IsVariable;
                bool isVar2 = p.Item2.IsVariable;

                // Case 1: x = t, where t is not a variable and x is a variable, and create substitution x/t
                if (isVar1 != isVar2)
                {
                    Name variable = (isVar1 ? p.Item1 : p.Item2);
                    Name value = isVar1 ? p.Item2 : p.Item1;
                    if (value.ContainsVariable(variable))		//Occurs check to prevent cyclical evaluations
                        return false;

                    candidate = new Substitution(variable, value);
                }
                else if (isVar1) //isVar1 == isVar2 == true
                {
                    //Case 2: x = x, where x is a variable, ignore it. otherwise add the substitution
                    if (!(p.Item1 == p.Item2))
                        candidate = new Substitution(p.Item1, p.Item2);
                }
                else //isVar1 == isVar2 == false
                {
                    // Case 3: t1 = t2, where t1,t2 are not variables.
                    // If they don't contain variables, compare them to see if they are equal. If they are not equal the unification fails.
                    if (p.Item1.IsGrounded && p.Item2.IsGrounded)
                    {
                        if (p.Item1 == p.Item2)
                            continue;
                        return false;
                    }

                    //If one or both contain variables, unify the terms
                    if (!FindSubst(p.Item1, p.Item2,allowPartialTerms, bindings))
                        return false;
                }

                if (candidate != null)
                {
                    // Step 4: check to see if the newly created substitution conflicts with any of the already created substitution.
                    // If it does, the unification fails.
                    if (!bindings.AddSubstitution(candidate))
                        return false;
                }
            }
            return true;
        }