Outlook-style rule engine - php

I am trying to construct a rule-based system for interpreting data. However, I am having issues deciding on a way to construct the logic for storing and interpreting rules.
Currently, there is a database structure that quite complex, but will deal with all aspects of storing the rule data. The idea is that the system will be able to mimic the way that MS Outlook allows a user to add rules.
My problems are as follows:
What pattern should I be using to store these rules inside objects?
Should I use eval() or proper object orientation to execute the rules?
And example rule might be:
Dog must have collar in area park
Where each element (dog, must have, collar, in area, park) is a separate piece of logic to be interpreted.
Any general advice to the above questions is much appreciated!

I've never implemented a system like you describe in a "real world" context, but I have played with them a considerable amount as hobby projects. My preferred approach is to use some kind of logic language like Prolog to make assertions and check them. You'd have assertions for where the park is, what it means to have something, what a dog is, and then you'd make a rule pretty much exactly like your example in parentheses at the bottom of your post. I'm sorry my Prolog is too rusty to give you a useful example... I've been playing with home-grown inference languages too long.
There are Prolog interpreters available for embedding in most languages, though I'm not sure about PHP5. You could throw together something simple that does forward-chaining inference on rule data structures of your own creation in fairly short order, if you can't find a Prolog interpreter. You may be interested in these notes on automated inference.

Probably this answer is too trivial / obvious for you but I just thought on how I would solve something like that in my current existing project which is a Zend Framework application. I thought of the filter- and validatorChains ZF uses. I assume you have a finit number of possible input objects, a finit number of conditions/constraints and a finit number of actions. In that case you would first instantiate an object and run it against a chain of conditions(validators), if all the conditions are satisfied you run the object against the actionChain. Here I would probably have to implement some kind of action priority system since some actions have to be carried out before others. Like 'sending a notification' and then 'delete' the object in question. So in ZF I would build a custom validator for every condition/constraint. I don't believe the Outlook system is very intelligent meaning that I don't think the validators are very generic.
In the db there could be a table for actual rules, one for the conditions and one for actions. Then there could be two many-to-many tables linking the rule with all needed conditions and actions.

Related

PHP Separation of Concerns and MVC

To start with I'm not exactly sure if this question will fit the question model of SO so moderators please close it if it doesn't fit...
I have been reading a lot on MVC and SoC lately and how they relate to programming in PHP and am having some difficulty grasping how the concepts differ to my current programming style. Any application I write uses url_rewrite routes, with the routing handled by a single file which selects the appropriate controller.php file based on the sub system requested. Then within that controller file the final smarty template file is assigned and the PHP file which contains the business logic of the page in question is included into the stack.
The issue I am having is that while I review MVC and SoC information all the examples I see are written as extensive inter-dependant classes and some rather deep namespaces, but when I code I only use objects where I need an object for reusable but distinct code (in line with object examples on the PHP site itself), and so wind up with some code being classed and namespaced nearly 70% of my application remains in the global namespace with no classing. It seems to me that this coding technique still meets the design principal of separation of concerns, though i'm not so sure about being MVC as every example of MVC I can find is built solely out of a very large number of classes.
Could someone please explain if I am way off base here or if I am achieving SoC and possibly even MVC with my current coding and explain to me if embedding the entire application within a range of classes is a requirement for SoC and MVC.
Thanks
OK I usually take on these open ended questions :P
Let me rephrase your whole thing to a new question – and feel free to tell me I am wrong!
I think you are asking - “Is my coding style in-line with best practice for x y z?”
Now look – i have been in a variety of roles, and I strongly believe that no one has ever cracked a complete SoC architecture – at some point there is always a trade-off between enabling someone at the view end to do their job, and someone upstream ingesting the input and making it work such that it ties to the database and logic.
For example - I am actually right now building a system that takes HTML files as input, reads them via PHP. PHP converts the HTML elements to a bunch of JSON and adds logic to that JSON based on x, y and z, then shoves it out to Facebook React components – which converts it all back to HTML / DOM – when you explain to someone that there is a sausage machine that takes, as input, HTML, and outputs HTML, you can see why they might go – holy shit what are you doing!!
Now – why did we do this? Because it made sense for the project, and it made sense for the team. We could have equally used some pre-defined framework and blah blah blah – however this worked for us.
(a caveat here would be, if you need a highly performant application, this might be the wrong approach, however – bear in mind that what you read (inter dependant classes etc.) may also not be performant – optimisation of PHP code is hard work – my advice here is, get it working first, then if the product is successful, pay someone to fix your shitty code – server time is cheap – your time is not)
My statement for you would be, without concrete examples and use cases – people will struggle to comment on your approach – but understand that there are many different ways of doing things and you may see much open source code written in one way or another, but if you write something that achieves a goal, your only concern should be that it is performant and that it has ‘enough’ separation such that your designer can work on design, your coder can work on code and your CEO can look at sales figures.
Hope this helps.
From the information you've given, you're doing a good job. I think you have a grasp at Separation of Concerns and probably MVC. I think you should take a closer look at how things are abstracted in other codebases, how things are modularized, and think about how you would handle things that potentially come up in web development.
What if I want to have some code run before every controller? Or middleware that runs between the request and the controller? Or inject a variable into the view after every controller is processed? How are you separating POST, GET, PUT, DELETE logic but still grouping them within a resource? Some of these can be achieved with your architecture but I think many of the solutions would be cleaner with a class-heavy architecture. Of course, something being cleaner is in the eye of the beholder.
Frameworks released to the public try to be generic and tackle as many use cases as possible. One issue I see is that you make the assumption that the controller file would be called last and then setup the view. So, even though the logic in there is in the global state, it only exists within that file. In essence, the file acts as a namespace. I don't think that's an assumption a generic web framework should make, but it's something you can get away with when writing something for yourself.

