g0n has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently spec'ing out some code, and there is a requirement to place dependencies on database entries, i.e.:

I want to place this logic in a database table, so that the logic is configurable, and in a human readable format. The obvious way is with statements like:

I've dug around cpan with all the search terms I can think of, and can't find anything that does anything like this. Does anyone know of anything appropriate?

--------------------------------------------------------------

$perlquestion=~s/Can I/How do I/g;

Replies are listed 'Best First'.
Re: Human readable logic statements
by sh1tn (Priest) on Sep 19, 2005 at 11:33 UTC
    While there are not so many mature NLP CPAN modules, maybe SQL::Abstract can help.


Re: Human readable logic statements
by graff (Chancellor) on Sep 19, 2005 at 17:37 UTC
    Personally, I think your conditions need to be stated a little more clearly (e.g. "has date within x months" means what, exactly? Months from now? Months ago? Months before or after some date specified in the entry to be made?)

    If you can express these conditions in sql, with suitable placeholders for the actual values required for the criteria, then why not just store the sql statements themselves in a table (with placeholders)? That way, a perl script could simply query to get the relevant sql statement, then prepare it and execute it with the appropriate values to see whether a given insertion should be allowed.

    (SQL statements are human-readable enough to be meaningful and clear in most cases.)

    Nitpick: don't think that just having the same phrase structure on every condition is sufficient to make your conditions understandable -- double and triple negatives are just hard to follow, and you should avoid them. How about:

    -- Entry can be made if (select count(*) from data_table where date>? and date<?) > 0;
    and so on. Perhaps there are other conditions needed to identify your "other record x", "other record y", "other record "z", etc? Putting the conditions in SQL form will help to make sure you use the appropriate amount of detail.
Re: Human readable logic statements
by sauoq (Abbot) on Sep 20, 2005 at 00:52 UTC

    I agree completely with graff. Don't try to create a new language when an existing one will do nicely and especially not when using the existing language will result in less work both for you and your application.

    If, however, you really feel that creating your own is a requirement—perhaps because some completely non-technical person will have to write or modify these condtions—I would suggest trying to avoid compounding words like 'indate' and 'notexist'. That's neither flexible nor very readable. Why wouldn't you use 'not exist' or 'in date' instead?

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Human readable logic statements
by g0n (Priest) on Sep 20, 2005 at 09:28 UTC
    Thanks for your replies. First of all I should say that it'll be a while before I need to implement this, I just wanted to check whether something already existed, and do a little research.

    I did consider using SQL for this, but as sauoq has suggested, the statements will need to be edited by a completely non-technical user. graffs suggestion of storing the required sql statement is a neat one, but I think the end user would probably stumble over the syntax - ideally I'd like to make it as simple to read as possible. As far as the criteria go, the compound statements are probably a bad idea - once I get to actually structuring the syntax, I'll bear that in mind, thanks.

    Finally, would this functionality be useful enough to others that it's worth bundling separately and uploading to CPAN if and when it gets implemented?

    cheers.

    --------------------------------------------------------------

    $perlquestion=~s/Can I/How do I/g;

Re: Human readable logic statements
by artist (Parson) on Sep 21, 2005 at 01:04 UTC
    On a paper, write your statements and convert them into SQL. Now, place them side by side and you will see the Logic. Expressing with standard computer language is sometimes better than English Language. SQL editors would allow you to write these expressions in configurable way. You can always put human readable items into comments.
    --Artist