[Mulgara-svn] r1620 - branches/consistency/src/jar/krule/java/org/mulgara/krule

pag at mulgara.org pag at mulgara.org
Tue Mar 17 19:54:24 UTC 2009


Author: pag
Date: 2009-03-17 12:54:23 -0700 (Tue, 17 Mar 2009)
New Revision: 1620

Modified:
   branches/consistency/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
Log:
Using APIs to generate queries. No longer parsing queries to generate the Query objects.

Modified: branches/consistency/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
===================================================================
--- branches/consistency/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2009-03-17 19:53:35 UTC (rev 1619)
+++ branches/consistency/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2009-03-17 19:54:23 UTC (rev 1620)
@@ -24,11 +24,12 @@
 // Third party packages
 import org.apache.log4j.Logger;
 import org.jrdf.graph.*;
+import org.jrdf.vocabulary.RDF;
 
 // Locally written packages
-import org.mulgara.itql.*;
 import org.mulgara.query.*;
 import org.mulgara.query.rdf.LiteralImpl;
+import org.mulgara.query.rdf.Mulgara;
 import org.mulgara.query.rdf.TripleImpl;
 import org.mulgara.query.rdf.URIReferenceImpl;
 import org.mulgara.resolver.OperationContext;
@@ -54,20 +55,17 @@
   /** The database session for querying. */
   private OperationContext operationContext;
 
-  /** The interpreter for parsing queries. */
-  private TqlInterpreter interpreter;
-
   /** The rules. */
   private RuleStructure rules;
 
-  /** The URI of the model containing the rule data. */
-  private URI ruleModel;
+  /** The Graph resource represented by ruleGraphUri. */
+  private final GraphResource ruleGraph;
 
-  /** The URI of the model containing the base data. */
-  private URI baseModel;
+  /** The URI of the graph containing the base data. */
+  private URI baseGraphUri;
 
-  /** The URI of the model to receive the entailed data. */
-  private URI destModel;
+  /** The URI of the graph to receive the entailed data. */
+  private URI destGraphUri;
 
   /** A map of namespace names to the URIs. */
   private Map<String,URI> aliases;
@@ -120,6 +118,9 @@
   /** URI for constraints predicate in a query. */
   private static final String HAS_WHERE_CLAUSE_STR = KRULE + "hasWhereClause";
 
+  /** URI for having constraints predicate in a query. */
+  private static final String HAS_HAVING_CLAUSE_STR = KRULE + "hasHavingClause";
+
   /** URI for the argument property. */
   private static final String ARGUMENT_STR = KRULE + "argument";
 
@@ -141,6 +142,9 @@
   /** URI for the simple constraint type. */
   private static final String SIMPLE_CONSTRAINT_STR = KRULE + "SimpleConstraint";
 
+  /** URI for the transitive constraint type. */
+  private static final String TRANSITIVE_CONSTRAINT_STR = KRULE + "TransitiveConstraint";
+
   /** URI for difference type. */
   private static final String DIFFERENCE_STR = KRULE + "Difference";
 
@@ -163,96 +167,110 @@
   private static final String QUERY_STR = KRULE + "Query";
 
   /** RDF reference for constraint subject. */
-  public static final URIReference HAS_SUBJECT = new URIReferenceImpl(URI.create(HAS_SUBJECT_STR));
+  public static final URIReferenceImpl HAS_SUBJECT = new URIReferenceImpl(URI.create(HAS_SUBJECT_STR));
 
   /** RDF reference for constraint predicate. */
-  public static final URIReference HAS_PREDICATE = new URIReferenceImpl(URI.create(HAS_PREDICATE_STR));
+  public static final URIReferenceImpl HAS_PREDICATE = new URIReferenceImpl(URI.create(HAS_PREDICATE_STR));
 
   /** RDF reference for constraint object. */
-  public static final URIReference HAS_OBJECT = new URIReferenceImpl(URI.create(HAS_OBJECT_STR));
+  public static final URIReferenceImpl HAS_OBJECT = new URIReferenceImpl(URI.create(HAS_OBJECT_STR));
 
   /** RDF reference for constraint model. */
-  public static final URIReference HAS_GRAPH = new URIReferenceImpl(URI.create(HAS_GRAPH_STR));
+  public static final URIReferenceImpl HAS_GRAPH = new URIReferenceImpl(URI.create(HAS_GRAPH_STR));
 
   /** RDF reference for query property on rules. */
