Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools

JTransformer: Logic-Based Program Analysis and Manipulation

Contact: Tobias, Günter

Example Tutorial

The following tutorial introduces logic-based representation for programs and models, the concept of logic-based Conditional Transformations (CTs) and the JTransformer system, which implements these ideas for Java. It will demonstrate a typical analysis scenario (design pattern detection) and a typical transformation scenario (implementation of own refactorings).

  • Before you start using JTransformer learn about its logic program representation and transformation concepts:

Logic-based Software Analysis and Transformation

  • Download and install JTransformer and its required software (for details see above)
  • Check the installation
    • Start Eclipse
    • You should now see a JTransformer and Prolog menu
    • Open Window > Show view > Other > JTransformer > PEF navigator
    • Open Window > Show view > Other > JTransformer > JTransformer Factbase
    • Open Window > Show view > Other > Prolog > Prolog Console
    • In the Prolog console type 'true.' (with the dot but without the quotes). You should get the answer YES.
    • Congratulations, everything seems to be installed well.
  • Create a sample Java project
    • You can use your favorite Java 1.4 or Java 5.0 project for your experiments. But be aware, that the Java 5 features Generics is not supported by JTransformer, yet.

The rest of the Java 5 features like enumerations, the for each construct and annotations fine.

  • Alternatively, download the JHotDraw project (273 source classes) and add it to your Eclipse workspace
    • unzip it into your workspace then use 'import existing project into workspace'
  • Activate JTransformer on your test project
  • Select your test project (either JHotDraw or your own) and from the context menu choose “JTransformer Nature”
  • You will see in your project overview that JTransformer has created a new project called like your test project with an added '-output' suffix. This is for storing the results of your transformations. JTransformer NEVER modifies the original project!
  • Try a few queries in the Prolog console to see whether the fact base was created correctly. For instance:
  • classDefT(Id,Pkg,Name,Members).
  • That should give you values for each of the variables Id,Pkg,Name, and Members. Type ';' (semicolon) and <return> to get more results or 'a' to abort further evaluation.
  • Look at the PEF documentation and try your own queries. A ',' (comma) means AND, ';' (semicolon) means OR, not(query) is the negation of query. A query must be terminated by a '.' (period) and <return>. Round brackets may be used as usual for grouping.
  • Explore JTransformer further
  • Look at the JTransformer site for more infos.
  • Select a program element identity returned by a query (mostly a 5 or 6 digit number) and try the context menu Options “Open in Java Editor” and “Open in PEF navigator”. (The first is only applicable to source code elements. If nothing happens, check with externalT(Id), whether the Id denotes a byte code element,)
  • In the PEF navigator, double-click on any element to get a reverse engineered representation of the element as source code.
  • Select some Java Code in an editor and choose “Selection to clipboard” or “Add selection to clipboard” to get an internal PEF representation of the code that you can paste into a Prolog file as a template for a query or transformation.
  • Try conditional transformations (Encapsulate Field Refactoring)
  • Make a new Simple Project
  • Give it the “Prolog nature” using the context menu
  • Let the project share the fact base of the project you want to analyse (e.g. the 'JHotDraw' project provided above).
    • In the project properties (context menu) select the “Prolog Preferences”
    • Set the 'Runtime' value to the name of the project whose fact base you want to share.
    • After you click OK or apply, the PDT will need a while to cleanup the unneeded Prolog process and recreate its meta data.
  • Add the "Encapsulate Field Refactoring" file to the project
    • Save it into the project folder and let Eclipse refresh the project (see project context menu)
  • Open the file and browse its contents to understand how it implements the encapsulate_field refactoring.
  • Load the file into the Prolog fact base by pressing F9 or selecting 'Consult' from the 'Prolog' menu.
  • Now you can paste the head of the encapsulate_field(…) predicate [that is the part left of :- ] as a query into your Prolog console (dont forget to add the '.' period and press return).
  • You will see the how JTransformer executes each CT and reports the number of instances on which the transformations had been applied.
  • Now you could select “JTransformer > Regenerate classes” to store the transformed fact base into the output project created for your test project.

[PEF = Program-Element-Fact. This is our name for the representation of a program element as a logic fact.]

teaching/labs/xp/2008a/seminar/jtransformer.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2020