You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by ha...@apache.org on 2018/11/14 03:04:10 UTC

[6/6] clerezza git commit: CLEREZZA-1026: Introduce GraphStore to remove dependency to TcProvider and refactor sparql

CLEREZZA-1026: Introduce GraphStore to remove dependency to TcProvider and refactor sparql


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/eec0ac73
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/eec0ac73
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/eec0ac73

Branch: refs/heads/reunited
Commit: eec0ac73d7d0496fb179093afb51f77bae9bb0a7
Parents: 829e5fa
Author: Hasan <ha...@apache.org>
Authored: Wed Nov 14 04:03:36 2018 +0100
Committer: Hasan <ha...@apache.org>
Committed: Wed Nov 14 04:03:36 2018 +0100

----------------------------------------------------------------------
 sparql/pom.xml                                  |  133 +
 .../org/apache/clerezza/sparql/GraphStore.java  |   22 +
 .../clerezza/sparql/NoQueryEngineException.java |   42 +
 .../org/apache/clerezza/sparql/QueryEngine.java |   45 +
 .../org/apache/clerezza/sparql/QueryParser.java |   71 +
 .../org/apache/clerezza/sparql/ResultSet.java   |   39 +
 .../apache/clerezza/sparql/SolutionMapping.java |   51 +
 .../apache/clerezza/sparql/SparqlPreParser.java |  160 ++
 .../clerezza/sparql/StringQuerySerializer.java  |   90 +
 .../sparql/query/AbstractOperation.java         |   41 +
 .../sparql/query/AlternativeGraphPattern.java   |   38 +
 .../apache/clerezza/sparql/query/AskQuery.java  |   29 +
 .../sparql/query/BasicGraphPattern.java         |   38 +
 .../clerezza/sparql/query/BinaryOperation.java  |   46 +
 .../query/BinaryPropertyPathOperation.java      |   44 +
 .../clerezza/sparql/query/BuiltInCall.java      |   47 +
 .../clerezza/sparql/query/ConstructQuery.java   |   41 +
 .../apache/clerezza/sparql/query/DataSet.java   |   47 +
 .../clerezza/sparql/query/DescribeQuery.java    |   50 +
 .../clerezza/sparql/query/Expression.java       |   42 +
 .../clerezza/sparql/query/FunctionCall.java     |   49 +
 .../sparql/query/GraphGraphPattern.java         |   42 +
 .../clerezza/sparql/query/GraphPattern.java     |   30 +
 .../sparql/query/GroupGraphPattern.java         |   70 +
 .../clerezza/sparql/query/InlineData.java       |   32 +
 .../sparql/query/LiteralExpression.java         |   40 +
 .../sparql/query/MinusGraphPattern.java         |   40 +
 .../sparql/query/OptionalGraphPattern.java      |   45 +
 .../clerezza/sparql/query/OrderCondition.java   |   37 +
 .../query/PathSupportedBasicGraphPattern.java   |   43 +
 .../sparql/query/PatternExistenceCondition.java |   66 +
 .../clerezza/sparql/query/PredicatePath.java    |   38 +
 .../sparql/query/PropertyPathExpression.java    |   29 +
 .../query/PropertyPathExpressionOrVariable.java |  104 +
 .../sparql/query/PropertyPathPattern.java       |   46 +
 .../clerezza/sparql/query/PropertySet.java      |   44 +
 .../org/apache/clerezza/sparql/query/Query.java |   59 +
 .../sparql/query/QueryWithSolutionModifier.java |   68 +
 .../sparql/query/ResourceOrVariable.java        |  106 +
 .../sparql/query/RhsListBinaryOperation.java    |   47 +
 .../clerezza/sparql/query/SelectQuery.java      |   70 +
 .../sparql/query/ServiceGraphPattern.java       |   41 +
 .../clerezza/sparql/query/SparqlUnit.java       |   51 +
 .../clerezza/sparql/query/TriplePattern.java    |   44 +
 .../clerezza/sparql/query/UnaryOperation.java   |   38 +
 .../query/UnaryPropertyPathOperation.java       |   37 +
 .../clerezza/sparql/query/UriRefExpression.java |   39 +
 .../clerezza/sparql/query/UriRefOrVariable.java |   43 +
 .../apache/clerezza/sparql/query/Variable.java  |   86 +
 .../impl/SimpleAlternativeGraphPattern.java     |   60 +
 .../sparql/query/impl/SimpleAskQuery.java       |   33 +
 .../query/impl/SimpleBasicGraphPattern.java     |   49 +
 .../sparql/query/impl/SimpleConstructQuery.java |   51 +
 .../sparql/query/impl/SimpleDataSet.java        |   52 +
 .../sparql/query/impl/SimpleDescribeQuery.java  |   61 +
 .../query/impl/SimpleGraphGraphPattern.java     |   55 +
 .../query/impl/SimpleGroupGraphPattern.java     |  221 ++
 .../sparql/query/impl/SimpleInlineData.java     |   54 +
 .../query/impl/SimpleMinusGraphPattern.java     |   67 +
 .../query/impl/SimpleOptionalGraphPattern.java  |   68 +
 .../sparql/query/impl/SimpleOrderCondition.java |   47 +
 .../SimplePathSupportedBasicGraphPattern.java   |   49 +
 .../query/impl/SimplePropertyPathPattern.java   |  132 +
 .../clerezza/sparql/query/impl/SimpleQuery.java |   76 +
 .../impl/SimpleQueryWithSolutionModifier.java   |   93 +
 .../sparql/query/impl/SimpleSelectQuery.java    |   80 +
 .../query/impl/SimpleServiceGraphPattern.java   |   66 +
 .../sparql/query/impl/SimpleSparqlUnit.java     |   65 +
 .../query/impl/SimpleStringQuerySerializer.java |  295 ++
 .../sparql/query/impl/SimpleTriplePattern.java  |  136 +
 .../apache/clerezza/sparql/update/Update.java   |   52 +
 .../clerezza/sparql/update/UpdateOperation.java |   57 +
 .../sparql/update/impl/AddOperation.java        |   29 +
 .../sparql/update/impl/BaseUpdateOperation.java |   86 +
 .../sparql/update/impl/ClearOperation.java      |   26 +
 .../update/impl/ClearOrDropOperation.java       |   61 +
 .../sparql/update/impl/CopyOperation.java       |   26 +
 .../sparql/update/impl/CreateOperation.java     |   55 +
 .../sparql/update/impl/DeleteDataOperation.java |   26 +
 .../update/impl/DeleteWhereOperation.java       |   26 +
 .../sparql/update/impl/DropOperation.java       |   26 +
 .../sparql/update/impl/InsertDataOperation.java |   26 +
 .../sparql/update/impl/LoadOperation.java       |   39 +
 .../sparql/update/impl/ModifyOperation.java     |  112 +
 .../sparql/update/impl/MoveOperation.java       |   26 +
 .../clerezza/sparql/update/impl/Quad.java       |   43 +
 .../sparql/update/impl/SimpleUpdate.java        |   52 +
 .../update/impl/SimpleUpdateOperation.java      |   77 +
 .../update/impl/UpdateOperationWithQuads.java   |   79 +
 .../sparql/JavaCCGeneratedQueryParser.jj        | 1285 +++++++++
 .../sparql/JavaCCGeneratedSparqlPreParser.jj    | 2557 ++++++++++++++++++
 .../QueryParserSerializerCombinationTest.java   |  135 +
 .../apache/clerezza/sparql/QueryParserTest.java |  367 +++
 .../clerezza/sparql/QuerySerializerTest.java    |  303 +++
 .../clerezza/sparql/SparqlPreParserTest.java    |  511 ++++
 95 files changed, 10492 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/pom.xml
