From c701e931456ed34bd1468c33029ff17634f5f65a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Mon, 17 Feb 2014 16:45:46 +0000
Subject: [PATCH] - Made TransformQuery accepting standard input as well as
 parameter

---
 .../exmo/align/cli/TransformQuery.java        | 65 ++++++++++---------
 test/clitest.sh                               | 62 ++++++++++++++----
 2 files changed, 85 insertions(+), 42 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/cli/TransformQuery.java b/src/fr/inrialpes/exmo/align/cli/TransformQuery.java
index 8be1ba5b..a90b0d37 100644
--- a/src/fr/inrialpes/exmo/align/cli/TransformQuery.java
+++ b/src/fr/inrialpes/exmo/align/cli/TransformQuery.java
@@ -48,10 +48,11 @@ import org.semanticweb.owl.align.AlignmentException;
 
 /**
  * Transform a query according to an alignment
- * TransformQuery alignmentURI -q query [-e]
- * would be better with:
  * TransformQuery [-a alignmentURI] [-e] [-q query] < query
- * definitely...todo
+ * Query can be taken from (priority):
+ * - parameter
+ * - a queryfile (-q)
+ * - standard input
  */
 
 public class TransformQuery extends CommonCLI {
@@ -62,6 +63,7 @@ public class TransformQuery extends CommonCLI {
 	options.addOption( "e", "echo", false, "Echo the input query" );
 	//options.addOption( OptionBuilder.withLongOpt( "process" ).hasArg().withDescription( "Process the query against a particular CLASS" ).withArgName("CLASS").create( 'p' ) );
 	options.addOption( OptionBuilder.withLongOpt( "query" ).hasArg().withDescription( "get the query from the corresponding FILE" ).withArgName("FILE").create( 'q' ) );
+	options.addOption( OptionBuilder.withLongOpt( "alignment" ).hasArg().withDescription( "use the alignment identified by URI" ).withArgName("URI").create( 'a' ) );
     }
 
     public static void main(String[] args) {
@@ -72,7 +74,7 @@ public class TransformQuery extends CommonCLI {
     public void run(String[] args) throws Exception {
 	BasicAlignment al = null;
 	String alignmentURL = null;
-	String query = "";
+	String query = null;
 	String result = null;
 	//String processorClass = null;
 	String queryFile = null;
@@ -86,14 +88,11 @@ public class TransformQuery extends CommonCLI {
 	    // Here deal with command specific arguments	    
 	    //if ( line.hasOption( 'p' ) ) processorClass = line.getOptionValue( 'p' );
 	    if ( line.hasOption( 'q' ) ) queryFile = line.getOptionValue( 'q' );
+	    if ( line.hasOption( 'a' ) ) alignmentURL = line.getOptionValue( 'a' );
 	    if ( line.hasOption( 'e' ) ) echo = true;
 	    String[] argList = line.getArgs();
 	    if ( argList.length > 0 ) {
-		alignmentURL = argList[0];
-	    } else {
-		logger.error("Require the alignement URL");
-		usage();
-		System.exit(-1);
+		query = argList[0];
 	    }
 	} catch( ParseException exp ) {
 	    logger.error( exp.getMessage() );
@@ -102,29 +101,34 @@ public class TransformQuery extends CommonCLI {
 	}
 
 	try {
-
-	    try {
-		InputStream in = new FileInputStream( queryFile );
-		BufferedReader reader =
-		    new BufferedReader(new InputStreamReader(in));
-		String line = null;
-		while ((line = reader.readLine()) != null) {
-		    query += line + "\n";
+	    if ( query == null ) {
+		query = "";
+		try {
+		    InputStream in = (queryFile!=null)?new FileInputStream( queryFile ):System.in;
+		    BufferedReader reader = new BufferedReader( new InputStreamReader(in) );
+		    String line = null;
+		    while ((line = reader.readLine()) != null) {
+			query += line + "\n";
+		    }
+		} catch (IOException x) {
+		    System.err.println(x);
+		    System.exit( -1 );
+		    //} finally {
 		}
-	    } catch (IOException x) {
-		System.err.println(x);
-		System.exit( -1 );
-		//} finally {
 	    }
 
-	    // load the alignment
-	    AlignmentParser aparser = new AlignmentParser();
-	    al = (BasicAlignment)aparser.parse( alignmentURL );
-
 	    if ( echo ) System.out.println( query );
 
-	    // Create query processor
-	    result = al.rewriteQuery( query, parameters );
+	    // load the alignment
+	    if ( alignmentURL != null ) {
+		AlignmentParser aparser = new AlignmentParser();
+		al = (BasicAlignment)aparser.parse( alignmentURL );
+
+		// Create query processor
+		result = al.rewriteQuery( query, parameters );
+	    } else {
+		result = query;
+	    }
 	    
 	    // Set output file
 	    OutputStream stream;
@@ -143,9 +147,8 @@ public class TransformQuery extends CommonCLI {
 	    } finally {
 		writer.flush();
 		writer.close();
-	    }	    
-	    
-	} catch (Exception ex) {
+	    }}
+	    catch (Exception ex) {
 	    ex.printStackTrace();
 	    logger.error( ex.getMessage() );
 	    System.exit(-1);
@@ -153,6 +156,6 @@ public class TransformQuery extends CommonCLI {
     }
 
     public void usage() {
-	usage( "java "+this.getClass().getName()+" [options] alignmentURI query\nTransforms the given query and transforms it according to the alignment" );
+	usage( "java "+this.getClass().getName()+" [options] QUERY\nTransforms the given QUERY according to an alignment" );
     }
 }
diff --git a/test/clitest.sh b/test/clitest.sh
index 0fdabaad..4c760b9b 100644
--- a/test/clitest.sh
+++ b/test/clitest.sh
@@ -19,8 +19,6 @@ mkdir -p $RESDIR
 # GOTO
 if false; then
 echo this is for avoiding some parts
-#GOTO
-fi
 
 echo "\t\tTHE -z OPTIONS RELY ON LOGBACK BEING PROPERLY DEFINED"
 echo "\t\t *** Testing Procalign ***"
@@ -939,6 +937,9 @@ echo "\t(same as Procalign)"
 echo "\t-P,--params <FILE>"
 echo "\t(same as Procalign)"
 
+#GOTO
+fi
+
 ########################################################################
 # TransformQuery
 ########################################################################
@@ -962,9 +963,39 @@ java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery --help &> $RESDIR/queryt
 if [ -s  $RESDIR/queryt-h.txt ]; then diff $RESDIR/queryt-h.txt $RESDIR/queryt-help.txt; else echo error with $RESDIR/queryt-h.txt; fi
 
 #-------------------
-echo "\tno-op"
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery &> $RESDIR/queryt-noop.rdf
-if [ -s  $RESDIR/queryt-noop.xml ]; then echo error with TRANSQ-NOOP; fi
+echo "\tno-op (not testable because wait for input)"
+#java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery &> $RESDIR/queryt-noop.rdf
+#if [ ! -s  $RESDIR/queryt-noop.xml ]; then echo error with TRANSQ-NOOP; fi
+
+#-------------------
+echo "\t<QUERY>"
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery 'PREFIX dt:   <http://example.org/datatype#> .
+PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+PREFIX xmls:  <http://www.w3.org/2001/XMLSchema#> .
+PREFIX foaf:  <http://xmlns.com/foaf/0.1/> .
+PREFIX onto1: <http://www.example.org/ontology1#> .
+
+SELECT *
+FROM XXX
+WHERE {
+    ?X rdf:type <http://www.example.org/ontology1#reviewedarticle>.
+    ?X rdf:type onto1:reviewedarticle .
+  }' > $RESDIR/queryt-q0.rdf
+diff $RESDIR/queryt-q0.rdf $CWD/examples/rdf/query.sparql
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -a file://$CWD/examples/rdf/newsample.rdf 'PREFIX dt:   <http://example.org/datatype#> .
+PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+PREFIX xmls:  <http://www.w3.org/2001/XMLSchema#> .
+PREFIX foaf:  <http://xmlns.com/foaf/0.1/> .
+PREFIX onto1: <http://www.example.org/ontology1#> .
+
+SELECT *
+FROM XXX
+WHERE {
+    ?X rdf:type <http://www.example.org/ontology1#reviewedarticle>.
+    ?X rdf:type onto1:reviewedarticle .
+  }' > $RESDIR/queryt-q1.rdf
+if [ -s $RESDIR/queryt-q1.rdf ]; then diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-noop.rdf > $RESDIR/diff-query1.txt; else echo error with TRANSQ-QUERY1; fi
+if [ ! -s $RESDIR/diff-query1.txt ]; then echo error with TRANSQ-QUERY2; fi
 
 #-------------------
 echo "\t-q,--query <FILE>"
@@ -974,26 +1005,34 @@ if [ ! -s $RESDIR/diff-query1.txt ]; then echo error with TRANSQ-QUERY2; fi
 java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql > $RESDIR/queryt-q2.rdf
 diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-q2.rdf
 
+#-------------------
+echo "\t-a,--alignment <URI>"
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -a file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql > $RESDIR/queryt-q1.rdf
+if [ -s $RESDIR/queryt-q1.rdf ]; then diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-noop.rdf > $RESDIR/diff-query1.txt; else echo error with TRANSQ-QUERY1; fi
+if [ ! -s $RESDIR/diff-query1.txt ]; then echo error with TRANSQ-QUERY2; fi
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery --alignment file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql > $RESDIR/queryt-q2.rdf
+diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-q2.rdf
+
 #-------------------
 echo "\t-o <F>, --output <F>"
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -o $RESDIR/queryt-o1.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -a file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -o $RESDIR/queryt-o1.rdf
 if [ -s $RESDIR/queryt-o1.rdf ]; then diff $RESDIR/queryt-o1.rdf $RESDIR/queryt-noop.rdf > $RESDIR/diff-output1.txt; else echo error with TRANSQ-OUTPUT1; fi
 if [ ! -s $RESDIR/diff-output1.txt ]; then echo error with TRANSQ-OUTPUT2; fi
 diff $RESDIR/queryt-o1.rdf $RESDIR/queryt-q1.rdf
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql --output $RESDIR/queryt-o2.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery --alignment file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql --output $RESDIR/queryt-o2.rdf
 diff $RESDIR/queryt-o1.rdf $RESDIR/queryt-o2.rdf
 
 #-------------------
 echo "\t-e,--echo"
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -e > $RESDIR/queryt-e1.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -a file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -e > $RESDIR/queryt-e1.rdf
 if [ -s $RESDIR/queryt-e1.rdf ]; then diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-e1.rdf > $RESDIR/diff-echo1.txt; else echo error with TRANSQ-ECHO1; fi
 if [ ! -s $RESDIR/diff-echo1.txt ]; then echo error with TRANSQ-ECHO2; fi
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql --echo > $RESDIR/queryt-e2.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery --alignment file://$CWD/examples/rdf/newsample.rdf --query $CWD/examples/rdf/query.sparql --echo > $RESDIR/queryt-e2.rdf
 diff $RESDIR/queryt-e1.rdf $RESDIR/queryt-e2.rdf
 
 #-------------------
 echo "\t-Dn=v (used for prefix)"
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -Donto2=http://www.example.org/ontology2# -o $RESDIR/queryt-d1.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -a file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -Donto2=http://www.example.org/ontology2# -o $RESDIR/queryt-d1.rdf
 if [ -s $RESDIR/queryt-d1.rdf ]; then diff $RESDIR/queryt-q1.rdf $RESDIR/queryt-d1.rdf > $RESDIR/diff-prefix1.txt; else echo error with TRANSQ-PREFIX1; fi
 if [ ! -s $RESDIR/diff-prefix1.txt ]; then echo error with TRANSQ-PREFIX2; fi
 
@@ -1004,10 +1043,11 @@ echo "<?xml version='1.0' encoding='utf-8' standalone='no'?>
 <properties>
 <entry key=\"onto2\">http://www.example.org/ontology2#</entry>
 </properties>" > $RESDIR/params.xml
-java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -P $RESDIR/params.xml file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -o $RESDIR/queryt-P1.rdf
+java -cp $CP fr.inrialpes.exmo.align.cli.TransformQuery -P $RESDIR/params.xml -a file://$CWD/examples/rdf/newsample.rdf -q $CWD/examples/rdf/query.sparql -o $RESDIR/queryt-P1.rdf
 if [ -s $RESDIR/queryt-P1.rdf ]; then diff $RESDIR/queryt-P1.rdf $RESDIR/queryt-d1.rdf > $RESDIR/diff-prefix2.txt; else echo error with TRANSQ-PREFIX3; fi
 if [ -s $RESDIR/diff-prefix2.txt ]; then echo error with TRANSQ-PREFIX4; fi
 
+exit
 ########################################################################
 # AlignmentService
 ########################################################################
-- 
GitLab