Creating and Coding an Expert System Using Visual Basic.NET
This exercise is designed to give students exposure to how computer programs are created using a computer programming language (VB.NET). In this exercise, students will create an expert system in the Visual Basic.NET programming environment as they work through a step-by-step tutorial that explains the Visual Basic.NET programming environment and the elements used to create the expert system program, such as IF statements, variables, multiple forms, and other elements of computer programming. This activity must be completed in the CBA computer lab or on some other computer that already has Visual Studio/VB.NET installed.
According to Webster’s dictionary, an expert is “one with the special skill or knowledge representing mastery of a particular subject.” When a human expert solves a problem, their knowledge of that subject area is used to reason through the problem, with the end result being a recommended solution.
Expert systems are computer applications that can be used to capture expertise and then make that expertise available for others to use. Expert systems are often used to solve diagnostic (figuring out what is wrong) and prescriptive (figuring out what should be done) types of problems. Examples of diagnostic and prescriptive types of problems include medical diagnosis, credit and loan evaluation, help desk analysis, and compliance analysis.
Although there are many different ways for expert systems to solve a problem or make a recommendation, most often expert systems follow some sort of decision making rules. Complex expert systems are usually created using a specialized type of development environment called expert system shells. However, the rule-based expert system that we are going to make is going to be created using Visual Basic.NET. Visual Basic.NET is a programming language designed to create applications for the Windows operating system environment.
The purpose of this exercise is create an application (an expert system) and to get a better understanding of exactly what expert systems do, how they are set up, and how expert system rules tend to work. At the same time, we are also going to learn some of the concepts and principles related to computer programming.
Visual Basic is one of the easier programming languages to learn, and Visual Basic lets us create and code GUIs, or graphical user interfaces. In a graphical user interface environment (such as Windows and most of today’s other operating systems), we have windows, icons and other pictures/elements that we work with. We can click or double click on a picture or icon, and the computer does something. When we create a computer program/application, such as our expert system, there are two primary components that we create:
1. The graphical user interface (what the user sees and works with)
2. The actual program code, which is a set of instructions that tells the computer how to perform a given task. A computer does not know what to do unless there is some sort of program or code that tells it exactly what must be done and how it is to be done.
In this exercise, we are going to create a loan evaluation expert system. The general decision making rules can be seen in the decision tree seen below, which shows conditions, outcomes, and appropriate actions. The decision tree diagram is read from left-to-right, with the far right-hand side of the diagram showing the final action to be taken (i.e. the expert system’s recommendation regarding how to handle that particular loan).
For the expert system to properly work, the user must provide input related to different questions asked by the expert system. Questions asked prior to a recommendation being made include questions about income, employment status, levels of education attained, and questions about the applicant’s references.
It is now time to open Visual Basic and begin creating our expert system.
: Please open the folder that contains the expert system files obtained prior to beginning this activity).
You should see two items in Windows Explorer (see picture)
Notice the file called Expert System, Microsoft Visual Studio Solution. A Visual Basic Solution file is simply a container file that stores all of the projects and files for an entire application. Double click the Expert System solution file. If you ever close down the Visual Basic program in the middle of a project, when you wish to resume your work, you will need to double click the solution file. It is always the file that you open, and opening that file gives you access to all of the other elements that make up your application.
Double clicking the/a solution file allows a programmer to enter the Visual Basic.NET programming environment, as seen below. Your screen should look similar to the picture below, although there is the possibility that items that you see in the picture might not be visible, or they may appear in other locations. (What you do or don’t see on your screen is largely dependent on the Visual Basic screen setup used by the last person using the computer that you now are using)
Notice the large 1 (one) on the right hand side of the picture above. The “1” represents the location of the Solution Explorer Window. (If you do not see a Solution Explorer Window on your screen, go to the View Menu, and select Solution Explorer to see the Solution Explorer Window.) The Solution Explorer Window displays a listing of all of the items contained within your current solution (Expert System). AssemlyInfo is a file that Visual Basic created automatically related to your present project. frmEmployment.vb, frmIncome.vb, frmReferences.vb and frmStartup.vb are four container controls that hold other controls (Items found on the interface/GUI such as buttons, labels, textboxes, etc) and the event procedures (i.e. coding) that tells the application how to respond when certain events occur. Notice that frmStartup.vb is highlighted in the Solution Explorer window, with the design screen/GUI for the Startup form appearing to the left of the Solution Explorer Window (See large “2” on picture). If you do not see the form labeled “2” on your screen, then double click frmStartup.vb in the Solution Explorer.
The large 3 (three) on lower right hand side of the picture identifies the Properties Window. (If you do not see a Properties Window on your screen, go to the View Menu, and select Properties Window to see the Properties Window.) The each object/control in Visual Basic.NET has a set of characteristics, called attributes or properties, which are assigned to it. The properties which determine an object’s appearance and behavior are listed in the Properties Window. The picture on the prior page shows the properties for the form called frmStartup. If the “Proceed” button was the currently selected item, then its properties would display instead.
The large 4 (four) appears to the right of the Toolbox. The Toolbox itself appears to the right of this
paragraph. The Toolbox is typically found on the left-hand side of the screen, but it can be located
elsewhere as well. (If you do not see a Toolbox on your screen, go to the View Menu, and select
Toolbox to see the Toolbox. Clicking on the button (shown in the previous picture) that looks like a
hammer and wrench also makes the Toolbox appear and disappear.) The Toolbox contains a
collection of tools that you can use when designing the GUI/interface for an application. You use the
Toolbox to place objects, or controls, on your form.
The tools/objects/controls that we will use in this exercise include:
· The label: used to simply display text.
· The button: used to initiate processing tasks (the user clicks on the button).
· The radio button, also called the option button, allows the user to select only one item
from a group.
The Start Button, which is used to run your application from the Design screen, is circled.
: Notice the arrow leaving from the textbox where it says: “View Code: click to view the code on a form.” Click the button that the arrow points to, and you should be able to see the code that is currently on that form. Go ahead and see how that works.
: Notice that the next button (on the right of the View Code button) is the View Designer button. You can click on that to see the interface/GUI of the form. Go ahead and click on that button.
: When you are done clicking on the View Code and View Designer button, you should see the GUI/interface, as seen in the large picture on the prior page.
We will first take a look at the form and the user interface. The words “This is the loan expert system. You will be presented with a sequence of input forms. Click “Proceed” to begin” are displayed in a label (a label without borders, which makes it somewhat difficult to see). Labels are used to simply display text to the user. The form also contains a button, or command button, labeled as “Proceed.” When the user clicks the button, its associated event procedure (i.e. coding) will begin to execute. Let’s take a look at the code for that button/event procedure.
: Please click the View Code button (seen on the prior page).
After clicking the View Code button, you should see the code
window (part of which is seen in the picture to the right).
The Code Window shows the code that is on the form that you are currently working with. The code above the box that says Windows Form Designer generated code is created automatically by VB.NET. Never delete code in this section. Any code you create will appear below this box. The code below the box on this form is the event procedure that executes when the “Proceed” button is clicked.
Private Sub and End Sub are words automatically added to the event procedure by VB.NET. Words that appear in blue font on your screen, such as these words, are considered to be keywords by VB.NET. Notice the three lines that are indented (in between the lines beginning with Private Sub and End Sub). Programmers typically indent their coding to make the code easier to read, and we will do that also.
In the first indented line, the blue keyword Dim appears. This is called a Dim statement (Dimension statement). Dim statements are used to create variables, memory locations that are used to store data. They are called variables because the data stored in these memory locations can change (or vary) as the program runs. The obj following the keyword Dim indicates the type of variable created is an object variable. Object data types, which take up the greatest amount of memory space, can be used to store anything. When a variable’s data type is chosen, one must ask how that variable is going to be used (what type of data will it store?). The string data type is usually assigned when you want to store a sequence of characters as text (letters and numbers not used in calculations). If a variable is to be used in a calculation, one of the numeric data types (integer, decimal, single, double, short, long) should be chosen.
The object variable created by our Dim statement is objFormIncome. The As New coding indicates we want to create a new object, and As New frmIncome ( ) indicates we want to pattern this new object based upon our existing form file of frmIncome (basically, a copy of the form file frmIncome will be stored in that object variable). Therefore, the line Dim objFormIncome As New frmIncome ( ) says to create a new object variable and store a copy of the form called frmIncome in that variable.
The line objFormIncome.Show ( ) asks the new object variable (objFormIncome) to execute its Show method, which will result in the object variable (its current contents) being loaded into memory and then displayed.
The line Me.Hide ( ) tells Visual Basic to keep the current form active (frmStartUp), but it will be hidden (invisible) to the user.
In essence, when the “Proceed” command button is clicked, Visual Basic creates a new object, representing the Income form (frmIncome), displays the new Income form, and then hides the prior form the user was seeing/using (frmStartup).
Now, after looking at the coding on the first form, it is now time to run our application (as it currently exists) and see
what it does.
: Switch back to the design screen. To start the application while in the design screen, click the Start button found on the toolbar (labeled in earlier VB programming environment picture). Go ahead and start/run the application. Running the application changes your screen as VB.NET compiles the program code (checks for errors). If no compile/syntax errors are found, VB.NET enters runtime mode, where the application actually runs (but you also see the design screen).
Application running in runtime mode.
Everything else that you might see is part of the design screen.
Next, I will mention a few items that
might appear on your screen at some future point, but don’t worry about them if you don’t see them now.
You might also see a new window opening at the bottom of your screen. That window is the Task List.
It could say “0 Build Error tasks shown” or it could say “Build: 1 succeeded, 0 failed, 0 skipped”. That means that you did not make a syntax or typing error, and your application will work as it is written/coded. If you ever make a syntax error or other type of error, then the application will not run and you will see some sort of message box indicating that you have build errors, which you will then need to stop and fix before the application is able to run. However, since we have not coded or typed anything in yet, we should be in good shape (with no error messages appearing).