GeeWhiz Prolog - Part Four - A Visual Prolog ModelerTuesday, May 20. 2008
This is Part Four, the fifth part of a series of entries describing an implementation of the Prolog language in the NetBeans IDE.
Part Zero is here. Part One is here. Part Two is here. Part Three is here. What We'll Be DoingSo far we've created an editor for Prolog source code within the NetBeans IDE that supports navigation and syntax highlighting. Most of the previous stuff we've done can be found in other tutorials, but now we're going to branch out from there and explore some unknown territory. Specifically, we're going to build a visual predicate mapper for Prolog that will look like this when it's done: ![]() Frankly, I'm not sure how useful the mapper really is in actual Prolog programming, but so far I have used it to find missing references in Prolog code I wrote and to get an overview of code others have written. For these use cases the diagram thing really does come in handy. My starting point for this segment of the project was the excellent tutorial, "A Visual Database Explorer for NetBeans". Thanks to Toni Epple for providing a lot of useful information. I should also confess here that I'm far from an expert in the NetBeans Visual Library, but learning more about it is high on my to-do list. The library so far seems easy to use, featureful and well worth investigating further. Creating A First-Cut DiagramThe first thing we'll do is create a static sample diagram in GeeWhiz using techniques described in the Visual Database Explorer tutorial, so let's create a new action in the project. This action will add an item to the main menu that opens our diagram window. Right-click on GeeWhiz in the Projects tab and select "New" "Action".
In the next screen of the wizard,
Next we need to create a TopComponent, which is essentially a top-level window in the NetBeans IDE. Right-click the project again and select "New" "Window Component". This starts the New Window Wizard, as you might expect.
Now we need to edit some files, but first we need to add the Visual Library to our project. Right-click on the GeeWhiz project, select "Properties", and select "Libraries" as the category. To the right of the Module Dependencies window click the "Add" button to bring up the Add Module Dependencies window.
Notice that the last line is commented out. We'll add this back in later. Right-click in the class and select "Fix Imports" to fix up the import statements. Save and close the file. Now we'll edit the top component. Open VPrologTopComponent.java. In the design view, click on "Scroll Pane" in the Swing Containers palette and drop it onto the frame. Make it fill the frame. Then click "Panel" in the palette and drop it onto the JScrollPane you just created. Right-click the JPanel and change the layout to BorderLayout. Now you should have a design view that looks like this.
Now we're going to create a class to render the diagram. Create a new Java class called "VPrologGraphScene" and replace the empty class with the following code:
Fix imports (I love that thing!) then save and close the file. Next we need to add some icons to our project. The easiest way to do this is to download one of the project files at the end of this article and unzip / untar the archive to get the icons. Copy them directly into your project's /src/org/myorg/geewhiz directory. As a final step, we're going to go into Bundle.properties in our project then right-click on one of the properties and select "Edit". This file is where descriptions and such are kept. Edit the descriptions of the properties as follows (or make up your own):
Testing The First-Cut DiagramNow it's time to test our changes. Clean and build the main project then install it into the target IDE (see earlier steps for instructions on doing this). When the installation completes and your personal domain is created, open the BraveNewWorld project. Let it index its little heart out then open fib1.pro. As a first step in testing, make sure everything that worked before still works. Click in fib1.pro and make sure the Navigator syncs with the editor pane and that the syntax is colored as it was before. Now go up to the menu and click "View". Our new menu item, "Show Prolog Diagram" should be there and be enabled, complete with icon.
Adding Prolog Structure Analysis To The DiagramNow that we can display a sample visual model, let's add Prolog support to the project. First we're going to create a Java data structure class to represent a Prolog clause and its attributes. Create a new Java class off your package node in the project and call it PrologClause. Replace the empty class with this:
Fix imports and save it. The class we just created is pretty straightforward. A Prolog clause is identified by its name and its arity. Arity is a Prolog term that means argument count. All clauses with the same name and same arity are considered part of a single predicate, traditionally denoted as name/arity. Thus, all the clauses in our fib1.pro code are instances of a single predicate, fib/2. In our PrologClause class, we're storing the name, arity, text body, and a count of clauses that comprise the predicate in the source code. See what's wrong? I misnamed the class -- it should have been PrologPredicate, since we're not generating an instance for each clause, only for each predicate. I promise I'll go back and change it One Day Soon. But for now, let's go on. You will also see in this class a list of clauses mysteriously called body. This is another unfortunate name, but what it represents is a list of predicates that are "called" by this predicate in one or more of its clauses, either in the arguments to the clause or in the body of the clause itself. To give you an example, take the following nonsense Prolog source code file:
some_list_function(A,B,C) :- another_function(A,B), do_something_else(B,C). some_list_function(param_function(A,D),B,C) :- B is C. This would generate one PrologClause object, with a name "some_list_function", an arity of 3, an instance count of 2 (there are two clauses), the text that is typed above, and a body list consisting of another_function/2, do_something_else/2, and param_function/2. As you can see, the body list resembles a list of functions called by this predicate. Now we need some code that creates the PrologClause objects we just defined. Create a new Java class and call it PrologAST. Copy the following code into it:
This is a obtuse little class, but in general what it does is search the AST for Prolog clauses and deconstructs them into predicates, then builds PrologClauses for them if they don't exist yet or increments the count if they do. Text for each clause in a predicate is added to the PrologClause, and any foreign clauses called with the current clause are added to the body list. When we're done searching the AST we have a nice little group of predicates ready to diagram. Note: this AST is of course the same one that appears when you click "AST View" in "Window/Other" that we looked at earlier. If you want to compare the text output of PrologAST to the AST view of fib1.pro, the code makes a lot more sense. Plus you can see how I was able to write the class in the first place. A couple of things in PrologAST are worth examining more closely. The IOProvider/OutputWriter stuff has not been introduced yet in this series, but what it does is create a "Prolog" output tab and writes to it. Technically I shouldn't be hanging on to the OutputWriter by declaring it at the class level, but it works for now and I'll probably end up getting rid of the text output entirely anyway. When I'm sure it works correctly. Probably the same day I rename PrologClause to PrologPredicate. Another thing to note is the ParserManagerImpl class in getASTRoot. I needed to obtain the root AST node somehow, and the only way I could readily find is by using ParserManagerImpl, which unfortunately is not part of the API. This one line causes us to use an implementation version of the GLF, a process I have to describe below. If anyone knows of an API way to get the root AST node, please let me know so I can get rid of the versioning and use the public API. There is essentially no documentation for the whole GLF, so finding something like that is a non-trivial task. Trust me. To get rid of the red squiggles in the source code, we need to add some module dependencies to our project. Right-click on the project, select libraries, and click "Add Module Dependencies". Add the "Editor," "Editor Library," and "IO APIs" libraries. (You have to add them one at a time.) Now, as mentioned above, we need to change the Generic Library Framework to use an implementation version, as opposed to the public API. In your project, find "Generic Libary Framework" in "Libraries", right-click it and select "Edit".
Now we need to edit VPrologGraphScene to use our new predicate objects. Open it up and add the following code right below the first constructor:
The new constructor grabs the list of PrologClause (predicate) objects and constructs pretty little images on our diagram using the same createNode etc. methods as before. For the entire program, see the archive files at the bottom of the entry. Fix imports if necessary and save and close the file. Next, we have to change VPrologTopComponent to call the new constructor with a passed DataObject. Open it and add the following method to the class:
Notice that we still have the VPrologGraphScene constructor without arguments called in the top component constructor. Our old friend VPrologAction will still call that, so we can test the functionality of the modeling without invoking PrologAST if necessary. Now we need to edit ShowDiagram, so open it up and remove the slashes on the win.loadProlog line to uncomment it. Now we should be ready to rock and roll. Testing The Prolog ModelNow you should clean and build the main project then install it in the target IDE and open the BraveNewWorld project. Open up our old buddy fib1.pro and then go to the menu and select "View" "Show Prolog Diagram". You should see something like this.
Save the file and perform "Show Prolog DIagram" from the menu again. You should get this.
Well, we're done with this segment. We now have a Prolog IDE complete with fins and an automatic headlight dimmer. What we need next is in-place compilation so we don't have to continually switch between NetBeans and our Prolog compiler. You got it -- it's coming next. In the meantime, if you want you can alter PrologAST to not print the informative text, and you can delete VPrologAction.java. If you delete VPrologAction, be sure to also delete the actions in layer.xml ("this layer" then delete the instance in "Actions" "Window" and the shadow in "Menu" "Window"). Files From This EntryTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Hello,
I am a newbie to Visual Lib, however, its proving to be such a cool tool to me. I have a small question. How can i convert my model to an xml data which I can then store in a repository? The Idea is I need the model object in the repo.
Looks like you forgot to mention that VPrologGraphScene must be added in some way to the VPrologTopComponent.
Visual Prolog is actually the name of a programming language (http://www.visual-prolog.com), so the title "A Visual Prolog Modeler" is a it ambiguous.
I went to the Visual Prolog site that you referenced, and was pleased to see that it belonged to PDC, who developed the first Prolog compiler I worked with. But, while I agree with your comment to the extent that the title of this blog entry is unfortunate given that there is an extant product named Visual Prolog, I think I'll let it stand since as far as I can determine the blog entry actually predates the renaming of the PDC Prolog product from PDC Prolog to Visual Prolog. However, you will be gratified to know that I don't intend to litigate the name.
But seriously, thanks for the comment. I was unaware of the product prior to your mentioning it, and as I mentioned, I like the PDC foks. Hopefully people who come to this entry by mistake will read this comment and accept my apologies.
|
CategoriesArchivessupersized.orgSyndicate This BlogHelp Support This BlogIf you find this information useful, are entertained by it, or just want to do something nice you can help encourage further entries by clicking the button below. Major credit cards and PayPal are accepted. Thank you for your support.
|

Owner login

