Skip to content

Instantly share code, notes, and snippets.

View AlBaker's full-sized avatar

Al Baker AlBaker

View GitHub Profile
@AlBaker
AlBaker / Projection.groovy
Created January 28, 2013 02:43
Stardog projection example
// there is also a projection of the results into the closure's binding
// if x, y, or z are not populated in the answer, then they are still valid binidng but are null
stardog.each("select ?x ?y ?z WHERE { ?x ?y ?z } LIMIT 2", {
println x
println y
println z // may be a LiteralImpl, so you get full access to manipulate Value objects
}
)
@AlBaker
AlBaker / StardogAddRemove.groovy
Created January 28, 2013 02:44
Stardog Add and Remove triples example
// insert and remove
stardog.insert([["urn:test3", "urn:test:predicate", "hello world"],
["urn:test4", "urn:test:predicate", "hello world2"]])
stardog.remove(["urn:test3", "urn:test:predicate", "hello world"])
stardog.remove(["urn:test4", "urn:test:predicate", "hello world2"])
@AlBaker
AlBaker / StardogWithConnection.groovy
Created January 28, 2013 02:46
Stardog withConnection closure example
// withConnection, tx safe
stardog.withConnection { con ->
def queryString = """
SELECT ?s ?p ?o
{
?s ?p ?o
}
"""
TupleQueryResult result = null;
try {
@AlBaker
AlBaker / GroovySparqlExample.groovy
Last active December 12, 2015 02:38
Using Groovy SPARQL with Dbpedia
@Grab('org.codehaus.groovy.sparql:groovy-sparql:0.5')
import groovy.sparql.*
def sparql = new Sparql(endpoint:"http://dbpedia.org/sparql")
def query = """
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX yago: <http://dbpedia.org/class/yago/>
SELECT ?president ?name WHERE {
?president a yago:PresidentsOfTheUnitedStates ;
@AlBaker
AlBaker / SampleSparql.groovy
Created February 9, 2013 22:19
Groovy Sparql released
@Grab('com.github.albaker:GroovySparql:0.6')
import groovy.sparql.*
def sparql = new Sparql(endpoint:"http://dbpedia.org/sparql")
def query = """
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX yago: <http://dbpedia.org/class/yago/>
SELECT ?president ?name WHERE {
?president a yago:PresidentsOfTheUnitedStates ;
@AlBaker
AlBaker / SNARLQueryForObjectExample.java
Last active December 18, 2015 10:48
SNARL Template queryForObject
String uri = snarlTemplate.queryForObject(sparql, new RowMapper<String>() {
String mapRow(BindingSet b) {
return b.getValue("myQueryParameter")
}
})
// OR
String result = snarlTemplate.queryForObject(sparql, new SingleMapper("b"));
@AlBaker
AlBaker / StardogGroovyNamespaces.groovy
Created July 1, 2013 16:21
Use Stardog Groovy to add namespaces
def db = new Stardog(home:"/opt/stardog", url: "snarl://localhost:5820/",to:"testdb", username:"admin", password:"admin")
db.withConnection { Connection c ->
c.namespaces().add("ns", "http://mydomain/data/")
c.namespaces().add("dcterms", "http://purl.org/dc/terms/")
c.namespaces().add("org", "http://mydomain/org/")
c.namespaces().add("per", "http://mydomain/people/")
}
@AlBaker
AlBaker / test.clj
Last active December 26, 2015 20:58
(require '[pallet.compute :refer [instantiate-provider]])
(require '[pallet.api :refer [group-spec server-spec node-spec plan-fn converge lift]])
(require '[pallet.compute :refer [images]]
'[clojure.pprint :refer [pprint]])
(require '[pallet.crate.java :as java])
(require '[pallet.crate.automated-admin-user :refer [automated-admin-user]])
(require '[pallet.actions :as pact])
(def vmfest (instantiate-provider "vmfest"
:vbox-comm :ws))
(defproject quickstart "0.1.0-SNAPSHOT"
:description "FIXME Pallet project for quickstart"
:dependencies [[org.clojure/clojure "1.5.1"]
[com.palletops/pallet "0.8.0-RC.3"]
[com.palletops/pallet-vmfest "0.3.0-beta.2"]
[com.palletops/pallet-docker "0.1.0"]
[com.palletops/app-deploy-crate "0.8.0-alpha.3"]
[com.palletops/runit-crate "0.8.0-alpha.1"]
[com.palletops/docker-crate "0.8.0-alpha.1"]
[com.palletops/java-crate "0.8.0-beta.5"]
SnarlTemplate tmp = new SnarlTemplate();
tmp.setDataSource(dataSource);
String sparql = "DELETE { ?a ?b \"aloha world\" } INSERT { ?a ?b \"shalom world\" } WHERE { ?a ?b \"aloha world\" }";
Map<String, Object> params = new HashMap<String, Object>() {{
put("b", new URIImpl(uriB));
}};
// Execute the SPARQL Update query