public void CastToStrongTypeUnchanged() { object o1 = 25; object o2 = 75; ObjectPair pair = new ObjectPair(o1, o2); Reflection.CastToStrongType(pair); o1 = pair.First; o2 = pair.Second; Assert.IsTrue(o1 is Int32, "(1) o1 type"); Assert.AreEqual(o1, 25, "(1) o1 value"); Assert.IsTrue(o2 is Int32, "(1) o2 type"); Assert.AreEqual(o2, 75, "(1) o2 value"); o1 = "25"; o2 = "75"; pair = new ObjectPair(o1, o2); Reflection.CastToStrongType(pair); o1 = pair.First; o2 = pair.Second; Assert.IsTrue(o1 is String, "(2) o1 type"); Assert.AreEqual(o1, "25", "(2) o1 value"); Assert.IsTrue(o2 is String, "(2) o2 type"); Assert.AreEqual(o2, "75", "(2) o2 value"); }
public void CastToStrongTypeChanged() { object o1 = "25"; object o2 = 75; ObjectPair pair = new ObjectPair(o1, o2); Reflection.CastToStrongType(pair); o1 = pair.First; o2 = pair.Second; Assert.IsTrue(o1 is Int32, "(1) o1 type"); Assert.AreEqual(o1, 25, "(1) o1 value"); Assert.IsTrue(o2 is Int32, "(1) o2 type"); Assert.AreEqual(o2, 75, "(1) o2 value"); o1 = Math.PI; o2 = "-6.55"; pair = new ObjectPair(o1, o2); Reflection.CastToStrongType(pair); o1 = pair.First; o2 = pair.Second; Assert.IsTrue(o1 is Double, "(2) o1 type"); Assert.AreEqual(o1, Math.PI, "(2) o1 value"); Assert.IsTrue(o2 is Double, "(2) o2 type"); Assert.AreEqual(o2, -6.55d, "(2) o2 value"); }
/// <summary> /// Converts either object1 to the type of object2, or the contrary, based on the object having /// the weakest type, i.e. System.String. If none of the objects are System.String /// object2 will be casted to object1. /// </summary> /// <param name="pair">A reference to the pair of objects to cast.</param> public static void CastToStrongType(ObjectPair pair) { if ((pair.First.GetType().GetInterface("System.IConvertible", false) != null) && (pair.Second.GetType().GetInterface("System.IConvertible", false) != null)) { // if they are both convertible, convert to the strongest type (non string) // or arbitrary cast the second to the first if (pair.First is System.String) { pair.First = CastValue(pair.First, pair.Second.GetType()); } else { pair.Second = CastValue(pair.Second, pair.First.GetType()); } } }
/// <summary> /// Checks if the current Atom matches with another one, i.e. if they are of same type, /// contain the same number of predicates, and if their Individual predicates are equal. /// </summary> /// <description> /// This functions takes care of casting as it always tries to cast to the strongest type /// of two compared individuals. Since predicates can come from weakly-typed rule files /// (Strings) and other predicates can be generated by the user, this function tries to /// convert from String to the type of the other predicate (as String is considered not /// strongly typed). /// </description> /// <param name="atom">The other atom to determine the matching.</param> /// <returns>True if the two atoms match.</returns> public bool Matches(Atom atom) { if (!BasicMatches(atom)) return false; for(int i=0; i<predicates.Length; i++) { if ((predicates[i] is Individual) && (atom.predicates[i] is Function) && (!((Function)atom.predicates[i]).Evaluate((Individual)predicates[i]))) return false; else if ((predicates[i] is Function) && (atom.predicates[i] is Individual) && (!((Function)predicates[i]).Evaluate((Individual)atom.predicates[i]))) return false; else if ((predicates[i] is Function) && (atom.predicates[i] is Function) && (!(predicates[i].Equals(atom.predicates[i])))) return false; else if ((predicates[i] is Individual) && (atom.predicates[i] is Individual)) { // we have two individuals if ((predicates[i].Value.GetType() == atom.predicates[i].Value.GetType()) && (!predicates[i].Equals(atom.predicates[i]))) { // the two individuals are of same types: direct compare return false; } else { // the two individuals are of different types ObjectPair pair = new ObjectPair(predicates[i].Value, atom.predicates[i].Value); Reflection.CastToStrongType(pair); if (!pair.First.Equals(pair.Second)) return false; } } } return true; }