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
May 26, 2007

Rule-Based Programming in C

(Page 2 of 4)

Rules in C

In most rule-based systems the rules are written in a language resembling English, like the rule in the previous example, and the rule interpreter converts them into language the machine can deal with. To dispense with the rule interpreter, we are going to write our rules directly in C. To someone familiar with the language these rules will be easy to read, and judicious use of the preprocessor can make them even clearer. (Similar techniques could be used in many programming languages.) Here is the first rule from Listing One.

if (av_speed > 60)
 fly =1:

When the function rules() is called, the global variable av_speed has a value calculated from the numbers entered by the user. The variable fly has its initial value, which is 0. If av_speed is less than or equal to 60, the rule does not fire and fly keeps the value of 0.

Nothing here is different from a normal C program. We are just using different names and structuring things a bit differently.

#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 with travel planning. \n"); printf("\n\n\nHow far are you going? (miles)\n"); gets(str); distances = atio(str); printf("\n\nHow much time do you have for the trip? (hours)\n:); gets(str); time = atio(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; rules(); if(fly_com) printf("\nFly commercial."); if(fly_tcart) printf("\nRent a Taylorcraft and fly low."); if(fly_bon) printf("\nRent a Bonanza and fly high."); if(m_cycle) printf("\nTake your motorcycle and ride the back roads."); if(car) printf("\nThere's nothing for it but to drive a car."); }

rule(s) { if(av_speed > 60) fly = 1; if (av_speed) <= 60) drive = 1; if (fly && !is_pilot) fly_com = 1; if(fly && is_pilot && like_scenery && av_speed < 100 fly_tcart = 1; if(fly && is_pilot && (100 < av_speed) && (av_speed < 200)) fly_bon = 1; if(drive && like_scenery) m_cycle = 1; if(drive && !m_cycl) car = 1; }

Listing One.

Previous Page | 1 Introduction | 2 Rules in C | 3 Control | 4 Another Way to Control Execution Next Page
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: