Re: linq - how to test expression reducers
"Tuna Toksöz" <[email protected]> Sat, 16 Aug 2008 01:39:56 +0300
| Newsgroups | gmane.comp.windows.dotnet.nhibernate.devel |
|---|---|
| Message-ID | <[email protected]> |
public static bool ExpressionEquals(this Expression source,Expression
toBeCompared)
{
if(object.Equals(source,toBeCompared))
return true;
else if ((source != null && toBeCompared == null) || (source == null &&
toBeCompared != null))
{
return false;
}
else if (source.GetType()==toBeCompared.GetType())
{
System.Type sourceType = source.GetType();
var properties = sourceType.GetProperties();
bool value = true;
for (int i = 0; i < properties.Length;i++ )
{
var info = properties[i];
var leftValue = info.GetValue(source, null);
var rightValue = info.GetValue(toBeCompared, null);
if (leftValue is Expression)
{
value &= ExpressionEquals((Expression)leftValue,
(Expression)rightValue);
}
else
value &= object.Equals(leftValue,rightValue);
}
return value;
}
else
return false;
}
This seems to work fine for me. Anything that I may missed with that?
On Fri, Aug 15, 2008 at 5:26 PM, Ayende Rahien <[email protected]> wrote:
> That is because the Equal doesn't know how to deal with them, you have to
> write the equality yourself.
>
>
> On Fri, Aug 15, 2008 at 5:20 PM, Chad Lee <[email protected]> wrote:
>
>> I just ran this simple test:
>>
>> var expr1 = Expression.And(
>> Expression.Constant(true),
>> Expression.Constant(false));
>>
>> var expr2 = Expression.And(
>> Expression.Constant(true),
>> Expression.Constant(false));
>>
>> Assert.Equal(expr1, expr2);
>>
>> which fails....
>>
>> That just seems stupid to me.
>>
>>
>> On Fri, Aug 15, 2008 at 9:08 AM, Ayende Rahien <[email protected]> wrote:
>>
>>> What about something like:
>>> var reducedExpression = Reduce( () => true && x>1 && x<2));
>>> AssertExpressionEq( reducedExpression,
>>> new BinaryExpression{ And, new (x>1), new (x<2));
>>>
>>> On Fri, Aug 15, 2008 at 4:54 PM, Tuna Toksöz <[email protected]>wrote:
>>>
>>>> Hello team,
>>>> I am working on Expression reducers to simplify complex expressions.
>>>> I made LogicalExpressionReducer which simply converts
>>>> not(not(not(true))) to ConstantExpression(false) and (true &
>>>> x.Name=="johndoe") to (x.Name=="john doe"), meaning that it just uses the
>>>> truth table to reduce expressions.
>>>>
>>>> The problem with expressions is that they are trees! For simple ones, it
>>>> is easy to check if I get the same result but complex ones are hard to deal
>>>> with.
>>>> I first thought that I can use .ToString() of expression which produces
>>>> string representation of an expression, that is some thing like
>>>> Assert.AreEqual("false",reducedExpression.ToString())
>>>>
>>>> But this simply sounds incorrect to me. I then thought about using
>>>> visitors to check for equality, but not sure about that.
>>>>
>>>> 2nd one sounds the way to go, but are there any simpler way to test it?
>>>>
>>>> Producing
>>>>
>>>> --
>>>> Tuna Toksöz
>>>>
>>>> Typos included to enhance the readers attention!
>>>>
>>>>
>>>> -------------------------------------------------------------------------
>>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>>>> challenge
>>>> Build the coolest Linux based applications with Moblin SDK & win great
>>>> prizes
>>>> Grand prize is a trip for two to an Open Source event anywhere in the
>>>> world
>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>>>> _______________________________________________
>>>> Nhibernate-development mailing list
>>>> [email protected]
>>>> https://lists.sourceforge.net/lists/listinfo/nhibernate-development
>>>>
>>>>
>>>
>>> -------------------------------------------------------------------------
>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>>> challenge
>>> Build the coolest Linux based applications with Moblin SDK & win great
>>> prizes
>>> Grand prize is a trip for two to an Open Source event anywhere in the
>>> world
>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>>> _______________________________________________
>>> Nhibernate-development mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/nhibernate-development
>>>
>>>
>>
>> -------------------------------------------------------------------------
>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>> challenge
>> Build the coolest Linux based applications with Moblin SDK & win great
>> prizes
>> Grand prize is a trip for two to an Open Source event anywhere in the
>> world
>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>> _______________________________________________
>> Nhibernate-development mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/nhibernate-development
>>
>>
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Nhibernate-development mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/nhibernate-development
>
>
--
Tuna Toksöz
Typos included to enhance the readers attention!
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Nhibernate-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nhibernate-development