Thread: How to model an AND/OR logic by relations ?

How to model an AND/OR logic by relations ?

From
Fritz Lehmann-Grube
Date:
Hi all,

I'm setting up a DB in which ( besides normal relations ) an
AND/OR-relation is to be stored:
I have two tables "documents" and "atoms", where a document shall
"depend" upon a logical expression of atoms.
e.g.  (please widen your window for this)
###########################################################################################
###                                            ###
###  document1 "depends" on (atom1 OR atom2) AND (atom1 OR atom3 OR
atom4) AND (atom5)  ###
###                                            ###
###########################################################################################
This is after some normalization - I could also have (.. AND ..) OR (..
AND ..)
I don't have NOT's.

My idea is to have have two extra-tables:

CREATE TABLE or_groups(
       id ... primary key,
       document_id ... references documents
       );

CREATE TABLE dependencies(
       or_id ... references or_groups,
       atom_id ... references atoms
       );

with ( mapping the example )(please widen your window for this)

    | documents        |         | or_groups          |
    | id    | name        |         | id    | document_id |
    | 1    | document1    |         | 1    | 1          |
    |    |        |         | 2    | 1          |
    |    |        |         | 3    | 1          |

    | dependencies        |         | atoms              |
    | or_id    | atom_id    |         | id    | name          |
    | 1    | 1        |         | 1    | atom1          |
    | 1    | 2        |         | 2    | atom2          |
    | 2    | 1        |         | 3    | atom3          |
    | 2    | 3        |         | 4    | atom4          |
    | 2    | 4        |         | 5    | atom5          |
    | 3    | 5        |         |        |          |

Because I'm a novice in Databases and the idea was pure naive intuition
I want to ask, whether this is a standard problem and whether there is a
standard solution.
Am I running into problems ?
Do bad things happen when "documents" and "atoms" are the same table ?

Greetings and Thanx       Fritz

Re: How to model an AND/OR logic by relations ?

From
"Steve Boyle \(Roselink\)"
Date:
Fritz,

Given that you may have to deal with (.. AND ..) OR (..AND ..) does this
imply that you may need to modify relation dependencies to allow a link to
either atoms or further dependencies?

Otherwise I don't see how you can store the nested conditional statements.

P.S. I've never had to do this before (in databases anyway) it looks more
like an AI / Parser type problem.  It may be worth looking at some parsing
code to look at the datastructures they use.

steve boyle

----- Original Message -----
From: "Fritz Lehmann-Grube" <lehmannf@math.TU-Berlin.DE>
To: <pgsql-general@postgresql.org>
Cc: "Erhard Zorn" <erhard@math.TU-Berlin.DE>; "Tilman Rassy"
<rassy@math.TU-Berlin.DE>
Sent: Thursday, January 17, 2002 10:14 AM
Subject: [GENERAL] How to model an AND/OR logic by relations ?


>
> Hi all,
>
> I'm setting up a DB in which ( besides normal relations ) an
> AND/OR-relation is to be stored:
> I have two tables "documents" and "atoms", where a document shall
> "depend" upon a logical expression of atoms.
> e.g.  (please widen your window for this)
>
############################################################################
###############
> ### ###
> ###  document1 "depends" on (atom1 OR atom2) AND (atom1 OR atom3 OR
> atom4) AND (atom5)  ###
> ### ###
>
############################################################################
###############
> This is after some normalization - I could also have (.. AND ..) OR (..
> AND ..)
> I don't have NOT's.
>
> My idea is to have have two extra-tables:
>
> CREATE TABLE or_groups(
>        id ... primary key,
>        document_id ... references documents
>        );
>
> CREATE TABLE dependencies(
>        or_id ... references or_groups,
>        atom_id ... references atoms
>        );
>
> with ( mapping the example )(please widen your window for this)
>
> | documents |      | or_groups       |
> | id | name |      | id | document_id |
> | 1 | document1 |      | 1 | 1       |
> | | |      | 2 | 1       |
> | | |      | 3 | 1       |
>
> | dependencies |      | atoms       |
> | or_id | atom_id |      | id | name       |
> | 1 | 1 |      | 1 | atom1       |
> | 1 | 2 |      | 2 | atom2       |
> | 2 | 1 |      | 3 | atom3       |
> | 2 | 3 |      | 4 | atom4       |
> | 2 | 4 |      | 5 | atom5       |
> | 3 | 5 |      | |       |
>
> Because I'm a novice in Databases and the idea was pure naive intuition
> I want to ask, whether this is a standard problem and whether there is a
> standard solution.
> Am I running into problems ?
> Do bad things happen when "documents" and "atoms" are the same table ?
>
> Greetings and Thanx    Fritz
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>