# What algorithm can I use for a rule based system? [closed]

I'm doing research for a system that is based on rules. The goal of the system is to fire when some of the rules are true.

Explanation (requirements/working of the algorithm):
The algorithm will run in the background of an app. There will be a lot of data available about the user. The algorithm will apply the defined Business Rules to the data. When an X amount of the rules result in true, the algorithm should fire. The goal of the algorithm is to fire when the user shows any possible problems (based on data / the business rules that result in true).

Examples of Business Rules:

IF a user is in the app between 2-5AM more than 5 times in 2 weeks THEN the user is not sleeping at the correct times.
IF a user moved house twice THEN a user moves house a lot

The Business Rules are in the If condition(s) then action format. The condition can be of any type. It would be really nice if it will be "easy" to add new forms of conditions in the future and the algorithm can handle such changes.

I'm looking for an algorithm that can be of use for the goal described above. Preferably without the use of Machine Learning. So I'm looking for an up-to-date/newer version of an Expert System.

When I did research, almost every time I came back to an Expert System. I think it perfectly fits my requirements. However I can't find anything recent on Expert Systems. It looks like it is outdated, as mentioned in this post, four years ago.

I saw something like an Recommendation System in this post. Still I don't think this will solve my problem because it focuses on recommendations...

Does anyone else have ideas for an algorithm?

EDIT: Tried to add useful information

## closed as unclear what you're asking by David Richerby, Evil, Discrete lizard♦, quicksort, vonbrandApr 25 '18 at 20:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• It's not really clear to me what this algorithm is supposed to do. You've not given any precise description of it. At the most basic level, when you say "an algorithm that can be of use", do you mean "An algorithm that will determine which rules to fire in each possible state of the system"? If so, that's going to depend very much on how the rules are written and how powerful they are. – David Richerby Apr 19 '18 at 16:05
• Sorry, I updated my answer. I hope it is more clear now. @DavidRicherby – 476rick Apr 20 '18 at 9:01
• Not really, unfortunately. So the rules are of the form If condition then action but it’s not clear what form the conditions can take. Your example conditions seem to involve some fairly complex functions of the state, e.g. implicit events (user logged in at a certain time), counting etc. – David Richerby Apr 20 '18 at 10:09
• As I added in the question, it would be nice if it is possible that the algorithm can handle many different types of conditions. @DavidRicherby – 476rick Apr 20 '18 at 11:04

You don't need an algorithm. Since you already have a set of rules, all you need to do is implement the rules.

The simplest implementation strategy is to simply re-evaluate all the rules each time something changes. However, in some cases this might repeat work unnecessarily and there might be more efficient implement the rules.

For instance, if one of your rules is "IF the user has bought more than 1000 items today THEN sound an alarm", then re-evaluating this each time an item is bought might be unnecessarily inefficient. A more efficient approach would be to keep a counter for each user that keeps track of how many items the user has bought today; each time an item is bought, increment the counter and sound an alarm if it is over 1000; and each midnight, reset all counters to zero.

So in some cases optimizations might be possible. However, that is entirely depend on the specific of the rule, and there's not much we can say in general. If you have a question about how to implement a specific rule efficiently, you can ask a new question specifically about that particular rule.