use of includesAnyOf:

Chris Muller chris at funkyobjects.org
Wed Mar 14 02:54:08 UTC 2007


Hi Hilaire, there are a couple of ways to accomplish the
"includesAnyOf:" and you articulated them both eloquently in your
original e-mail:

>> Is the following correct ?
>>
>> myMagmaCollection where: [:each | each endDate includesAnyOf: {Date
>> today . Date yesterday}
>>
>> With endDate returning a Date instance.

Yep.

> Or may be with endDate returning a collection of Date will be even
more
> useful?

Yep.

Either approach can work, depending on your needs.

Let's clarify with a code example.  You may wish to create this
repository to have a working example where you may see the results.

	| mc eighties nineties |
	mc _ MagmaCollection new
	    addIndex: (MaDateAndTimeIndex attribute: #key) ;
	    yourself.
	eighties _ (1981 to: 1989) collect: [ : year | (DateAndTime year: year
day: 2 hour: 7 minute: 35 second: 51) ].
	nineties _ (1991 to: 1999) collect: [ : year | (DateAndTime year: year
day: 2 hour: 7 minute: 35 second: 51) ].
	"add eighties object"
	mc add: eighties -> 'the eighties'.
	mc add: nineties -> 'the nineties'.
	MagmaRepositoryController create: 'c:\temp\dtsIndexTest' root: mc.

We now have a repository with a MagmaCollection as the root.  This
MagmaCollection has one MaDateAndTimeIndex on the #key attribute
(since, for simplicity of the example, we have just added two
Associations to the collection).  Inspect a MagmaSession on this
repository to perform some test queries.

At this point, it is important to reiterate the nature of indexing an
object at multiple values like we did.  It means that, as far as the
MagmaCollection is concerned, the object is equal: to *all* of those
attribute values it is indexed at.  To understand this, try this query
on your open MagmaSession:

	| anyEighty |
	anyEighty := ((1985 to: 1987) collect: [ : year | DateAndTime year:
year day: 2 hour: 7 minute: 35 second: 51 ]) atRandom.
	self root where:
		[ : each | 
		each key equals: anyEighty ]

When we added these Associations, since the Association>>#key message
answered a Collection of pointer objects instead of a single object,
Magma treated them like it treats keyword indexes; indexing each
Association object at all of the multiple key values; in that, a match
on any of the associated "keywords" (DateAndTimes) will cause that
object to match.  And, the logic in Magma doesn't treat multi-value
indexes any differently; indexing on a single value quickly gets
converted to a collection of one values.  So ALL indexes are inherently
multi-value.

Also, being indexed at multiple values, the other operators besides
equals: work correctly too; #from:to:, etc. which I think will prove
useful for Brent in pursuing the WildCardIndex.

Now, this is a little different than the first approach, indexing the
object at a single date and then finding it on any of several dates.

	| someEighties |
	someEighties := (1985 to: 1987) collect: [ : year | DateAndTime year:
year day: 2 hour: 7 minute: 35 second: 51 ].
	self root where:
		[ : each | 
		each key includesAnyOf: someEighties ]

The #includesAnyOf: operator (in MaClause), just converts the
expression to a series of OR'd clauses.  IOW, the above use of
#includesAnyOf: is just a short-hand way of writing:

	| someEighties |
	someEighties := (1985 to: 1987) collect: [ : year | DateAndTime year:
year day: 2 hour: 7 minute: 35 second: 51 ].
	self root where:
		[ : each | 
		(each key equals: someEighties first)
		| (each key equals: someEighties second)
		| (each key equals: someEighties third) ]

I'm sure you'll notice that you get a Reader with a size of 3.  That's
because the same object qualified three times (if you convert the
reader to a Set (via asSet) you will see it is really just the one same
object in there).  This phenomena is explained in the documentation at:

	http://wiki.squeak.org/squeak/5859 

under the heading "Non-distinct Results".  Basically, people may wish
to create more sophisticated "weighted" indexes and knowing how many
critieria for which an object qualifies affects its weight, so that's
why this is a good feature.

I can now see how #includesAnyOf: is confusing in the context of
"single-value" indexes; it should be called something like
"matchesAnyOf:".  But again, Magma treats all indexes as multi-value
behind the scenes, and you might truly have an expression that reads:

	myArticles where: [ : each | each keywords includesAnyOf: #('etoys'
'olpc') ]

and, in that context, it makes better English sense too.

Whew, this got long, I hope it didn't confuse the issue!

 - Chris


----- Original Message ----
From: Hilaire Fernandes <hilaire2006 at laposte.net>
To: magma at lists.squeakfoundation.org
Sent: Monday, March 12, 2007 6:43:33 PM
Subject: Re: use of includesAnyOf:

I am sorry to bother you Chris, but I have an
understanding/communication problem and I cannot understand you.
May be a small example will make it clearer for me.

Thanks

Hilaire

Chris Muller a écrit :
> Yes, index the same object at multiple endDates and then "equals"
will inherently be "includesAnyOf:"..
>
> ----- Original Message ----
> From: Hilaire Fernandes <hilaire2006 at laposte.net>
> To: magma at lists.squeakfoundation.org
> Sent: Sunday, March 11, 2007 6:41:01 AM
> Subject: Re: use of includesAnyOf:
>
> Hilaire Fernandes a écrit :
>> I am wondering about the use of includesAnyOf: query. Is it the same
use
>> than inclidesAnyOf: from the collection classes?
>>
>> Is the following correct ?
>>
>> myMagmaCollection where: [:each | each endDate includesAnyOf: {Date
>> today . Date yesterday}
>>
>> With endDate returning a Date instance.
>
> Or may be with endDate returning a collection of Date will be even
more
> useful?
>
> Hilaire
>
> _______________________________________________
> Magma mailing list
> Magma at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/magma
>
>
>

_______________________________________________
Magma mailing list
Magma at lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/magma



More information about the Magma mailing list