----------------------------------------------------------------------
diff --git a/sparql/pom.xml b/sparql/pom.xml
new file mode 100644
index 0000000..10e64ad
--- /dev/null
+++ b/sparql/pom.xml
@@ -0,0 +1,133 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!--
+
+   Licensed to  the Apache Software Foundation (ASF)  under  one  or  more
+   contributor license agreements.  See the NOTICE file  distributed  with
+   this work for  additional information  regarding  copyright  ownership.
+   The ASF licenses this file to You 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.
+
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.clerezza</groupId>
+        <artifactId>clerezza</artifactId>
+        <version>8-SNAPSHOT</version>
+        <relativePath/>
+    </parent>
+
+    <artifactId>sparql</artifactId>
+    <packaging>bundle</packaging>
+    <version>1.0.2-SNAPSHOT</version>
+    <name>Clerezza - SPARQL</name>
+    <description>Model SPARQL query and update</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.clerezza</groupId>
+            <artifactId>api</artifactId>
+            <version>8-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.clerezza</groupId>
+            <artifactId>api.impl</artifactId>
+            <version>8-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
+            <version>1.2.8</version>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>javacc-maven-plugin</artifactId>
+                <version>2.5</version>
+                <executions>
+                    <execution>
+                        <id>javacc</id>
+                        <goals>
+                            <goal>javacc</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.codehaus.xsite</groupId>
+                <artifactId>xsite-maven-plugin</artifactId>
+                <configuration>
+                    <sourceDirectoryPath>${basedir}/src/site/xsite</sourceDirectoryPath>
+                    <sitemapPath>content/sitemap.xml</sitemapPath>
+                    <skinPath>templates/skin.html</skinPath>
+                    <outputDirectoryPath>${basedir}/target/site/documentation</outputDirectoryPath>
+                </configuration>
+                <executions>
+                    <execution>
+                        <phase>site</phase>
+                        <goals>
+                            <goal>run</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Export-Package>org.apache.clerezza.sparql.*</Export-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>generate-scr-scrdescriptor</id>
+                        <goals>
+                            <goal>scr</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/GraphStore.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/GraphStore.java b/sparql/src/main/java/org/apache/clerezza/sparql/GraphStore.java
new file mode 100644
index 0000000..34113b9
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/GraphStore.java
@@ -0,0 +1,22 @@
+package org.apache.clerezza.sparql;
+
+import org.apache.clerezza.api.IRI;
+
+import java.util.Set;
+
+public interface GraphStore {
+
+    /**
+     * Lists the name of the <Code>Graph</code>s available through this <code>GraphStore</code>.
+     *
+     * @return the list of <Code>Graph</code>s
+     */
+    Set<IRI> listGraphs();
+
+    /**
+     * Lists the name of the named <Code>Graph</code>s available through this <code>GraphStore</code>.
+     *
+     * @return the list of named <Code>Graph</code>s
+     */
+    Set<IRI> listNamedGraphs();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/NoQueryEngineException.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/NoQueryEngineException.java b/sparql/src/main/java/org/apache/clerezza/sparql/NoQueryEngineException.java
new file mode 100644
index 0000000..37547ef
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/NoQueryEngineException.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+/**
+ * Indicates that there is no available query engine for resolving a query.
+ *
+ * @author rbn
+ */
+public class NoQueryEngineException extends RuntimeException {
+
+    /**
+     * Creates a new instance of <code>NoQueryEngineException</code> without detail message.
+     */
+    public NoQueryEngineException() {
+    }
+
+
+    /**
+     * Constructs an instance of <code>NoQueryEngineException</code> with the specified detail message.
+     * @param msg the detail message.
+     */
+    public NoQueryEngineException(String msg) {
+        super(msg);
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/QueryEngine.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/QueryEngine.java b/sparql/src/main/java/org/apache/clerezza/sparql/QueryEngine.java
new file mode 100644
index 0000000..1592fa7
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/QueryEngine.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import org.apache.clerezza.api.Graph;
+import org.apache.clerezza.sparql.query.Query;
+
+/**
+ * A QueryEngine can process SPARQL queries against an arbitrary set of graphs.
+ * 
+ * @author rbn
+ */
+public interface QueryEngine {
+
+	/**
+	 * Executes any sparql query. The type of the result object will vary
+	 * depending on the type of the query.
+	 * 
+	 * @param graphStore
+	 *            where the query originates.
+	 * @param defaultGraph
+	 *            the default ImmutableGraph against which to execute the query if no
+	 *            FROM clause is present
+	 * @param query
+	 *            string to be executed.
+	 * @return the resulting ResultSet, ImmutableGraph or Boolean value
+	 */
+	public Object execute(GraphStore graphStore, Graph defaultGraph, String query);
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/QueryParser.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/QueryParser.java b/sparql/src/main/java/org/apache/clerezza/sparql/QueryParser.java
new file mode 100644
index 0000000..259d282
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/QueryParser.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import org.apache.clerezza.sparql.query.Query;
+import org.osgi.service.component.annotations.Component;
+
+import java.io.StringReader;
+
+/**
+ * This class implements an OSGi service to provide a method to parse a
+ * SPARQL query and generate a {@link Query} object.
+ *
+ * @author hasan
+ */
+
+@Component(service = QueryParser.class)
+public class QueryParser {
+
+    private static volatile QueryParser instance;
+    public QueryParser() {
+        QueryParser.instance = this;
+    }
+
+    /**
+     * Returns an instance of this class.
+     * This method is provided due to backward compatibility.
+     */
+    public static QueryParser getInstance() {
+        if (instance == null) {
+            synchronized (QueryParser.class) {
+                if (instance == null) {
+                    new QueryParser();
+                }
+            }
+        }
+        return instance;
+    }
+
+    /**
+     * Parses a SPARQL query string into a {@link Query} object.
+     *
+     * @param queryString
+     *        SPARQL query string
+     * @return
+     *        {@link Query} object corresponding to the specified query string
+     *
+     * @throws org.apache.clerezza.sparql.ParseException
+     */
+    public Query parse(final String queryString) throws ParseException {
+        JavaCCGeneratedQueryParser parser = new JavaCCGeneratedQueryParser(
+                new StringReader(queryString));
+        return parser.parse();
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/ResultSet.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/ResultSet.java b/sparql/src/main/java/org/apache/clerezza/sparql/ResultSet.java
new file mode 100644
index 0000000..bf5ae89
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/ResultSet.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * The reult of a sparql SELECT-query. This corresponds to a Solution Sequence
+ * as per section 12.1.6 of http://www.w3.org/TR/rdf-sparql-query/.
+ *
+ * Note that the scope of blank nodes is the reult set and not the
+ * Graph from where they originate.
+ *
+ * @author rbn
+ */
+public interface ResultSet extends Iterator<SolutionMapping> {
+
+    /** Iterate over the variable names (strings) in this QuerySolution.
+     * @return Iterator of strings
+     */ 
+    public List<String> getResultVars() ;
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/SolutionMapping.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/SolutionMapping.java b/sparql/src/main/java/org/apache/clerezza/sparql/SolutionMapping.java
new file mode 100644
index 0000000..0831d0b
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/SolutionMapping.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import org.apache.clerezza.api.RDFTerm;
+import org.apache.clerezza.sparql.query.Variable;
+
+import java.util.Map;
+
+/**
+ * A set of mapping from variable names to solutions.
+ *
+ * a variable name has the form: ( PN_CHARS_U | [0-9] ) ( PN_CHARS_U | [0-9]
+ * | #x00B7 | [#x0300-#x036F] | [#x203F-#x2040] )*
+ * where PN_CHARS_U =      [A-Z] | [a-z] | [#x00C0-#x00D6] | [#x00D8-#x00F6]
+ * | [#x00F8-#x02FF] | [#x0370-#x037D] | [#x037F-#x1FFF] | [#x200C-#x200D]
+ * | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF]
+ * | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] | '_'
+ *
+ *
+ * @author rbn
+ */
+public interface SolutionMapping extends Map<Variable, RDFTerm> {
+
+    /**
+     * Should be the equivalent to this:
+     * public Resource get(String name) {
+     *    return get(new Variable(name));
+     * }
+     *
+     * @param name
+     * @return
+     */
+    public RDFTerm get(String name);
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/SparqlPreParser.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/SparqlPreParser.java b/sparql/src/main/java/org/apache/clerezza/sparql/SparqlPreParser.java
new file mode 100644
index 0000000..eb34511
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/SparqlPreParser.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import org.apache.clerezza.api.IRI;
+import org.apache.clerezza.sparql.query.*;
+import org.apache.clerezza.sparql.update.Update;
+
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * This class implements an OSGi service to provide a method to obtain referred Graphs in a SPARQL Query or Update.
+ *
+ * @author hasan
+ */
+
+
+public class SparqlPreParser {
+
+    GraphStore graphStore;
+
+    public SparqlPreParser() {
+    }
+
+    public SparqlPreParser(GraphStore graphStore) {
+        this.graphStore = graphStore;
+    }
+
+    /**
+     * This returns the graphs targeted by the queryString. These are the 
+     * triple collections explicitly referred in FROM and FROM NAMED clauses, 
+     * and if the queryString contains no FROM clause the defaultGraph.
+     * 
+     * For queries that are not limited to a specified set of graphs, null is returned.
+     * 
+     * @param queryString
+     * @param defaultGraph
+     * @return 
+     * @throws ParseException 
+     */
+    public Set<IRI> getReferredGraphs(String queryString, IRI defaultGraph) throws ParseException {
+        Set<IRI> referredGraphs;
+        JavaCCGeneratedSparqlPreParser parser = new JavaCCGeneratedSparqlPreParser(new StringReader(queryString));
+        SparqlUnit sparqlUnit;
+        sparqlUnit = parser.parse();
+        boolean referringVariableNamedGraph = false;
+        if (sparqlUnit.isQuery()) {
+            Query q = sparqlUnit.getQuery();
+            DataSet dataSet = q.getDataSet();
+            if (dataSet != null) {
+                referredGraphs = dataSet.getDefaultGraphs();
+                referredGraphs.addAll(dataSet.getNamedGraphs());
+            } else {
+                referredGraphs = new HashSet<IRI>();
+            }
+            GroupGraphPattern queryPattern = q.getQueryPattern();
+            if (queryPattern != null) {
+                Set<GraphPattern> graphPatterns = queryPattern.getGraphPatterns();
+                for (GraphPattern graphPattern : graphPatterns) {
+                }
+            }
+//            referringVariableNamedGraph = q.referringVariableNamedGraph();
+            referringVariableNamedGraph = referringVariableNamedGraph(q);
+        } else {
+            Update u = sparqlUnit.getUpdate();
+            referredGraphs = u.getReferredGraphs(defaultGraph, graphStore);
+        }
+        if (referredGraphs.isEmpty()) {
+            if (referringVariableNamedGraph) {
+                return null;
+            }
+            referredGraphs.add(defaultGraph);
+        }
+        return referredGraphs;
+    }
+
+    private boolean referringVariableNamedGraph(Query query) {
+        GroupGraphPattern queryPattern = query.getQueryPattern();
+        if (queryPattern == null) {
+            return false;
+        }
+        Set<GraphPattern> graphPatterns = queryPattern.getGraphPatterns();
+        return referringVariableNamedGraph(graphPatterns);
+    }
+
+    private boolean referringVariableNamedGraph(Set<GraphPattern> graphPatterns) {
+        boolean referringVariableNamedGraph = false;
+        for (GraphPattern graphPattern : graphPatterns) {
+            if (referringVariableNamedGraph(graphPattern)) {
+                referringVariableNamedGraph = true;
+                break;
+            }
+        }
+        return referringVariableNamedGraph;
+    }
+
+    private boolean referringVariableNamedGraph(GraphPattern graphPattern) {
+        if (graphPattern instanceof GraphGraphPattern) {
+            return ((GraphGraphPattern) graphPattern).getGraph().isVariable();
+        }
+        if (graphPattern instanceof AlternativeGraphPattern) {
+            List<GroupGraphPattern> alternativeGraphPatterns =
+                    ((AlternativeGraphPattern) graphPattern).getAlternativeGraphPatterns();
+            boolean referringVariableNamedGraph = false;
+            for (GroupGraphPattern groupGraphPattern : alternativeGraphPatterns) {
+                if (referringVariableNamedGraph(groupGraphPattern)) {
+                    referringVariableNamedGraph = true;
+                    break;
+                }
+            }
+            return referringVariableNamedGraph;
+        }
+        if (graphPattern instanceof OptionalGraphPattern) {
+            GraphPattern mainGraphPattern = ((OptionalGraphPattern) graphPattern).getMainGraphPattern();
+            if (referringVariableNamedGraph(mainGraphPattern)) {
+                return true;
+            }
+            GroupGraphPattern optionalGraphPattern = ((OptionalGraphPattern) graphPattern).getOptionalGraphPattern();
+            return referringVariableNamedGraph(optionalGraphPattern);
+        }
+        if (graphPattern instanceof MinusGraphPattern) {
+            GraphPattern minuendGraphPattern = ((MinusGraphPattern) graphPattern).getMinuendGraphPattern();
+            if (referringVariableNamedGraph(minuendGraphPattern)) {
+                return true;
+            }
+            GroupGraphPattern subtrahendGraphPattern = ((MinusGraphPattern) graphPattern).getSubtrahendGraphPattern();
+            return referringVariableNamedGraph(subtrahendGraphPattern);
+        }
+        if (graphPattern instanceof GroupGraphPattern) {
+            GroupGraphPattern groupGraphPattern = (GroupGraphPattern) graphPattern;
+            if (groupGraphPattern.isSubSelect()) {
+                Query query = ((GroupGraphPattern) graphPattern).getSubSelect();
+                return referringVariableNamedGraph(query);
+            } else {
+                Set<GraphPattern> graphPatterns = ((GroupGraphPattern) graphPattern).getGraphPatterns();
+                return referringVariableNamedGraph(graphPatterns);
+            }
+        }
+        return false;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/StringQuerySerializer.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/StringQuerySerializer.java b/sparql/src/main/java/org/apache/clerezza/sparql/StringQuerySerializer.java
new file mode 100644
index 0000000..54c548a
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/StringQuerySerializer.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql;
+
+import org.apache.clerezza.sparql.query.*;
+
+/**
+ * This abstract class provides a method to generate a {@link String}
+ * representation of a {@link Query}.
+ *
+ * @author hasan
+ */
+public abstract class StringQuerySerializer {
+
+    /**
+     * Serializes a {@link Query} object to a {@link String}.
+     *
+     * @param query
+     *        the Query object to be serialized
+     * @return
+     *        a String representation of the specified Query object.
+     */
+    public String serialize(Query query) {
+        if (query instanceof SelectQuery) {
+            return serialize((SelectQuery) query);
+        } else if (query instanceof ConstructQuery) {
+            return serialize((ConstructQuery) query);
+        } else if (query instanceof DescribeQuery) {
+            return serialize((DescribeQuery) query);
+        } else {
+            return serialize((AskQuery) query);
+        }
+    }
+
+    /**
+     * Serializes a {@link SelectQuery} object to a {@link String}.
+     *
+     * @param selectQuery
+     *        the SelectQuery object to be serialized
+     * @return
+     *        a String representation of the specified SelectQuery object.
+     */
+    public abstract String serialize(SelectQuery selectQuery);
+
+    /**
+     * Serializes a {@link ConstructQuery} object to a {@link String}.
+     *
+     * @param constructQuery
+     *        the ConstructQuery object to be serialized
+     * @return
+     *        a String representation of the specified ConstructQuery object.
+     */
+    public abstract String serialize(ConstructQuery constructQuery);
+
+    /**
+     * Serializes a {@link DescribeQuery} object to a {@link String}.
+     *
+     * @param describeQuery
+     *        the DescribeQuery object to be serialized
+     * @return
+     *        a String representation of the specified DescribeQuery object.
+     */
+    public abstract String serialize(DescribeQuery describeQuery);
+
+    /**
+     * Serializes an {@link AskQuery} object to a {@link String}.
+     *
+     * @param askQuery
+     *        the AskQuery object to be serialized
+     * @return
+     *        a String representation of the specified AskQuery object.
+     */
+    public abstract String serialize(AskQuery askQuery);
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/AbstractOperation.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/AbstractOperation.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/AbstractOperation.java
new file mode 100644
index 0000000..8e76209
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/AbstractOperation.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Defines an operation in an {@link Expression}. An operation has an operator
+ * and one or more operands.
+ *
+ * @author hasan
+ */
+public abstract class AbstractOperation implements Expression {
+    private String operator;
+
+    public AbstractOperation(String operator) {
+        this.operator = operator;
+    }
+
+    /**
+     * A string representation of the operator
+     * @return The operator as a string
+     */
+    public String getOperatorString() {
+        return operator;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/AlternativeGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/AlternativeGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/AlternativeGraphPattern.java
new file mode 100644
index 0000000..f42f317
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/AlternativeGraphPattern.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.List;
+
+/**
+ * Defines alternative ImmutableGraph patterns.
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#alternatives">
+ * SPARQL Query Language: 7 Matching Alternatives</a>
+ *
+ * @author hasan
+ */
+public interface AlternativeGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return
+     *        a list of alternative {@link GroupGraphPattern}s
+     */
+    public List<GroupGraphPattern> getAlternativeGraphPatterns();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/AskQuery.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/AskQuery.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/AskQuery.java
new file mode 100644
index 0000000..8bdb9fa
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/AskQuery.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * <p>This interface represents a SPARQL ASK query.</p>
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#ask">
+ * SPARQL Query Language: 10.3 ASK</a>
+ *
+ * @author hasan
+ */
+public interface AskQuery extends Query {
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/BasicGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/BasicGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/BasicGraphPattern.java
new file mode 100644
index 0000000..7f0bf4c
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/BasicGraphPattern.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.Set;
+
+/**
+ * Defines a basic ImmutableGraph pattern.
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#BasicGraphPatterns">
+ * SPARQL Query Language: 5.1 Basic ImmutableGraph Patterns</a>
+ *
+ * @author hasan
+ */
+public interface BasicGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return a set of all triple patterns to match.
+     */
+    public Set<TriplePattern> getTriplePatterns();
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryOperation.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryOperation.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryOperation.java
new file mode 100644
index 0000000..2c06cdc
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryOperation.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Defines an operation with two operands: a left hand side and a right hand side
+ * operand.
+ *
+ * @author hasan
+ */
+public class BinaryOperation extends AbstractOperation {
+
+    private Expression lhsOperand;
+    private Expression rhsOperand;
+
+    public BinaryOperation(String operator,
+            Expression lhsOperand, Expression rhsOperand) {
+        super(operator);
+        this.lhsOperand = lhsOperand;
+        this.rhsOperand = rhsOperand;
+    }
+
+    public Expression getLhsOperand() {
+        return lhsOperand;
+    }
+
+    public Expression getRhsOperand() {
+        return rhsOperand;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryPropertyPathOperation.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryPropertyPathOperation.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryPropertyPathOperation.java
new file mode 100644
index 0000000..d3847fb
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/BinaryPropertyPathOperation.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ *
+ * @author hasan
+ */
+public class BinaryPropertyPathOperation implements PropertyPathExpression {
+    private String operator;
+    private PropertyPathExpression lhsOperand;
+    private PropertyPathExpression rhsOperand;
+
+    public BinaryPropertyPathOperation(String operator, PropertyPathExpression lhsOperand, PropertyPathExpression rhsOperand) {
+        this.operator = operator;
+        this.lhsOperand = lhsOperand;
+        this.rhsOperand = rhsOperand;
+    }
+
+    public PropertyPathExpression getLhsOperand() {
+        return lhsOperand;
+    }
+
+    public PropertyPathExpression getRhsOperand() {
+        return rhsOperand;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/BuiltInCall.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/BuiltInCall.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/BuiltInCall.java
new file mode 100644
index 0000000..81b9905
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/BuiltInCall.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.List;
+
+/**
+ * Defines a built-in call which is one form of {@link Expression}.
+ * A built-in call has a name of type String and a list of arguments,
+ * where each argument is an {@link Expression}.
+ *
+ * @author hasan
+ */
+public class BuiltInCall implements Expression {
+
+    protected String name;
+    private final List<Expression> arguments;
+
+    public BuiltInCall(String name, List<Expression> arguments) {
+        this.name = name;
+        this.arguments = arguments;
+    }
+
+    public String getName() {
+        return name;
+    };
+
+    public List<Expression> getArguements() {
+        return arguments;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/ConstructQuery.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/ConstructQuery.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/ConstructQuery.java
new file mode 100644
index 0000000..53bb293
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/ConstructQuery.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.Set;
+
+/**
+ * <p>This interface represents a SPARQL CONSTRUCT query.</p>
+ *
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#construct">
+ * SPARQL Query Language: 10.2 CONSTRUCT</a>
+ *
+ * @author hasan
+ */
+public interface ConstructQuery extends QueryWithSolutionModifier {
+
+    /**
+     * <p>Gets the template for constructing triples in a CONSTRUCT query.</p>
+     * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#construct">
+     * SPARQL Query Language: 10.2 CONSTRUCT</a>
+     * @return a template as a set of triple patterns for constructing
+     *         new triples.
+     */
+    public Set<TriplePattern> getConstructTemplate();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/DataSet.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/DataSet.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/DataSet.java
new file mode 100644
index 0000000..c1c9ae3
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/DataSet.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.IRI;
+
+import java.util.Set;
+
+/**
+ * This interface definition is not yet stable and may change in future.
+ * 
+ * @author hasan
+ */
+public interface DataSet {
+
+    /**
+     * 
+     * @return
+     *        an empty set if no default ImmutableGraph is specified,
+     *        otherwise a set of their UriRefs
+     */
+    public Set<IRI> getDefaultGraphs();
+
+    /**
+     *
+     * @return
+     *        an empty set if no named ImmutableGraph is specified,
+     *        otherwise a set of their UriRefs
+     */
+    public Set<IRI> getNamedGraphs();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/DescribeQuery.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/DescribeQuery.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/DescribeQuery.java
new file mode 100644
index 0000000..313c2a1
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/DescribeQuery.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.List;
+
+/**
+ * <p>This interface represents a SPARQL SELECT query.</p>
+ *
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#describe">
+ * SPARQL Query Language: 10.4 DESCRIBE (Informative)</a>
+ *
+ * @author hasan
+ */
+public interface DescribeQuery extends QueryWithSolutionModifier {
+
+    /**
+     * <p>Tests if all variables in the query should be described.</p>
+     * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#describe">
+     * SPARQL Query Language: 10.4 DESCRIBE (Informative)</a>
+     * @return <code>true</code> if the query should return all variables.
+     */
+    public boolean isDescribeAll();
+
+    /**
+     * <p>Gets the list of {@link ResourceOrVariable}s to describe.
+     * If {@link #isDescribeAll()} returns <code>true</code> then
+     * this list contains all the variables from the query.</p>
+     * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#describe">
+     * SPARQL Query Language: 10.4 DESCRIBE (Informative)</a>
+     * @return A list of {@link ResourceOrVariable}s to describe.
+     */
+    public List<ResourceOrVariable> getResourcesToDescribe();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/Expression.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/Expression.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/Expression.java
new file mode 100644
index 0000000..0bc9422
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/Expression.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.io.Serializable;
+
+/**
+ * This interface models logical, relational, and numeric expression.
+ * This includes terms and factors in mathematical formulas which can contain
+ * variables, literals, and function calls.
+ * In a SPARQL query, expressions can occur in an ORDER BY clause or
+ * a FILTER constraint.
+ *
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#modOrderBy">
+ * SPARQL Query Language: 9.1 ORDER BY</a>
+ *
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#termConstraint">
+ * SPARQL Query Language: 3 RDF Term Constraints (Informative)</a>
+ * 
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#evaluation">
+ * SPARQL Query Language: 11.2 Filter Evaluation</a>
+ *
+ * @author hasan
+ */
+public interface Expression extends Serializable {
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/FunctionCall.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/FunctionCall.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/FunctionCall.java
new file mode 100644
index 0000000..8190dec
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/FunctionCall.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.IRI;
+
+import java.util.List;
+
+/**
+ * Defines a function call which is one form of {@link Expression}.
+ * A function call has a name of type {@link IRI} and a list of arguments,
+ * where each argument is an {@link Expression}.
+ *
+ * @author hasan
+ */
+public class FunctionCall implements Expression {
+
+    private final IRI name;
+    private final List<Expression> arguments;
+
+    public FunctionCall(IRI name, List<Expression> arguments) {
+        this.name = name;
+        this.arguments = arguments;
+    }
+
+    public IRI getName() {
+        return name;
+    };
+
+    public List<Expression> getArguements() {
+        return arguments;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphGraphPattern.java
new file mode 100644
index 0000000..86f1a97
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphGraphPattern.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Defines a ImmutableGraph ImmutableGraph pattern.
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#queryDataset">
+ * SPARQL Query Language: 8.3 Querying the Dataset</a>
+ *
+ * @author hasan
+ */
+public interface GraphGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return a {@link UriRefOrVariable} which specifies the ImmutableGraph
+     *        against which the pattern should match.
+     */
+    public UriRefOrVariable getGraph();
+
+    /**
+     * 
+     * @return the pattern to match.
+     */
+    public GroupGraphPattern getGroupGraphPattern();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphPattern.java
new file mode 100644
index 0000000..048bb84
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/GraphPattern.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * This is the generic interface for all types of ImmutableGraph patterns:
+ * {@link BasicGraphPattern}, {@link PathSupportedBasicGraphPattern}, {@link GroupGraphPattern},
+ * {@link GraphGraphPattern}, {@link AlternativeGraphPattern}, and
+ * {@link OptionalGraphPattern}
+ *
+ * @author hasan
+ */
+public interface GraphPattern {
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/GroupGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/GroupGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/GroupGraphPattern.java
new file mode 100644
index 0000000..4025e03
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/GroupGraphPattern.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.IRI;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Defines a group ImmutableGraph pattern.
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#GroupPatterns">
+ * SPARQL Query Language: 5.2 Group ImmutableGraph Patterns</a>
+ *
+ * @author hasan
+ */
+public interface GroupGraphPattern extends GraphPattern {
+
+	/**
+	 *
+	 * @return 
+     *      true if it wraps a {@link SelectQuery}, false otherwise.
+	 */
+	public boolean isSubSelect();
+
+	/**
+	 *
+	 * @return 
+     *      the wrapped subselect if it wraps a {@link SelectQuery}, null otherwise.
+	 */
+	public SelectQuery getSubSelect();
+
+	/**
+	 *
+	 * @return
+     *      null if it wraps a {@link SelectQuery}, otherwise
+     *      a set of all patterns, ANDed together.
+	 */
+	public Set<GraphPattern> getGraphPatterns();
+
+    /**
+     * 
+     * @return
+     *      all graphs referred in this ImmutableGraph pattern.
+     */
+    public Set<IRI> getReferredGraphs();
+
+    /**
+	 * @return 
+     *      null if it wraps a {@link SelectQuery}, otherwise
+	 *		a list of filter expressions for all patterns in the group if any.
+	 */
+	public List<Expression> getFilter();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/InlineData.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/InlineData.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/InlineData.java
new file mode 100644
index 0000000..6bd4377
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/InlineData.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.RDFTerm;
+
+import java.util.List;
+
+/**
+ *
+ * @author hasan
+ */
+public interface InlineData extends GraphPattern {
+    public List<Variable> getVariables();
+    public List<List<RDFTerm>> getValues();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/LiteralExpression.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/LiteralExpression.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/LiteralExpression.java
new file mode 100644
index 0000000..9532908
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/LiteralExpression.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.Literal;
+
+/**
+ * Wraps a {@link Literal} in an {@link Expression}.
+ *
+ * @author hasan
+ */
+public class LiteralExpression implements Expression {
+
+    private final Literal literal;
+
+    public LiteralExpression(Literal literal) {
+        this.literal = literal;
+    }
+
+    public Literal getLiteral() {
+        return literal;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/MinusGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/MinusGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/MinusGraphPattern.java
new file mode 100644
index 0000000..5696fab
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/MinusGraphPattern.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * 
+ * @author hasan
+ */
+public interface MinusGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return
+     *        the minuend ImmutableGraph pattern to match
+     */
+    public GraphPattern getMinuendGraphPattern();
+
+    /**
+     *
+     * @return
+     *        the subtrahend ImmutableGraph pattern to match
+     */
+    public GroupGraphPattern getSubtrahendGraphPattern();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/OptionalGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/OptionalGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/OptionalGraphPattern.java
new file mode 100644
index 0000000..ae09193
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/OptionalGraphPattern.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Specifying an optional ImmutableGraph pattern implies the existence of a main ImmutableGraph
+ * pattern.
+ * The main ImmutableGraph pattern is an empty group pattern if it is not specified.
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#optionals">
+ * SPARQL Query Language: 6 Including Optional Values</a>
+ * 
+ * @author hasan
+ */
+public interface OptionalGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return
+     *        the main ImmutableGraph pattern to match
+     */
+    public GraphPattern getMainGraphPattern();
+
+    /**
+     *
+     * @return
+     *        the optional ImmutableGraph pattern to match
+     */
+    public GroupGraphPattern getOptionalGraphPattern();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/OrderCondition.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/OrderCondition.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/OrderCondition.java
new file mode 100644
index 0000000..5c5ace5
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/OrderCondition.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Defines an order condition in an ORDER BY clause.
+ *
+ * @see <a href="http://www.w3.org/TR/rdf-sparql-query/#modOrderBy">
+ * SPARQL Query Language: 9.1 ORDER BY</a>
+ *
+ * @author hasan
+ */
+public interface OrderCondition {
+
+    public Expression getExpression();
+
+    /**
+     * @return <code>true</code> if ascending, <code>false</code> if descending
+     */
+    public boolean isAscending();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/PathSupportedBasicGraphPattern.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/PathSupportedBasicGraphPattern.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/PathSupportedBasicGraphPattern.java
new file mode 100644
index 0000000..535bdd5
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/PathSupportedBasicGraphPattern.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.Set;
+
+/**
+ * Defines a basic ImmutableGraph pattern that supports property path expressions.
+ * A {@link PathSupportedBasicGraphPattern} is a set of {@link PropertyPathPattern}s.
+ * A {@link PropertyPathPattern} is a generalization of a {@link TriplePattern} to include
+ * a {@link PropertyPathExpression} in the property position.
+ * Therefore, a {@link PathSupportedBasicGraphPattern} can be seen as a generalization of a {@link BasicGraphPattern}
+ * @see <a href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#sparqlBasicGraphPatterns">
+ * SPARQL 1.1 Query Language: 18.1.6 Basic ImmutableGraph Patterns</a>
+ * and <a href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#sparqlPropertyPaths">
+ * SPARQL 1.1 Query Language: 18.1.7 Property Path Patterns</a>
+ *
+ * @author hasan
+ */
+public interface PathSupportedBasicGraphPattern extends GraphPattern {
+
+    /**
+     *
+     * @return a set of all property path patterns to match.
+     */
+    public Set<PropertyPathPattern> getPropertyPathPatterns();
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/PatternExistenceCondition.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/PatternExistenceCondition.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/PatternExistenceCondition.java
new file mode 100644
index 0000000..fb0aa2b
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/PatternExistenceCondition.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This expression is intended to be used as a filter expression to test whether a ImmutableGraph pattern matches 
+ * the dataset or not, given the values of variables in the group ImmutableGraph pattern in which the filter occurs.
+ * It does not generate any additional bindings.
+ * 
+ * @see <a href="http://www.w3.org/TR/sparql11-query/#neg-pattern">SPARQL 1.1 Query Language: 8.1 Filtering Using ImmutableGraph Patterns</a>
+ * 
+ * @author hasan
+ */
+public class PatternExistenceCondition extends BuiltInCall {
+    private boolean negated = false;
+    private GroupGraphPattern pattern;
+
+    public PatternExistenceCondition() {
+        super("EXISTS", new ArrayList<Expression>());
+    }
+
+    public PatternExistenceCondition(String name, List<Expression> arguments) {
+        super(name, new ArrayList<Expression>());
+        if (!(name.equalsIgnoreCase("EXISTS") || name.equalsIgnoreCase("NOT EXISTS"))) {
+            throw new RuntimeException("Unsupported name: " + name);
+        } else {
+            this.negated = name.equalsIgnoreCase("NOT EXISTS");
+        }
+    }
+
+    public boolean isExistenceTest() {
+        return !negated;
+    }
+
+    public GroupGraphPattern getPattern() {
+        return pattern;
+    }
+
+    public void setExistenceTest(boolean existenceTest) {
+        this.negated = !existenceTest;
+        this.name = existenceTest ? "EXISTS" : "NOT EXISTS";
+    }
+
+    public void setPattern(GroupGraphPattern pattern) {
+        this.pattern = pattern;
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/PredicatePath.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/PredicatePath.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/PredicatePath.java
new file mode 100644
index 0000000..a63e227
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/PredicatePath.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+import org.apache.clerezza.api.IRI;
+
+/**
+ *
+ * @author hasan
+ */
+public class PredicatePath implements PropertyPathExpression {
+    private IRI predicatePath;
+
+    public PredicatePath(IRI predicatePath) {
+        this.predicatePath = predicatePath;
+    }
+
+    public IRI getPredicatePath() {
+        return predicatePath;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpression.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpression.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpression.java
new file mode 100644
index 0000000..905aa71
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpression.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * This interface models property path expressions.
+ * @see <a href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#propertypaths">
+ * SPARQL 1.1 Query Language: 9 Property Paths</a>
+ * 
+ * @author hasan
+ */
+public interface PropertyPathExpression {
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/eec0ac73/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpressionOrVariable.java
----------------------------------------------------------------------
diff --git a/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpressionOrVariable.java b/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpressionOrVariable.java
new file mode 100644
index 0000000..552f8a4
--- /dev/null
+++ b/sparql/src/main/java/org/apache/clerezza/sparql/query/PropertyPathExpressionOrVariable.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.clerezza.sparql.query;
+
+/**
+ * Wraps either a {@link PropertyPathExpression} or a {@link Variable}
+ *
+ * @author hasan
+ */
+public class PropertyPathExpressionOrVariable {
+
+    private final PropertyPathExpression propertyPathExpression;
+    private final Variable variable;
+
+    public PropertyPathExpressionOrVariable(PropertyPathExpression propertyPathExpression) {
+        if (propertyPathExpression == null) {
+            throw new IllegalArgumentException("Invalid propertyPathExpression: null");
+        }
+        this.propertyPathExpression = propertyPathExpression;
+        variable = null;
+    }
+
+    public PropertyPathExpressionOrVariable(Variable variable) {
+        if (variable == null) {
+            throw new IllegalArgumentException("Invalid variable: null");
+        }
+        this.variable = variable;
+        propertyPathExpression = null;
+    }
+
+    /**
+     *
+     * @return
+     *        true if it is a {@link Variable}, false if it is a {@link PropertyPathExpression}
+     */
+    public boolean isVariable() {
+        return propertyPathExpression == null;
+    }
+
+    /**
+     * 
+     * @return
+     *        the wrapped PropertyPathExpression if it is a PropertyPathExpression, null otherwise
+     */
+    public PropertyPathExpression getPropertyPathExpression() {
+        return propertyPathExpression;
+    }
+    
+    /**
+     * 
+     * @return
+     *        the wrapped Variable if it is a Variable, null otherwise
+     */
+    public Variable getVariable() {
+        return variable;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (!(obj instanceof PropertyPathExpressionOrVariable)) {
+            return false;
+        }
+        final PropertyPathExpressionOrVariable other = (PropertyPathExpressionOrVariable) obj;
+        if (this.isVariable() != other.isVariable()) {
+            return false;
+        }
+        if (this.isVariable()) {
+            if (!this.getVariable().equals(other.getVariable())) {
+                return false;
+            }
+        } else {
+            if (!this.getPropertyPathExpression().equals(other.getPropertyPathExpression())) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return (isVariable()
+                ? 13 * getVariable().hashCode() + 17
+                : 13 * getPropertyPathExpression().hashCode() + 17);
+    }
+}