
/*
 * Copyright 2011 Revelytix Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package spark.protocol;

import java.util.Map;

import junit.framework.Assert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import spark.api.Command;
import spark.api.Connection;
import spark.api.DataSource;
import spark.api.Solutions;
import spark.api.credentials.NoCredentials;
import spark.api.rdf.RDFNode;

public class SparqlQuery {

  private static final Logger logger = LoggerFactory.getLogger(SparqlQuery.class);
  
  public static void testQuery() throws Exception {
    DataSource myDS = new ProtocolDataSource("http://DBpedia.org/sparql");
    Connection conn = myDS.getConnection(NoCredentials.INSTANCE);
    Command query = conn.createCommand("SELECT ?p ?o WHERE { <http://dbpedia.org/resource/Terry_Gilliam> ?p ?o }");    
    Solutions solutions = query.executeQuery();
    
    logger.debug("vars = {}", solutions.getVariables());
    int row = 0;
    while(solutions.next()) {
      logger.debug("Row {}: {}", ++row, solutions.getResult());
    }
    solutions.close();
    query.close();
    conn.close();
    myDS.close();
  }
  
  public static void testQuery2() throws Exception {
    DataSource myDS = new ProtocolDataSource("http://DBpedia.org/sparql");
    Connection conn = myDS.getConnection(NoCredentials.INSTANCE);
    Command query = conn.createCommand("SELECT ?p ?o WHERE { <http://dbpedia.org/resource/Terry_Gilliam> ?p ?o }");    
    Solutions solutions = query.executeQuery();
    
    logger.debug("vars = {}", solutions.getVariables());
    int row = 0;
    for(Map<String, RDFNode> solution : solutions) {
      logger.debug("Row {}: {}", ++row, solution);
    }
    solutions.close();
    query.close();
    conn.close();
    myDS.close();
  }
  
  static int iterateResults(Solutions s) {
    int c = 0;
    while (s.next()) {
      c++;
      Assert.assertNotNull(s.getBinding("s"));
      Assert.assertNotNull(s.getBinding("p"));
      Assert.assertNotNull(s.getBinding("o"));
    }
    return c;
  }
  
  public static void main(String arg[]) throws Exception {
    testQuery();
    testQuery2();
  }
}

@@ -729,16 +729,48 @@
 ctory;%0A%0A
+import spark.api.BooleanResult;%0A
 import s
@@ -787,16 +787,16 @@
 ommand;%0A
-
 import s
@@ -845,16 +845,41 @@
 Source;%0A
+import spark.api.Result;%0A
 import s
@@ -1083,24 +1083,195 @@
 .class);%0A  %0A
+  private static void showMetadata(Result r) %7B%0A    if (r instanceof ProtocolResult) %7B%0A      logger.debug(%22metadata = %7B%7D%22, ((ProtocolResult)r).getMetadata());%0A    %7D%0A  %7D%0A  %0A
   public sta
@@ -1616,32 +1616,61 @@
 teQuery();%0A    %0A
+    showMetadata(solutions);%0A
     logger.debug
@@ -2275,24 +2275,53 @@
 ery();%0A    %0A
+    showMetadata(solutions);%0A
     logger.d
@@ -2577,16 +2577,939 @@
 %0A  %7D%0A  %0A
+  public static void testAsk() throws Exception %7B%0A    DataSource myDS = new ProtocolDataSource(%22http://DBpedia.org/sparql%22);%0A    Connection conn = myDS.getConnection(NoCredentials.INSTANCE);%0A    Command query = conn.createCommand(%22ASK %7B %3Chttp://dbpedia.org/resource/Terry_Gilliam%3E ?p ?o %7D%22);    %0A    %0A    Result r = query.execute();%0A    showMetadata(r);%0A    logger.debug(%22result = %7B%7D%22, ((BooleanResult)r).getResult());%0A    %0A    r.close();%0A    query.close();%0A    conn.close();%0A    myDS.close();%0A  %7D%0A  %0A  public static void testAsk2() throws Exception %7B%0A    DataSource myDS = new ProtocolDataSource(%22http://DBpedia.org/sparql%22);%0A    Connection conn = myDS.getConnection(NoCredentials.INSTANCE);%0A    Command query = conn.createCommand(%22ASK %7B %3Chttp://dbpedia.org/resource/Terry_Gilliam%3E ?p ?o %7D%22);    %0A    %0A    logger.debug(%22result = %7B%7D%22, query.executeAsk());%0A    %0A    query.close();%0A    conn.close();%0A    myDS.close();%0A  %7D%0A  %0A
   static
@@ -3832,24 +3832,24 @@
 estQuery();%0A
-
     testQuer
@@ -3850,18 +3850,49 @@
 stQuery2();%0A
+    testAsk();%0A    testAsk2();%0A
   %7D%0A%7D%0A

Add test cases for ASK protocol queries