clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject svn commit: r1536057 - in /clerezza/trunk/rdf.core/src: main/java/org/apache/clerezza/rdf/core/sparql/query/ main/java/org/apache/clerezza/rdf/core/sparql/query/impl/ main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ main/javacc/org/apache/cle...
Date Sun, 27 Oct 2013 06:39:08 GMT
Author: hasan
Date: Sun Oct 27 06:39:08 2013
New Revision: 1536057

URL: http://svn.apache.org/r1536057
Log:
CLEREZZA-761: Added support of parsing pattern existence condition and modify operation in
sparql pre parser

Added:
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/PatternExistenceCondition.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ModifyOperation.java
Modified:
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/BuiltInCall.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/GroupGraphPattern.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleDataSet.java
    clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleGroupGraphPattern.java
    clerezza/trunk/rdf.core/src/main/javacc/org/apache/clerezza/rdf/core/sparql/JavaCCGeneratedSparqlPreParser.jj
    clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/sparql/SparqlPreParserTest.java

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/BuiltInCall.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/BuiltInCall.java?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/BuiltInCall.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/BuiltInCall.java
Sun Oct 27 06:39:08 2013
@@ -29,7 +29,7 @@ import java.util.List;
  */
 public class BuiltInCall implements Expression {
 
-    private final String name;
+    protected String name;
     private final List<Expression> arguments;
 
     public BuiltInCall(String name, List<Expression> arguments) {

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/GroupGraphPattern.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/GroupGraphPattern.java?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/GroupGraphPattern.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/GroupGraphPattern.java
Sun Oct 27 06:39:08 2013
@@ -20,6 +20,7 @@ package org.apache.clerezza.rdf.core.spa
 
 import java.util.List;
 import java.util.Set;
+import org.apache.clerezza.rdf.core.UriRef;
 
 /**
  * Defines a group graph pattern.
@@ -52,7 +53,14 @@ public interface GroupGraphPattern exten
 	 */
 	public Set<GraphPattern> getGraphPatterns();
 
-	/**
+    /**
+     * 
+     * @return
+     *      all graphs referred in this graph pattern.
+     */
+    public Set<UriRef> getReferredGraphs();
+
+    /**
 	 * @return 
      *      null if it wraps a {@link SelectQuery}, otherwise
 	 *		a list of filter expressions for all patterns in the group if any.

Added: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/PatternExistenceCondition.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/PatternExistenceCondition.java?rev=1536057&view=auto
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/PatternExistenceCondition.java
(added)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/PatternExistenceCondition.java
Sun Oct 27 06:39:08 2013
@@ -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.rdf.core.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 graph
pattern matches 
+ * the dataset or not, given the values of variables in the group graph 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 Graph 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;
+    }
+}

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleDataSet.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleDataSet.java?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleDataSet.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleDataSet.java
Sun Oct 27 06:39:08 2013
@@ -41,11 +41,11 @@ public class SimpleDataSet implements Da
         return namedGraphs;
     }
 
-    void addDefaultGraph(UriRef defaultGraph) {
+    public void addDefaultGraph(UriRef defaultGraph) {
         defaultGraphs.add(defaultGraph);
     }
 
-    void addNamedGraph(UriRef namedGraph) {
+    public void addNamedGraph(UriRef namedGraph) {
         namedGraphs.add(namedGraph);
     }
 }

Modified: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleGroupGraphPattern.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleGroupGraphPattern.java?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleGroupGraphPattern.java
(original)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/query/impl/SimpleGroupGraphPattern.java
Sun Oct 27 06:39:08 2013
@@ -19,17 +19,24 @@
 package org.apache.clerezza.rdf.core.sparql.query.impl;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.sparql.query.AlternativeGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.BasicGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.Expression;
+import org.apache.clerezza.rdf.core.sparql.query.GraphGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.GraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.GroupGraphPattern;
+import org.apache.clerezza.rdf.core.sparql.query.MinusGraphPattern;
+import org.apache.clerezza.rdf.core.sparql.query.OptionalGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.PathSupportedBasicGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.PropertyPathPattern;
 import org.apache.clerezza.rdf.core.sparql.query.SelectQuery;
 import org.apache.clerezza.rdf.core.sparql.query.TriplePattern;
+import org.apache.clerezza.rdf.core.sparql.query.UriRefOrVariable;
 
 /**
  * This class implements {@link GroupGraphPattern}.
@@ -180,4 +187,50 @@ public class SimpleGroupGraphPattern imp
 		graphPatterns.add(new SimpleMinusGraphPattern(prevGraphPattern, subtrahend));
         lastBasicGraphPatternIsComplete = true;
 	}
+
+    @Override
+    public Set<UriRef> getReferredGraphs() {
+        Set<UriRef> referredGraphs = new HashSet<UriRef>();
+        if (subSelect != null) {
+            GroupGraphPattern queryPattern = subSelect.getQueryPattern();
+            referredGraphs.addAll(queryPattern.getReferredGraphs());
+        } else {
+            for (GraphPattern graphPattern : graphPatterns) {
+                referredGraphs.addAll(getReferredGraphs(graphPattern));
+            }
+        }
+        return referredGraphs;
+    }
+
+    private Set<UriRef> getReferredGraphs(GraphPattern graphPattern) {
+        Set<UriRef> referredGraphs = new HashSet<UriRef>();
+        if (graphPattern instanceof GraphGraphPattern) {
+            GraphGraphPattern graphGraphPattern = (GraphGraphPattern) graphPattern;
+            UriRefOrVariable graph = graphGraphPattern.getGraph();
+            if (!graph.isVariable()) {
+                referredGraphs.add(graph.getResource());
+            }
+            referredGraphs.addAll(graphGraphPattern.getGroupGraphPattern().getReferredGraphs());
+        } else if (graphPattern instanceof AlternativeGraphPattern) {
+            List<GroupGraphPattern> alternativeGraphPatterns =
+                    ((AlternativeGraphPattern) graphPattern).getAlternativeGraphPatterns();
+            for (GroupGraphPattern groupGraphPattern : alternativeGraphPatterns) {
+                referredGraphs.addAll(groupGraphPattern.getReferredGraphs());
+            }
+        } else if (graphPattern instanceof OptionalGraphPattern) {
+            GraphPattern mainGraphPattern = ((OptionalGraphPattern) graphPattern).getMainGraphPattern();
+            referredGraphs.addAll(getReferredGraphs(mainGraphPattern));
+            GroupGraphPattern optionalGraphPattern = ((OptionalGraphPattern) graphPattern).getOptionalGraphPattern();
+            referredGraphs.addAll(optionalGraphPattern.getReferredGraphs());
+        } else if (graphPattern instanceof MinusGraphPattern) {
+            GraphPattern minuendGraphPattern = ((MinusGraphPattern) graphPattern).getMinuendGraphPattern();
+            referredGraphs.addAll(getReferredGraphs(minuendGraphPattern));
+            GroupGraphPattern subtrahendGraphPattern = ((MinusGraphPattern) graphPattern).getSubtrahendGraphPattern();
+            referredGraphs.addAll(subtrahendGraphPattern.getReferredGraphs());
+        } else if (graphPattern instanceof GroupGraphPattern) {
+            GroupGraphPattern groupGraphPattern = (GroupGraphPattern) graphPattern;
+            referredGraphs.addAll(groupGraphPattern.getReferredGraphs());
+        }
+        return referredGraphs;
+    }
 }

Added: clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ModifyOperation.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ModifyOperation.java?rev=1536057&view=auto
==============================================================================
--- clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ModifyOperation.java
(added)
+++ clerezza/trunk/rdf.core/src/main/java/org/apache/clerezza/rdf/core/sparql/update/impl/ModifyOperation.java
Sun Oct 27 06:39:08 2013
@@ -0,0 +1,111 @@
+/*
+ * 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.rdf.core.sparql.update.impl;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.TcProvider;
+import org.apache.clerezza.rdf.core.sparql.query.GroupGraphPattern;
+import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleDataSet;
+import org.apache.clerezza.rdf.core.sparql.update.UpdateOperation;
+
+/**
+ * This ModifyOperation is a DELETE/INSERT operation.
+ * @see <a href="http://www.w3.org/TR/2013/REC-sparql11-update-20130321/#deleteInsert">SPARQL
1.1 Update: 3.1.3 DELETE/INSERT</a>
+ * 
+ * The DELETE/INSERT operation can be used to remove or add triples from/to the Graph Store
based on bindings 
+ * for a query pattern specified in a WHERE clause.
+ * 
+ * @author hasan
+ */
+public class ModifyOperation implements UpdateOperation {
+    private UriRef fallbackGraph = null;
+    private UpdateOperationWithQuads deleteOperation = null;
+    private UpdateOperationWithQuads insertOperation = null;
+    private SimpleDataSet dataSet = null;
+    private GroupGraphPattern queryPattern = null;
+
+    public void setFallbackGraph(UriRef fallbackGraph) {
+        this.fallbackGraph = fallbackGraph;
+    }
+
+    public void setDeleteOperation(UpdateOperationWithQuads deleteOperation) {
+        this.deleteOperation = deleteOperation;
+    }
+
+    public void setInsertOperation(UpdateOperationWithQuads insertOperation) {
+        this.insertOperation = insertOperation;
+    }
+
+    public void setDataSet(SimpleDataSet dataSet) {
+        this.dataSet = dataSet;
+    }
+
+    public void addGraphToDataSet(UriRef graph) {
+        if (dataSet == null) {
+            dataSet = new SimpleDataSet();
+        }
+        dataSet.addDefaultGraph(graph);
+    }
+
+    public void addNamedGraphToDataSet(UriRef namedGraph) {
+        if (dataSet == null) {
+            dataSet = new SimpleDataSet();
+        }
+        dataSet.addNamedGraph(namedGraph);
+    }
+
+    public void setQueryPattern(GroupGraphPattern queryPattern) {
+        this.queryPattern = queryPattern;
+    }
+
+    @Override
+    public Set<UriRef> getInputGraphs(UriRef defaultGraph, TcProvider tcProvider) {
+        Set<UriRef> graphs = new HashSet<UriRef>();
+        if (dataSet != null) {
+            graphs.addAll(dataSet.getDefaultGraphs());
+            graphs.addAll(dataSet.getNamedGraphs());
+        } else {
+            if (fallbackGraph != null) {
+                graphs.add(fallbackGraph);
+            }
+        }
+        if (graphs.isEmpty()) {
+            graphs.add(defaultGraph);
+        }
+        if (queryPattern != null) {
+            graphs.addAll(queryPattern.getReferredGraphs());
+        }
+        return graphs;
+    }
+
+    @Override
+    public Set<UriRef> getDestinationGraphs(UriRef defaultGraph, TcProvider tcProvider)
{
+        Set<UriRef> graphs = new HashSet<UriRef>();
+        UriRef dfltGraph = (fallbackGraph != null) ? fallbackGraph : defaultGraph;
+        if (deleteOperation != null) {
+            graphs.addAll(deleteOperation.getDestinationGraphs(dfltGraph, tcProvider));
+        }
+        if (insertOperation != null) {
+            graphs.addAll(insertOperation.getDestinationGraphs(dfltGraph, tcProvider));
+        }
+        return graphs;
+    }
+}

Modified: clerezza/trunk/rdf.core/src/main/javacc/org/apache/clerezza/rdf/core/sparql/JavaCCGeneratedSparqlPreParser.jj
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/main/javacc/org/apache/clerezza/rdf/core/sparql/JavaCCGeneratedSparqlPreParser.jj?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/main/javacc/org/apache/clerezza/rdf/core/sparql/JavaCCGeneratedSparqlPreParser.jj
(original)
+++ clerezza/trunk/rdf.core/src/main/javacc/org/apache/clerezza/rdf/core/sparql/JavaCCGeneratedSparqlPreParser.jj
Sun Oct 27 06:39:08 2013
@@ -56,6 +56,7 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.core.sparql.query.GroupGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.InlineData;
 import org.apache.clerezza.rdf.core.sparql.query.LiteralExpression;
+import org.apache.clerezza.rdf.core.sparql.query.PatternExistenceCondition;
 import org.apache.clerezza.rdf.core.sparql.query.PredicatePath;
 import org.apache.clerezza.rdf.core.sparql.query.PropertyPathExpressionOrVariable;
 import org.apache.clerezza.rdf.core.sparql.query.PropertyPathExpression;
@@ -75,6 +76,7 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleAlternativeGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleAskQuery;
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery;
+import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleDataSet;
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleDescribeQuery;
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleGraphGraphPattern;
 import org.apache.clerezza.rdf.core.sparql.query.impl.SimpleGroupGraphPattern;
@@ -100,6 +102,7 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.core.sparql.update.impl.DropOperation;
 import org.apache.clerezza.rdf.core.sparql.update.impl.InsertDataOperation;
 import org.apache.clerezza.rdf.core.sparql.update.impl.LoadOperation;
+import org.apache.clerezza.rdf.core.sparql.update.impl.ModifyOperation;
 import org.apache.clerezza.rdf.core.sparql.update.impl.MoveOperation;
 import org.apache.clerezza.rdf.core.sparql.update.impl.SimpleUpdate;
 import org.apache.clerezza.rdf.core.sparql.update.impl.SimpleUpdateOperation;
@@ -390,6 +393,7 @@ TOKEN [IGNORE_CASE] :
 |   < ALL : "ALL" >
 |   < IN : "IN" >
 |   < NOT : "NOT" >
+|   < EXISTS : "EXISTS" >
 |   < BNODE : "BNODE" >
 |   < RAND : "RAND" >
 |   < CONCAT : "CONCAT" >
@@ -760,12 +764,16 @@ private void Update1(Update update) : {
     |
         updateOperation = Create()
     |
+        LOOKAHEAD(2)
         updateOperation = InsertData()
     |
         LOOKAHEAD(2)
         updateOperation = DeleteData()
     |
+        LOOKAHEAD(2)
         updateOperation = DeleteWhere()
+    |
+        updateOperation = Modify()
     ) {
     if (updateOperation != null) {
         update.addOperation(updateOperation);
@@ -919,9 +927,77 @@ private UpdateOperation DeleteWhere() : 
 }
 
 /* [41]    Modify ::= ( 'WITH' iri )? ( DeleteClause InsertClause? | InsertClause ) UsingClause*
'WHERE' GroupGraphPattern */
+private UpdateOperation Modify() : {
+    UriRef fallbackGraph = null;
+    UpdateOperationWithQuads deleteOperation = null;
+    UpdateOperationWithQuads insertOperation = null;
+    SimpleDataSet dataSet = new SimpleDataSet();
+    GroupGraphPattern queryPattern;
+    ModifyOperation operation; } {
+    (
+        <WITH>
+        fallbackGraph = Iri()
+    )?
+    (
+        deleteOperation = DeleteClause()
+        (
+            insertOperation = InsertClause()
+        )?
+    |
+        insertOperation = InsertClause()
+    ) {
+    operation = new ModifyOperation();
+    if (fallbackGraph != null) {
+        operation.setFallbackGraph(fallbackGraph);
+    }
+    if (deleteOperation != null) {
+        operation.setDeleteOperation(deleteOperation);
+    }
+    if (insertOperation != null) {
+        operation.setInsertOperation(insertOperation);
+    } }
+    (
+        UsingClause(dataSet)
+    )* {
+    operation.setDataSet(dataSet); }
+
+    <WHERE>
+    queryPattern = GroupGraphPattern() {
+    operation.setQueryPattern(queryPattern);
+    return operation; }
+}
+
 /* [42]    DeleteClause ::= 'DELETE' QuadPattern */
+private UpdateOperationWithQuads DeleteClause() : {
+    UpdateOperationWithQuads operation; } {
+    <DELETE> {
+    operation = new UpdateOperationWithQuads(); }
+    QuadPattern(operation) {
+    return operation; }
+}
+
 /* [43]    InsertClause ::= 'INSERT' QuadPattern */
+private UpdateOperationWithQuads InsertClause() : {
+    UpdateOperationWithQuads operation; } {
+    <INSERT> {
+    operation = new UpdateOperationWithQuads(); }
+    QuadPattern(operation) {
+    return operation; }
+}
+
 /* [44]    UsingClause ::= 'USING' ( iri | 'NAMED' iri ) */
+private void UsingClause(SimpleDataSet dataSet) : {
+    UriRef graph; } {
+    <USING>
+    (
+        graph = Iri() {
+        dataSet.addDefaultGraph(graph); }
+    |
+        <NAMED>
+        graph = Iri() {
+        dataSet.addNamedGraph(graph); }
+    )
+}
 
 /* [45]    GraphOrDefault ::= 'DEFAULT_T' | 'GRAPH'? iri */
 private GraphRefAllSpec GraphOrDefault() : {
@@ -2146,11 +2222,12 @@ private BuiltInCall BuiltInCall() : {
         <COALESCE> {
         name = "COALESCE"; }
         args=ExpressionList()
-/*
     |
         e=NotExistsFunc() {
         return (BuiltInCall) e; }
-*/
+    |
+        e=ExistsFunc() {
+        return (BuiltInCall) e; }
     |
         name=BuiltInCallName()
         "("
@@ -2182,10 +2259,30 @@ private String BuiltInCallName() : {
     return t.image; }
 }
 
-/*
-[125]    ExistsFunc ::= 'EXISTS' GroupGraphPattern
-[126]    NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern
-*/
+/* [125]    ExistsFunc ::= 'EXISTS' GroupGraphPattern */
+private Expression ExistsFunc() : {
+    GroupGraphPattern pattern; 
+    PatternExistenceCondition patternExistenceCondition; } {
+
+    <EXISTS>
+    pattern = GroupGraphPattern() {
+    patternExistenceCondition = new PatternExistenceCondition();
+    patternExistenceCondition.setPattern(pattern);
+    return patternExistenceCondition; }
+}
+
+/* [126]    NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern */
+private Expression NotExistsFunc() : {
+    GroupGraphPattern pattern; 
+    PatternExistenceCondition patternExistenceCondition; } {
+
+    <NOT> <EXISTS>
+    pattern = GroupGraphPattern() {
+    patternExistenceCondition = new PatternExistenceCondition();
+    patternExistenceCondition.setPattern(pattern);
+    patternExistenceCondition.setExistenceTest(false);
+    return patternExistenceCondition; }
+}
 
 /* [128]    IriOrFunction ::= iri ArgList? */
 private Expression IriOrFunction() : {

Modified: clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/sparql/SparqlPreParserTest.java
URL: http://svn.apache.org/viewvc/clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/sparql/SparqlPreParserTest.java?rev=1536057&r1=1536056&r2=1536057&view=diff
==============================================================================
--- clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/sparql/SparqlPreParserTest.java
(original)
+++ clerezza/trunk/rdf.core/src/test/java/org/apache/clerezza/rdf/core/sparql/SparqlPreParserTest.java
Sun Oct 27 06:39:08 2013
@@ -365,4 +365,113 @@ public class SparqlPreParserTest {
         expected.add(new UriRef("http://example.com/addresses"));
         Assert.assertTrue(referredGraphs.containsAll(expected));
     }
+
+    @Test
+    public void testModifyOperationWithFallbackGraph() throws ParseException {
+        String queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> WITH " + TEST_GRAPH.toString()
+
+                " DELETE { ?person foaf:givenName 'Bill' } INSERT { ?person foaf:givenName
'William' }" +
+                " WHERE { ?person foaf:givenName 'Bill' }";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+        Assert.assertTrue(referredGraphs.toArray()[0].equals(TEST_GRAPH));
+    }
+
+    @Test
+    public void testDeleteOperationInDefaultGraph() throws ParseException {
+        String queryStr = "PREFIX dc:  <http://purl.org/dc/elements/1.1/> PREFIX xsd:
<http://www.w3.org/2001/XMLSchema#> " +
+                "DELETE { ?book ?p ?v } WHERE { ?book dc:date ?date . " +
+                "FILTER ( ?date > \"1970-01-01T00:00:00-02:00\"^^xsd:dateTime ) ?book
?p ?v }";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+        Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH));
+    }
+
+    @Test
+    public void testInsertOperationToNamedGraph() throws ParseException {
+        String queryStr = "PREFIX dc:  <http://purl.org/dc/elements/1.1/> PREFIX xsd:
<http://www.w3.org/2001/XMLSchema#> " +
+                "INSERT { GRAPH <http://example/bookStore2> { ?book ?p ?v } } " +
+                "WHERE { GRAPH <http://example/bookStore> { ?book dc:date ?date . "
+
+                "FILTER ( ?date > \"1970-01-01T00:00:00-02:00\"^^xsd:dateTime ) ?book
?p ?v } }";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+
+        Set<UriRef> expected = new HashSet<UriRef>();
+        expected.add(new UriRef("http://example/bookStore2"));
+        expected.add(new UriRef("http://example/bookStore"));
+        Assert.assertTrue(referredGraphs.containsAll(expected));
+    }
+
+    @Test
+    public void testInsertAndDeleteWithCommonPrefix() throws ParseException {
+        String queryStr = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
+                "PREFIX dcmitype: <http://purl.org/dc/dcmitype/>\n" +
+                "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n\n" +
+                "INSERT\n" +
+                "  { GRAPH <http://example/bookStore2> { ?book ?p ?v } }\n" +
+                "WHERE\n" +
+                "  { GRAPH <http://example/bookStore>\n" +
+                "    { ?book dc:date ?date . \n" +
+                "      FILTER ( ?date < \"2000-01-01T00:00:00-02:00\"^^xsd:dateTime )\n"
+
+                "      ?book ?p ?v\n" +
+                "    }\n" +
+                "  } ;\n\n" +
+                "WITH <http://example/bookStore>\n" +
+                "DELETE\n" +
+                " { ?book ?p ?v }\n" +
+                "WHERE\n" +
+                "  { ?book dc:date ?date ;\n" +
+                "          dc:type dcmitype:PhysicalObject .\n" +
+                "    FILTER ( ?date < \"2000-01-01T00:00:00-02:00\"^^xsd:dateTime ) \n"
+
+                "    ?book ?p ?v\n" +
+                "  }";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+
+        Set<UriRef> expected = new HashSet<UriRef>();
+        expected.add(new UriRef("http://example/bookStore2"));
+        expected.add(new UriRef("http://example/bookStore"));
+        Assert.assertTrue(referredGraphs.containsAll(expected));
+    }
+
+    @Test
+    public void testExistsFunction() throws ParseException {
+        String queryStr = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
\n" +
+                "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n\n" +
+                "SELECT ?person\n" +
+                "WHERE \n" +
+                "{\n" +
+                "  ?person rdf:type foaf:Person .\n" +
+                "  FILTER EXISTS { ?person foaf:name ?name }\n" +
+                "}";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+        Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH));
+    }
+
+    @Test
+    public void testNotExistsFunction() throws ParseException {
+        String queryStr = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
\n" +
+                "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n\n" +
+                "SELECT ?person\n" +
+                "WHERE \n" +
+                "{\n" +
+                "  ?person rdf:type foaf:Person .\n" +
+                "  FILTER NOT EXISTS { ?person foaf:name ?name }\n" +
+                "}";
+
+        SparqlPreParser parser;
+        parser = new SparqlPreParser(TcManager.getInstance());
+        Set<UriRef> referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH);
+        Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH));
+    }
 }



Mime
View raw message