Are action parameters bad design/architecture?

This question can be applied to any programming language, but as I am thinking of PHP, I will phrase it accordingly...
I'm wondering if it is considered bad design/architecture if a web application uses action parameters, versus seperate files for each action.
For example:
/index.php?action=edit
Versus
/edit.php or /index/edit.php
I know mod_rewrite can translate a pretty-url to a parametrized url, but I try to avoid uneeded complexity when not necessary.
Thanks.
Quite often, for big applications, (especially with Frameworks, such as symfony, Zend Framework, ...) we tend to use one entry point : index.php.
That entry point will receive some informations (like your action parameter), that will allow it to route the request to the correct controller (or any equivalent you might have).
So, to make things short : no, using action parameters is not bad design / architecture.
Of course, this depends on the kind of application -- but, generally speaking, have a unique entry-point is quite a good idea.
Well I don't think it is a bad design - of course there is other possibilities - but overall it is about your in-house agreements between the programmers how you do it. As much as you can you should split the PHP and HTML code to make the development easier further on.
I prefer the MVC-coding style, which splits the PHP and HTML from each other as much as you "want it to".
Hope this is helpful :)
I would call both your examples at least outdated or short of best practice.
/index.php?action=edit
Doesn't look good and is therefore not user friendly and isn't SE friendly either.
/edit.php
Means that there is indeed a single file for each action which clearly is bad practice in the 21st century where we have great MVC frameworks which enable us to get rid of this clutter and keep the concerns separated.
A good URL looks for example like that:
mysite.com/user/profile/edit
meaning where in the user module, the user-profile controller and the edit action.
There is nothing actually bad in either, both can be used all right
Separate files considered to be better for the small application, to avoid unnecessary complexity as you said.
Action way is considered better to serve complex applications featuring single entry-point working as a boot-strap, initializing all the site features first and then calling appropriate module.
I just have to warn you against using such action in silly way, doing include $_GET['action'] in the middle of main 'design' file. it's both insecure and unreliable.
It depends on your requirements and scale.
Using separate files is ok. However, you will probably find yourself copying code and not properly reusing code and techniques. It's easier to do this with small, ad hoc applications, but could very well turn into spaghetti code or a jungle nest over time.
If you use a single point of entry (class loading with url handlers), you do have to learn how that works (CodeIgniter and ExpressionEngine are good MVC systems to use if you're not real good at it yet), but it's more consistent in coding practice, and scales better than a bunch of separate pages or a switch() statement you pass everything through.
It's not a panacea, either way, but most professional operations use something like an entry point with a class loading system (such as MVC).
About using mod_rewrite: mod_rewrite was not created and should not be used as part of your PHP architecture.
About having one file per logical element. This is a very good and practical way of separating logical units in your app. It way better than creating huge files with a lot of mixed logic inside, which will become unmaintainable as the app grows. This has no contradiction on having one point of entry and an MVC architecture.
Having action parameters is the most normal approach for CRUD controllers for example where makes a lot of sense to group actions to a common controller
// blog controller
-> create blog entry
-> edit
-> view
-> delete
-> list ( this is a very common addition to CRUD
all this have a common architecture in the sense that almost all accept an id and do related thing.
If you are talking strictly about GET parameters than you'll see that creating medium/large application where you direct everything from a file and the only thing changing is the get parameters will outgrow you very fast. Computer architecture is like real architecture, try to split thing in small, simple (maybe reusable) units.

Looking for ideas on a computer science course project [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Hey. I'm taking a course titled Principles of Programming Languages, and I need to decide on a project to do this summer. Here is a short version of what the project needs to accomplish:
"The nature of the project is language processing. Writing a Scheme/Lisp processor is a project of this type. A compiler for a language like C or Pascal is also a potential project of this type. Some past students have done projects related to databases and processing SQL. Another possible project might relate to pattern matching and manipulating XML. Lisp, Pascal, and C usually result in the most straight forward projects."
I am very interested in web technologies, and have some experience with PHP, MySql, JavaScript, etc. and I would like to do something web oriented, but I'm having trouble coming up with any ideas. I also want this to be a worthwhile project that could have some significance, instead of just doing the same thing as everyone else in class.
Any ideas? Thanks!
EDIT: I really like the idea of a Latex to XHTML/MathML translator, and I passed the idea to my instructor, in which he wrote back:
"I think the idea is interesting, my question (and yours) is whether it is appropriate.
I think of LateX as a low-level mark-up language. I'm wondering if converting this to XHTML or MathML is really a change in levels and complexity. I think you can make your point with a little more discussion and some examples. You might also think of some other mark-up constructs which made it easier to describe equations."
Any ideas on how to convince him this may be appropriate, or any extensions of this idea that could work for the goals of my project?
Thanks for all the responses so far!
Hm, neat! Maybe:
1. A web-based language interpreter. eg, a very simple assembly interpreter in javascript, or a PHP-based C interpreter (PHP script reads C code, and executes it in some sort of sandboxed kind of way. Obviously it would only be able to implement a small subset of the C language)
2. Maybe some automated way to transform PHP data structures (like PHP arrays) into SQL queries, and vice versa. That kind of stuff has already been done, but you might be able to do something which (for example) takes an SQL query and creates the array datastructure that would be needed to "hold" the information returned by the SQL. It could support complex things like JOINS and GROUP BYs.
3. Maybe a C-to-PHP compiler? (or a PHP-to-C compiler, to be able to run simple PHP code natively. Use this with any combination of languages)
edit:
4. Maybe a regex-to-C parser. That is, something that takes a regex, and generates C code to match that pattern. Or something which takes a regex, and converts it into an FSM which represents the "mathematical" translation of that expression. Or the opposite - something which takes an FSM for a CFL and generates the perl-syntax regex for it.
5. Maybe an XML-to-PHP/MySQL parser. eg, an XML file might contain information about a database and fields, and then your program creates the SQL to create those tables, or the HTML/PHP code for the forms.
Best of luck!
I'd stay away from PHP and MySQL for a project like this. Both are commercial platforms that have compromised a lot of core CS principles in order to gain market share and solve user's problems. Given what you've described it sounds like the point of this project is to think about how programming languages are processed. Javascript The Language (not the browser API) might be a good choice here. Writing a processor/interpreter/compiler for Javascript or using Javascript itself to write a processor/interpreter/compiler for another language would meet the criteria for the assignment. Writing a Javascript "minifier" that removes all unnecessary white space (for smaller file sizes) while maintaining the program's functionality is another possible project.
Here's something I'd love: a PHP-based LaTeX-to-MathML translator. It wouldn't have to do everything, but if I could just cut-and-paste mathematical formulas written in valid LaTeX code into a window and have the script parse it and convert it into valid MathML, that'd be awesome.
Let me expand on this some more. The current state of scientific publishing on the web isn't great. Titles, headers, section numbers, tables, etc. can all be done in HTML, but for mathematical and chemical formulas which depend on precise two-dimensional formatting, scientific authors have only second-class options:
Publish their work in pdf format, which looks great but has a (comparably) huge file size and doesn't do hyperlinking well, or
Use something like latex-to-html, which converts formulas into .gif files (or some similar image file), which are semantically meaningless and thus doesn't lend themselves to indexing or searching.
Moreover, neither of these options allow for mathematical formulas to be generated programmatically, which would be helpful to the education community (think randomly-generated online homework).
Publishing scientific work in MathML would solve all of these issues, but it has a few of issues of its own, namely:
It's really too verbose to code by hand. I mean, you can do it, but c'mon.
The scientific community uses LaTeX for publishing, they're happy with it (for good reason), and they're not about to learn another mathematical markup language when they've got their own research and lesson-planning to do.
Browser support for MathML is currently pretty limited. I know this, and I don't mean to stick my head in the sand about it.
In other words: scientific authors know LaTeX, they use it daily, it's the de facto standard for authoring scientific content. MathML isn't and won't ever be the way math and science is authored, but it's the only semantically rich way to put hypertext mathematics on the web. Browser support for MathML is weak because nobody uses it; nobody uses it because it's too hard to write by hand. Now, maybe this is wishful thinking, but I have to believe that if it were only easier to write MathML, more scientists and mathematicians, especially the early-adopter types, would at least try it, and this would inspire browsers (especially open-source browsers) to improve their support, which would then lead to more authors using it, etc.
Here's where the translator comes in: Until the barrier-to-entry for MathML drops, it'll never be widely adopted. A simple LaTeX-to-MathML converter would take care of that. It would reduce the barrier-to-entry for MathML to near zero. If it leads to widespread use of and better support for MathML, it would be a major benefit to the scientific and education communities.
I finished this course last semester :)
IMHO the best way to go is to build an expression evaluator. build the simplest expression evaluator you can.
Then add these features in order as many as you like:
1- constant symbols, just place holders for variables. your evaluator should ask for their values after parsing the expression.
2- imperative-style variables. Like variables in any imperative language, where the user can change the value of a symbol anywhere in code.
3- simple control-statements. 'if-else' and pretest while loop are the simplest to consider.
4- arrays. if you really want your expression evaluator to be really like a programming language. It would be interesting if you add variable dimension arrays to your 'language'. you have to build a generic mapping function for your arrays.
Now you got a real programming language. To be a useful one, you might add subroutines.
so the list continuous:
5- subroutines. This is little harder than previous features, but it should not be impossible :)
6- build a simple math library for your new language in your language it self! and that is the fun part in my opinion ;)
Sebest book is a good book to overview famous imperative programming languages.
You shouldn't view creating an implementation of a particular language as insignificant. Everyone probably wants to be a famous programmer and not many people achieve it. This is a great opportunity to be familiar with very cool uncommon languages. (Lisp, APL, etc) If this is your first time creating a compiler/interpreter then it will also be a better choice to go with an already existent language (so you can see what design elements are needed to create a successful language.)
Significant ideas typically arise from necessity. People began using a language because they either needed it or it was a lot easier to accomplish the task they wanted to do. I don't think you will find the answer or the motivation to start a project from scratch here. That being said, I've always thought it would be cool to have a language that uses processor native byte code to create dynamic websites (without using something like cgi).
In response to your edit, here are some latex ideas:
LaTeX-to-ASCII pretty print, perhaps just for a small subset of TeX
LaTeX-to-Maple/Mathcad/Mathematica script, so that equations can be imported or edited or solved (don't know if that already exists)
Javascript LaTeX translator. basically, as you type, it does a translation from latex to html/css/.gif/whatever, so you can see your math "live" as you type it, kinda like the stackoverflow text editor.
Perhaps some sort of latex macros for expressing C code or something? Or how about this: often, C code is doing math: "det = (b*b - 4*a*c); det_sqrt = sqrt(det); etc" How about something which takes C (or java or whatever) code, which is performing a series of arithmetic assignments, and converts it into a nicely-formatted latex list of equations that are human-readable (ie, a \begin{eqnarray} block)
Or something that does the opposite: take a listing of latex computations or equations, and generates C code which declares the requisite variables, gets requisite user input, and performs the computations listed in your latex?
Why not write some sort of interface that can be interpreted/compiled down to the appropriate web technology of the users choice?
Or something like a Python to C compiler?
Just something I thought of recently: write a Ruby interpreter in Lisp.
Something that can be interesting to work on, is a regexp to automaton using Glouchkov's algorithm, here are some key features that can be implemented
Syntaxical analysis of regexp
Transformation into an automaton using Glouchkov's algorithm
Generating random phrases matching the regexp with that automaton / Validating phrases
Exporting automatons using XML
That's not a very long assignment so you may be able to handle it in a few months
You can try to make a scripting language in the vein of nadvsh if you want to do something interesting, but it might be too removed from what your instructor is expecting of you.
New Adventure Shell (nadvsh)
If you want to process language you can do a UIMA program. UIMA stands for Unstructured Information Management Architecture, it was developped by IBM at a cost of about 45Million dollars and is now available opensource. Basically UIMA is ascii codecs to analyse text documents to find patterns. It is made to find things where there is no order(finding needles in hay stacks). It uses XML and C.
The web is a rich area for doing work with languages. Take a look at a popular web framework like Ruby on Rails, and you'll find that much of its productivity comes from the fact that it implements a domain specific language well suited to web applications. Ruby just so happened to be a good language to implement such a language because of its dynamic nature, but the power comes from the language they created from it.
In your case, perhaps you could try designing your own domain specific language using a language that you are familiar with, such as PHP, to implement the essential core of a web framework:
routing URLs to pages
generating pages dynamically using a template (and maybe implement your own template syntax!)
connecting objects to underlying databases (object relational mapping)
If you are really ambitious, instead of building from an existing language, you could build your own language from the ground up (lexer, parser, code generator, etc) to do this.
You can ideas from this massive list.
Writing compiler for C or Pascal will likely take you months or years, if you are not compiler guru.
Write a simple web server. It will be fun and might prove useful as a simple and free solution. I once met a guy who said he did something like this and used for simple customer sites. Yours could become a useful thing as well.

Generic Declarative Expression Builder in PHP

Folks,
I'm looking to build a piece of PHP5 UI that I'm pretty sure is common to a bunch of applications. Basically, it's an expression builder that allows users to specify expressions combined through logical operators (AND/OR), like this:
FieldX > 3 AND FieldY = 5
FieldY = "bob" and FieldZ is not null
FieldX > '5/23/2007' OR (FieldY = 5 AND FieldY is not null)
Ideally, a system like that would allow me as a programmer to specify the list of parameters that the user can pick (columns) and the data type for each. It would also provide a nice, user-friendly interface for defining expressions like that - I'm imagining something like a table, where each row has several pickers:
[Column] [Condition] [Value] [AND/OR]
[Column] [Condition] [Value] [AND/OR]
...
Do you know of an open-source component that does something like that? Or maybe a part of this kind of functionality?
A word of caution. These types of generic expression builders are commonly thought of as excellent solutions to a variety of user problems. The thinking tends to go along these lines:
The user wants all these different reports
There's no way we're giving them SQL access (even if they could understand it)
We don't have time to write all these custom reports
Therefore, I'll make an easy way for them to write queries without having to know specific syntax, tables and fields.
This is not necessarily a bad idea, but what we have found at my company is that non-techie users have surprising difficulty understanding and building expressions more complex than a group of ANDS or a group of ORS. This is hard for us programmers to grasp, as most of us probably had an intuitive understanding of boolean logic even before we learned to program.
I would recommend that you not try to give them complete flexibility to create whatever expression they want, and instead you put together a UI that lets you the programmer define something more complicated on the back-end but gives the user simple choices on the front-end. This is easier said than done, obviously.
Remember - sometimes the difficulty for end users isn't the fact that they don't know the right syntax to express their idea in. More often it's because they don't even know how to formulate their thoughts in an unambiguous way, even if they were provided an easy way to do so.
NOTE: I'm not saying the end users are always morons - just that their minds may work differently from us crazy developers.
This isn't exactly a component, but you might take a look at the Conditions tab of the Shopping Cart rule builder in Magento for a solid PHP implementation of this type of functionality. It is customized towards e-commerce, so it is not a generic database overlay, but it does have a great condition builder interface.
I think this is a very interesting idea. Are you planning on working on this project for personal use or through work?
If you are planning on working on this project personally, maybe you should write it yourself and turn it into an open source project. It looks like there is already some interest in this area and it would be a good learning experience.
Otherwise, I can see how a company could find value in something like this. It would give the programmers another utility that could help them do their job a little bit easier and pay dividends in the long run.
Either way, this project is win. You will learn alot, and create something that other people find useful.
Check this out: http://www.codeproject.com/KB/custom-controls/SqlWhereBuilder.aspx
Download the client side JavaScript Library version of the component and customize it to your needs. Cheers.
I'd start by creating an object oriented query model. Eg. Criteria-objects. The next step is then to write a UI which allows manipulation of this model structure.
I've recently done such functionality by myself and, IMHO, it's easier to write your own implementation.
I think this is more related to UI than PHP in general. You'd better retag your question. Maybe you just want to parse these definitions in PHP, in this case I'd suggest using preg_replace_callback.
why dont try using smarty which is a template engine that. you just need to get and parse the user input.
Now that I'm thinking, do this in any way will need you checking the user input to avoid injections.

Extensible rule-based access pattern

I control access to some of my static web resources with some PHP logic. Because directory-based authorization through the webserver is not suitable or easily possible.
A combination of things determines whether access is granted or denied. And these rules change from time to time.
In the beginning it was a simple regex path match, and a check of one session variable. It's more complicated now as there's a few more variables involved.
I'm wondering how to go about refactoring this so it's quick and easy to change the rules. When it was a simple "if this AND this, then deliver, else 403." it was fine to do it in straight PHP. Now the conditions are more complex and there's a couple levels of nesting each with common but slightly different conditions within. This is all easy enough to refactor, but it's not the most intuitive and easy to update.
I'm thinking of one of two things.
Establish classes for each of the top level of conditions and use a Strategy Factory to pick the right one authorize. Derive them all from a base class containing the common bits and overload whatever's necessary. I think this could still be prone to some shuffling around when some conditions change.
Make a simple engine that iterates a 2d array of ordered rules sort of like firewall rules. Something like: <allow|deny>, <auth_group>, <path_regex>, <other vars>
I haven't fully thought this one through but it seems like it would be easier to update and also to read as a human.
What would you do? Is there an established pattern or library I can use for this?
I faced this similar problem in another app some time ago. Where I wanted an easy to update way of chaining rules and outcomes together based on several levels of conditions. This isn't as complicated as that app, but I'd be interested to hear about patterns people use to solve this kind of problem.
You might want to check out Zend_Acl, which is an object-oriented PHP class to manage hierarchies of privileges on hierarchies of resources. You might not be able to use this component as is, but it could give you some insight into how you want to implement your own system.
Zend_Acl is part of the Zend Framework PHP 5 class library, but like many classes in ZF, Zend_Acl can be used independently. There's no need to use the rest of Zend Framework.
You could also use a small logic engine (think prolog) to easily state facts and rules which would allow you to quickly query wether or not access to a resource should be allowed. Logic based rule engines like this are often very efficient and should allow you to very easily model a solution for this kind of problem domain.
I would use the Specification Pattern. It allows you to combine rules. You could even create a class to represent a compound rule that would be run (following the pattern) in the IsSatisfiedBy() method.

Categories