6

Could someone translate the following polish notation to its SQL counterpart:

['|', '&', ('is_company','=', True),('parent_id', '=', False),('company_name', '!=', False),('company_name', '!=', '')]

My guess is :

is_company = True OR parent_id = False AND company_name <> False AND company_name <> ''

I could not get the concept of this notation no matter how hard I tried to understand it. Please help.

UPDATE

I was trying to extend the above notation to be:

((is_company = True AND parent_id = False) OR company_name <> False) AND company_name <> '' AND customer_type_id <> False

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Lekhnath
  • 4,532
  • 6
  • 36
  • 62
  • This is not polish notation. RPN is: `10, 5, +` and that equals `10 + 5` in "normal" notation. Starting with an or does not make any sense. Also the operands are missing to connect the four elements (not counting the and and or)... – mrCarnivore Jan 30 '18 at 14:02
  • 1
    I think `[ "|", "&", A, B, C, D ]` <==> `[((A & B) | C) & D ]` – khelili miliana Jan 30 '18 at 14:11
  • 1
    @mrCarnivore much appreciate your help. I think you are right and it is only polish notation *inspired* syntax or something like that. – Lekhnath Jan 30 '18 at 14:15
  • @Anonymousmiliana I think you are correct because I read somewhere that each operator takes two operands to the right in exception of `!` which negates next operand. Also `&` is the default operator so it makes sense. – Lekhnath Jan 30 '18 at 14:35
  • @Anonymousmiliana could you please answer my updated question. – Lekhnath Jan 30 '18 at 14:39
  • 2
    @Lekhnath, I think you need juste to add the fifth condition, then the final answer will be `['|', '&', ('is_company','=', True),('parent_id', '=', False),('company_name', '!=', False),('company_name', '!=', ''), ('customer_type_id', '!=', False)]` – khelili miliana Jan 30 '18 at 15:06
  • Isn't the whole point of Polish notation that it does away for the need for parentheses and operator precedence? – Mad Physicist Jan 30 '18 at 17:01
  • 1
    @Anonymousmiliana Yes, that worked. Thanks :) – Lekhnath Jan 31 '18 at 05:56

1 Answers1

12

I totally agree with you, each time I have to do a complex domain using this kind of Polish notation, I have to rack my brains to manage it.

I think the domain you're looking for is:

['&amp;', '|', '&amp;', ('is_company', '=', True), ('parent_id', '=', False), ('company_name', '!=', False), '&amp;', ('company_name', '!=', ''), ('customer_type_id', '!=', False)]

I made up a method to get these complex domains, and it's working:

First I write a letter instead of each condition:

A => is_company = True => ('is_company', '=', True)
B => parent_id = False => ('parent_id', '=', False)
C => company_name <> False => ('company_name', '!=', False)
D => company_name <> '' => ('company_name', '!=', '')
E => customer_type_id <> False => ('customer_type_id', '!=', False)

Then build the expression you need using only the letters and the standard operators, forget about the Polish notation and the conditions:

Step 0. => ((A and B) or C) and D and E

Afterwards, group the operations you should execute the first (don't mind about the missing operators so far):

Step 1. => ((A and B) or C) and D and E
Step 2. => (AB or C) and D and E
Step 3. => ABC and D and E
Step 4. => ABC and DE

Now we have only an operator, let's start decomposing it again (and moving the operator to the left of each couple of conditions), following the reverse order in which you grouped the operations (for example, from step 3 to 4 you grouped DE, so now, from step 4 to 3, decompose DE and move its operator to its left):

Step 4. => and ABC DE
Step 3. => and ABC and D E
Step 2. => and or AB C and D E
Step 1. => and or and A B C and D E

Now change the operators and add the commas, the quotes and the brackets:

['&amp;', '|', '&amp;', A, B, C, '&amp;', D, E]

Finally, replace the letters with the conditions, and there you have your domain. It would be better to explain it face to face but may be you were able to understand everything.

Note: I don't remove the &amp; operators (despite if you don't write them, Odoo should take them by default) because my experience is that with largest domains, if I didn't write the &amp;, the domain didn't work. I guess this happened when there was a lot of nested conditions, but my advice is to write always them.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • 2
    You don't really need the first `&`, b'coz the `&` is the default operator – khelili miliana Jan 30 '18 at 20:58
  • 1
    @forvas you're a great teacher :) I can clearly see what you are doing in each steps. And your statements are so neat. I) **Step 1-4** simplify the condition to get only one operator to start with II) **Step 4-1** convert to polish notation taking one operator a time in reverse order of first set of steps (ie. Step 1-4). I agree with you that we should explicitly state the operators in our domain expression so that it is comparatively more readable and manageable. You should do a screencast on this topic that would be helpful for lots of people. :). And thanks. – Lekhnath Jan 31 '18 at 05:40
  • 1
    @Anonymousmiliana you're right. But sometimes (especially with large domains) is mandatory to write &, otherwise the domain won't work. I still don't know the reason why, so whereas I don't get it, I prefer to write every operator. – forvas Feb 06 '18 at 09:46
  • @forvas, with & or without, the essential that works fine, thanks – khelili miliana Feb 06 '18 at 09:56