Diese Anleitung ist in Bearbeitung. Bitte nicht löschen.
In the course of this tutorial we will be using the JTransformer Tutorial Project. Please add the JTransformer Tutorial Project to your workspace:
It will take some seconds to build the factbase. The creation of the factbase is indicated by the tiny progress bar in the lower right corner of the Eclipse window. As usual, you can open the Progress View (Window→Show View→Progress), to get more detailed information. The initial factbase creation may take a while on large projects1) but it only happens once. From this point on, the fact representation will be updated whenever a Java editor is saved, as long as there are no compilation problems. Updates are very fast since only the changes are propagated. They will mostly be unnoticeable.
During factbase creation, a new project named like your current project but with an added '-output' suffix will appear in the Eclipse Navigator and the Eclipse Package Explorer. It is used for storing the results of transformations when you activate the menu item “JTransformer → Save Factbase… → To Output Project(s)…”.
You should now see the JT_Tutorial and the JT_Tutorial-output project in your package explorer:
There are some example classes and packages in the source folder of this project.
If you want to see how JTransformer can handle a larger project you can download and import the JHotDraw Project (273 source classes).
If you choose to work with the JTransformer Tutorial Project you can skip the following steps, since the assignment of the factbase has already been done. You can continue with your first jtransformer query.
You can, of course, use your own favorite Java 1.4 or Java 5.0 project for your experiments but be aware that Java 5 “generics” are not supported by JTransformer, yet. All other Java 5 features (enumerations, the for each construct, annotations, etc.) are supported.
In order to be able to use JTransformer on a particular project you must first assign it to a JTransformer Factbase.
It is now time to test the newly created factbase by trying out a few basic queries in the Prolog Console.
To enter a query, locate the Prolog Console view. If it is not visible, you might have to open it: From the JTransformer Menu, select Open Prolog Console. The console in which you can type your queries will appear.
Before you can ask your first query you must tell the Prolog Console which factbase you want to query. This is done by selecting a factbase in the drop down menu in the Prolog Console toolbar:
By default, the Factbase Switcher uses “Follow Mode”. Follow Mode means that the console always shows the factabase associated to the currently active editor window (if any).
Since we haven't opened any Java source file yet, we deactivate Follow Mode and select the JT_Tutorial factbase explicitly2). Now you see the prompt ”?-“ that invites you to enter a query:
Type the following query in the Prolog Console and hit <Enter>3):
?- classT(Id, CompilationUnit, Name, Members).
This way you ask for any class in the current factbase. A query basically asks the system to find correct values for the variables in the query. The capital spelling of Id, CompilationUnit, Name and Members indicates that these are variables. In our example, the first tuple of correct values for the variables is displayd like this:
This tells us that the first class found is the class named “Object”. The values for the variable
The yellow background of the Prolog Console indicates that the query is not completed yet – there might be more results:
Try replacing the variable “Name” with a class name, e.g. 'B'. Be sure to surround the class name with simple quotes – otherwise it will be misinterpreted as a variable 8):
?- classT(Id, CompilationUnit, 'B', Members).
Now only one fact is matched: the fact that represents the “B” class.
If you just want to know which ID belongs to the class 'B' (and you are not interessted in the values CompilationUnit or Members) you can use an underscore “_” for this arguments.:
?- classT(Id, _, 'B', _).
Now the only result is the Class ID for Class 'B'.
If you just want to see your source-code classes (and not the ones from bytecode, like Object) you can connect two predicates with ”,“:
?- classT(Id, CompilationUnit, Name, Members), not(externT(Id)).
Now you can:
The Factbase Inspectors of JTransformer let you view and easily navigate the fact representation of your Java program. You can open an FBI instance to inspect the AST subtree belonging to a particular
Enter the following query in the Console:
The result will look similar to this9): <code prolog > Id = 17252, CompilationUnit = 17327, Members = [17253, 17257, 17258] </Code>
Now select the number representing the ID of the class B and from the context menu select “Open In Factbase Inspector”:
A new Factbase Inspector View will open, showing a single classT element representing the class B, as the root of a tree. Expanding the classT element reveals the elements inside B10).
Now select an element (for example a methodT element). Bring up the context menu of that method and select the “Show Source Code” item. A Java Editor opens and the source code corresponding to the selected fact is displayed in the editor, setting the editor selection to precisely the text representing the element:
Now select the field “dbAccess” in the Java editor. From the context menu select the “Open In Factbase Inspector” item (left-hand-side-image). A new Factbase Inspector View opens with the corresponding PEF as root (right-hand-side image):
For more details see the Factbase Inspector tutorial.
An alternative to enter your query in the console is to work with Prolog Files (*.pl).
Download and import the JT-Tutorial Prolog Project. It consists of some examples for analysing and modifying your factbase.
If you want to edit a prolog file, just click on it in your Package Explorer. The Prolog Editor will open and you can edit the Prolog Code. To create a new Prolog file just click “New” → “File” and chose the file extension ”.pl“.
To let a prolog file work on your factbase you have to consult it. This is the Prolog term for compiling a file and loading it into the Prolog system. You can consult the currently active Prolog Editor file via the menu item 'Prolog → Consult' or the shortcut 'F9'. It is not necessary, that the prolog files belong to the same project like your factbase. Just make sure, that you choose the right factbase in your Prolog console. If a popup shows up just click “No” (you don't want to switch to default runtime).
To consult all Prolog files belonging to the JT_Tutorial project open the load.pl and press 'F9'. You should see something like this:
Now you can analyse your program with some examples:
In the Prolog project there is a subfolder called 'patterns'. You can use the files in this folder to search for design patterns in your source code. One simple example is the Singleton Pattern. You can describe it in the following way:
The file 'singleton.pl' provides a prolog statement called: mineSingleton. The implementation is the following:
mineSingleton(TypeName, MethodName, FieldName) :- classMethodReturnsOwnInstance(Type, Method, Field), classT(Type,_,TypeName,_), % Get type name methodT(Method,_,MethodName,_,_,_,_), % Get method name fieldT(Field,_,_,FieldName,_). % Get field name
classMethodReturnsOwnInstance is also implemented in the 'singleton.pl'. So the result will consist of all classes which have a (static) method that returns the own instance.
Let's try it with the JT_Tutorial project (of course you can take any project you like which has a singleton pattern).
Make sure you choose the right factbase in your Prolog console (JT_Tutorial : JTtransformer). Consult the load.pl file and type:
mineSingleton(ClassName, MethodName, FieldName).
The result should look like this:
Maybe you want to make sure that all singleton classes should only have private constructors. This can be done by extending the singleton.pl file with the following code:
mineSingletonWithNonPrivateConstructor(ClassName, MethodName, FieldName) :- classMethodReturnsOwnInstance(Class, Method, Field), not(allConstructorsPrivate(Class)), % there are non-private constructors classT(Class,_,ClassName,_), % Get type name methodT(Method,_,MethodName,_,_,_,_), % Get method name fieldT(Field,_,_,FieldName,_). % Get field name allConstructorsPrivate(Class) :- classT(Class,_,_,_), not(externT(Class)), forall( constructorT(Method,Class,_,_,_), % Constructor method modifierT(Method, private) ).
Save the file, consult it again and call the just created Prolog statement from your console:
mineSingletonWithNonPrivateConstructor(ClassName, MethodName, FieldName).
The only result for this query should be the 'BadSingleton' class.
Now you can try to analyse your program with other Prolog files (find bad smells, nested DB accesses etc. ). Or you can make modifications on your factbase by using Conditional Transformations.
Now you now the basics of working with JTransformer: creating factbases, analysing and inspecting them. Next, you could continue to the tutorials on