Go to the first, previous, next, last section, table of contents.


Tutorial

The making of a family-tree database.

This chapter is a small tutorial describing how the basic things of MUIbase work. Within the tutorial a small project is developed that allows managing of your family tree. The project that results after applying all steps in this tutorial can be found in the `Demos' directory of your MUIbase installation.

How MUIbase works

MUIbase could be said to operate in two different modes, record-editing and structure-editing mode.

The record-editing mode is where you change, add, or delete your records.

The structure-editor lets you edit how your database should look like and what tables and attributes it contains.

Besides this there is the program-editor where you write program functions that are executed either automatically when entering some data in an attribute or when pressing a program button.

Starting with a project, the structure-editor

To create a database you first of all need to define it's contents. In MUIbase this is done in the structure-editor. To enter the structure-editor you either press RAMIGA-s (Right Amiga button and the letter `s') or by choosing `Structure Editor' from the `Project' menu. There you will find three different sections:

`Tables'
In `Tables' you change, add, or delete the tables you need.
`Attributes'
In `Attributes' you change, add, or delete attributes. These attributes each belong in one of the above mentioned tables.
`Display'
In `Display' you design the looks of your database, how it should be displayed.

Adding a table

First we need a table, press the `New' button just below the list view in the `Table' section. You will then get a requester which asks you to enter some data:

`Name'
This is where you enter the name of a table. The name must start with an uppercase letter and can consist of up to 20 characters. The name can be changed later. In this tutorial we set the name to `Persontable' since it's going to hold all the persons names in this database.
`Number of records'
A table can either consist of only one or of an unlimited number of records. In this case it should be set to unlimited since we are going to add more than one person.
`Trigger functions'
Any call from the user to add or delete records can be controlled by a program function, this is where you set which function to call. Since we don't have written any program functions yet, a look in any of the list-views will not show anything.

When this is done, just press the `OK' button and we have our first table, Persontable.

Adding an attribute

Then we need a string attribute for that table, press `New' in the attributes-section. Attributes also need some settings:

`Name'
The same as for a table, first letter an uppercase one and altogether a maximum of 20 characters. This attribute we will set to `Name' since it's to contain the names of the persons we are about to add.
`Type'
Here we choose what type this attribute should be. There are a couple of different ones but for this attribute we use a string attribute.
`Max length'
Here you can define the maximum number of characters a user can enter for the string. We set this to 30.
`Initial value'
It's possible to have some attributes to use an initial value for every new record you add, here is where you enter what it should contain. Leave this line blank.
`Trigger'
An attribute could also trigger a program function to be executed. For example, if you enter a name then you can have a program to check if this name already exists.

Displaying the project

After pressing `OK' you now should notice some changes in the display section. Change the choice button on the top of the display section to `Root window'. There you see what the root window holds, currently `Persontable'. If you change the choice button back to `Table mask' you can see how this table, `Persontable', is presented. Currently it's displayed as one panel with one attribute.

Now double-click on `Panel(Persontable)' at the top of the list in the display section and a window should appear, allowing you to set how this panel should be displayed:

`Title'
The title of a table can be different than what it's real name is. Our table is called `Persontable' but here we could set it to be `THIS IS THE TABLE PERSONTABLE!' if we prefer that better.
`Background'
The background can be changed to whatever suits your taste.
`Gadgets'
Here we can define what gadgets we want the panel to have.

Press `OK' and then double-click on `Name' in the list-view in the display section. That should bring up the window that contains the settings for how to display the string attribute `Name'.

`Title'
The same as for the panel. The string you enter here is what is really displayed when in record-editing mode.
`Shortcut'
Here you can set a letter that should be used together with RAMIGA to jump to this attribute, when in record-editing mode.
`Home'
Makes the cursor to jump to this attribute whenever a new record is added. In our case we will always or most of the time enter the name first in a new record, so set it.
`Read only'
Set this if the attribute should be read only. Leave it unset.
`Weight'
Decides how much of this attribute should be visible when competing for the space with other attributes. For example, if three 50-character strings resides in a window that only has room for 100 characters, then this number decides how much space the string gets relatively to the other ones. Leave it at 100.
`Background'
The same as for the panel.
`Bubble help'
Here you enter any text you think would be helpful to the user. The bubble help appears after holding the mouse still over an attribute for some seconds. Set this to `If you need help, call the author at 112'.

Leave the structure-editor (RAMIGA-s or `Structure Editor' in the `Project' menu) and go to record-editing mode, to see how the database looks in real life. You'll see the headline which is the string you may have entered in the display section for the panel. The record counter should say `#0/0' since we haven't added any records yet. Thereafter is the filter button and the two step-buttons. Below all this you should have `Name' and the text you may have entered in the display section for this attribute. If you haven't changed any text at all when in the display section then the panel should be named `Persontable', and the string attribute `Name'. Move the mouse above the string attribute `Name' and leave it there for a couple of seconds. If you entered something in the bubble help above then this text should appear in a bubble.

