X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Forg%2Fz3950%2Fzing%2Fcql%2FCQLGenerator.java;h=7cde23e845caa0fbaba4640668a9adcd23557f56;hb=af923f9b50aecdbf896af612e733817f96c4388a;hp=bc732e27859065cd77ccdd9dfb8e3d76f3ceb1b7;hpb=70be8275aabe87c909bf11148bb279c3e1380bb2;p=cql-java-moved-to-github.git
diff --git a/src/org/z3950/zing/cql/CQLGenerator.java b/src/org/z3950/zing/cql/CQLGenerator.java
index bc732e2..7cde23e 100644
--- a/src/org/z3950/zing/cql/CQLGenerator.java
+++ b/src/org/z3950/zing/cql/CQLGenerator.java
@@ -1,4 +1,4 @@
-// $Id: CQLGenerator.java,v 1.1 2002-10-30 09:19:26 mike Exp $
+// $Id: CQLGenerator.java,v 1.7 2007-06-27 22:39:55 mike Exp $
package org.z3950.zing.cql;
import java.util.Properties;
@@ -18,11 +18,11 @@ import java.io.FileNotFoundException;
* the parser of your choice, and check that the XCQL it comes up with
* is the same what you got from your initial rendering.
*
- * This code is based on the grammar in the Grammar file of
+ * This code is based on the same grammar as the CQLParser class in
* this distribution - there is a generate_x() method
* for each grammar element X.
*
- * @version $Id: CQLGenerator.java,v 1.1 2002-10-30 09:19:26 mike Exp $
+ * @version $Id: CQLGenerator.java,v 1.7 2007-06-27 22:39:55 mike Exp $
* @see http://zing.z3950.org/cql/index.html
*/
@@ -64,7 +64,7 @@ public class CQLGenerator {
* [mandatory] A floating-point number between 0.0 and 1.0,
* indicating the probability for each search-clause
* node that it will be expanded into a full sub-query rather
- * than a [ qualifier relation ] term triplet.
+ * than an [ index relation ] term triplet.
*
*
*
proxOp
@@ -125,17 +125,18 @@ public class CQLGenerator {
* method, or decompiled into CQL using its toCQL
* method.
*/
- public CQLNode generate() throws ParameterMissingException {
+ public CQLNode generate() throws MissingParameterException {
return generate_cql_query();
}
- private CQLNode generate_cql_query() throws ParameterMissingException {
+ private CQLNode generate_cql_query() throws MissingParameterException {
if (!maybe("complexQuery")) {
return generate_search_clause();
}
CQLNode node1 = generate_cql_query();
CQLNode node2 = generate_search_clause();
+ // ### should generate prefix-mapping nodes
if (maybe("proxOp")) {
// ### generate proximity nodes
} else {
@@ -149,46 +150,48 @@ public class CQLGenerator {
return generate_search_clause();
}
- private CQLNode generate_search_clause() throws ParameterMissingException {
+ private CQLNode generate_search_clause() throws MissingParameterException {
if (maybe("complexClause")) {
return generate_cql_query();
}
- String qualifier = generate_qualifier();
- String relation = generate_relation();
+ // ### Should sometimes generate index/relation-free terms
+ String index = generate_index();
+ CQLRelation relation = generate_relation();
String term = generate_term();
- return new CQLTermNode(qualifier, relation, term);
+ return new CQLTermNode(index, relation, term);
}
// ### Should probably be more configurable
- private String generate_qualifier() {
- String qualifier = ""; // shut up compiler warning
+ private String generate_index() {
+ String index = ""; // shut up compiler warning
if (rnd.nextInt(2) == 0) {
switch (rnd.nextInt(3)) {
- case 0: qualifier = "dc.author"; break;
- case 1: qualifier = "dc.title"; break;
- case 2: qualifier = "dc.subject"; break;
+ case 0: index = "dc.author"; break;
+ case 1: index = "dc.title"; break;
+ case 2: index = "dc.subject"; break;
}
} else {
switch (rnd.nextInt(4)) {
- case 0: qualifier = "bath.author"; break;
- case 1: qualifier = "bath.title"; break;
- case 2: qualifier = "bath.subject"; break;
- case 3: qualifier = "foo>bar"; break;
+ case 0: index = "bath.author"; break;
+ case 1: index = "bath.title"; break;
+ case 2: index = "bath.subject"; break;
+ case 3: index = "foo>bar"; break;
}
}
- return qualifier;
+ return index;
}
- // ### Representation of relations will change when we handle modifiers
- private String generate_relation() throws ParameterMissingException {
- return generate_base_relation();
+ private CQLRelation generate_relation() throws MissingParameterException {
+ String base = generate_base_relation();
+ CQLRelation rel = new CQLRelation(base);
// ### should generate modifiers too
+ return rel;
}
- private String generate_base_relation() throws ParameterMissingException {
+ private String generate_base_relation() throws MissingParameterException {
if (maybe("equalsRelation")) {
return "=";
} else if (maybe("numericRelation")) {
@@ -240,10 +243,10 @@ public class CQLGenerator {
return ""; // shut up compiler warning
}
- boolean maybe(String param) throws ParameterMissingException {
+ boolean maybe(String param) throws MissingParameterException {
String probability = params.getProperty(param);
if (probability == null)
- throw new ParameterMissingException(param);
+ throw new MissingParameterException(param);
double dice = rnd.nextDouble();
double threshhold = new Double(probability).doubleValue();
@@ -258,13 +261,14 @@ public class CQLGenerator {
* A simple test-harness for the generator.
*
* It generates a single random query using the parameters
- * specified in a nominated properties file, and decompiles it
- * into CQL which is written to standard output.
+ * specified in a nominated properties file, plus any additional
+ * name value pairs provided on the command-line, and
+ * decompiles it into CQL which is written to standard output.
*
* For example,
- * java org.z3950.zing.cql.CQLGenerator etc/generate.properties
+ * java org.z3950.zing.cql.CQLGenerator
+ * etc/generate.properties seed 18398,
* where the file generate.properties contains:
- * seed=18398
* complexQuery=0.4
* complexClause=0.4
* equalsRelation=0.5
@@ -279,25 +283,32 @@ public class CQLGenerator {
* @param configFile
* The name of a properties file from which to read the
* configuration parameters (see above).
+ * @param name
+ * The name of a configuration parameter.
+ * @param value
+ * The value to assign to the configuration parameter named in
+ * the immediately preceding command-line argument.
* @return
* A CQL query expressed in a form that should be comprehensible
* to all conformant CQL compilers.
*/
public static void main (String[] args) throws Exception {
- if (args.length != 1) {
- System.err.println("Usage: CQLGenerator ");
+ if (args.length % 2 != 1) {
+ System.err.println("Usage: CQLGenerator "+
+ "[ ]...");
System.exit(1);
}
String configFile = args[0];
InputStream f = new FileInputStream(configFile);
if (f == null)
- throw new FileNotFoundException("getResourceAsStream(" +
- configFile + ")");
+ throw new FileNotFoundException(configFile);
Properties params = new Properties();
params.load(f);
f.close();
+ for (int i = 1; i < args.length; i += 2)
+ params.setProperty(args[i], args[i+1]);
CQLGenerator generator = new CQLGenerator(params);
CQLNode tree = generator.generate();