Rule Engine in JavaScript [closed]
Asked Answered
P

3

23

Is there any Rule engine in JavaScript?

The question is in this context:

  • Consider a web application having a form that users fill up.
  • As a user fills up each field and proceeds to the next, business logic written in JavaScript controls the visibility(and other attributes) of form elements further down the page.
  • The same business logic is also applied at the server side after the form gets submitted, albeit, in Java to guard against any mishaps/manipulations at the browser side.

  • Now, Wouldn't it be nice if we have a JSR 94/Drools/JRules like rule engine that would execute rules in both Java and in JavaScript? With such a rule engine, I can avoid hard coding my rules, and I also retain the flexibility of having client-side as well as server-side validation

(PS: I've tried the AJAX route and seen that the application becomes a lot less responsive, making it hard to sell to users who've been accustomed to a hand-coded, pure-javascript version.)

Pym answered 5/10, 2011 at 12:31 Comment(2)
I can't help it if I keep getting brain lock thinking "O'Doyle rules" from the 1995 Billy Madison movie...Epinasty
There's always the possibility of server-side JavaScript. I believe there are JavaScript engines that run within Java, and there's also Node.JS or similar.Jaal
B
8

JSR-94 is a Java specification, so I don't see what it has to do with the browser.

There's Google JSON rules:

http://code.google.com/p/jsonrules/

You said you tried the AJAX route. Does that mean a rules engine running on the server and an asynch call to access it?

Barnett answered 5/10, 2011 at 12:35 Comment(3)
Yes.. I was wondering if someone had a product that implemented JSR-94 in JavaScript as well.. and by "AJAX route", I did mean a rule engine running on the server and the JavaScript invoking XHR requests (for example, via the DWR library) to talk to itPym
Project has now been closed, was still in Beta.Hoxsie
Were you connected with it, Ted? Sorry to hear it. I wonder if it was used much?Barnett
T
4

This is a valid question. From this article, JSR 94

does not standardize the following: The rule engine itself The execution flow for rules The language used to describe the rules The deployment mechanism for Java EE technology

Thus, it may be possible to use a DSL that could be executed on the client and server, and this could be developed, executed, and managed as per the JSR 94 architecture. Or not.

Another article Creating a simple rules engine using the Java scripting API employs JSR-233 plus other stuff to create a rule engine system. This however predates the JSR 94.

I got to this stackoverflow page since I too was looking for a solution. Currently, I have a page to validate on client side where groups of fields can trigger different validation rules, and requirements are changing. To write this in imperative code just creates a mess with high cyclomatic complexity.

However, the easiest thing to do is use one of the many JavaScript form validation libraries out there. Still looking.

Thorlie answered 2/7, 2014 at 4:40 Comment(0)
S
1

Since the javascript lives in the browser, it's fairly easy for a user to check your source code and bypass any js validation mechanisms. That's why it's usually done server-side.

It's a pain but I usually implement validation both in javascript and on the server, that way "normal" users will have a quick response, and "hackers" will be kept out of the system. Unfortunately I think that's the way you need to go if you want both good user experience and good security.

To answer your question, to my knowledge there's no common library that can be used both on the client side and the server side.

PS. remember JavaScript is not Java ! :-) http://en.wikipedia.org/wiki/JavaScript#JavaScript_and_Java

Shalon answered 5/10, 2011 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.