Perl
The following are two sample Perl programs that allow you to issue iTQLTM commands. The first uses Inline Java, the second uses SOAP Lite.
For the first example, ensure you have the following installed:
- Inline package for Perl
- Inline::Java
- driver-1.1.0.jar in the
Resources
directory of your Mulgara installation - log4j-1.2.8.jar or higher
Ensure that the Inline::Java test suite operates correctly and that Mulgara is running.
Note - In the program below, replace mulgaradirectory
, ApacheLog4Jdirectory
and mysite.com
(highlighted in bold), with the appropriate settings for your local environment.
use CGI;
use Inline (
Java => <<'END',
// Java 2 standard packages
import java.sql.*;
// Mulgara packages
import org.mulgara.itql.ItqlInterpreterBean;
import org.mulgara.query.Answer;
// Logging
import org.apache.log4j.*;
/**
* iTQL Perl Bean. <p>
*
* This class provides a simple interface for the execution of iTQL queries
* via Perl using Inline Java 4.0
* </p>
*
* @created 2004-March-01
*
* @author <a href="http://staff.PIsoftware.com/tate/">Tate Jones</a>
*
* @version $Revision: 1.5 $
*
* @modified $Date: 2004/12/22 05:01:58 $ by $Author: newmana $
*
* @copyrights ©2001 <a href="http://www.pisoftware.com/">Tucana Technologies, Inc.</a>
*
* @licence <a href="http://www.mozilla.org/MPL/MPL-1.1.html">Mozilla Public License v1.1</a>
*/
public class iTQLPerlBean {
/** iTQL Bean used to query Mulgara */
private ItqlInterpreterBean interpreter;
/** Answer object to hold the result of an iTQL query */
Answer answer = null;
/**
* Creates a iTQL interpreter bean for queries.
*
*/
public iTQLPerlBean() {
// Initialise logging
BasicConfigurator.configure();
// Set logging level
Logger.getRootLogger().
setLevel( Level.WARN );
// Create the iTQL Bean to use for queries
interpreter = new ItqlInterpreterBean();
}
/**
* Executes an iTQL query.
*
* @param query String containing the iTQL query
* @return the Answer containing the result of the query.
*/
public void execute(String query) {
try {
// close any previous answer
this.closeAnswer();
// Do the query
answer = interpreter.executeQuery(query);
// move to the first row
if ( answer != null ) {
answer.beforeFirst();
}
}
catch (Exception e) {
System.err.println("iTQLInterpreterBean Error : ");
e.printStackTrace(System.err);
}
}
/**
* Return an answer row as a Perl Array.
*
* @param answer Answer
* @return String arrary for conversion to a perl array
**/
public String[] getRow() {
String columns[] = null;
try {
if ( answer != null && answer.next() ) {
// initialise the number of columns
int numberOfColumns = answer.getNumberOfVariables();
columns = new String[numberOfColumns];
// populate the array with the column values
for ( int column=0; column < numberOfColumns; column++ ) {
columns[column] = answer.getObject(column).toString();
}
}
}
catch (Exception e) {
System.err.println("iTQLInterpreterBean fetchRow error : ");
e.printStackTrace(System.err);
}
return columns;
}
/**
* Close the answer to reclaim resources.
*
*/
public void closeAnswer() {
try {
// close the answer
if ( answer != null ) {
answer.close();
}
}
catch (Exception e) {
System.err.println("iTQLInterpreterBean Error : ");
e.printStackTrace(System.err);
}
}
}
END
DIRECTORY => '/tmp',
CLASSPATH =>
'/mulgaradirectory/driver-2.0.jar:/ApacheLog4Jdirectory/log4j-1.2.8.jar',
) ;
# Create a iTQL session bean for executing queries
my $bean = new iTQLPerlBean();
# Execute the query via the bean
$bean->execute('select $s $p $o from <rmi://mysite.com/server1#> where $s $p $o ;');
# Print out the results until empty
while ( my $cols = $bean->getRow() ) {
print($cols->[0] . " ");
print($cols->[1] . " ");
print($cols->[2] . "");
}
# Close answer resources
$bean->closeAnswer();
As stated previously, this second example uses SOAP Lite.
#!usr/bin/perl
# -*- perl-indent-level: 2 -*-
#
# SoapModule.pm
#
# $Id: 746.htm,v 1.5 2004/12/22 05:01:58 newmana Exp $
#
use 5.006;
use warnings;
use SOAP::Lite;
1;
my $serverEnd =
"http://mysite.com:8080/webservices/services/ItqlBeanService";
my $iTQLcommand = "select \$subject \$predicate \$object from
<rmi://mysite.com/server1#example> where \$subject \$predicate \$object ;";
print soapProcess($serverEnd, $iTQLcommand);
sub soapProcess {
my ($serverSoapEndpoint, $iTQLcommand) = @_;
my $resultString = SOAP::Lite
-> uri($serverSoapEndpoint)
-> proxy($serverSoapEndpoint)
-> executeQueryToString($iTQLcommand)
-> result;
return $resultString;
}