Here - Control Network Programming

advertisement
1
MY FIRST CNP PROJECT HELP DOCUMENT (version 2012-11-29)
CREATING A NEW PROJECT
1. Summaries of how to create a new application
A. Creating a graphical (window) application
1.
2.
3.
4.
5.
6.
Open Lazarus
File > New > Application
Save All
Create a TButton and a TEdit buttons
Double-click Button1
In Unit1 Add
a. Where the cursor points (in the implementation of procedure TForm1.Button1Click:
Edit1.Text := inttostr(SpiderSolutions);
b. In uses: SpiderUnit
7. Open Spider:
Tools> Spider CNP Environment
8. Change the CN as needed
9. Press the Bulb (compile and run)
B. Creating a console application
1.
2.
3.
4.
5.
Open Lazarus
File > New > Console Application
OK
Save All
In project1.lpr Add
a. In uses:
SpiderUnit
b. Above Type:
var sol:integer;
c. After { add your program here } in procedure TMyApplication.DoRun;:
writeln('Hello from lazarusCNP!');
sol:=SpiderSolutions;
writeln; writeln; writeln('Bye from lazarusCNP!');
writeln('Number of solutions: ',sol);
readln;
2
6. Open Spider:
Tools> Spider CNP Environment
7. Change the CN as needed
8. Press the Bulb (compile and run)
2. Overview of the compilation and execution processes
It is supposed that the lazarusCNP programming environment has been installed and integrated
as a lazarus tool – see document Installation instructions.docx.
A lazarus project is a collection of files that are used to create a target application. This
collection of files consists of the files you include and modify directly, such as source code files
and resources, and other files that lazarus maintains to store project settings, such as the .lpr
project file.
There are two types of applications in lazarus – window applications (also called graphical
applications or simply applications), and console applications. In the former, the I/O is
organized through special windows (forms). In the second type a simple console is used for I/O.
We will discuss both application types, starting with the window applications.
The below figure illustrates the compilation of a lazarusCNP project.
3
Lazarus IDE
SpiderCNP IDE
lazarus project files
Spider files
SpiderUnit.pas
(Primitives)
Project1.lpr
SpiderNet.txt
(CN)
Unit1.pas
(Form1)
…
SpiderCompiler.exe
SpiderUnit.SPI
(CN + primitives)
lazbuild.exe
CNP application (EXE)
e.g., project1.exe
We will see below how the files mentioned are created. In particular, Unit1.pas is created by lazarus
while SpiderUnit.pas and SpiderNet.txt are created by lazarusCNP. When the “Compile and Run” button
is SpiderCNP is pressed (an electric bulb is shown on the icon of the button), the Spider compiler which is
part of the SpiderCNP IDE is invoked. If no errors are encountered, the file SpiderUnit.spi will be
generated by the Spider compiler. This file is an internal representation of the CN and is technically a
proper Pascal file that includes the definitions of the primitives as well as a generated algorithm that
corresponds to how the CN should be interpreted by a CN interpreter. Then the .spi program is compiled
by the Pascal compiler included in the lazarus IDE. An executable file will be created: if the name of the
lazarus project is project1 then the name of this .exe file will be project1.exe. This is the executable file
4
of the application. Now, this file will be executed. All the above actions are result of pressing the Bulb
button. Typically (and this is how the examples that follow are described) the execution of the CNP
application will cause a form to be displayed; this form contains a start button (in the examples called
Button1). Pressing the button causes a search in the CN to be performed starting from the initial node.
In the examples below a second button (called Edit1) is also placed in the form. The value displayed in
that field after the execution shows the number of the solution paths found while traversing the CN. Of
course, the CNP programmer may define a different I/O associated with their application.
3. Creating a new project with a window application
3.1 Open lazarus.
You will see a screen similar to the one shown below.
3.2 From File tab in the menu, select “New”
5
3.3 The window shown in the below screenshot will appear. Choose “Application” for a
windows application, or “Console Application” for a console application, and click OK. Here we
describe the creation of a windows application.
6
Alternatively, instead of File > New > Application, we may use Project > New project >
Application.
When the new project is created, the default file Unit1 will be generated. It has the following
form:
3.4. Saving the created empty project
Press the Save All icon – see the screenshot below. [Alternatively, you can press File>Save All]
7
A window similar to the one shown below will pop up.
Select or create a working directory (folder) in which to save the project.
8
We have chosen to create a new directory aa on the desktop, as shown below.
Reply with Save to the two questions from lazarus. If you check you can see that the new folder
aa contains now the following files:
The File unit1.pas contains the module Unit1.
Note that, by default the new project contains a single form Form1.
At this point, we have the new empty project saved in the specified folder.
3.5 Introducing lazarus windows
9
Let us describe quickly the windows that you when lazarus has been launched.
When you launch Lazarus for the first time, a series of separate disconnected or 'floating'
windows will appear on your desk-top.
The first, running right along the top of the desk-top, is titled Lazarus Editor vXXXXXX project1 (which will subsequently be modified to reflect the name of your currently-open
project). This is the main controlling window for your project, and contains the Main Menu and
the Component Palette.
On the line below the title bar is the Main Menu with the usual entries for File, Edit, Search,
View categories and so on, with a few selections that are specific to Lazarus. Below this on the
left is a set of symbols (icons which take you rapidly to particular menu commands); and on the
right is the Component Palette.
Under the Lazarus Editor window will appear the Object Inspector window on the left, and the
Lazarus Source Editor on the right. There may be another smaller window, labeled Form1,
overlying the Lazarus Source Editor window. If this is not visible immediately, it can be made to
appear by pressing the F12 key or the Toggle Form/Unit button (see the red arrow above), which
toggle between the Source Editor view and the Form view. The Form window is the one on
which you will construct the graphical interface for your application, while the Source Editor is
the window which displays the Free Pascal code associated with the application which you are
developing. Finally, there may also be a Message window in the lower of the screen: this one is
used by Lazarus to show feedback to the programmer, for instance when compiling a program.
When you start a new project (or when you first launch Lazarus) a default Form will be
constructed, which consists of a box in which there is a grid of dots to help you to position the
various components of the form, and a bar along the top which contains the usual Minimise,
Maximise and Close buttons. If you click with your mouse cursor anywhere in this box, you will
see the properties of this form displayed in the Object Inspector on the left side of the desk-top.
Other windows that may become visible during your work: the Project Inspector, which contains
details of the files included in your project, and allows you to add files to or delete files from
your project; the Messages window, which displays compiler messages, errors or progress
reports on your project.
10
The Component Palette is a tabbed toolbar which displays a large number of icons representing
commonly used components for building Forms. To use the Palette, there must be an open form
on view in the editor (if there isn't one, select File -> New Form). Click on the icon in the
appropriate tab of the Palette for the component you want to use, then click on the Form, near
where you want the component to appear. When the desired component appears, you can select it
by clicking with the mouse. Once selected on the form, the object is also selected in the Object
Inspector window, where you can edit its properties and events. Adjustments to the visual
appearance of an object can be made either by altering the picture itself on the Form using the
mouse, or by changing the relevant Property in the Object Editor for that component. A great
number of useful components can be used. Let us mention here a few most important and simple
ones.
TButton
Puts a button in the form. The user will be able to 'click' on this button and call the onClick event
handler (form class method) in your program. The programmer should supply this method to
define the action to be taken.
TEdit
For display or user-entry of a single line of text.
More information can be found at http://wiki.lazarus.freepascal.org/Lazarus_Tutorial, in
particular at http://wiki.lazarus.freepascal.org/Standard_tab.
3.6 Placing buttons on the main form Form1
This can be done from the standard components palette. We will add a TButton button and a
TEdit button.
11
More precisely, you must press the TButton button, and then press in a position in the main form
where you want a TButton button to be placed.
If needed, you may easily drag the button to a new position (with the left-click button on your
mouse kept pressed), change the size and form of the button, change its name or other properties.
Similarly, add a TEdit button:
12
We will use the TButton button to start the application, and the TEdit button for I/O. However,
how the buttons will be used has not been defined yet.
As it currently is, it would be possible to run the application by pressing Button1, but there will
be no reaction to pressing the button as such a reaction has not been defined. We will include a
CNP program for handling the OnClick event for Button1.
3.7 Adding a Control Network
A CN must be defined now. The CN is specified by two files: SpiderUnit.pas and SpiderNet.txt.
The main application will invoke the Spider program (the CN) through these files. To create the
CN you must call the SpiderCNP tool from Tools:
Tools> Spider CNP Environment
13
The following Spider window will appear:
A default CN has been generated, and the graphical representation of this CN is shown by the
graphical editor. If you want you can click on Text View to see the textual representation of the
same CN.
14
As is known, a CN Program basically consists of a CN and definitions of the primitives. The CN
is shown in either its graphical or textual representation as described above. Its corresponding
file is SpiderNet.txt. The primitive definitions and all remaining necessary code are included in
the file SpiderUnit.pas which can also be seen by pressing the corresponding label in the toolbar:
15
If you save the lazarus project now (by pressing the SaveAll button), the aa folder will contain
the following files:
The existing default CN can be actually compiled and run by pressing the ‘electric bulb’ button
on the very right end of the Spider environment toolbar. The first picture below is a screenshot
after compiling the CN.
16
Clicking OK will lead to execution of the application which actually means displaying the main
form. As we know, however, pressing the button on the form has no assigned reaction yet.
Note that a new icon corresponding to the execution of the application, has appeared in the quick
start bar at the bottom (shown with a red arrow). The previous two icons correspond to Spider
and lazarus, correspondingly.
17
Therefore, we must close the application (by clicking the X button on it) and proceed to linking
the CN to the button.
3.8 Linking the main project form (Unit1.pas) with the Spider program (SpiderUnit.pas).
We want to start the execution of the CN (invoke the Spider program) on clicking the Button1
button in the main form.
To achieve that, we must specify the handler for the OnClick event of the Button1 button. In
lazarus, make Form1 visible and double-click on Button1. This modifies automatically the code
of Unit1. The modified bottom part of Unit1 is shown below:
18
The red arrow shows where the cursor will be automatically placed. This is where you should
specify the action when Button1 is clicked when the application is running. Write the text
Edit1.Text := inttostr(SpiderSolutions);
as shown on the next figure:
19
The modified code achieves the following. SpiderSolutions is a function which is defined in
SpiderUnit. The call to this function actually starts the execution of the CN (search in it until
system state Finish is reached). The SpiderSolutions function returns as a return value the
number of the solutions found, that is, the number of the successful paths in the CN. This number
will be shown in Edit1.
You must also go to the upper part of Unit1 in the Source editor and add the module SpiderUnit
in the uses list – see the figure below:
20
These are the only two additions to Unit1 that you must make.
3.9 Checking the default CN
Lazarus is now prepared for work with SpiderCNP.
We open again LazarusCNP as described in Section 3.8 (by Tools> Spider CNP Environment).
The default CN will be displayed.
21
We now have a complete CNP application. We can compile and run this default application by
pressing the Electric Bulb button (shown with red arrow above). The success message will
appear as shown below.
Clicking the OK button starts the application.
22
We will see Form1 that we defined earlier (see Section 3.6). Pressing Button1 starts the traversal
of the CN. The execution of the application completes and the value displayed in field Edit1 is
changed to 0.
This means that no solution paths have been found during the traversal of the CN. If we examine
the definitions of the two default primitives used in the default CN, ActionPrimitive and
ConditionPrimitive (in file SpiderUnit – see picture below) we will notice that
ConditionPrimitive will yield failure while ActionPrimitive will be completed successfully.
Consequently, there is no successful path in the CN, indeed.
23
Close the application (Form1). We can now quickly modify the CN in order to have some
successful paths. Draw two new arrows without primitives as shown below. To draw an arrow,
press the Arrow button in SpiderCNP (see red arrow) and then draw an arrow between the
corresponding nodes.
If we again press the Bulb button and run the application, the number of solutions found will be
1 because the system will find one solution and then exit as the default value of the system
option SOLUTIONS is 1. We can change the value of the system option by right-clicking in the
field Global Options in the Spider Object Inspector (see red arrow above), choose SOLUTIONS
24
in the list of options that will show, and then value ALL. Now the system will find all possible
solution paths. There number now is two.
Note: After each new system option added, the system automatically adds a comma ‘,’. The
comma after the last system option must be deleted!
3.10. Editing the CN
The CN can be modified in Graphical View only (and not in Text View) – see red arrow above.
Read Chapter below for more information.
3.11. Editing the Primitives
Primitives can be edited directly in SpiderUnit – see red line below.
25
4. Creating a new project with a console application
The process of creating a new console application has many similarities with creating a window
application but also essential differences. This process is explained below. A summary is given
in Chapter 1B.
4.1 Open lazarus.
You will see a screen similar to the one shown below.
26
4.2 From File tab in the menu, select “New”
4.3 The window shown in the below screenshot will appear. Choose “Console Application” and
click OK.
27
Click OK in the form that will appear.
28
You will see the screen shown below.
Alternatively, instead of File > New > Console Application, we may use Project > New project >
Console Application.
The file you see in the source editor, called project1, is actually project1.lpr.
4.4. Saving the created empty project
Press the Save All icon – see the screenshot below. [Alternatively, you can press File>Save All]
29
Alternatively, you may press File > Save All.
A window similar to the one shown below will pop up.
30
Select or create a working directory (folder) in which to save the project.
We have chosen to create a new directory aa on the desktop, as shown below.
Reply with Save. If you check you can see that the new folder aa contains now the following
files:
31
4.5. Linking the project (project1.lpr) with the Spider program (SpiderUnit.pas).
At the moment, the lazarus screen shows the file project1.lpr. We need to place the cursor within
uses, in the position shown below with a red arrow.
We must add there the usit SpiderUnit – see the figure below.
32
Then go a couple of lines down just before the type keyword and add
var sol:integer;
as shown on the figure below (see red arrow).
33
Now we scroll down the screen showing project1.lpr and go to procedure
TMyApplication.DoRun, after the comment { add your program here } (see red arrow below).
Write the following text there:
34
writeln('Hello from lazarusSpider!');
sol:=SpiderSolutions;
writeln; writeln; writeln('Bye from lazarusCNP!');
writeln('Number of solutions: ',sol);
readln;
Note: do not copy the above text from a Word file into lazarusCNP but rather write it.
Otherwise, illegal symbols might appear in your files. If you wish you can write this text in a .txt
file, and copy from the text file. I have created for you such a text file - Copy into
project1.lpr.txt.
The result of what we have done is that when the generated application is run function
SpiderSolutions will be called – this will cause the traversal of the CN. The return value of
function SpiderSolutions is equal to the number of solution paths found. A corresponding output
will be shown on the console.
You can see the output that will be produced in the next section. If you prefer a different format
for the output you should correspondingly modify the included text. The format of the I/O is up
to you; the only vital requirement is that the function SpiderSolution must be somehow called.
35
4.6 Adding a Control Network
A CN must be defined now. To create the CN you must call the SpiderCNP tool from Tools:
Tools> Spider CNP Environment
The following Spider window will appear:
36
Note that SpiderCNP has detected that a console application is being developed and, in addition
to the automatically created files SpiderUnit.txt (in graphical and textual representations) and
SpiderUnit.pas also displays a tag for the file project1.pas (on the window shown as Console
Main).
A default CN has been generated, and the graphical representation of this CN is shown by the
graphical editor. If you want you can click on Text View to see the textual representation of the
same CN.
37
As is known, a CN Program basically consists of a CN and definitions of the primitives. The CN
is shown in either its graphical or textual representation as described above. Its corresponding
file is SpiderNet.txt. The primitive definitions and all remaining necessary code are included in
the file SpiderUnit.pas which can also be seen by pressing the corresponding label in the toolbar:
38
The project folder aa will currently contain the following files:
39
The existing default CN can be actually compiled and run by pressing the ‘electric bulb’ button
on the very right end of the Spider environment toolbar. The first picture below is a screenshot
after compiling the CN.
40
Clicking OK will lead to execution of the application. The console will appear. The icon for the
console is pointed by the red arrow.
41
The number of solutions for the defaults CN is 0.
We can close the console by clicking the X button on it, or by pressing the Enter button (because
of the instruction readln in procedure TMyApplication.DoRun in project1.lpr.
We can now proceed to modifying the CN as appropriate.
4.7 Modifying the Control Network
See Section 3.9 for more details.
For example, we can modify the CN as shown below. We have also added
SOLUTIONS = ALL
After pressing the Bulb button we can observe the following solution.
42
4.8. Editing the CN
The CN can be modified using the Graphical View of SpiderNet.txt.
4.9. Editing the Primitives
Primitives can be edited directly in SpiderUnit .
5 Opening an existing project
To open an existing project, in lazarus click
Project > Open Project
43
(see the red arrow above). You can also use
Project > Open Recent Project
44
You will see a window similar to
Click on the file with extension .lpi.
Alternatively, you can find the project folder using Windows Explorer, and click on the file with
extension .lpi, or the file with extension .lpr.
45
Download