Boolean Operations

From wiki.jriver.com
Revision as of 15:25, 7 June 2015 by Glynor (talk | contribs)
Jump to navigation Jump to search

The 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))

  if(isequal(isempty([album gain]):isempty([replay gain]), 1, 7), at least one empty, all not empty)

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.

if(isequal(isempty([album gain]):isempty([replay gain]), 1, 7), at least one empty, all not empty)

If any of album gain or replay gain is empty, output at least one empty, otherwise output all not empty. This works by using the concatenated output of the two isempty() functions, and using the sub-string search mode of isequal() to look for any true (1) value.

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.