Exemplo n.º 1
0
        public void Tell(Name property, Name value, Name perspective, float certainty)
		{
			if (property.IsPrimitive)
				throw new ArgumentException("The given property name cannot be a primitive value.",nameof(property));

			if (!property.IsConstant)
				throw new ArgumentException("The given property name is not constant. Only constant names can be stored",nameof(property));

			var ToMList = AssertPerspective(perspective, nameof(perspective));
			property = RemovePropertyPerspective(property, ToMList);

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

			if (_registry.WillEvaluate(property))
				throw new ArgumentException("The given name will be obfuscated by a dynamic property", nameof(property));

			var mind_key = ToMList2Key(ToMList);

			var fact = property.ApplyToTerms(p => ApplyDynamic(p,mind_key));

			KnowledgeEntry entry;
			if (!m_knowledgeStorage.TryGetValue(fact, out entry))
			{
				entry = new KnowledgeEntry();
				m_knowledgeStorage[fact] = entry;
			}

			entry.TellValueFor(mind_key,value,certainty);
			if (entry.IsEmpty())
				m_knowledgeStorage.Remove(fact);
		}
Exemplo n.º 2
0
        private Name ApplyDynamic(Name property, Name perspective)
        {
            if (!property.IsComposed)
            {
                return(property);
            }

            Name value = null;
            bool found = false;

            using (var it = _registry.Evaluate(property).SelectMany(m => m.Evaluate(this, perspective, null)).GetEnumerator())
            {
                while (it.MoveNext())
                {
                    if (found)
                    {
                        throw new Exception($"{nameof(KB)} found multiple valid values for {property}");
                    }

                    value = it.Current.Item1.Value;
                    found = true;
                }
            }

            return(found ? value : property.ApplyToTerms(p => ApplyDynamic(p, perspective)));
        }
Exemplo n.º 3
0
        private IEnumerable <BeliefPair> 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 AskDynamicProperties(property, mind_key, constraints))
                {
                    dynamicFound = true;
                    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(g.Key.GetPrimitiveValue(), (IEnumerable <SubstitutionSet>)g));

                    continue;
                }

                Name fact;
                try
                {
                    fact = property.ApplyToTerms(p => SimplifyProperty(p, ToMList));
                }
                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);
                    if (value == null)
                    {
                        continue;
                    }
                    yield return(Tuples.Create(value, r.Distinct()));
                }
            }
        }
Exemplo n.º 4
0
        public void Tell(Name property, PrimitiveValue value, Name perspective)
        {
            if (property.IsPrimitive)
            {
                throw new ArgumentException("The given property name cannot be a primitive value.", nameof(property));
            }

            if (!property.IsConstant)
            {
                throw new ArgumentException("The given property name is not constant. Only constant names can be stored", nameof(property));
            }

            perspective = perspective.ApplyPerspective(Perspective);
            var ToMList = AssertPerspective(perspective, nameof(perspective));

            property = RemovePropertyPerspective(property, ToMList);

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

            if (m_dynamicProperties.Unify(property).Any())
            {
                throw new ArgumentException("The given name will be objuscated by a dynamic property", nameof(property));
            }

            var fact = property.ApplyToTerms(p => SimplifyProperty(p, ToMList));

            KnowledgeEntry entry;

            if (!m_knowledgeStorage.TryGetValue(fact, out entry))
            {
                entry = new KnowledgeEntry();
                m_knowledgeStorage[fact] = entry;
            }

            var mind_key = ToMList2Key(ToMList);

            entry.TellValueFor(mind_key, value);
            if (entry.IsEmpty())
            {
                m_knowledgeStorage.Remove(fact);
            }
        }
Exemplo n.º 5
0
        private IEnumerable<BeliefPair> 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 AskDynamicProperties(property, mind_key, constraints))
                {
                    dynamicFound = true;
                    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(g.Key, (IEnumerable<SubstitutionSet>)g);
                    continue;
                }

                Name fact;
                try
                {
                    fact = property.ApplyToTerms(p => SimplifyProperty(p, ToMList));
                }
                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);
                    if (value == null)
                        continue;
                    yield return Tuples.Create(value, r.Distinct());
                }
            }
        }
Exemplo n.º 6
0
        public void Tell(Name property, Name value, Name perspective)
        {
            if (property.IsPrimitive)
                throw new ArgumentException("The given property name cannot be a primitive value.",nameof(property));

            if (!property.IsConstant)
                throw new ArgumentException("The given property name is not constant. Only constant names can be stored",nameof(property));

            var ToMList = AssertPerspective(perspective, nameof(perspective));
            property = RemovePropertyPerspective(property, ToMList);

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

            if (m_dynamicProperties.Unify(property).Any())
                throw new ArgumentException("The given name will be objuscated by a dynamic property", nameof(property));

            var fact = property.ApplyToTerms(p => SimplifyProperty(p, ToMList));

            KnowledgeEntry entry;
            if (!m_knowledgeStorage.TryGetValue(fact, out entry))
            {
                entry = new KnowledgeEntry();
                m_knowledgeStorage[fact] = entry;
            }

            var mind_key = ToMList2Key(ToMList);
            entry.TellValueFor(mind_key,value);
            if (entry.IsEmpty())
                m_knowledgeStorage.Remove(fact);
        }
Exemplo n.º 7
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);
				}
			}
		}