Adding two reference attributes

Now we will add two reference attributes. Reference attributes are a bit different than other attributes. As their name might imply they refer to records. This will get more understandable as you try it out for yourself in just awhile.

Enter the structure-editor again and add two more attributes to `Persontable'. Press `New' in the attributes section, name it `Father', and change it's type to `Reference'. A reference attribute has only one setting:

`Set reference to'
Tells which table the attribute it's about to refer to. Should already be pointing to `Persontable', leave it that way and press `Ok'.

Add another attribute by pressing `New' in the attribute section and call it `Mother'. The attributes type should also be set to reference and point to table `Persontable'.

As you may have noticed there are now three attributes in the display section. Click on `Father' and then on the buttons up and down located just to the left. This will change where `Father' is positioned when looking at it while in record-editing mode. Put `Father' at the top, `Name' in the middle, and `Mother' at the bottom.

Then we just have to set what contents the reference attributes `Father' and `Mother' should display from the referenced records. Double-click on `Father' in the display section and then click on `Extras'. There we choose to display the string attribute `Name', then we press `Ok' and repeat the procedure for `Mother'.

Adding records

Now we should add some records. Leave the structure-editor and enter record-editing mode. To add a new record you simply press RAMIGA-n or choose `New record' from the `Table' menu. The cursor should now automatically jump to the attribute we have set to `Home' earlier in the display section in the structure-editor. Add two records, one with your fathers name in `Name' and another one with your mothers name in `Name'. Thereafter you add another record with your own name in `Name'.

Now it's time to understand those reference attributes. By pressing on the list-view button on `Father' we get a list of all records this reference attribute could refer to. Choose your fathers name and do accordingly down below on the mothers list-view.

Now you should have three records, you, your father and your mother. In your record, your fathers name should be visible in `Father' at the top and your mothers name should be in `Mother' at the bottom. You can browse through the records by pressing ALT together with Cursor up/down.

But hey! My parents also has/had parents you say! So let's add another four records, the third generation. Just add the records one by one and write their names in `Name'. If you don't remember their names then just enter `fathers father', `mothers father' or something like that instead. Then you browse through all the records and set `Father' and `Mother' to what they should contain. When this is done you should have at least seven records, your record, your parents records and your grandparents records.

Filter

Since we now have some records to work with, we could try out the filter function. The filter can sort out records you don't want to display, they will still remain in the database itself but they will not be displayed.

To edit the filter you press LAMIGA-f or choose `Change filter' from the `Table' menu. When you have done so a strange looking window with loads of operators will appear. This is where you set what conditions a record must fulfill to get displayed.

In this small example we will use the LIKE command, which lets you do a joker comparison of an attribute. Press once on the LIKE button to the right and then double-click on `Name' in the list to the left and (LIKE Name ) should appear in the string just above the `Ok' and `Cancel' buttons. Thereafter you type "*a*" so the whole string shows (LIKE Name "*a*"). This means that MUIbase should display all records that contain the letter `a' anywhere in `Name'.

Press `Ok' and you may notice that records with no `a' in `Name', no longer are visible. Since the letter `a' is quite common in most languages and names, all records might still be visible but you can try other letters to make the filter function more clear. When you're done, go to record-editing mode.

As mentioned earlier there is a button on the panel that says `F'. This `F' indicates if the filter is on or off. Finally when you're done testing, turn the filter off so that all records are visible.

Queries

