Skip to content

Commit

Permalink
add blank node functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
anqit committed Sep 14, 2017
1 parent 2cb5706 commit dde6e84
Show file tree
Hide file tree
Showing 29 changed files with 340 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
/**
* Represents a SPARQL connective operation, which takes an arbitrary number of
* arguments
*
* @author Ankit
*
*/
class ConnectiveOperation extends Operation<ConnectiveOperation> {
ConnectiveOperation(ConnectiveOperator operator) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/anqit/spanqit/constraint/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* A SPARQL expression. Used by filters, having clauses, order and group by
* clauses, assignments, and as arguments to other expressions.
*
* @author Ankit
* @param <T>
* the type of Expression (ie, Function or Operation). Used to
* support fluency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
/**
* An element that can be used as an operand to an
* {@link com.anqit.spanqit.constraint.Expression}
*
* @author Ankit
*
*/
public interface ExpressionOperand extends QueryElement { }
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/constraint/Expressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#SparqlOps">
* SPARQL Function Definitions</a>
* @author Ankit
*
*/
public class Expressions {
// prevent instantiation of this class
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/core/Prefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
* A SPARQL Prefix declaration
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#prefNames">
* SPARQL Prefix</a>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/core/PrefixDeclarations.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
/**
* A collection of SPARQL Prefix declarations
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#prefNames">
* SPARQL Prefix</a>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/core/Projection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
* A SPARQL Projection
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#selectproject">
* SPARQL Projections</a>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/anqit/spanqit/core/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public T offset(int offset) {
* @return a SparqlVariable object that is unique to this query instance
*/
public Variable var() {
return new Variable("x" + ++varCount);
return Spanqit.var("x" + ++varCount);
}

protected abstract String getQueryActionString();
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/core/QueryPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
/**
* A SPARQL Query Pattern (<code>WHERE</code> clause)
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#GraphPattern">
* Query Pattern Definition</a>
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/anqit/spanqit/core/Spanqit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

/**
* A class to with static methods to create SPARQL query elements.
*
* @author Ankit
*
*/
public class Spanqit {
// prevent instantiation of this class
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/anqit/spanqit/core/SpanqitStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public static void appendAndNewlineIfNonNull(QueryElement element, StringBuilder
builder.append(Optional.ofNullable(element).map(e -> e.getQueryString() + "\n").orElse(""));
}

public static String getBracketedString(String contents) {
public static String getBracedString(String contents) {
return getEnclosedString("{", "}", contents);
}

public static String getBracketedString(String contents) {
return getEnclosedString("[", "]", contents);
}

public static String getParenthesizedString(String contents) {
return getEnclosedString("(", ")", contents);
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/anqit/spanqit/core/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
/**
* A SPARQL query variable
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynVariables">
* SPARQL Variable Syntax</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.anqit.spanqit.graphpattern;

import com.anqit.spanqit.rdf.RdfBlankNode.PropertiesBlankNode;
import com.anqit.spanqit.rdf.RdfObject;
import com.anqit.spanqit.rdf.RdfPredicate;

/**
* A triple pattern formed by a property-list blank node
*
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynBlankNodes">
* blank node syntax</a>
*/
class BNodeTriplePattern implements TriplePattern<BNodeTriplePattern> {
private PropertiesBlankNode bnode;

BNodeTriplePattern(PropertiesBlankNode subject) {
this.bnode = subject;
}

@Override
public BNodeTriplePattern andHas(RdfPredicate predicate, RdfObject... objects) {
bnode.andHas(predicate, objects);

return this;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public String getQueryString() {
return bnode.getQueryString() + SUFFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
* Denotes a SPARQL Graph Pattern
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#GraphPattern">
* SPARQL Graph Patterns</a>
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/com/anqit/spanqit/graphpattern/GraphPatterns.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package com.anqit.spanqit.graphpattern;

import com.anqit.spanqit.core.Projectable;
import com.anqit.spanqit.rdf.RdfBlankNode.PropertiesBlankNode;
import com.anqit.spanqit.rdf.RdfObject;
import com.anqit.spanqit.rdf.RdfPredicate;
import com.anqit.spanqit.rdf.RdfSubject;

/**
* A class with static methods to create graph patterns. Obviously there's some
* more flushing out TODO still
* A class with static methods to create graph patterns.
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#GraphPattern">SPARQL
* Graph Pattern</a>
*/
public class GraphPatterns {
// prevent instantiation of this class
private GraphPatterns() {
}
private GraphPatterns() { }

/**
* Create a triple pattern with the given subject, predicate, and object(s)
Expand All @@ -33,9 +30,21 @@ private GraphPatterns() {
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynTriples">
* Triple pattern syntax</a>
*/
public static TriplePattern tp(RdfSubject subject,
RdfPredicate predicate, RdfObject... objects) {
return new TriplePattern(subject, predicate, objects);
public static TriplePattern tp(RdfSubject subject, RdfPredicate predicate, RdfObject... objects) {
return new TriplesSameSubject(subject, predicate, objects);
}

/**
* Create a triple pattern from a property-list blank node
* @param bnode the PropertiesBlankNode instance to convert to a triple pattern
*
* @return the triple pattern represented by the expansion of this blank node
*
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynBlankNodes">
* blank node syntax</a>
*/
public static TriplePattern tp(PropertiesBlankNode bnode) {
return new BNodeTriplePattern(bnode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String getQueryString() {
}

if (bracketize) {
pattern.append(SpanqitStringUtils.getBracketedString(innerPattern
pattern.append(SpanqitStringUtils.getBracedString(innerPattern
.toString()));
} else {
pattern.append(innerPattern.toString());
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/anqit/spanqit/graphpattern/SubSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
/**
* A SPARQL subquery
*
* @author Ankit
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#subqueries">
* SPARQL Subquery</a>
Expand Down Expand Up @@ -140,6 +138,6 @@ public String getQueryString() {
// TODO: VALUES
// subselect.append(values.getQueryString());

return SpanqitStringUtils.getBracketedString(subSelect.toString());
return SpanqitStringUtils.getBracedString(subSelect.toString());
}
}
82 changes: 28 additions & 54 deletions src/main/java/com/anqit/spanqit/graphpattern/TriplePattern.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,28 @@
package com.anqit.spanqit.graphpattern;

import com.anqit.spanqit.core.QueryElementCollection;
import com.anqit.spanqit.rdf.RdfObject;
import com.anqit.spanqit.rdf.RdfPredicate;
import com.anqit.spanqit.rdf.RdfSubject;

/**
* A SPARQL Triple Pattern.
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynTriples">
* Triple pattern syntax</a>
*/
public class TriplePattern extends QueryElementCollection<RdfPredicateRdfObjectListPair> implements GraphPattern {
private RdfSubject subject;

TriplePattern(RdfSubject subject, RdfPredicate predicate,
RdfObject... objects) {
super(" ;\n\t");
this.subject = subject;
andHas(predicate, objects);
}

/**
* Using the predicate-object and object list mechanisms, expand this triple pattern to include
* triples consisting of this subject, and the given predicate and object(s)
*
* @param predicate the predicate of the triple to add
* @param objects the object or objects of the triple to add
*
* @return this triple pattern
*
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#predObjLists">
* Predicate-Object Lists</a>
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#objLists">
* Object Lists</a>
*/
public TriplePattern andHas(RdfPredicate predicate, RdfObject... objects) {
elements.add(new RdfPredicateRdfObjectListPair(predicate, objects));

return this;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public String getQueryString() {
return subject.getQueryString() + " " + super.getQueryString() + " .";
}
}
package com.anqit.spanqit.graphpattern;

import com.anqit.spanqit.rdf.RdfObject;
import com.anqit.spanqit.rdf.RdfPredicate;

/**
* Denotes a SPARQL Triple Pattern
*
* @param <T> the type of triple pattern; used to support fluency
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynTriples">
* Triple pattern syntax</a>
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynBlankNodes">
* blank node syntax</a>
*/
public interface TriplePattern<T extends TriplePattern<T>> extends GraphPattern {
static String SUFFIX = " .";

/**
* add predicate-object lists describing this triple pattern's subject
*
* @param predicate the predicate to use to describe this triple pattern's subject
* @param objects the corresponding object(s)
* @return this triple pattern
*/
public T andHas(RdfPredicate predicate, RdfObject... objects);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.anqit.spanqit.graphpattern;

import com.anqit.spanqit.rdf.Rdf;
import com.anqit.spanqit.rdf.RdfObject;
import com.anqit.spanqit.rdf.RdfPredicate;
import com.anqit.spanqit.rdf.RdfPredicateObjectListCollection;
import com.anqit.spanqit.rdf.RdfSubject;

/**
* A SPARQL Triple Pattern.
*
* @see <a
* href="http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#QSynTriples">
* Triple pattern syntax</a>
*/
class TriplesSameSubject implements TriplePattern<TriplesSameSubject> {
private RdfSubject subject;
private RdfPredicateObjectListCollection predicateObjectLists = Rdf.predicateObjectListCollection();

TriplesSameSubject(RdfSubject subject, RdfPredicate predicate, RdfObject... objects) {
this.subject = subject;
andHas(predicate, objects);
}

/**
* Using the predicate-object and object list mechanisms, expand this triple pattern to include
* triples consisting of this subject, and the given predicate and object(s)
*
* @param predicate the predicate of the triple to add
* @param objects the object or objects of the triple to add
*
* @return this triple pattern
*
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#predObjLists">
* Predicate-Object Lists</a>
* @see <a href="https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#objLists">
* Object Lists</a>
*/
@Override
public TriplesSameSubject andHas(RdfPredicate predicate, RdfObject... objects) {
predicateObjectLists.andHas(predicate, objects);

return this;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public String getQueryString() {
return subject.getQueryString() + " " + predicateObjectLists.getQueryString() + SUFFIX;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/anqit/spanqit/rdf/Rdf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.anqit.spanqit.rdf;

/**
* A class with static methods to create basic RDF objects
*/
public class Rdf {

private Rdf() { }

public static RdfPredicateObjectListCollection predicateObjectListCollection() {
return new RdfPredicateObjectListCollection();
}
}
Loading

0 comments on commit dde6e84

Please sign in to comment.