Return-Path: X-Original-To: apmail-clerezza-commits-archive@www.apache.org Delivered-To: apmail-clerezza-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A805D1038A for ; Sun, 27 Oct 2013 06:39:43 +0000 (UTC) Received: (qmail 8553 invoked by uid 500); 27 Oct 2013 06:39:42 -0000 Delivered-To: apmail-clerezza-commits-archive@clerezza.apache.org Received: (qmail 8488 invoked by uid 500); 27 Oct 2013 06:39:39 -0000 Mailing-List: contact commits-help@clerezza.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@clerezza.apache.org Delivered-To: mailing list commits@clerezza.apache.org Received: (qmail 8481 invoked by uid 99); 27 Oct 2013 06:39:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Oct 2013 06:39:37 +0000 X-ASF-Spam-Status: No, hits=-1996.5 required=5.0 tests=ALL_TRUSTED,URIBL_BLACK X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Oct 2013 06:39:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2C0812388831; Sun, 27 Oct 2013 06:39:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@clerezza.apache.org From: hasan@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131027063909.2C0812388831@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 arguments; public BuiltInCall(String name, List 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 getGraphPatterns(); - /** + /** + * + * @return + * all graphs referred in this graph pattern. + */ + public Set 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 SPARQL 1.1 Query Language: 8.1 Filtering Using Graph Patterns + * + * @author hasan + */ +public class PatternExistenceCondition extends BuiltInCall { + private boolean negated = false; + private GroupGraphPattern pattern; + + public PatternExistenceCondition() { + super("EXISTS", new ArrayList()); + } + + public PatternExistenceCondition(String name, List arguments) { + super(name, new ArrayList()); + 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 getReferredGraphs() { + Set referredGraphs = new HashSet(); + if (subSelect != null) { + GroupGraphPattern queryPattern = subSelect.getQueryPattern(); + referredGraphs.addAll(queryPattern.getReferredGraphs()); + } else { + for (GraphPattern graphPattern : graphPatterns) { + referredGraphs.addAll(getReferredGraphs(graphPattern)); + } + } + return referredGraphs; + } + + private Set getReferredGraphs(GraphPattern graphPattern) { + Set referredGraphs = new HashSet(); + 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 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 SPARQL 1.1 Update: 3.1.3 DELETE/INSERT + * + * 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 getInputGraphs(UriRef defaultGraph, TcProvider tcProvider) { + Set graphs = new HashSet(); + 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 getDestinationGraphs(UriRef defaultGraph, TcProvider tcProvider) { + Set graphs = new HashSet(); + 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; } { + ( + + 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); } + + + queryPattern = GroupGraphPattern() { + operation.setQueryPattern(queryPattern); + return operation; } +} + /* [42] DeleteClause ::= 'DELETE' QuadPattern */ +private UpdateOperationWithQuads DeleteClause() : { + UpdateOperationWithQuads operation; } { + { + operation = new UpdateOperationWithQuads(); } + QuadPattern(operation) { + return operation; } +} + /* [43] InsertClause ::= 'INSERT' QuadPattern */ +private UpdateOperationWithQuads InsertClause() : { + UpdateOperationWithQuads operation; } { + { + operation = new UpdateOperationWithQuads(); } + QuadPattern(operation) { + return operation; } +} + /* [44] UsingClause ::= 'USING' ( iri | 'NAMED' iri ) */ +private void UsingClause(SimpleDataSet dataSet) : { + UriRef graph; } { + + ( + graph = Iri() { + dataSet.addDefaultGraph(graph); } + | + + graph = Iri() { + dataSet.addNamedGraph(graph); } + ) +} /* [45] GraphOrDefault ::= 'DEFAULT_T' | 'GRAPH'? iri */ private GraphRefAllSpec GraphOrDefault() : { @@ -2146,11 +2222,12 @@ private BuiltInCall BuiltInCall() : { { 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; } { + + + pattern = GroupGraphPattern() { + patternExistenceCondition = new PatternExistenceCondition(); + patternExistenceCondition.setPattern(pattern); + return patternExistenceCondition; } +} + +/* [126] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern */ +private Expression NotExistsFunc() : { + GroupGraphPattern pattern; + PatternExistenceCondition patternExistenceCondition; } { + + + 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: 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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + Assert.assertTrue(referredGraphs.toArray()[0].equals(TEST_GRAPH)); + } + + @Test + public void testDeleteOperationInDefaultGraph() throws ParseException { + String queryStr = "PREFIX dc: PREFIX xsd: " + + "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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH)); + } + + @Test + public void testInsertOperationToNamedGraph() throws ParseException { + String queryStr = "PREFIX dc: PREFIX xsd: " + + "INSERT { GRAPH { ?book ?p ?v } } " + + "WHERE { GRAPH { ?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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + + Set expected = new HashSet(); + 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: \n" + + "PREFIX dcmitype: \n" + + "PREFIX xsd: \n\n" + + "INSERT\n" + + " { GRAPH { ?book ?p ?v } }\n" + + "WHERE\n" + + " { GRAPH \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 \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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + + Set expected = new HashSet(); + 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: \n" + + "PREFIX foaf: \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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH)); + } + + @Test + public void testNotExistsFunction() throws ParseException { + String queryStr = "PREFIX rdf: \n" + + "PREFIX foaf: \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 referredGraphs = parser.getReferredGraphs(queryStr, DEFAULT_GRAPH); + Assert.assertTrue(referredGraphs.toArray()[0].equals(DEFAULT_GRAPH)); + } }