Login | Register
My pages Projects Community openCollabNet

How to use mbt with perl

The perl script below is an example on how to integrate org.tigris.mbt into perl. The script uses the module IPC::Open2 to launch and establish communication with the java process.

The model being used in the example below is: keepass.graphml

#!/usr/bin/perl -w
use strict;

# Open a process for both reading and writing
use IPC::Open2;

# Declare som variables
my $subroutine;
my $testFailed = 0;


################################################################################
#
# Main loop to run org.tigris.mbt
#
################################################################################

# Launch mbt
open2(*README, *WRITEME, "java -jar mbt.jar dynamic -o -g Main.graphml")
or die "Could not start the execution of mbt.jar: $!";

# Read from mbt, which subroutine to call
while( defined( my $output = <README> ) )
{
chomp( $output );

# Extract the name of the subroutine to run
if ( ( $output =~ /^(\w+)/ ) )
{
$subroutine = $1;
}
else
{
undef( $subroutine );
}

# Call that subroutine
if ( defined( $subroutine ) )
{
eval $subroutine
or die "Could not run subroutine: $subroutine\nDoes it exist?\n";
}

# Depending if to do BACKTRACKING or not, send back the integer 0 or 1
# to the java process
if ( $testFailed == 1 )
{
# Please note. If backtracking is not enabled in the model, this
# input to org.tigris.org will fail.
print WRITEME "1\n";
$testFailed = 0;
}
else
{
print WRITEME "0\n";
}
}

close(WRITEME);
close(README);



################################################################################
#
# Below are all subroutines defined, which org.tigris.mbt will need to run the
# model: keepass.graphml
#
################################################################################

sub e_Cancel
{
print "sub: e_Cancel\n";
}

sub e_CloseApp
{
print "sub: e_CloseApp\n";
}

sub e_CloseDB
{
print "sub: e_CloseDB\n";
}

sub e_CloseDialog
{
print "sub: e_CloseDialog\n";
}

sub e_EnterCorrectKey
{
print "sub: e_EnterCorrectKey\n";
}

sub e_EnterInvalidKey
{
print "sub: e_EnterInvalidKey\n";
}

sub e_Initialize
{
print "sub: e_Initialize\n";
}

sub e_No
{
print "sub: e_No\n";
}

sub e_Start
{
print "sub: e_Start\n";
}

sub e_StartWithDatabase
{
print "sub: e_StartWithDatabase\n";
}

sub e_Yes
{
print "sub: e_Yes\n";
}

sub v_EnterMasterCompositeMasterKey
{
print "sub: v_EnterMasterCompositeMasterKey\n";
}

sub v_InvalidKey
{
print "sub: v_InvalidKey\n";
}

sub v_KeePassNotRunning
{
print "sub: v_KeePassNotRunning\n";
}

sub v_MainWindowEmpty
{
print "sub: v_MainWindowEmpty\n";
}

sub v_MainWindow_DB_Loaded
{
print "sub: v_MainWindow_DB_Loaded\n";
}

sub v_SaveBeforeCloseLock
{
print "sub: v_SaveBeforeCloseLock\n";
#$testFailed = 1;
}