예제 #1
0
        private void internal_RegistDynamicProperty(Name propertyName, MethodInfo surogate, DynamicPropertyCalculator converted)
        {
            if (!propertyName.IsPrimitive)
            {
                throw new ArgumentException("The property name must be a primitive symbol.", nameof(propertyName));
            }

            var p = surogate.GetParameters();
            var propertyParameters = p.Skip(1).Select(p2 => Name.BuildName("[" + p2.Name + "]")).ToArray();
            var template           = Name.BuildName(propertyParameters.Prepend(propertyName));

            var r = m_dynamicProperties.Unify(template).FirstOrDefault();

            if (r != null)
            {
                var t = r.Item1.DeclaringType;
                if (t == surogate.DeclaringType)
                {
                    return;
                }

                throw new ArgumentException("There is already a registed property with the name " + propertyName + " that receives " + (p.Length - 3) + "parameters.");
            }

            if (_willCollideDelegate(template))
            {
                throw new ArgumentException("There are already stored property values that will collide with the given dynamic property.");
            }

            m_dynamicProperties.Add(template, new DynamicKnowledgeEntry(converted, propertyParameters, surogate.DeclaringType));
        }
예제 #2
0
        private IEnumerable <Pair <PrimitiveValue, SubstitutionSet> > EventIdPropertyCalculator(KB kb, Name perspective, IDictionary <string, Name> args, IEnumerable <SubstitutionSet> constraints)
        {
            List <Pair <PrimitiveValue, SubstitutionSet> > results = new List <Pair <PrimitiveValue, SubstitutionSet> >();

            if (!perspective.Match(Name.SELF_SYMBOL))
            {
                return(results);
            }

            Name type    = GetArgument(args, "type");
            Name subject = GetArgument(args, "subject");
            Name def     = GetArgument(args, "def");
            Name target  = GetArgument(args, "target");

            var key = Name.BuildName(EVT_NAME, type, subject, def, target);

            foreach (var c in constraints)
            {
                foreach (var pair in m_typeIndexes.Unify(key, c))
                {
                    foreach (var id in pair.Item1)
                    {
                        results.Add(Tuples.Create((PrimitiveValue)id, new SubstitutionSet(pair.Item2)));
                    }
                }
            }
            return(results);
        }
 public void NameDictionary_Invalid_Unify(NameSearchTree <int> dict, Name expression, SubstitutionSet bindings)
 {
     for (int i = 0; i < rep; i++)
     {
         var result = dict.Unify(expression, bindings);
         Assert.That(!result.Any(), "The unification returned valid results.");
     }
 }
예제 #4
0
        private IEnumerable <DynamicPropertyResult> LastEventIdPropertyCalculator(IQueryContext context, Name type, Name subject, Name def, Name target)
        {
            ulong min = ulong.MinValue;


            var allEvents = m_registry.Values;


            foreach (var eve in allEvents)
            {
                if (eve.Timestamp >= min)
                {
                    min = eve.Timestamp;
                }
            }

            var lastEvents = m_registry.Where(x => x.Value.Timestamp == min);



            var lastIndexes = new NameSearchTree <List <uint> >();

            //  Now we get a similar object as m_typeIndexes but only with the last events
            foreach (var ind in m_typeIndexes)
            {
                foreach (var eve in lastEvents)
                {
                    if (ind.Key.ToString() == eve.Value.EventName.ToString() && !lastIndexes.Contains(ind))
                    {
                        lastIndexes.Add(ind);
                    }
                }
            }



            // Now that we have the events of the last tick we can now ask the context

            var key = Name.BuildName((Name)AMConsts.EVENT, type, subject, def, target);

            foreach (var c in context.Constraints)
            {
                var unifiedSet = lastIndexes.Unify(key, c);
                foreach (var pair in unifiedSet)
                {
                    foreach (var id in pair.Item1)
                    {
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(id)), new SubstitutionSet(pair.Item2)));
                    }
                }


                if (!unifiedSet.Any())
                {
                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(-1)), c));
                }
            }
        }
        public IEnumerable <Pair <T, SubstitutionSet> > UnifyAll(Name expression, KB knowledgeBase, Name perspective, SubstitutionSet bindings)
        {
            if (bindings == null)
            {
                bindings = new SubstitutionSet();
            }

            var p1 = m_dictionary.Unify(expression, bindings);

            return(p1.SelectMany(p => p.Item1.MatchConditions(knowledgeBase, perspective, p.Item2)));
        }
