-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
244 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## Fluent API | ||
|
||
The jena-sparql-api offers several fluent classes for conveniently creating and configuring instances of QueryExecutionFactory, UpdateExecutionFactory, SparqlService and SparqlServiceFactory. | ||
These fluent classes have the same names as those just mentioned, prefixed with `Fluent` - such as `FluentQueryExecutionFactory` or `FluentQueryExecutionFactoryFn`. | ||
There are also two versions of each fluent: Those whose name ends in `Fn` and the ones that do not. | ||
|
||
* The 'Fn' versions, which we refer to as _function fluents_, are use to build _decoration functions_ that can be lazily applied to entities (e.g. QueryExecutionFactory). | ||
* The non-'Fn' versions are used to apply decorations _directly_ to a specific entity. | ||
|
||
The jena-sparql-api fluents generally offer these 3 methods: | ||
* .end() Move up to the parent fluent in case of nested fluents. If no such parent exists, an exception is raised indicating that a call to .create() is expected. | ||
* .value() Returns the value built so far with the fluent. This method should not be used directly. | ||
* .create() Returns the result of .value() iff this method is called on the root fluent, i.e. the one which does not have a parent. Otherwise throw an exception that indicates that a call to .end() is expected. | ||
|
||
Function fluents in addition offer the method: | ||
* .compose(Function<T, T> nextFn) Append a new decoration function to the prior ones. This is an extension point for adding custom decorators that are not natively supported by the FluentAPIs. | ||
|
||
|
||
### Examples | ||
|
||
Building a query execution factory with pagination and delay between requests: | ||
```java | ||
QueryExecutionFactory qef = FluentQueryExecutionFactory | ||
.http("http://dbpedia.org/sparql", "http://dpbedia.org") | ||
.config() | ||
.withDelay(500, TimeUnit.MILLISECONDS) | ||
.withPagination(1000) | ||
.end() | ||
.create(); | ||
``` | ||
|
||
Same as above, however with explicitly creating the decoraton function: | ||
```java | ||
// Create the decorator function | ||
Function<QueryExecutionFactory, QueryExecutionFactory> decoratorFn = | ||
QueryExecutionFactoryFn.start() | ||
.withDelay(500, TimeUnit.MILLISECONDS) | ||
.withPagination(1000) | ||
.create(); | ||
|
||
// At some point create a QueryExecutionFactory ... | ||
QueryExecutionFactory qef = FluentQueryExecutionFactory | ||
.http("http://dbpedia.org/sparql", "http://dpbedia.org") | ||
.create(); | ||
|
||
// ... and apply the decorations | ||
qef = fn.apply(qef); | ||
``` | ||
|
||
Example of how to build a SparqlService | ||
```java | ||
SparqlService sparqlService = FluentSparqlService | ||
.http("http://dbpedia.org/sparql", "http://dpbedia.org") | ||
.config() // Enter configuration via FluentSparqlServiceFn | ||
.configQuery() // Enter configuration of the query aspects via FluentQueryExecutionFactoryFn | ||
.withPagination(100) | ||
.end() | ||
.end() | ||
.create(); | ||
``` | ||
|
||
### Further Reads | ||
[SparqlUpdate](SparqlUpdate) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 86 additions & 84 deletions
170
jena-sparql-api-core/src/main/java/org/aksw/jena_sparql_api/core/SparqlServiceReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,102 @@ | ||
/** | ||
* | ||
* | ||
*/ | ||
package org.aksw.jena_sparql_api.core; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import com.hp.hpl.jena.sparql.core.DatasetDescription; | ||
|
||
/** | ||
* Wraps a SPARQL service instance(dataset) accessible via HTTP. | ||
* @author Lorenz Buehmann | ||
* | ||
*/ | ||
public class SparqlServiceReference { | ||
|
||
private final String serviceURL; | ||
private final Collection<String> defaultGraphURIs; | ||
private final Collection<String> namedGraphURIs; | ||
|
||
public SparqlServiceReference(String serviceURL) { | ||
this(serviceURL, Collections.<String>emptySet()); | ||
} | ||
|
||
public SparqlServiceReference(String serviceURL, Collection<String> defaultGraphURIs) { | ||
this(serviceURL, defaultGraphURIs, Collections.<String>emptySet()); | ||
} | ||
|
||
public SparqlServiceReference(String serviceURL, Collection<String> defaultGraphURIs, Collection<String> namedGraphURIs) { | ||
this.serviceURL = serviceURL; | ||
this.defaultGraphURIs = defaultGraphURIs; | ||
this.namedGraphURIs = namedGraphURIs; | ||
} | ||
|
||
/** | ||
* @return the serviceURL | ||
*/ | ||
public String getServiceURL() { | ||
return serviceURL; | ||
} | ||
|
||
/** | ||
* @return the defaultGraphURIs | ||
*/ | ||
public Collection<String> getDefaultGraphURIs() { | ||
return defaultGraphURIs; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.lang.Object#toString() | ||
*/ | ||
@Override | ||
public String toString() { | ||
return super.toString(); | ||
} | ||
|
||
/** | ||
* @return the namedGraphURIs | ||
*/ | ||
public Collection<String> getNamedGraphURIs() { | ||
return namedGraphURIs; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((defaultGraphURIs == null) ? 0 : defaultGraphURIs.hashCode()); | ||
result = prime * result + ((namedGraphURIs == null) ? 0 : namedGraphURIs.hashCode()); | ||
result = prime * result + ((serviceURL == null) ? 0 : serviceURL.hashCode()); | ||
return result; | ||
} | ||
private final String serviceURL; | ||
//private final Collection<String> defaultGraphURIs; | ||
//private final Collection<String> namedGraphURIs; | ||
private final DatasetDescription datasetDescription; | ||
|
||
// public SparqlServiceReference(String serviceURL) { | ||
// this(serviceURL, Collections.<String>emptySet()); | ||
// } | ||
// | ||
// public SparqlServiceReference(String serviceURL, List<String> defaultGraphURIs) { | ||
// this(serviceURL, defaultGraphURIs, Collections.<String>emptySet()); | ||
// } | ||
|
||
public SparqlServiceReference(String serviceURL, DatasetDescription datasetDescription) { | ||
this.serviceURL = serviceURL; | ||
this.datasetDescription = datasetDescription; | ||
} | ||
|
||
/** | ||
* @return the serviceURL | ||
*/ | ||
public String getServiceURL() { | ||
return serviceURL; | ||
} | ||
|
||
public DatasetDescription getDatasetDescription() { | ||
return datasetDescription; | ||
} | ||
|
||
/** | ||
* @return the defaultGraphURIs | ||
*/ | ||
public Collection<String> getDefaultGraphURIs() { | ||
return datasetDescription.getDefaultGraphURIs(); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.lang.Object#toString() | ||
*/ | ||
@Override | ||
public String toString() { | ||
return super.toString(); | ||
} | ||
|
||
/** | ||
* @return the namedGraphURIs | ||
*/ | ||
public Collection<String> getNamedGraphURIs() { | ||
return datasetDescription.getNamedGraphURIs(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
SparqlServiceReference other = (SparqlServiceReference) obj; | ||
if (defaultGraphURIs == null) { | ||
if (other.defaultGraphURIs != null) | ||
return false; | ||
} else if (!defaultGraphURIs.equals(other.defaultGraphURIs)) | ||
return false; | ||
if (namedGraphURIs == null) { | ||
if (other.namedGraphURIs != null) | ||
return false; | ||
} else if (!namedGraphURIs.equals(other.namedGraphURIs)) | ||
return false; | ||
if (serviceURL == null) { | ||
if (other.serviceURL != null) | ||
return false; | ||
} else if (!serviceURL.equals(other.serviceURL)) | ||
return false; | ||
return true; | ||
} | ||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime | ||
* result | ||
+ ((datasetDescription == null) ? 0 : datasetDescription | ||
.hashCode()); | ||
result = prime * result | ||
+ ((serviceURL == null) ? 0 : serviceURL.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
SparqlServiceReference other = (SparqlServiceReference) obj; | ||
if (datasetDescription == null) { | ||
if (other.datasetDescription != null) | ||
return false; | ||
} else if (!datasetDescription.equals(other.datasetDescription)) | ||
return false; | ||
if (serviceURL == null) { | ||
if (other.serviceURL != null) | ||
return false; | ||
} else if (!serviceURL.equals(other.serviceURL)) | ||
return false; | ||
return true; | ||
} | ||
} |
Oops, something went wrong.