public IGraphNode ObjectToGraphNode(object value, Graph serializationGraph) { PrimitiveValue v = (PrimitiveValue)value; switch (v.TypeCode) { case TypeCode.String: return(serializationGraph.BuildStringNode(((ConcreteValue <string>)v).value)); case TypeCode.Boolean: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <bool>)v).value)); case TypeCode.SByte: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <sbyte>)v).value)); case TypeCode.Byte: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <byte>)v).value)); case TypeCode.Int16: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <short>)v).value)); case TypeCode.UInt16: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <ushort>)v).value)); case TypeCode.Int32: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <int>)v).value)); case TypeCode.UInt32: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <uint>)v).value)); case TypeCode.Int64: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <long>)v).value)); case TypeCode.UInt64: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <ulong>)v).value)); case TypeCode.Single: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <float>)v).value)); case TypeCode.Double: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <double>)v).value)); case TypeCode.Decimal: return(serializationGraph.BuildPrimitiveNode(((ConcreteValue <decimal>)v).value)); } throw new InvalidOperationException("Unexpected PrimitiveValue type. " + v.ValueType + " is not a primitive v."); }
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); } }
private static IEnumerable <Pair <PrimitiveValue, SubstitutionSet> > CountPropertyCalculator(KB kb, Name perspective, IDictionary <string, Name> args, IEnumerable <SubstitutionSet> constraints) { var arg = args["x"]; var set = kb.AskPossibleProperties(arg, perspective, constraints).ToList(); PrimitiveValue count = set.Count; IEnumerable <SubstitutionSet> sets; if (set.Count == 0) { sets = constraints; } else { sets = set.SelectMany(s => s.Item2).Distinct(); } foreach (var d in sets) { yield return(Tuples.Create(count, d)); } }
/// <summary> /// This method provides a way to search for properties/predicates in the WorkingMemory /// that match with a specified name with unbound variables. /// /// In order to understand this method, let’s examine the following example. Suppose that /// the memory only contains properties about two characters: Luke and John. /// Furthermore, it only stores two properties: their name and strength. So the KB will /// only store the following objects: /// - Luke(Name) : Luke /// - Luke(Strength) : 8 /// - John(Name) : John /// - John(Strength) : 4 /// /// The next table shows the result of calling the method with several distinct names. /// The function works by finding substitutions for the unbound variables, which make /// the received name equal to the name of an object stored in memory. /// /// Name Substitutions returned /// Luke([x]) {{[x]/Name},{[x]/Strength}} /// [x](Strength) {{[x]/John},{[x]/Luke}} /// [x]([y]) {{[x]/John,[y]/Name},{[x]/John,[y]/Strength},{[x]/Luke,[y]/Name},{[x]/Luke,[y]/Strength}} /// John(Name) {{}} /// John(Height) null /// Paul([x]) null /// /// In the first example, there are two possible substitutions that make “Luke([x])” /// equal to the objects stored above. The third example has two unbound variables, /// so the returned set contains all possible combinations of variable attributions. /// /// If this method receives a ground name, as seen on examples 4 and 5, it checks /// if the received name exists in memory. If so, a set with the empty substitution is /// returned, i.e. the empty substitution makes the received name equal to some object /// in memory. Otherwise, the function returns null, i.e. there is no substitution /// that applied to the name will make it equal to an object in memory. This same result /// is returned in the last example, since there is no object named Paul, and therefore no /// substitution of [x] will match the received name with an existing object. /// </summary> /// <param name="name">a name (that correspond to a predicate or predicate)</param> /// <returns>a set of SubstitutionSets that make the received name to match predicates or properties that do exist in the KB</returns> //public IEnumerable<SubstitutionSet> Unify(Name name, SubstitutionSet constraints = null) //{ // return m_knowledgeStorage.Unify(name, constraints).Select(p => p.Item2); //} public void Tell(Name property, PrimitiveValue value) { Tell(property, value, Name.SELF_SYMBOL); }
public object AskProperty(Name property, Name perspective) { if (!property.IsGrounded) { throw new ArgumentException("The given Well Formed Name must be grounded", nameof(property)); } var results = AskPossibleProperties(property, perspective, null).Select(p => PrimitiveValue.Extract(p.Item1)).ToArray(); if (results.Length == 0) { return(null); } if (results.Length == 1) { return(results[0]); } return(results); }