예제 #6
0
        public IEnumerable <Pair <DialogStateAction, SubstitutionSet> > GetAllDialogsForKey(Name key, SubstitutionSet bindings = null)
        {
            var a = m_nameToDialogues.Unify(key, bindings);

            foreach (var pair in a)
            {
                foreach (var d in pair.Item1)
                {
                    yield return(Tuples.Create(d, pair.Item2));
                }
            }
        }
예제 #7
0
        public void RegistDynamicProperty(Name propertyTemplate, DynamicPropertyCalculator surogate, IEnumerable <string> arguments)
        {
            if (surogate == null)
            {
                throw new ArgumentNullException(nameof(surogate));
            }

            if (propertyTemplate.IsGrounded)
            {
                throw new ArgumentException("Grounded names cannot be used as dynamic properties", nameof(propertyTemplate));
            }

            var r = m_dynamicProperties.Unify(propertyTemplate).FirstOrDefault();

            if (r != null)
            {
                throw new ArgumentException(
                          $"The given template {propertyTemplate} will collide with already registed {propertyTemplate.MakeGround(r.Item2)} dynamic property", nameof(propertyTemplate));
            }

            if (m_knowledgeStorage.Unify(propertyTemplate).Any())
            {
                throw new ArgumentException($"The given template {propertyTemplate} will collide with stored constant properties", nameof(propertyTemplate));
            }

            Name[] args;
            if (arguments == null)
            {
                args = new Name[0];
            }
            else
            {
                args = arguments.Distinct().Select(s => Name.BuildName("[" + s + "]")).ToArray();
            }
            m_dynamicProperties.Add(propertyTemplate, new DynamicKnowledgeEntry(surogate, args));
        }
        public void Special_Test_For_Bug_22_11_2017()
        {
            for (int i = 0; i < rep; i++)
            {
                var tree = new NameSearchTree <byte>();

                var n1    = (Name)"Speak(-,end,Meanings(a(b),c(d)),t)";
                var n2    = (Name)"Speak(-,end,Meanings(a(c),e),t)";
                var test1 = (Name)"Speak(-,end,*,t)";

                tree.Add(n1, 0);
                tree.Add(n2, 0);
                Assert.IsTrue(tree.Unify(test1).All(r => r.Item1 == 0));
            }
        }
예제 #9
0
 public AppraisalRule Evaluate(IBaseEvent evt, KB kb)
 {
     foreach (var possibleAppraisals in Rules.Unify(evt.EventName.ApplyPerspective(kb.Perspective)))
     {
         var conditions = new[] { possibleAppraisals.Item2 };
         foreach (var appraisal in possibleAppraisals.Item1)
         {
             if (appraisal.Conditions.Evaluate(kb, Name.SELF_SYMBOL, conditions))
             {
                 return(appraisal);
             }
         }
     }
     return(null);
 }
        public void NameDictionary_Valid_Unify(NameSearchTree <int> dict, Name expression, SubstitutionSet bindings,
                                               IEnumerable <Pair <int, SubstitutionSet> > expectedResults)
        {
            var result     = dict.Unify(expression, bindings);
            var resultDict = result.ToDictionary(p => p.Item1, p => p.Item2);
            var expectDict = expectedResults.ToDictionary(p => p.Item1, p => p.Item2);

            Assert.AreEqual(expectDict.Count, resultDict.Count, "Number of results mismatch");

            foreach (var pair in resultDict)
            {
                SubstitutionSet t;
                Assert.That(expectDict.TryGetValue(pair.Key, out t), "Unable to find entry");
                Assert.That(t.Equals(pair.Value), "Binding list is not equal to the expected");
            }
        }
