Skip to content

Commit

Permalink
added more documentatino
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Jul 22, 2015
1 parent d4edcfd commit 37b94a1
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 90 deletions.
66 changes: 66 additions & 0 deletions doc/Fluent-Api.md
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)



31 changes: 30 additions & 1 deletion SparqlUpdate.md → doc/SparqlUpdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import com.hp.hpl.jena.update.UpdateRequest;

class MainSparqlUpdateSimpleDemo {

public static void main(String[] args) throws Exception
public static void demoWithModel() throws Exception
{
// Define the listeners
List<DatasetListener> listeners = Collections.<DatasetListener>singletonList(new DatasetListener() {
Expand All @@ -70,6 +70,35 @@ class MainSparqlUpdateSimpleDemo {
.createUpdateProcessor(updateRequest)
.execute();
}

public static void demoWithRemoteHttpAccess() throws Exception
List<DatasetListener> listeners = Collections.<DatasetListener>singletonList(new DatasetListener() {
@Override
public void onPreModify(Diff<Set<Quad>> diff, UpdateContext updateContext) {
// Print out any changes to the console
System.out.println(diff);
}
});

HttpAuthenticator auth = new SimpleAuthenticator("dba", "dba".toCharArray());

SparqlService sparqlService = FluentSparqlService
.http("http://your.write.enabled/sparql-endpoint", "http://dpbedia.org", auth)
.config()
.configQuery()
.withPagination(100)
.end()
.withUpdateListeners(new UpdateStrategyEventSource(), listeners)
.end()
.create();

UpdateRequest updateRequest = UpdateUtils.parse("Prefix ex: <http://example.org/> Insert { ex:s ex:p ex:o }");
sparqlService
.getUpdateExecutionFactory()
.createUpdateProcessor(updateRequest)
.execute();


}

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public FluentQueryExecutionFactory<P> get() {

return result;
}



/*
public FluentQueryExecutionFactory<T> withDelay(long delayDuration, TimeUnit delayTimeUnit){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public FluentQueryExecutionFactoryFn(Supplier<P> parentSupplier, Function<QueryE
this.fn = fn;
}

public FluentQueryExecutionFactoryFn<P> withDelay(final long delayDuration, final TimeUnit delayTimeUnit) {
public FluentQueryExecutionFactoryFn<P> withDelay(final int delayDuration, final TimeUnit delayTimeUnit) {
compose(new Function<QueryExecutionFactory, QueryExecutionFactory>() {
@Override
public QueryExecutionFactory apply(QueryExecutionFactory qef) {
Expand All @@ -51,7 +51,7 @@ public QueryExecutionFactory apply(QueryExecutionFactory qef) {
return this;
}

public FluentQueryExecutionFactoryFn<P> withPagination(final long pageSize) {
public FluentQueryExecutionFactoryFn<P> withPagination(final int pageSize) {
compose(new Function<QueryExecutionFactory, QueryExecutionFactory>() {
@Override
public QueryExecutionFactory apply(QueryExecutionFactory qef) {
Expand Down Expand Up @@ -107,7 +107,7 @@ public QueryExecutionFactory apply(QueryExecutionFactory qef) {
*
* @return
*/
public FluentQueryExecutionFactoryFn<P> selectOnly(){
public FluentQueryExecutionFactoryFn<P> selectOnly() {
compose(new Function<QueryExecutionFactory, QueryExecutionFactory>() {
@Override
public QueryExecutionFactory apply(QueryExecutionFactory qef) {
Expand All @@ -119,5 +119,9 @@ public QueryExecutionFactory apply(QueryExecutionFactory qef) {
return this;
}

public static FluentQueryExecutionFactoryFn<?> start() {
return new FluentQueryExecutionFactoryFn<Object>();
}

}

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;
}
}
Loading

0 comments on commit 37b94a1

Please sign in to comment.