FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
TABLE OF CONTENTS
May 26, 2007

Rule-Based Programming in C

(Page 4 of 4)

Yet another way to control the execution of the rules is to break them into several modules that are called as needed by the main program. Listing Three is an example of this technique. The first function, air_land, decides between driving and flying and returns to the main program. The main program then calls either fly_rul() or drive_rul() to choose a specific vehicle. The module to call next is chosen by the main program on the basis of the information on the global variables.

As always, this sort of modularity improves the clarity of a program. It also increases speed by eliminating sections of rules from consideration.

#include "stdio.h"

int av_speed;

char like_scenery = 0, is_pilot = 0, fly = 0, drive = 0, fly_com = 0, fly_tcart = 0, fly_bon = 0, m_cycle = 0, car = 0;

main() { int done, distance, /* in miles */ time; /* in hours */ char c; char str[10];

printf("This is a program to help you with travel planning.\"); printf("\n\nHow far are you going? (miles)\n"); gets(str); distance = atoi(str); printf("\n\nHow much time do you have for the trip? (hours)\n"); gets(str); time = atoi(str); av_speed = distance/time; printf("n\%d\n", av_speed); printf("Do you prefer scenery over speed? (Y/N)\n"); gets(str); if(str[0] == 'Y') like_scenery = 1; printf("Are you a pilot? (Y/N)\n"); gets(str); if(str[0] == 'Y') is_pilot = 1; air_land(): if(fly) fly_rul(); if(drive) drive_rul(): if(fly_com) printf("\nFly COmmerical."); if(fly_tcart) printf("\nRent a Taylorcraft and fly low."); if(fly_bon) printf("\nRent a Bonanza and fly high."); if(m_cycle) printf("\Take your motocycle and ride the back roads."); } air_land() { if(av_speed > 60) { fly=1; return; } if(av_speed <= 60) drive = 1; } fly_rul() { if(fly && !is_pilot) fly_com = 1; if(fly && is_pilot && like_scenery && av_speed < 199) fly_tcart = 1; if(fly && is_pilot && (100 < av_speed) && (av_speed < 200)) fly_bon = 1; } drive_rul() { if(drive && like_scenery) m_cycle = 1; if(drive && !m_cycle) car = 1; }

Listing Three.

In the module air_land(), the first rule causes a return form the module if it fires. This is valid because the average speed cannot satisfy both of the rules in the module. It is desirable because we gain a bit of speed by not considering the second rule of the first one fires.

Another method of accomplishing almost the same thing as separately called modules is to include a state requirement in the IF portion of each rule. Some means must be provided for changing to the next state, typically a line in the THEN portion is some of the rules

Not every rule will need to cause a state change. For example:

if(state== air_land
&& av_speed < 60) {  
   drive = TRUE
   state = choose_ground_veh;
}

It is also possible to change states at a certain point in the execution of a rule module without reference to the data. In that case, a rule such as this one would be used:

if  (state == air_land)
     state = choose_ground_veh;

This rule would probably be used at the end of a section of rules, organized so that it could not be reached until the state change was appropriate.

Going Further

The ideas here are best used for simple jobs or as a foundation for a study of more complex cases. However, do not overlook the fact that the techniques demonstrated in these examples can be used for a very large but uncomplicated jobs. They could be embedded in a C program to solve a classification problem or replace a complex decision tree.

To learn more about the subject, see the references. You might also want to experiment with the demonstration programs offered by several of the companies that sell expert systems shells. I found the EXSYS demos useful.

Courtesy AI Expert

Previous Page | 1 Introduction | 2 Rules in C | 3 Control | 4 Another Way to Control Execution
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: