Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Rule-Based Programming in C



Despite what you've been told, you don't necessarily need a fancy expert system shelf to enjoy the advantages of rule-based programming, one of the most useful and widely used ideas to come out of AI. In this article, we'll learn how to write rules in plain vanilla C, look at some programming examples, and discuss forward chaining and simple control structures. By the time we're done, you should have a clear idea of how to use rule-based programming techniques in a conventional programming language.

Rules allow us to represent certain kinds of knowledge that are difficult to employ a conventional procedural program. Rule-based programs also separate the knowledge from the rest of the program in such a way that a change in the knowledge base is not propagated throughout the program like a change in a procedural program can be.

This flexibility allows you to defer some kinds of decisions or reconsider them halfway through the program without completely rebuilding it. Rules are ideal for expert systems, where the process the program is to perform is very imperfectly understood, at least at the outset.

Rule-based programs can be written several ways. Some languages, such as OPS-5, are intended for this purpose. There are also expert systems shells of many kinds, prices, and capabilities. Either approach can offer a great deal of power in rule-based programming and should be considered for an expert system or any program that will have a great many rules. However, in some cases these approaches sacrifice the capability of the procedural programming on such prosaic areas as I/O and screen control.

In any case, you are required to learn new ways to do old things. For a program with just a few rules or for experimenting, you might consider writing a rule-based program in C.

In a rule-based program, the function is embodied in a set of rules written in something resembling English. A rule has a condition or set of conditions called the IF part and a result or set of results called the THEN part. The THEN part of the rule usually consists of actions to be performed. If the IF part of the rule is satisfied the rule can fire. If the rule fires, actions in the THEN part are performed

IF   there is rain
AND   you must walk
THEN    umbrella is necessary

In this case the THEN portion doesn't look like an action. It could also be written umbrella = necessary , and in either case could have the effect of assigning the string necessary to the variable umbrella or giving the Boolean umbrella a value of TRUE or whatever your program requires. The IF part could also be rewritten as IF rain is true, which would look more like a normal program.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.