Difference between revisions of "Boolean Operations"

From JRiverWiki
Jump to: navigation, search
(Rewrite with new constructs; simplified and clarified.)
(AND : added another mechanism for simulating AND)
Line 34: Line 34:
 
       if(test2, TRUE, FALSE),
 
       if(test2, TRUE, FALSE),
 
       FALSE)</tt>
 
       FALSE)</tt>
 +
 +
  <tt>if(isequal(test1:test2, 1:1), TRUE, FALSE)</tt>
  
 
Examples
 
Examples
Line 44: Line 46:
  
 
If the artist is Elton John and the year is prior to 1976, output ''In Prime'', otherwise output ''Washed Up''.  If the artist is not Elton John, output ''Unknown''.
 
If the artist is Elton John and the year is prior to 1976, output ''In Prime'', otherwise output ''Washed Up''.  If the artist is not Elton John, output ''Unknown''.
 +
 +
  if(isequal(isempty([album gain]):isempty([replay gain]), 1:1), both empty, both not empty)
 +
 +
If both album gain and replay gain are empty, output ''both empty'', otherwise output ''both not empty''.  This works by concatenating the output of the two isempty() functions into a single string and comparing it to the desired true/true values as a string ''1:1''.  This form can be used in a variety of ways.
  
 
===XOR===
 
===XOR===

Revision as of 10:25, 3 October 2012

Emulating AND, OR and XOR

The Media Center Expression Language currently does not have AND, OR and XOR connectives. These can be emulated using either the Math() expression or by using nested If() statements.

In the general forms shown below, test1 and test2 are Boolean expressions such as IsEmpty(...), IsEqual(...) or Regex(...). The TRUE and FALSE Booleans indicate the outcome of the connective test.

OR

The OR connective can be emulated using any of the constructs below:

  if(math(test1 | test2), TRUE, FALSE)

  if(math(test1 + test2), TRUE, FALSE)

  if(test1, TRUE,
     if(test2, TRUE, FALSE))

Examples

if(math(isequal([artist], Nat King Cole) | isequal([artist], John Lee Hooker)), Legend, Other)
if(isequal([artist], Nat King Cole), Legend,
     if(isequal([artist], John Lee Hooker), Legend, Other))

If the artist is either Nat King Cole or John Lee Hooker, output Legend, otherwise output Other.

AND

The AND connective can be emulated using any of the constructs below:

  if(math(test1 & test2), TRUE, FALSE)

  if(math(test1 * test2), TRUE, FALSE)

  if(test1,
     if(test2, TRUE, FALSE),
     FALSE)

  if(isequal(test1:test2, 1:1), TRUE, FALSE)

Examples

if(math(isequal([artist], Elton John) & below([Year], 1976)), In Prime, Washed Up)
if(isequal([artist], Elton John),
  if(compare([year], <, 1976), In Prime, Washed Up),
  Unknown)

If the artist is Elton John and the year is prior to 1976, output In Prime, otherwise output Washed Up. If the artist is not Elton John, output Unknown.

 if(isequal(isempty([album gain]):isempty([replay gain]), 1:1), both empty, both not empty)

If both album gain and replay gain are empty, output both empty, otherwise output both not empty. This works by concatenating the output of the two isempty() functions into a single string and comparing it to the desired true/true values as a string 1:1. This form can be used in a variety of ways.

XOR

The XOR connective can be emulated using any of the constructs below:

  if(math(test1 - test2), TRUE, FALSE)

  if(test1,
     if(test2, FALSE, TRUE),
     if(test2, TRUE, FALSE))

Examples

 if(math(isequal([artists], Michael Jackson, 8) - isequal([artists], Paul McCartney, 8)), Play It, Skip It)
 if(regex([artists], /#\bMichael Jackson\b#/),
    if(regex([artists], /#\bPaul McCartney\b#/),  Skip It, Play It),
    if(regex([artists], /#\bPaul McCartney\b#/),  Play It, Skip It))

If either artist, but not both, is Michael Jackson or Paul McCartney, output Play It, otherwise output Skip It. The example assumes a custom list field named Artists which has a list of relevant artists.