-  public static final URIReference HAS_QUERY = new URIReferenceImpl(URI.create(HAS_QUERY_STR));
+  public static final URIReferenceImpl HAS_QUERY = new URIReferenceImpl(URI.create(HAS_QUERY_STR));
 
   /** RDF reference for axiom subject. */
-  public static final URIReference AXIOM_SUBJECT = new URIReferenceImpl(URI.create(AXIOM_SUBJECT_STR));
+  public static final URIReferenceImpl AXIOM_SUBJECT = new URIReferenceImpl(URI.create(AXIOM_SUBJECT_STR));
 
   /** RDF reference for axiom predicate. */
-  public static final URIReference AXIOM_PREDICATE = new URIReferenceImpl(URI.create(AXIOM_PREDICATE_STR));
+  public static final URIReferenceImpl AXIOM_PREDICATE = new URIReferenceImpl(URI.create(AXIOM_PREDICATE_STR));
 
   /** RDF reference for axiom object. */
-  public static final URIReference AXIOM_OBJECT = new URIReferenceImpl(URI.create(AXIOM_OBJECT_STR));
+  public static final URIReferenceImpl AXIOM_OBJECT = new URIReferenceImpl(URI.create(AXIOM_OBJECT_STR));
 
   /** RDF reference for rule triggering predicate. */
-  public static final URIReference TRIGGERS = new URIReferenceImpl(URI.create(TRIGGERS_STR));
+  public static final URIReferenceImpl TRIGGERS = new URIReferenceImpl(URI.create(TRIGGERS_STR));
 
   /** RDF reference for selection variables predicate. */
-  public static final URIReference SELECTION_VARS = new URIReferenceImpl(URI.create(SELECTION_VARS_STR));
+  public static final URIReferenceImpl SELECTION_VARS = new URIReferenceImpl(URI.create(SELECTION_VARS_STR));
 
   /** RDF reference for hasWhereClause predicate. */
-  public static final URIReference HAS_WHERE_CLAUSE = new URIReferenceImpl(URI.create(HAS_WHERE_CLAUSE_STR));
+  public static final URIReferenceImpl HAS_WHERE_CLAUSE = new URIReferenceImpl(URI.create(HAS_WHERE_CLAUSE_STR));
 
+  /** RDF reference for hasHavingClause predicate. */
+  public static final URIReferenceImpl HAS_HAVING_CLAUSE = new URIReferenceImpl(URI.create(HAS_HAVING_CLAUSE_STR));
+
   /** RDF reference for the argument property. */
-  public static final URIReference ARGUMENT = new URIReferenceImpl(URI.create(ARGUMENT_STR));
+  public static final URIReferenceImpl ARGUMENT = new URIReferenceImpl(URI.create(ARGUMENT_STR));
 
   /** RDF reference for the transitive constraint argument. */
-  public static final URIReference TRANSITIVE_ARGUMENT = new URIReferenceImpl(URI.create(TRANSITIVE_ARGUMENT_STR));
+  public static final URIReferenceImpl TRANSITIVE_ARGUMENT = new URIReferenceImpl(URI.create(TRANSITIVE_ARGUMENT_STR));
 
   /** RDF reference for the transitive constraint anchor argument. */
-  public static final URIReference ANCHOR_ARGUMENT = new URIReferenceImpl(URI.create(ANCHOR_ARGUMENT_STR));
+  public static final URIReferenceImpl ANCHOR_ARGUMENT = new URIReferenceImpl(URI.create(ANCHOR_ARGUMENT_STR));
 
   /** RDF reference for the name argument. */
-  public static final URIReference NAME = new URIReferenceImpl(URI.create(NAME_STR));
+  public static final URIReferenceImpl NAME = new URIReferenceImpl(URI.create(NAME_STR));
 
   /** RDF reference for constraint conjunction class. */
-  public static final URIReference CONSTRAINT_CONJUNCTION = new URIReferenceImpl(URI.create(CONSTRAINT_CONJUNCTION_STR));
+  public static final URIReferenceImpl CONSTRAINT_CONJUNCTION = new URIReferenceImpl(URI.create(CONSTRAINT_CONJUNCTION_STR));
 
   /** RDF reference for constraint disjunction class. */