예제 #11
0
        private IEnumerable <DynamicPropertyResult> EventIdPropertyCalculator(IQueryContext context, Name type, Name subject, Name def, Name target)
        {
            var key = Name.BuildName((Name)AMConsts.EVENT, type, subject, def, target);

            foreach (var c in context.Constraints)
            {
                var unifiedSet = m_typeIndexes.Unify(key, c);
                foreach (var pair in unifiedSet)
                {
                    foreach (var id in pair.Item1)
                    {
                        yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(id)), new SubstitutionSet(pair.Item2)));
                    }
                }

                if (!unifiedSet.Any())
                {
                    yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(-1)), c));
                }
            }
        }
        public void NameDictionary_Valid_Unify(NameSearchTree<int> dict, Name expression, SubstitutionSet bindings,
			IEnumerable<Pair<int, SubstitutionSet>> expectedResults)
        {
            var result = dict.Unify(expression, bindings);
            var resultDict = result.ToDictionary(p => p.Item1, p => p.Item2);
            var expectDict = expectedResults.ToDictionary(p => p.Item1, p => p.Item2);

            Assert.AreEqual(expectDict.Count, resultDict.Count, "Number of results mismatch");

            foreach (var pair in resultDict)
            {
                SubstitutionSet t;
                Assert.That(expectDict.TryGetValue(pair.Key, out t), "Unable to find entry");
                Assert.That(t.Equals(pair.Value), "Binding list is not equal to the expected");
            }
        }
 public void NameDictionary_Invalid_Unify(NameSearchTree<int> dict, Name expression, SubstitutionSet bindings)
 {
     var result = dict.Unify(expression, bindings);
     Assert.That(!result.Any(),"The unification returned valid results.");
 }
        public void Special_Test_For_Bug_22_11_2017()
        {
            var tree = new NameSearchTree<byte>();

            var n1 = (Name) "Speak(-,end,Meanings(a(b),c(d)),t)";
            var n2 = (Name)"Speak(-,end,Meanings(a(c),e),t)";
            var test1 = (Name)"Speak(-,end,*,t)";

            tree.Add(n1,0);
            tree.Add(n2, 0);
            Assert.IsTrue(tree.Unify(test1).All(r => r.Item1 == 0));
        }
예제 #15
0
		private IEnumerable<BeliefValueSubsPair> internal_AskPossibleProperties(Name property, List<Name> ToMList, IEnumerable<SubstitutionSet> constraints)
		{
			property = RemovePropertyPerspective(property, ToMList);

			//ToM property shift
			property = ExtractPropertyFromToM(property, ToMList, nameof(property));

			var mind_key = ToMList2Key(ToMList);
			if (!property.IsVariable)
			{
				bool dynamicFound = false;
				//foreach (var r in _registry.Evaluate(this, property, mind_key, constraints))
				foreach (var match in _registry.Evaluate(property))
				{
					dynamicFound = true;
					foreach (var r in match.Evaluate(this, mind_key, constraints))
					{
						yield return r;
					}
				}
				if (dynamicFound)
					yield break;
			}

			var group = constraints.GroupBy(property.MakeGround);

			foreach (var g in group)
			{
				if (g.Key.IsPrimitive)
				{
					yield return Tuples.Create(new ComplexValue(g.Key),(IEnumerable<SubstitutionSet>)g);
					continue;
				}

				Name fact;
				try
				{
					fact = property.ApplyToTerms(p => ApplyDynamic(p,mind_key));
				}
				catch (Exception)
				{
					continue;
				}

				var g2 = g.SelectMany(c => m_knowledgeStorage.Unify(fact, c)).GroupBy(r => r.Item1, r => r.Item2);
				foreach (var r in g2)
				{
					var value = r.Key.GetValueFor(mind_key,GetFinalPerspective(ToMList));
					if (value == null)
						continue;
                    var subSets = r.Distinct();
                    foreach(var set in subSets)
                    {
                        foreach(var sub in set)
                        {
                            if(sub.SubValue.Certainty == -1) //Trick
                            {
                                sub.SubValue.Certainty = value.Certainty;
                            }
                        }
                    }
					yield return Tuples.Create(new ComplexValue(value.Value, value.Certainty), subSets);
				}
			}
		}