Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Programming Technology

Building Intelligent, Rule-Based Applications? 57

Donald Hughes asks: "What are good approaches for building intelligent, rule-based applications? In particular, I typically build ASP.Net applications. I came across P#, which translates Prolog into C#. I also came across NxBRE, which touts itself as the first open-source rules engine for the .Net platform. Currently, the intelligence in my applications consist of data stored in the database, which is either processed in T-SQL or in a C# class using linear conditional logic. This approach works fine to a point, after which the complexity of the if/then statements becomes extremely difficult to manage. This is especially problematic when returning to a project after several months of not looking at it, or when someone needs to be introduced to it for the first time. Does anyone have some helpful advice?"
This discussion has been archived. No new comments can be posted.

Building Intelligent, Rule-Based Applications?

Comments Filter:
  • Generator? (Score:3, Interesting)

    by Usquebaugh ( 230216 ) on Wednesday June 15, 2005 @07:02PM (#12828132)
    I'm not an AI or Prolog type person. But if there are a lot of conditional statements that are based upon some know steps then generating them might be an idea.

    Of course I tend to be slightly biased towards generators as they save me days of coding and testing in my current job.
    • Why would you write rules that get translated into an intermediate language when you could write what academics call a "compiler" for translating the rules directly into an executable or an "interpreter" for evaluating the rules on the fly? Generators are a sign of an inflexible, poorly designed language (I should know: I get paid to write C#!).
  • P# / LISP (Score:3, Interesting)

    by johaness ( 792198 ) on Wednesday June 15, 2005 @07:22PM (#12828286)
    there is a prolog compiler for .net around:
    http://www.dcs.ed.ac.uk/home/jjc/psharp/psharp-1.1 .3/dlpsharp.html [ed.ac.uk]

    as you can implement a prolog interpreter in 30 lines of lisp, you might also want to check lisp.net compilers ;)
  • by Anonymous Coward on Wednesday June 15, 2005 @07:24PM (#12828308)
    I'm not sure I understand the question but you definitely don't want if/then statements in rule-based programming. You basically want to create a separate function for each "if" and each "then", and then declare the relationship between them.

    For example (I'm just making this up off the top of my head, and using Ruby syntax), you might want to call "customer.save" to send a customer object to the database. You can't call this function until the customer object has had certain things happen to it, for instance "not customer.empty?" must return true. "customer.agreed_to_terms?" must return true. Each of these tests might itself depend on other tests, and so forth. And some tests might need input from the user, or the result of a complex calculation, or whatever.

    Just write a function for each and then write some generic code that doesn't call a function until all the dependencies return true. This is like, 5 lines of code.

    You can configure the relationships with a simple text file. This is a very powerful system. An analogous system for shell scripts would be Makefiles.

    This is super-easy in Lisp, Ruby, etc., not sure about .NET languages, but I'm sure it can be done with a little reflection (pun intended).

    This is a really easy and powerful concept once you figure it out. Basically your code will "discover" the proper order to do things in based on the dependencies.

    A further step is to encode the state of your app in a tuple (an array), and use tuple spaces to distribute work among many machines. It's quite magical when you've got it all working right.

    Study the prolog examples as well, Prolog basically generalizes this procedure even more.
    • Yes. Re-invent the wheel. A wheel that weakly does something specialized languages do extremely well. Let me go get that hammer to do the welding while we're at it.
      • Yes. Re-invent the wheel. A wheel that weakly does something specialized languages do extremely well. Let me go get that hammer to do the welding while we're at it.

        You're exactly right: Most languages have very weak support for that kind of dynamic, introspective, functional programming. It would be ugly in C, for example, and impossible in Fortran. (In theory, everything is possible in every Turing-equivalent language. But eventually you blow out all your tendons and have to be put in a madhouse.) You s

      • Let me go get that hammer to do the welding while we're at it.

        Actually - this is possible - it is called (IIRC) "forge welding" - basically, you get both of your pieces of steel super red hot, place them one on top of the other on an anvil, and pound them as hard as you can with your blacksmith's hammer. This may require more than one person to accomplish, and you need some good strength, because you have to pound that shit HARD. Plus, you need to keep it red hot as you work it. In the end, though, with a

        • Funny? Why is this post modded as "funny"? Do the mods think that the method of "forge welding" is something that I picked out of thin air after playing one too many sessions of WoW? Not that I have ever played that game; I actually have a life and I learn about and practice real-world skills, like welding!

          Need proof that this is a real form of welding? HERE IS YOUR PROOF [dfoggknives.com]

          A couple of excerpts from the site:

          Forge welding is the core technique involved in creating Damascus steels. It is a solid-phase bondi

    • What is it with script programmers with delusions that their languages are panaceas? The languages are created for rapid prototyping and tiny applications, then they get crufted into larger and larger domains. Repeat after me:

      OO is not the one true paradigm.
      Weak and Dynamic is not the one true typing.
      Interpreted is not the one true execution mode.
      My language is not the one language to rule them all.

      This could be a mature application with business analysts and real money on the line, he doesn't care about

  • Book (Score:3, Informative)

    by bearclaw ( 217359 ) on Wednesday June 15, 2005 @07:53PM (#12828544)
    You might want to check out Enterprise Patterns and MDA : Building Better Software with Archetype Patterns and UML (Addison-Wesley Object Technology Series) by Jim Arlow and Ila Neustadt. They present a decent model to describe rules.
  • What's intelligent, rule-based application design? Is it different from pattern design?
    • Roughly (and correct me if I'm wrong), a rule-based application is one that stores formulae, business/validation rules, process orchestration steps etc as dynamic data, usually in a database.
      The application will load and interpret the rules in order to perform processing functions; and the rules can be modified at runtime, typically by expert users or adminsitrators.
  • Finite State Machine (Score:4, Informative)

    by the eric conspiracy ( 20178 ) on Wednesday June 15, 2005 @08:12PM (#12828665)

    Depending on what you are doing a finite state machine could be a good answer. Take a look at the state machine compiler (SMC) on sourceforge.

  • Mercury might almost be considered a dialect of Prolog, and offers a .NET compiler, in case you need it. http://mercury.sf.net/ [sf.net]
  • basic question (Score:3, Interesting)

    by astrashe ( 7452 ) on Wednesday June 15, 2005 @11:53PM (#12829750) Journal
    What kinds of problems would you solve with a prolog type system?

    I've read about prolog, going back for years (it was a scare, a long time ago, in the '80's, I think -- the japanese were spending government money on prolog type logic systems, and they were going to bury us), but I've never really understood how it plugs into the real world.

    I mean, if you're doing something practical, what does it do for you?

    Even this question is pretty vague... I don't have any idea of what the poster wants the thing to do.

    • Re:basic question (Score:3, Informative)

      by dlakelan ( 43245 )
      Rule based programming's purpose is to solve a problem that requires complicated logic by declaring what the relationships are, instead of describing a procedure that solves the problem.

      The advantage in these sorts of uses is that you can reason about the declarative program much easier than you can about the set of steps that you would write down in a procedural language.

      SQL is a kind of declarative language that you might be familiar with. Although it is much less powerful than say prolog, its advantage
      • I understand that (sort of), but could you give an example of a specific problem that fits this type of solution?
        • Re:basic question (Score:5, Informative)

          by darnok ( 650458 ) on Thursday June 16, 2005 @03:55AM (#12830522)
          How about this: consider an institutional currency trading system, such as those used by banks to hedge their currency positions.

          You need a set of rules about when traders can go ahead and execute a particular trade. For example, if the bank already has a huge stash of Japanese yen, they might not want to buy any more (regardless of the price); they'll have a particular risk profile that each trade must fall into.

          Now, the people who write the code to check how much the bank holds in Japanese yen at a particular point in time, will almost certainly NOT be the people who create and maintain the risk profiles that the bank needs to trade under when it buys or sells yen. The first set of guys will probably be full-time "normal" programmers who reference stuff out of one or more databases; the second set of guys will be trading management type people. While there's obviously a level of synergy required between the two, they're unlikely to (want to) sit and work together on a regular basis.

          So far, it's not that unusual a problem.

          Where it gets interesting is that both areas tend to be very dynamic: the definition of a good risk profile tends to vary over time, and the way of calculating current positions in Japanese yen may also change over time (as the bank starts/stops trading in different exchanges, as the bank opens/closes trading rooms in different parts of the world and different timezones, and so on).

          Where rule-based apps shine in this scenario is that the two requirements can (and should) be largely separated. Provided the data coming from the data guys can be kept valid (which can be a huge challenge in itself), there's no reason for the risk management guys to be aware of where that data comes from or to question its integrity; they should be adjusting risk profiles via a rules-based mechanism rather than a programming mechanism that requires them to understand anything more than the actual values of the data.

          Their rules may include things like "if we're going to buy more yen, we have to sell Brazilian real because we believe there's a relation between the two"; sure, you could employ a regular OO-type coder to do this stuff, but it's much quicker/safer for the guys making such rules to implement them in a rules engine. In this scenario, when a trader puts in a request to buy more yen, it will have to be matched with the amount of Brazilian real that has been sold to see if the appropriate ratios exist to allow the yen trade to go through.

          Frequently, things get much more complicated than that, and maybe you'll have to deal with half a dozen other factors as well; this is a simplistic example that could probably be done with a few Excel macros and stored procs, but hopefully you can see how the complexity could scale to the point that a rules-based approach becomes significantly better than "traditional" alternatives as the problem becomes less and less trivial.
  • by Tablizer ( 95088 ) on Thursday June 16, 2005 @12:32AM (#12829952) Journal
    One approach is to have a database table with the rules in them. Example schema:

    table: rules
    ----------------
    ruleID
    ruleDescript
    expression // example: "x > 3 and y == 7"
    ifTrueSnippet // what to execute if true
    priority // order of execution

    This allows one to easily create a log of the of the rules and their result. Note that it takes a fairly dynamic language to impliment this so that it can execute code snippets from a database. You probably would want to go with something like Python or PHP instead of C++.

    However, I am skeptical any such tool can be as flexible as just plain IF statements in code. But it may be useful for non-programmers, yet power-users to manage the rules themselves. The "ifTrueSnippet" may instead be predefined operation names rather than code in that case. And, maybe a parameter column or two may be in order, depending on the domain.

    -T-
    • Note that it takes a fairly dynamic language to impliment this so that it can execute code snippets from a database

      Hence you'd probably want to write it in lisp so you can do away with that whole useless data vs. code dichotemy ;-)

      You can probably find some decent rule-system implementations floating near academia if you dig.

      • Yes, but Lisp tends not to have good RDBMS drivers and tools for it; and also it's native expression syntax is too foreign to most power users. People grow up in an infix world for good or bad.

        Hmmm. What about TCL?
        • Lisp might not be good for RDBMS, but the close correspondance between S-expressions and XML might be extremely helpful if the application is distributed. And anyone who is willing to code rules in Prolog should have no problem with prefix syntax and higher-order functions.

          High-end programmers should have no problem switching between prefix and infix: it's only cog-like code monkeys who insist that the most popular languages have C-like syntax. Experts will continue to implement obscure applications in obs
    • I haven't thought this through, but after reading your post and following through on the path of another post, I came across the following regarding the Rete alogorithm [wikipedia.org].

      The rete algorithm appears to relate to creating a tree structure where each node is a rule and each leaf is a function. So, as the process goes from node to node, and the process evaluates rule after rule, the process comes to the final leaf and calls the desired function.

      Celko's books deal with the implementation of trees in databas
    • Your not suggesting he move away from .NET to PHP are you to perform better business logic?
  • If you have a complex enough set of rules, it might be better to pony up the licensing costs for Ilog [ilog.com]. It's native .NET code, and very capable.
  • Regression testing (Score:2, Interesting)

    by EvilAndrew ( 581758 )
    As other people have suggested, one path is to invest in an expert system. You haven't mentioned how many rules your expert system consists of, and you may switch to some other expert system to find that it hasn't improved your capability to manage your rules. Regardless of whether you switch or not, I cannot overemphasize the importance of having a good suite of regression tests for your rules.
  • Java Rules Engines (Score:4, Informative)

    by Nazrax ( 131272 ) * on Thursday June 16, 2005 @07:31AM (#12831057)
    I know the original poster typically builds .NET applications, but for those times when you want to do this in Java, there are at least two good engines: http://www.mandarax.org/ [mandarax.org] is a backwards-chained engine (like Prolog), and http://www.drools.org/ [drools.org] is a forward-chained engine (takes action based on input).
  • I haven't used these in real life, but they seem like they could be quite useful in your rule-based situation:

    Recursive queries in SQL Server 2005 [sqlservercentral.com]
  • http://www.ghg.net/clips/CLIPS.html [ghg.net] It's C based, mature, multi platformed and easy to interface to (if you can code in C). Currently it has interfaces to Python, PHP and Java.
  • I'll keep it short.

    1. there is no way to completely get rid of if/then statements. they'll be somewhere. What's good with CLIPS or so is that there is not much else than the if/thens of the rules, so you don't get lost in memory allocation and the rest of the bull. On the other hand, the rules (and facts) tend to be localized in the same file, you don't have to read 200 classes of assembler (:-)) files to find all of them

    2. depending on the nature of dynamics of your rules , if you don't have that man
  • Who is going to maintain the code you write? Is this just for you, a company you work for or a company you are starting?
    If it is just for you it really does not matter.
    If it a company you work for you may want to check. I had a guy working for me that wanted to write everything in FoxPro because that is what he knew. I told him no but he wrote some little utilities for a different department. He left and no one knows how modify his code. That goodness they were trivial.
    If it is a company you are starting. W
  • Rules Engine... (Score:2, Informative)

    by Trubetskoy ( 892700 )
    I am a PROLOG practitioner and I work in the field of AI, specializing in complex systems. Prolog is an excellent choice when the rules become unusually complex. You don't need a translator or compiler if you use Amzi! Prolog. You can generate compiled modules and call them via a DLL. No problem there. Check them out at www.Amzi.com. As to maintainability, any complex system can become formidable. Prolog will make long-term maintenance easier, not harder. Let me know if I can help. Best wishes, Burt Pi
  • Depending on what kind of logic you're trying to code up, you might find it easier to create a domain-specific language [wikipedia.org] that allows coding in whatever paradigm is most appropriate rather than having to shoehorn everything into logic-language assertions. Haskell is considered one of the best languages for create DSLs (my favourite example is a DSL for financial contracts [google.com] which I was reminded of by this post [slashdot.org]); however, like most non-Microsoft, non-trivial languages, it is very difficult to port to the .Net CL
  • "CLIPS: A Tool for Building Expert Systems"
    http://www.ghg.net/clips/CLIPS.html [ghg.net]
  • Very rarely the answer is Prolog.

    For the rest of cases, building your own rule processing engine that serves a particular purpose and interacts with the rest of the system, is such an easy task, it is not worth generalizing.

    For $deity sake, netfilter has some (simple) rule-based processing, and it's merely a small piece of code inside the Linux kernel.

He has not acquired a fortune; the fortune has acquired him. -- Bion

Working...