-  public static final URIReference CONSTRAINT_DISJUNCTION = new URIReferenceImpl(URI.create(CONSTRAINT_DISJUNCTION_STR));
+  public static final URIReferenceImpl CONSTRAINT_DISJUNCTION = new URIReferenceImpl(URI.create(CONSTRAINT_DISJUNCTION_STR));
 
   /** RDF reference for the simple constraint type. */
-  public static final URIReference SIMPLE_CONSTRAINT = new URIReferenceImpl(URI.create(SIMPLE_CONSTRAINT_STR));
+  public static final URIReferenceImpl SIMPLE_CONSTRAINT = new URIReferenceImpl(URI.create(SIMPLE_CONSTRAINT_STR));
 
+  /** RDF reference for the transitive constraint type. */
+  public static final URIReferenceImpl TRANSITIVE_CONSTRAINT = new URIReferenceImpl(URI.create(TRANSITIVE_CONSTRAINT_STR));
+
   /** RDF reference for the Difference type. */
-  public static final URIReference DIFFERENCE = new URIReferenceImpl(URI.create(DIFFERENCE_STR));
+  public static final URIReferenceImpl DIFFERENCE = new URIReferenceImpl(URI.create(DIFFERENCE_STR));
 
   /** RDF reference for the Value type. */
-  public static final URIReference URI_REF = new URIReferenceImpl(URI.create(URI_REF_STR));
+  public static final URIReferenceImpl URI_REF = new URIReferenceImpl(URI.create(URI_REF_STR));
 
   /** RDF reference for the Variable type. */
-  public static final URIReference VARIABLE = new URIReferenceImpl(URI.create(VARIABLE_STR));
+  public static final URIReferenceImpl VARIABLE = new URIReferenceImpl(URI.create(VARIABLE_STR));
 
   /** RDF reference for the Literal type. */
-  public static final URIReference LITERAL = new URIReferenceImpl(URI.create(LITERAL_STR));
+  public static final URIReferenceImpl LITERAL = new URIReferenceImpl(URI.create(LITERAL_STR));
 
   /** RDF reference for the Axiom type. */
-  public static final URIReference AXIOM = new URIReferenceImpl(URI.create(AXIOM_STR));
+  public static final URIReferenceImpl AXIOM = new URIReferenceImpl(URI.create(AXIOM_STR));
 
   /** RDF reference for the Rule type. */
-  public static final URIReference RULE = new URIReferenceImpl(URI.create(RULE_STR));
+  public static final URIReferenceImpl RULE = new URIReferenceImpl(URI.create(RULE_STR));
 
   /** RDF reference for the Query type. */
-  public static final URIReference QUERY = new URIReferenceImpl(URI.create(QUERY_STR));
+  public static final URIReferenceImpl QUERY = new URIReferenceImpl(URI.create(QUERY_STR));
 
+  /** RDF reference for rdf:type. */
+  public static final URIReferenceImpl RDF_TYPE = new URIReferenceImpl(RDF.TYPE);
 
+  /** RDF reference for rdf:value. */
+  public static final URIReferenceImpl RDF_VALUE = new URIReferenceImpl(RDF.VALUE);
+
+  /** A field used in queries to indicate no prior constraints on the answer. */
+  private static final UnconstrainedAnswer UNCONSTRAINED = new UnconstrainedAnswer();
+
   /**
    * Principle constructor.
    *
-   * @param ruleModel The name of the model with the rules to run.
-   * @param baseModel The name of the model with the base data.
-   * @param destModel The name of the model which will receive the entailed data.
+   * @param ruleGraphUri The name of the graph with the rules to run.
+   * @param baseGraphUri The name of the graph with the base data.
+   * @param destGraphUri The name of the graph which will receive the entailed data.
    */
