SelectionCriterion class: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
Objects in the SelectionCriterion class are used primarily as input to the | Objects in the <var>SelectionCriterion</var> class are used primarily as input to the | ||
collection class searching methods | collection class [[Collection classes#Sorting an Arraylist using one sort criterion|searching methods]]. | ||
The searching methods take a SelectionCriterion object as a parameter which | The searching methods take a <var>SelectionCriterion</var> object as a parameter which | ||
provides an item selection specification for the search. | provides an item selection specification for the search. | ||
Line 8: | Line 8: | ||
that tests a collection item value to determine whether the item | that tests a collection item value to determine whether the item | ||
(or its item number) is to be returned by the searching method. | (or its item number) is to be returned by the searching method. | ||
The SelectionCriterion methods are named for the operation they provide: | The <var>SelectionCriterion</var> methods are named for the operation they provide: | ||
<ul> | <ul> | ||
<li> | <li><var>Eq</var> (equal to), <var>Ne</var> (not equal to), <var>Lt</var> (less than), <var>Le</var> (less than or equal to), | ||
<var>Gt</var> (greater than), <var>Ge</var> (greater than or equal to). | |||
<li>The | <li>The <var>And</var>, <var>Or</var>, and <var>Not</var> methods let you combine comparison operations in a single criterion. | ||
<li>The | <li>The <var>True</var> and <var>False</var> methods respectively match all or no items. | ||
<li>The IsNull and IsNotNull methods respectively match a collection object that is Null | <li>The <var>IsNull</var> and <var>IsNotNull</var> methods respectively match a collection object that is <var>Null</var> | ||
or one that is not Null. | or one that is not <var>Null</var>. | ||
</ul> | </ul> | ||
Most SelectionCriterion objects use two parameters to construct a single criterion | Most <var>SelectionCriterion</var> objects use two parameters to construct a single criterion | ||
for selecting a collection item; some use two or more SelectionCriterion objects | for selecting a collection item; some use two or more <var>SelectionCriterion</var> objects | ||
to construct a single criterion. | to construct a single criterion. | ||
As an example specification, | As an example specification, | ||
the selection criterion | the selection criterion <code>%sel</code> in the following | ||
statement, instantiated by the LT constructor method, matches | statement, instantiated by the LT constructor method, matches | ||
a number whose square root is less than 10: | a number whose square root is less than 10: | ||
< | <p class="code"><nowiki>%sel = lt(squareRoot, 10) | ||
</nowiki></p> | |||
</ | |||
Where the left- and right-hand elements of the comparison operation are, | Where the left- and right-hand elements of the comparison operation are, | ||
respectively, the | respectively, the <var>Lt</var> parameter values: | ||
<ul> | <ul> | ||
<li>The <var>[[SquareRoot (Float function)|SquareRoot]]</var> intrinsic Float function | <li>The <var>[[SquareRoot (Float function)|SquareRoot]]</var> intrinsic <var>Float</var> function | ||
is applied to each item value before the comparison is made. | is applied to each item value before the comparison is made. | ||
<li><tt>10</tt> might be any expression of any User Language intrinsic type. | <li><tt>10</tt> might be any expression of any <var class="product">User Language</var> intrinsic type. | ||
</ul> | </ul> | ||
This | This <code>%sel</code> criterion might be used as follows to locate the | ||
next Arraylist item after item 3 whose square root is less than 10: | next <var>Arraylist</var> item after item 3 whose square root is less than 10: | ||
< | <p class="code"><nowiki>%itemnum = %balance:findNextItemNumber(%sel, start=3) | ||
</nowiki></p> | |||
</ | |||
The requirements for the SelectionCriterion parameter exemplified by the SquareRoot | The requirements for the <var>SelectionCriterion</var> parameter exemplified by the <var>SquareRoot</var> | ||
method above are: | method above are: | ||
<ul> | <ul> | ||
Line 52: | Line 50: | ||
</ul> | </ul> | ||
For more about the method in a SelectionCriterion, see "[[#Specifying a SelectionCriterion's parameters|Specifying a SelectionCriterion's parameters]]" below. | For more about the method in a <var>SelectionCriterion</var>, see "[[#Specifying a SelectionCriterion's parameters|Specifying a SelectionCriterion's parameters]]" below. | ||
Additional notes: | Additional notes: | ||
<ul> | <ul> | ||
<li>Comparison values are evaluated at SelectionCriterion construction time, | <li>Comparison values are evaluated at <var>SelectionCriterion</var> construction time, | ||
not when the search method is evaluated. | not when the search method is evaluated. | ||
That is, the following sequence selects | That is, the following sequence selects | ||
items with income greater than or equal to 50000, not 100000: | items with income greater than or equal to 50000, not 100000: | ||
< | <p class="code"><nowiki>%income = 50000 | ||
%sel = ge(income, %income) | |||
%income = 100000 | |||
%foo2 = %foo:subsetNew(%sel) | |||
</nowiki></p> | |||
</ | <li><var>SelectionCriterion</var> have a size limit: the | ||
<li>SelectionCriterion have a size limit: the | <var>SelectionCriterion</var>, including the values for <var>Eq</var>, <var>Ne</var>, <var>Lt</var>, <var>Le</var>, <var>Ge</var>, and <var>Gt</var> comparisons must take less than 252 bytes. | ||
SelectionCriterion, including the values for | |||
comparisons must take less than 252 bytes. | |||
<var>And</var>s and <var>Or</var>s take 4 bytes, and | |||
comparison selectors have 12 bytes of overhead plus the length of the value | comparison selectors have 12 bytes of overhead plus the length of the value | ||
rounded to a 4-byte multiple. | rounded to a 4-byte multiple. | ||
So, for example, an And condition with | So, for example, an <var>And</var> condition with | ||
seven 20-byte values would take 4+7*(20+12), or 228, bytes. | seven 20-byte values would take 4+7*(20+12), or 228, bytes. | ||
Line 79: | Line 75: | ||
encapsulating some or all of the conditions in a local method, since a local | encapsulating some or all of the conditions in a local method, since a local | ||
enhancement method can be the method parameter for a comparison | enhancement method can be the method parameter for a comparison | ||
SelectionCriterion. | <var>SelectionCriterion</var>. | ||
<li>The SelectionCriterion methods are constructors and as such they can be | <li>The <var>SelectionCriterion</var> methods are [[Object variables#Virtual Constructor methods|virtual constructors]] and as such they can be | ||
called with no method object, with an explicit class name, or with an | called with no method object, with an explicit class name, or with an | ||
object variable, even if that object is | object variable, even if that object is <var>Null</var>: | ||
< | <p class="code"><nowiki>%selc1 = NE(criterion1, criterion2) | ||
%selc2 = %(selectionCriterion for float): - | |||
AND(criterion1, criterion2) | |||
%selc3 = %selc3:OR(criterion1, criterion2) | |||
</ | </nowiki></p> | ||
</ul> | </ul> | ||
The SelectionCriterion class is new as of | The <var>SelectionCriterion</var> class is new as of <var class="product">Sirius Mods</var> version 7.6. | ||
The [[List of SelectionCriterion methods|"List of SelectionCriterion methods"]] lists all the methods in this class. | The [[List of SelectionCriterion methods|"List of SelectionCriterion methods"]] lists all the methods in this class. | ||
===Declaring a SelectionCriterion object variable=== | ===Declaring a SelectionCriterion object variable=== | ||
The SelectionCriterion class operates on specific objects, so a variable of the | The <var>SelectionCriterion</var> class operates on specific objects, so a variable of the | ||
SelectionCriterion class must be qualified with the object to which it applies: | <var>SelectionCriterion</var> class must be qualified with the object to which it applies: | ||
For example: | For example: | ||
< | <p class="code"><nowiki>%sel1 is selectionCriterion for object myClass | ||
%sel2 is selectionCriterion for object xmlNode | |||
%sel3 is selectionCriterion for longstring | |||
</nowiki></p> | |||
</ | |||
The declaration for | The declaration for <code>%sel</code> in the example in the preceding section | ||
might be: | might be: | ||
< | <p class="code"><nowiki>%sel is selectionCriterion for float | ||
</nowiki></p> | |||
</ | |||
====SelectionCriterion object variable declaration syntax==== | ====SelectionCriterion object variable declaration syntax==== | ||
Line 116: | Line 109: | ||
Where: | Where: | ||
< | <table> | ||
< | <tr><th><objvar> | ||
< | </th><td>The name of the <var>SelectionCriterion</var> object variable. | ||
< | </td></tr> | ||
< | <tr><th><itemtype> | ||
</ | </th><td>The datatype of the items in the collection to be searched. | ||
</td></tr></table> | |||
===Specifying a SelectionCriterion's parameters=== | ===Specifying a SelectionCriterion's parameters=== | ||
As stated earlier, most of the SelectionCriterion methods | As stated earlier, most of the <var>SelectionCriterion</var> methods | ||
accept two parameters, the first a “method value” and the second an | accept two parameters, the first a “method value” and the second an | ||
intrinsic value. | intrinsic value. | ||
Regarding the first, just as | Regarding the first, just as | ||
a User Language “numeric” argument can be a numeric literal or a variable or even an | a <var class="product">User Language</var> “numeric” argument can be a numeric literal or a variable or even an | ||
expression that results in a number, a method value argument can be a method | expression that results in a number, a method value argument can be a method | ||
literal or a method variable or an expression that results in a method. | literal or a method variable or an expression that results in a method. | ||
Line 133: | Line 127: | ||
More specifically, | More specifically, | ||
a method value is a value assigned to a [[Method variables|method variable]]. | a method value is a value assigned to a [[Method variables|method variable]]. | ||
And for a SelectionCriterion, the method variable's ''implicit'' | And for a <var>SelectionCriterion</var>, the method variable's ''implicit'' | ||
declaration is: | declaration is: | ||
< | <p class="code"><nowiki>Is Function (<itemtype>):<methname> Is <intrinType> | ||
</nowiki></p> | |||
</ | |||
Where: | Where: | ||
< | <table> | ||
< | <tr><th><itemtype> | ||
< | </th><td>The class of the items in the collection to be sorted. | ||
< | </td></tr> | ||
< | <tr><th><methname> | ||
Any method (system or user), class | </th><td>A method name, but merely a placeholder in an actual declaration. Any method (system or user), class <var>Variable</var> or <var>Property</var>, local method, or method variable that fits the declaration can be used in the <var>SelectionCriterion</var>. | ||
Variable or Property, local method, or method variable that fits the | </td></tr> | ||
declaration can be used in the SelectionCriterion. | <tr><th><intrinType> | ||
< | </th><td>A <var class="product">User Language</var> [[Intrinsic classes#intrins_classes|intrinsic class]] type returned by the function. | ||
< | </td></tr></table> | ||
</ | |||
The method value argument must conform to the following rules: | The method value argument must conform to the following rules: | ||
Line 158: | Line 150: | ||
The following examples show different method argument types | The following examples show different method argument types | ||
as the first parameter in a SelectionCriterion. | as the first parameter in a <var>SelectionCriterion</var>. | ||
In the examples below, the method values are respectively a class Variable, | In the examples below, the method values are respectively a class <var>Variable</var>, | ||
a local method, and a special method value, all of which | a local method, and a special method value, all of which | ||
satisfy the Function template described above. | satisfy the <var>Function</var> template described above. | ||
<ol> | <ol> | ||
<li><div id="xmpxsub"></div>The selectionCriterion in the following example uses a class Variable, < | <li><div id="xmpxsub"></div>The selectionCriterion in the following example uses a class <var>Variable</var>, <code>Income</code>, as the criterion method parameter. The collection method, <var>SubsetNew</var>, | ||
< | returns a <var>NamedArraylist</var> that contains all the items that satisfied the selection | ||
criterion. | |||
<p class="code"><nowiki>Begin | |||
class foo | |||
public | |||
variable name is string len 20 | |||
variable status is string len 8 | |||
variable income is float | |||
constructor newf(%a is string len 10, - | |||
%b string len 8, %c is float) | |||
function myprint is longstring | |||
end public | |||
constructor newf(%a is string len 10, - | |||
%b string len 8, %c is float) | |||
%this:name = %a | |||
%this:status = %b | |||
%this:income = %c | |||
end constructor | |||
function myprint is longstring | |||
return %this:name with ' ' with - | |||
%this:status with ' ' with - | |||
'(income: ' with %this:income with ')' | |||
end function | |||
end class | |||
%fool is namedArraylist of object foo | |||
%fool = new | |||
%fool('Jim') = newf('Red', 'employed', 45000) | |||
%fool('Jed') = newf('Black', 'retired', 30000) | |||
%fool('Jan') = newf('Green', 'employed', 55000) | |||
%fool('Jud') = newf('Brown', 'expired', 0) | |||
%fool('Jon') = newf('White', 'tired', 70000) | |||
%fool = %fool:subsetNew(gt(income, 0)) | |||
for %i from 1 to %fool:count | |||
print %fool:namebynumber(%i) And - | |||
%fool:itembyNumber(%i):myprint | |||
end for | |||
End | |||
</ | </nowiki></p> | ||
The result is: | The result is: | ||
< | <p class="code"><nowiki>Jan Green employed (income: 55000) | ||
Jed Black retired (income: 30000) | |||
Jim Red employed (income: 45000) | |||
Jon White tired (income: 70000) | |||
</nowiki></p> | |||
</ | <li>The second parameter in most <var>SelectionCriterion</var> methods is a <var class="product">User Language</var> | ||
<li>The second parameter in most <var>SelectionCriterion</var> methods is a User Language | |||
intrinsic-type value, often a string or numeric literal. | intrinsic-type value, often a string or numeric literal. | ||
Consequently, you cannot specify a criterion that compares two method | Consequently, you cannot specify a criterion that compares two method | ||
values. | values. | ||
For example, for a < | For example, for a <code>Cust</code> class (similar to the class in the previous | ||
example) with variables < | example) with variables <code>Expenses</code> | ||
and < | and <code>Income</code>, you might want to find all the customers with expenses | ||
greater than income — but the following <b><i>is not allowed</i></b>: | greater than income — but the following <b><i>is not allowed</i></b>: | ||
< | <p class="code"><nowiki>%custDebtors = %custs:subsetNew(gt(expenses, income)) | ||
</nowiki></p> | |||
</ | |||
However, you can create a [[Local and Common entities#Defining and invoking a local method|local method]] | However, you can create a [[Local and Common entities#Defining and invoking a local method|local method]] | ||
with which you can achieve the same result: | with which you can achieve the same result: | ||
< | <p class="code"><nowiki>local function (cust):netIncome is float | ||
return %this:income - %this:expenses | |||
end function | |||
%custDebtors = %custs:subsetNew(lt(netIncome, 0)) | |||
</nowiki></p> | |||
</ | <li><div id="xmpthis"></div>The <var>SelectionCriterion</var> in the following request uses the special method value <var>This</var> as the method parameter, and the <var>And</var> constructor combines two criteria to create one criterion. Valid only for intrinsic classes, <var>This</var> simply ret | ||
<li><div id="xmpthis"></div>The <var>SelectionCriterion</var> in the following request uses the special method value <var>This</var> as the method parameter, and the <var>And</var> constructor combines two criteria to create one criterion. Valid only for intrinsic classes, <var>This</var> simply | <p class="code"><nowiki>Begin | ||
< | %fooflt is arraylist of float | ||
%fooflt = list(111, 29, 93, 77, -345) | |||
%sel is object selectioncriterion for float | |||
%sel = and(gt(this, 50), le(this, 93)) | |||
%fooflt = %fooflt:subsetnew(%sel) | |||
%fooflt:print | |||
End | |||
</ | </nowiki></p> | ||
The result is: | The result is: | ||
< | <p class="code"><nowiki>1: 93 | ||
2: 77 | |||
</nowiki></p> | |||
</ | |||
</ol> | </ol> | ||
==See also== | ==See also== |
Revision as of 20:45, 1 August 2011
Objects in the SelectionCriterion class are used primarily as input to the collection class searching methods. The searching methods take a SelectionCriterion object as a parameter which provides an item selection specification for the search.
A selection specification, or criterion, is a relational expression that tests a collection item value to determine whether the item (or its item number) is to be returned by the searching method. The SelectionCriterion methods are named for the operation they provide:
- Eq (equal to), Ne (not equal to), Lt (less than), Le (less than or equal to), Gt (greater than), Ge (greater than or equal to).
- The And, Or, and Not methods let you combine comparison operations in a single criterion.
- The True and False methods respectively match all or no items.
- The IsNull and IsNotNull methods respectively match a collection object that is Null or one that is not Null.
Most SelectionCriterion objects use two parameters to construct a single criterion for selecting a collection item; some use two or more SelectionCriterion objects to construct a single criterion.
As an example specification,
the selection criterion %sel
in the following
statement, instantiated by the LT constructor method, matches
a number whose square root is less than 10:
%sel = lt(squareRoot, 10)
Where the left- and right-hand elements of the comparison operation are, respectively, the Lt parameter values:
- The SquareRoot intrinsic Float function is applied to each item value before the comparison is made.
- 10 might be any expression of any User Language intrinsic type.
This %sel
criterion might be used as follows to locate the
next Arraylist item after item 3 whose square root is less than 10:
%itemnum = %balance:findNextItemNumber(%sel, start=3)
The requirements for the SelectionCriterion parameter exemplified by the SquareRoot method above are:
- It must be a method or method variable defined to operate on the type of the items in the collection being searched.
- It must return an intrinsic (number, string, unicode) value.
For more about the method in a SelectionCriterion, see "Specifying a SelectionCriterion's parameters" below.
Additional notes:
- Comparison values are evaluated at SelectionCriterion construction time,
not when the search method is evaluated.
That is, the following sequence selects
items with income greater than or equal to 50000, not 100000:
%income = 50000 %sel = ge(income, %income) %income = 100000 %foo2 = %foo:subsetNew(%sel)
- SelectionCriterion have a size limit: the SelectionCriterion, including the values for Eq, Ne, Lt, Le, Ge, and Gt comparisons must take less than 252 bytes. Ands and Ors take 4 bytes, and comparison selectors have 12 bytes of overhead plus the length of the value rounded to a 4-byte multiple. So, for example, an And condition with seven 20-byte values would take 4+7*(20+12), or 228, bytes. You can work around a criterion that exceeds 252 bytes by encapsulating some or all of the conditions in a local method, since a local enhancement method can be the method parameter for a comparison SelectionCriterion.
- The SelectionCriterion methods are virtual constructors and as such they can be
called with no method object, with an explicit class name, or with an
object variable, even if that object is Null:
%selc1 = NE(criterion1, criterion2) %selc2 = %(selectionCriterion for float): - AND(criterion1, criterion2) %selc3 = %selc3:OR(criterion1, criterion2)
The SelectionCriterion class is new as of Sirius Mods version 7.6.
The "List of SelectionCriterion methods" lists all the methods in this class.
Declaring a SelectionCriterion object variable
The SelectionCriterion class operates on specific objects, so a variable of the SelectionCriterion class must be qualified with the object to which it applies: For example:
%sel1 is selectionCriterion for object myClass %sel2 is selectionCriterion for object xmlNode %sel3 is selectionCriterion for longstring
The declaration for %sel
in the example in the preceding section
might be:
%sel is selectionCriterion for float
SelectionCriterion object variable declaration syntax
<objvar> Is Object SelectionCriterion For <itemtype>
Where:
<objvar> | The name of the SelectionCriterion object variable. |
---|---|
<itemtype> | The datatype of the items in the collection to be searched. |
Specifying a SelectionCriterion's parameters
As stated earlier, most of the SelectionCriterion methods accept two parameters, the first a “method value” and the second an intrinsic value. Regarding the first, just as a User Language “numeric” argument can be a numeric literal or a variable or even an expression that results in a number, a method value argument can be a method literal or a method variable or an expression that results in a method.
More specifically, a method value is a value assigned to a method variable. And for a SelectionCriterion, the method variable's implicit declaration is:
Is Function (<itemtype>):<methname> Is <intrinType>
Where:
<itemtype> | The class of the items in the collection to be sorted. |
---|---|
<methname> | A method name, but merely a placeholder in an actual declaration. Any method (system or user), class Variable or Property, local method, or method variable that fits the declaration can be used in the SelectionCriterion. |
<intrinType> | A User Language intrinsic class type returned by the function. |
The method value argument must conform to the following rules:
- It must return an intrinsic value.
- It cannot have any parameters other than the method object.
The following examples show different method argument types as the first parameter in a SelectionCriterion. In the examples below, the method values are respectively a class Variable, a local method, and a special method value, all of which satisfy the Function template described above.
- The selectionCriterion in the following example uses a class Variable,
Income
, as the criterion method parameter. The collection method, SubsetNew,returns a NamedArraylist that contains all the items that satisfied the selection criterion.
Begin class foo public variable name is string len 20 variable status is string len 8 variable income is float constructor newf(%a is string len 10, - %b string len 8, %c is float) function myprint is longstring end public constructor newf(%a is string len 10, - %b string len 8, %c is float) %this:name = %a %this:status = %b %this:income = %c end constructor function myprint is longstring return %this:name with ' ' with - %this:status with ' ' with - '(income: ' with %this:income with ')' end function end class %fool is namedArraylist of object foo %fool = new %fool('Jim') = newf('Red', 'employed', 45000) %fool('Jed') = newf('Black', 'retired', 30000) %fool('Jan') = newf('Green', 'employed', 55000) %fool('Jud') = newf('Brown', 'expired', 0) %fool('Jon') = newf('White', 'tired', 70000) %fool = %fool:subsetNew(gt(income, 0)) for %i from 1 to %fool:count print %fool:namebynumber(%i) And - %fool:itembyNumber(%i):myprint end for End
The result is:
Jan Green employed (income: 55000) Jed Black retired (income: 30000) Jim Red employed (income: 45000) Jon White tired (income: 70000)
- The second parameter in most SelectionCriterion methods is a User Language
intrinsic-type value, often a string or numeric literal.
Consequently, you cannot specify a criterion that compares two method
values.
For example, for a
Cust
class (similar to the class in the previous example) with variablesExpenses
andIncome
, you might want to find all the customers with expenses greater than income — but the following is not allowed:%custDebtors = %custs:subsetNew(gt(expenses, income))
However, you can create a local method with which you can achieve the same result:
local function (cust):netIncome is float return %this:income - %this:expenses end function %custDebtors = %custs:subsetNew(lt(netIncome, 0))
- The SelectionCriterion in the following request uses the special method value This as the method parameter, and the And constructor combines two criteria to create one criterion. Valid only for intrinsic classes, This simply ret
Begin %fooflt is arraylist of float %fooflt = list(111, 29, 93, 77, -345) %sel is object selectioncriterion for float %sel = and(gt(this, 50), le(this, 93)) %fooflt = %fooflt:subsetnew(%sel) %fooflt:print End
The result is:
1: 93 2: 77