Now that we have played with the filter function a bit, we might as well play around with the query feature that's in MUIbase. Queries can be used to display information from a database matching certain criteria.

Choose `Queries' from the `Program' menu or press `RAMIGA-*' to open the query editor. Now a window with some buttons on the top and two larger areas below appear. The string to the upper left is where you enter the name of what you want to call the query you make.

`Run'
Compiles and runs the query. It goes through the database and then displays the info according to your specifications.
`Print'
Prints out the result of the query.
`Load and Save'
Lets you load and save each of the queries.

The first large area is where you enter the query. The second large area is where the result is displayed.

Now let's produce a list of all those persons we tried to filter out previously. Type `Persons with an a in their name' in the string to the upper left. This is the title for this query. In the upper large area, type:

SELECT Name FROM Persontable WHERE (LIKE Name "*a*")

Now when you run this query, either by RAMIGA-r or by pressing the `Run' button, it will produce a list of all persons with the letter `a' in their name. Try changing the letter to see different results.

At this time we can introduce the AND command. Press the list-view button just to the left of the `Run' button, in the query editor. Then choose `New' and name it `Persons with both letter a and s in their names'. Then type

SELECT Name FROM Persontable WHERE
(AND (LIKE Name "*a*") (LIKE Name "*s*"))

Note that we are still using the LIKE command for choosing records containing the letters `a' or `s' in their names, but the AND command requires that BOTH LIKE criteria are met. Therefore, only records with BOTH the letters `a' and `s' in their name are displayed when the query is run.

Adding a table with a memo and a button attribute

This was two ways of selecting and displaying the database. Another way of displaying data can be done by a program. In order to display data we can use an attribute type called memo.

Enter the structure-editor and press `New' in the table section. Name the new table `Controltable' and set it's number of records to `Exactly one'. Click and hold down the mouse button on the new table. Now drag it just a bit above the middle of `Persontable' and release the button. In the table section, `Controltable' should now be on top, and `Persontable' below of it.

Make sure that `Controltable' is activated, then press `New' in the attribute section. Set this new attributes type to `Memo' and give it the name `Resultmemo'. Press `Ok' and then add another attribute to `Controltable' by once again pressing `New' in the attribute section. This time, set it's type to `Button' and name it `Pedigree'.

To give the database a better look, click once on `Pedigree' in the display section and push it to the top by pressing the `Up' button once.

Programming MUIbase to do a pedigree

Ok so now we have a button that can start a program and a memo to display data in. It's therefore time to enter the program-editor. This is done either by pressing RAMIGA-p or by choosing `Edit' from the `Program' menu. The editor has three buttons:

`Compile & Close'
Which does just that. It compiles the program and leaves the program-editor.
`Compile'
Compiles the programs but stays in the program-editor.
`Undo'
Undo all changes from the time you entered the program-editor.

As all program functions you write will reside in this one window, we will need to separate them from each other. In MUIbase this is done by the DEFUN command. Everything between the two parenthesis will be a part of the function pedigree in this example:

(DEFUN pedigree ()

; This is DEFUN's end parenthesis
)

With this in mind we now type in the first function which will produce a family tree of the current person in the database and place the result in `Resultmemo'. This pedigree function is in fact three functions:

; The program pedigree

(DEFUN pedigree ()
    (SETQ Controltable.Resultmemo (pedigree2memo (getpedigree Persontable NIL) 0 3))
)

; The program getpedigree

(DEFUN getpedigree (person:Persontable level:INT)
    (IF (AND person (OR (NULL level) (> level 0)))
        (LIST person.Name
            (getpedigree person.Father (1- level))
            (getpedigree person.Mother (1- level))
        )
    )
)

; The program pedigree2memo

(DEFUN pedigree2memo (pedigree:LIST indent:INT level:INT)
    (IF (> level 0)
        (+
            (pedigree2memo (NTH 1 pedigree) (+ indent 8) (1- level))
            (IF pedigree (SPRINTF "%*s%s\n" indent "" (FIRST pedigree)) "\n")
            (pedigree2memo (NTH 2 pedigree) (+ indent 8) (1- level))
        )
        ""
    )
)

Ok, so here we have the final program functions. Type this in and make sure that all the parenthesis are where they should be. Too many or too few parenthesis is the most common fault you'll get when MUIbase is pre-compiling your programs. The error message from MUIbase will probably be `Syntax Error', in that case. Press `Compile & Close' and hopefully the window will close, which means that MUIbase couldn't find any faults in it while pre-compiling.