-  KruleLoader(URI ruleModel, URI baseModel, URI destModel) {
-    this.ruleModel = ruleModel;
-    this.baseModel = baseModel;
-    this.destModel = destModel;
+  KruleLoader(URI ruleGraphUri, URI baseGraphUri, URI destGraphUri) {
+    this.baseGraphUri = baseGraphUri;
+    this.destGraphUri = destGraphUri;
 
+    ruleGraph = new GraphResource(ruleGraphUri);
+
     // set the query objects to null
     operationContext = null;
-    interpreter = null;
 
     // initialize the aliases
     newAliases();
@@ -286,9 +304,6 @@
   public Rules readRules(Object opContextParam, URI systemModel) throws InitializerException, RemoteException {
     this.operationContext = (OperationContext)opContextParam;
 
-    // get a new interpreter
-    interpreter = new TqlInterpreter(aliases);
-
     rules = null;
     try {
       if (logger.isDebugEnabled()) logger.debug("Initializing for rule queries.");
@@ -298,7 +313,7 @@
       if (logger.isDebugEnabled()) logger.debug("Querying for rules");
       rules = findRules();
       // set the target model
-      rules.setTargetModel(destModel);
+      rules.setTargetModel(destGraphUri);
 
       // find the triggers
       loadTriggers();
@@ -363,14 +378,10 @@
    * @throws QueryException When there is an exception finding the rules.
    */
   private RuleStructure findRules() throws QueryException, TuplesException {
-  	Query query;
-    try {
-      // find all of the rules
-      query = interpreter.parseQuery("select $rule from <" + ruleModel + "> where $rule <rdf:type> <krule:Rule> ;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query attempted while finding rules.", e);
-    }
-    
+    // select $rule from <ruleGraph> where $rule <rdf:type> <krule:Rule>
+    Variable ruleV = new Variable("rule");
+    Query query = createQuery(new ConstraintImpl(ruleV, RDF_TYPE, RULE), ruleV);
+
     Answer ruleAnswer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Got response for rule query");
 
@@ -399,12 +410,10 @@
    * @throws InitializerException Data structures did not meet preconditions.
    */
   private void loadTriggers() throws QueryException, TuplesException, InitializerException {
-    Query query;
-    try {
-      query = interpreter.parseQuery("select $src $dest from <" + ruleModel + "> where $src <krule:triggers> $dest ;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while finding triggers.", e);
-    }
+    // select $src $dest from <ruleGraph> where $src <krule:triggers> $dest
+    Variable srcV = new Variable("src");
+    Variable destV = new Variable("dest");
+    Query query = createQuery(new ConstraintImpl(srcV, TRIGGERS, destV), srcV, destV);
 
     Answer answer = query(query);
 
@@ -433,22 +442,36 @@
    */
   private void loadQueries() throws TuplesException, QueryException, KruleStructureException, InitializerException {
     if (logger.isDebugEnabled()) logger.debug("Loading Queries");
+
+    // create some of the loop-invariant resources
+    final URIReferenceImpl sysPrefix = new URIReferenceImpl(URI.create(PREFIX_GRAPH));
+    final URIReferenceImpl mulgaraPrefix = new URIReferenceImpl(Mulgara.PREFIX_URI);
+    final URIReferenceImpl seqPrefix = new URIReferenceImpl(URI.create(RDF.BASE_URI + "_"));
+
     // go through the rules to set their queries
     Iterator<Rule> ri = rules.getRuleIterator();
     while (ri.hasNext()) {
       Rule rule = ri.next();
 
+      // create a resource for this rule
+      URIReferenceImpl ruleRef = new URIReferenceImpl(URI.create(rule.getName()));
       if (logger.isDebugEnabled()) logger.debug("Reading query for rule: " + rule.getName());
-      Query query;
-      try {
-        // get the query data for this rule
-        query = interpreter.parseQuery("select $pre $v $t from <" + ruleModel +
-            "> where <" + rule.getName() + "> <krule:hasQuery> $q and $q <krule:selectionVariables> $vs and" +
-            " $vs $pre $v and $pre <mulgara:prefix> <rdf:_> in <"+ PREFIX_GRAPH +
-            "> and $v <rdf:type> $t ;");
-      } catch (Exception e) {
-        throw new QueryException("Invalid query while getting rule queries.", e);
-      }
+      // select $pre $v $t from <ruleGraph>
+      // where <#ruleRef> <krule:hasQuery> $q and $q <krule:selectionVariables> $vs
+      // and $vs $pre $v and $pre <mulgara:prefix> <rdf:_> in <sys:prefix> and $v <rdf:type> $t
+      Variable qV = new Variable("q");
+      Variable vsV = new Variable("vs");
+      Variable preV = new Variable("pre");
+      Variable vV = new Variable("v");
+      Variable tV = new Variable("t");
+      ConstraintExpression where = new ConstraintConjunction(
+          new ConstraintImpl(ruleRef, HAS_QUERY, qV),
+          new ConstraintImpl(qV, SELECTION_VARS, vsV),
+          new ConstraintImpl(vsV, preV, vV),
+          new ConstraintImpl(preV, mulgaraPrefix, seqPrefix, sysPrefix),
+          new ConstraintImpl(vV, RDF_TYPE, tV)
+      );
+      Query query = createQuery(where, preV, vV, tV);
       Answer answer = query(query);
 
       // get the length of the sequence prefix
@@ -490,13 +513,15 @@
       QueryStruct queryStruct = new QueryStruct(elements, types, aliases, uriReferences, varReferences, literalReferences);
 
       // read in the WHERE reference
-      try {
-        // get the WHERE clause for this rule
-        query = interpreter.parseQuery("select $w from <" + ruleModel +
-            "> where <" + rule.getName() + "> <krule:hasQuery> $q and $q <krule:hasWhereClause> $w;");
-      } catch (Exception e) {
-        throw new QueryException("Invalid query reading WHERE clause for rule: " + rule.getName(), e);
-      }
+
+      // select $w from <ruleGraph>
+      // where <#rule.getName()> <krule:hasQuery> $q and $q <krule:hasWhereClause> $w
+      Variable wV = new Variable("w");
+      where = new ConstraintConjunction(
+          new ConstraintImpl(ruleRef, HAS_QUERY, qV),
+          new ConstraintImpl(qV, HAS_WHERE_CLAUSE, wV)
+      );
+      query = createQuery(where, wV);
       answer = query(query);
 
       try {
@@ -518,7 +543,7 @@
 
       if (logger.isDebugEnabled()) logger.debug("Setting models for the query");
       // set the models
-      queryStruct.setModelExpression(baseModel, destModel);
+      queryStruct.setModelExpression(baseGraphUri, destGraphUri);
 
       if (logger.isDebugEnabled()) logger.debug("Setting query structure for the rule");
       // create a new query and set it for the rule
@@ -539,15 +564,20 @@
   private Set<org.jrdf.graph.Triple> findAxioms() throws TuplesException, QueryException, KruleStructureException, InitializerException {
     if (logger.isDebugEnabled()) logger.debug("Loading Axioms");
 
-    Query query;
-    try {
-      // get the query data for this rule
-      query = interpreter.parseQuery("select $s $p $o from <" + ruleModel +
-          "> where $axiom <rdf:type> <krule:Axiom> and $axiom <krule:subject> $s" +
-          " and $axiom <krule:predicate> $p and $axiom <krule:object> $o;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while finding axioms.", e);
-    }
+    // select $s $p $o from <ruleGraph>
+    // where $axiom <rdf:type> <krule:Axiom> and $axiom <krule:subject> $s
+    // and $axiom <krule:predicate> $p and $axiom <krule:object> $o
+    Variable sV = new Variable("s");
+    Variable pV = new Variable("p");
+    Variable oV = new Variable("o");
+    Variable axiomV = new Variable("axiom");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(axiomV, RDF_TYPE, AXIOM),
+        new ConstraintImpl(axiomV, AXIOM_SUBJECT, sV),
+        new ConstraintImpl(axiomV, AXIOM_PREDICATE, pV),
+        new ConstraintImpl(axiomV, AXIOM_OBJECT, oV)
+    );
+    Query query = createQuery(where, sV, pV, oV);
     Answer answer = query(query);
 
     // prepare the set of axioms
@@ -618,15 +648,15 @@
   private void findUriReferences() throws TuplesException, QueryException, InitializerException {
     if (logger.isDebugEnabled()) logger.debug("Querying for URI reference objects.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $ref $uri from <" +
-          ruleModel + "> where $ref <rdf:type> <krule:URIReference> and $ref <rdf:value> $uri ;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while looking for URI references.", e);
-    }
-    
+    // select $ref $uri from <ruleGraph>
+    // where $ref <rdf:type> <krule:URIReference> and $ref <rdf:value> $uri
+    Variable refV = new Variable("ref");
+    Variable uriV = new Variable("uri");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(refV, RDF_TYPE, URI_REF),
+        new ConstraintImpl(refV, RDF_VALUE, uriV)
+    );
+    Query query = createQuery(where, refV, uriV);
     Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all URI references.");
 
@@ -657,15 +687,17 @@
   private void findVarReferences() throws TuplesException, QueryException, InitializerException {
     if (logger.isDebugEnabled()) logger.debug("Querying for variable reference objects.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $ref $name from <" +
-          ruleModel + "> where $ref <rdf:type> <krule:Variable> and $ref <krule:name> $name ;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while finding variable references.", e);
-    }
-    
+    // find the URI references and the referred URIs.
+
+    // select $ref $name from <ruleGraph>
+    // where $ref <rdf:type> <krule:Variable> and $ref <krule:name> $name
+    Variable refV = new Variable("ref");
+    Variable nameV = new Variable("name");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(refV, RDF_TYPE, VARIABLE),
+        new ConstraintImpl(refV, NAME, nameV)
+    );
+    Query query = createQuery(where, refV, nameV);
     Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all variable references.");
 
@@ -696,15 +728,15 @@
   private void findLiteralReferences() throws TuplesException, QueryException, InitializerException {
     if (logger.isDebugEnabled()) logger.debug("Querying for Literal objects.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $lit $str from <" +
-          ruleModel + "> where $lit <rdf:type> <krule:Literal> and $lit <rdf:value> $str ;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while looking for literal references.", e);
-    }
-    
+    // select $lit $str from <ruleGraph>
+    // where $lit <rdf:type> <krule:Literal> and $lit <rdf:value> $str
+    Variable litV = new Variable("lit");
+    Variable strV = new Variable("str");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(litV, RDF_TYPE, LITERAL),
+        new ConstraintImpl(litV, RDF_VALUE, strV)
+    );
+    Query query = createQuery(where, litV, strV);
     Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all Literals.");
 
@@ -735,16 +767,26 @@
   private void loadSimpleConstraints() throws KruleStructureException, TuplesException, QueryException {
     if (logger.isDebugEnabled()) logger.debug("Querying for Simple constraints.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $c $p $o from <" + ruleModel +
-          "> where $c <rdf:type> <krule:SimpleConstraint> and $c $p $o and " +
-          "($p <mulgara:is> <krule:hasSubject> or $p <mulgara:is> <krule:hasPredicate> or " +
-          "$p <mulgara:is> <krule:hasObject> or $p <mulgara:is> <krule:hasModel>);");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while looking for simple constraints.", e);
-    }
+    // select $c $p $o from <ruleGraph>
+    // where $c <rdf:type> <krule:SimpleConstraint> and $c $p $o
+    // and ($p <mulgara:is> <krule:hasSubject>
+    //   or $p <mulgara:is> <krule:hasPredicate>
+    //   or $p <mulgara:is> <krule:hasObject>
+    //   or $p <mulgara:is> <krule:hasModel>)
+    Variable cV = new Variable("c");
+    Variable pV = new Variable("p");
+    Variable oV = new Variable("o");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(cV, RDF_TYPE, SIMPLE_CONSTRAINT),
+        new ConstraintImpl(cV, pV, oV),
+        new ConstraintDisjunction(
+            new ConstraintIs(pV, HAS_SUBJECT),
+            new ConstraintIs(pV, HAS_PREDICATE),
+            new ConstraintIs(pV, HAS_OBJECT),
+            new ConstraintIs(pV, HAS_GRAPH)
+        )
+    );
+    Query query = createQuery(where, cV, pV, oV);
 
     Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all simple constraints.");
@@ -802,16 +844,23 @@
     // build constraints in place, recursively constructing child constraints until all are found
     if (logger.isDebugEnabled()) logger.debug("Querying for Join constraints.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $constraint $constraint2 $type from <" + ruleModel +
-          "> where $constraint <krule:argument> $constraint2 and $constraint <rdf:type> $type and " +
-          "($type <mulgara:is> <krule:ConstraintConjunction> or $type <mulgara:is> <krule:ConstraintDisjunction>);");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while looking for join constraints.", e);
-    }
-
+    // select $constraint $constraint2 $type from <ruleGraph>
+    // where $constraint <krule:argument> $constraint2
+    //   and $constraint <rdf:type> $type
+    //   and ($type <mulgara:is> <krule:ConstraintConjunction>
+    //     or $type <mulgara:is> <krule:ConstraintDisjunction>)
+    Variable constraintV = new Variable("constraint");
+    Variable constraint2V = new Variable("constraint2");
+    Variable typeV = new Variable("type");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(constraintV, ARGUMENT, constraint2V),
+        new ConstraintImpl(constraintV, RDF_TYPE, typeV),
+        new ConstraintDisjunction(
+            new ConstraintIs(typeV, CONSTRAINT_CONJUNCTION),
+            new ConstraintIs(typeV, CONSTRAINT_DISJUNCTION)
+        )
+    );
+    Query query = createQuery(where, constraintV, constraint2V, typeV);
     Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all join constraints.");
 
@@ -877,16 +926,12 @@
   private void loadHavingConstraints() throws KruleStructureException, TuplesException, QueryException {
     if (logger.isDebugEnabled()) logger.debug("Querying for Having constraints.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $constraint from <" + ruleModel +
-          "> where $rule <krule:hasHavingClause> $constraint;");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while searching on having clauses.", e);
-    }
+    // select $constraint from <ruleGraph> where $rule <krule:hasHavingClause> $constraint
+    Variable ruleV = new Variable("rule");
+    Variable constraintV = new Variable("constraint");
+    Query query = createQuery(new ConstraintImpl(ruleV, HAS_HAVING_CLAUSE, constraintV), constraintV);
+    Answer answer = query(query);
 
-    Answer answer = query(query);
     if (logger.isDebugEnabled()) logger.debug("Found all having constraints.");
 
     try {
@@ -907,15 +952,22 @@
   private void loadTransitiveConstraints() throws KruleStructureException, TuplesException, QueryException {
     if (logger.isDebugEnabled()) logger.debug("Querying for Transitive constraints.");
 
-    Query query;
-    try {
-      // find the URI references and the referred URIs.
-      query = interpreter.parseQuery("select $c $p $arg from <" + ruleModel +
-          "> where $c <rdf:type> <krule:TransitiveConstraint> and $c $p $arg and " +
-          "($p <mulgara:is> <krule:transitiveArgument> or $p <mulgara:is> <krule:anchorArgument>);");
-    } catch (Exception e) {
-      throw new QueryException("Invalid query while querying for transitive constraints.", e);
-    }
+    // select $c $p $arg from <ruleGraph>
+    // where $c <rdf:type> <krule:TransitiveConstraint> and $c $p $arg
+    // and ($p <mulgara:is> <krule:transitiveArgument>
+    //   or $p <mulgara:is> <krule:anchorArgument>)
+    Variable cV = new Variable("c");
+    Variable pV = new Variable("p");
+    Variable argV = new Variable("arg");
+    ConstraintExpression where = new ConstraintConjunction(
+        new ConstraintImpl(cV, RDF_TYPE, TRANSITIVE_CONSTRAINT),
+        new ConstraintImpl(cV, pV, argV),
+        new ConstraintDisjunction(
+            new ConstraintIs(pV, TRANSITIVE_ARGUMENT),
+            new ConstraintIs(pV, ANCHOR_ARGUMENT)
+        )
+    );
+    Query query = createQuery(where, cV, pV, argV);
     Answer answer = query(query);
 
     if (logger.isDebugEnabled()) logger.debug("Retrieved all transitive constraints.");
@@ -1107,7 +1159,30 @@
     }
   }
 
+
   /**
+   * Utility method to create a query.
+   * @param constraintExpression The constraint expression making up the WHERE clause of the query.
+   * @param selection The variables to select in the query.
+   * @return The new query.
+   */
+  @SuppressWarnings("unchecked")
+  private Query createQuery(ConstraintExpression constraintExpression, Variable... selection) {
+    List<Variable> selectList = Arrays.asList(selection);
+    return new Query(
+        selectList,                                 // SELECT
+        ruleGraph,                                  // FROM
+        constraintExpression,                                      // WHERE
+        null,                                       // HAVING
+        (List<Order>)Collections.EMPTY_LIST,        // ORDER BY
+        null,                                       // LIMIT
+        0,                                          // OFFSET
+        UNCONSTRAINED                               // GIVEN
+    );
+  }
+
+
+  /**
    * Local wrapper for querying on an OperationContext. Since {@link OperationContext#doQuery(Query)}
    * throws an {@link Exception}, this is captured and wrapped in or cast to a {@link QueryException}.
    * @param q The query to execute.




More information about the Mulgara-svn mailing list