Don't worry too much if you don't understand all the commands at first. As with all programming languages it requires some time and practice to master.

Now we have a program to run, but first we have to assign the program function to the `Pedigree' button. This is done by entering the structure-editor, selecting `Controltable' in the table section and double-clicking on the `Pedigree' attribute in the attribute section. Then open the list-view `Trigger'. In this list, all your program functions will be listed, currently there should be three functions: pedigree, getpedigree and pedigree2memo. Double-click on pedigree as the program function the `Pedigree' button will trigger, then press `Ok' and leave the structure-editor.

Now if everything is done correctly, pushing the `Pedigree' button will produce a pedigree of the current person. Try changing person to see some different pedigrees.

Programming MUIbase to list a person's children

As the next addition to this database requires some more records, you should add your brothers and sisters. If you don't have any then write `My faked sister 1', `My faked brother 1' which of course should be set to have the same parents as you.

Then go to the program-editor and type the following for creating another program.

; The program children counts how many children a person has.
; First we define the variables we will use, i.e "children" is set to contain "\n\n".

(DEFUN children ()
    (LET ( (children "\n\n") (nrofchildren 0) (currentperson Persontable) )

    ; For all records in Persontable do the following:
    ; If the current person appears as father or mother in any of the records then:
    ;     add the name to the variable children
    ;     increase the nr of children - counter with 1.

    (FOR ALL Persontable DO
        (IF (OR (= currentperson Father) (= currentperson Mother))
            (
                (SETQ children (+ children Name "\n"))
                (SETQ nrofchildren (+ nrofchildren 1))
            )
        )
    )

    ; Then we write the result into the Controltable memo, Resultmemo.
    ; If the current person doesn't have any children then write one string.
    ; If he/she has children then write another string.

    (SETQ Controltable.Resultmemo
        (+ Persontable.Name (IF (> nrofchildren 0)
            (+ " is the proud parent of " (STR nrofchildren) " children(s).")
            " does not have any children (yet :-)."
        ))
    )

    ; If the current person has children then add their names.

    (IF (<> nrofchildren 0)
        (SETQ Controltable.Resultmemo
            (+ Controltable.Resultmemo "\n\n"
                (IF (= nrofchildren 1)
                    "The childs name is:"
                    "The children names are:"
                )
                children
            )
        )
    )

    ; This is the end parenthesis of the LET-command.
    )

; This is the end parenthesis of DEFUN children.
)

To create variables, we use the LET command. Variables created with the LET command are local and only visible within the `LET' commands open and closing parenthesis. So any command that want to access these variables will have to be within these parenthesis.

All we need to execute this program is a new program button, so enter the structure-editor and add a button attribute in `Controltable'. Call it `Children' and choose children as the program function it should trigger.

To bring some order in the mask of `Controltable' it's now time to introduce groups. All objects can be ordered into vertically or horizontally aligned groups.

In the display section, click on `Pedigree' and shift-click on `Children', thereafter you click on the `Group' button to the left. Now the two program buttons will be together in a vertically aligned group. However, we want this one to be horizontally aligned so double-click on the `VGroup' that has appeared in the display section. This will open a window that lets you change the settings for this group. Set the title to `Programs' and check the `Horizontal' button.

At this time we can remove the name of `Resultmemo' in `Controltable'. Double-click on `Resultmemo' in the display section and remove the name. `Resultmemo' will still exist but it's name won't be shown anymore.

To make things easier if we add more programs or attributes in the `Controltable', we should place `Resultmemo' and the `Programs' group in a vertical group. Be sure of that you only have marked the group `Programs' and `Resultmemo' and then press on `Group'. This will put `Programs' and `Resultmemo' into a vertical group.

Leave the structure-editor and take a look at the result. Then press the `Children' button to see the number of children and their names of the current person.

This example could very well be extended into a full-grown pedigree-program. The only real limits are your fantasy and the size of your hard drive.


Go to the first, previous, next, last section, table of contents.