本文整理匯總了Java中com.hp.hpl.jena.query.Dataset.begin方法的典型用法代碼示例。如果您正苦於以下問題:Java Dataset.begin方法的具體用法?Java Dataset.begin怎麽用?Java Dataset.begin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.hp.hpl.jena.query.Dataset
的用法示例。
在下文中一共展示了Dataset.begin方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: loadData
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
/**
* Import the data into the data set. When a new data set is imported the old data is deleted.
* @param dataset
* @param file
*/
public void loadData(Dataset dataset, String file){
log.info("Start loading") ;
long startTime = System.nanoTime() ;
dataset.begin(ReadWrite.WRITE) ;
try {
Model m = dataset.getDefaultModel() ;
log.info("Number of triples before loading: " + m.size());
RDFDataMgr.read(m, file) ;
log.info("Number of triples after loading: " + m.size());
dataset.commit() ;
}
finally {
dataset.end() ;
}
long finishTime = System.nanoTime() ;
double time = (finishTime-startTime)/1.0e6 ;
log.info(String.format("Finish loading - %.2fms", time)) ;
}
示例2: TDBloading
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
/**
* Load jena TDB
*/
private void TDBloading(){
logger.info("TDB loading");
// create model from tdb
Dataset dataset = TDBFactory.createDataset(tdbDirectory);
// assume we want the default model, or we could get a named model here
dataset.begin(ReadWrite.READ);
model = dataset.getDefaultModel();
dataset.end() ;
// if model is null load local dataset into jena TDB
if(model == null)
TDBloading(datasetFile);
}
示例3: demoOfReadTransaction
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
private static void demoOfReadTransaction(Dataset dataset) {
dataset.begin(ReadWrite.READ);
// Get model inside the transaction
Model model = dataset.getDefaultModel();
// query the inserted facts
StringBuilder query = SPARQLUtils.getRegualrSPARQLPREFIX();
query.append("PREFIX foaf: <http://xmlns.com/foaf/0.1/>").append(Constants.NEWLINE);
query.append("SELECT DISTINCT ?person WHERE {?person rdf:type foaf:Person}");
SPARQLUtils.query(model, query.toString(), "?person");
model.close();// closing the model to flush
dataset.end();
}
示例4: demoOfWriteTransaction
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
private static void demoOfWriteTransaction(Dataset dataset) {
dataset.begin(ReadWrite.WRITE);
Model model = dataset.getDefaultModel();
ModelUtils.fillModel(model, FOAF_BASE_URI, FOAF_SCHEMA_FilePath);
// insert foaf:me rdf:type foaf:Person
Resource me = model.createResource(FOAF_BASE_URI + "me");
Property rdfType = model.getProperty(Constants.RDF_TYPE_URL);
Resource FOAFPersonClass = model.getResource(FOAF_BASE_URI + "Person");
model.add(me, rdfType, FOAFPersonClass);
// model.write(System.out);// for debug
model.close();// closing the model to flush
dataset.commit();
dataset.end();
}
示例5: addModel
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
@Test
public void addModel() throws Exception {
final Dataset ds = TDBFactory.createDataset();
final DatasetPopulator dsp = new DatasetPopulator(ds);
final Model model = ModelFactory.createDefaultModel();
final Resource s = model.createResource();
final Property p = model.createProperty("urn:example:prop", "foo");
final Resource o = model.createResource();
model.add(s, p, o);
dsp.addModel(model);
ds.begin(ReadWrite.READ);
try {
assertTrue(ds.getDefaultModel().containsAll(model));
} finally {
ds.end();
}
}
示例6: inferMissingPropertyNames
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
@Test
public void inferMissingPropertyNames() throws Exception {
final Dataset ds = TDBFactory.createDataset();
final DatasetPopulator dsp = new DatasetPopulator(ds);
dsp.addModel(loadModel("infer-property-names/data.ttl"));
final Model x = loadModel("infer-property-names/expected.ttl");
ds.begin(ReadWrite.READ);
try {
final Model m = ds.getDefaultModel();
assertTrue(m.containsAll(x));
} finally {
ds.end();
}
}
示例7: test_rdfcreation_fb
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
@Test
public void test_rdfcreation_fb() throws SAXException, IOException, ParserConfigurationException, Exception {
Document dataDoc = parser.parse(RdfFactoryTest.class.getResourceAsStream(
"/data/fb-20121231.xml"), -1);
RdfFactory factory = new RdfFactory(new RunConfig(domain));
factory.createRdfs(dataDoc, testTdbDir);
Dataset dataset = TDBFactory.createDataset(testTdbDir);
dataset.begin(ReadWrite.READ);
Model model = dataset.getDefaultModel();
Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());
dataset.end();
}
示例8: test_rdfcreation_msft
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
@Test
public void test_rdfcreation_msft() throws SAXException, IOException, ParserConfigurationException, Exception {
Document dataDoc = parser.parse(RdfFactoryTest.class.getResourceAsStream(
"/data/msft-20130630.xml"), -1);
RdfFactory factory = new RdfFactory(new RunConfig(domain));
factory.createRdfs(dataDoc, testTdbDir);
Dataset dataset = TDBFactory.createDataset(testTdbDir);
dataset.begin(ReadWrite.READ);
Model model = dataset.getDefaultModel();
Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());
dataset.end();
}
示例9: isCachedGraph
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
public boolean isCachedGraph(Dataset dataset, String graphName){
boolean isCached = false;
dataset.begin(ReadWrite.READ);
try {
Iterator<String> inames = getDataset().listNames();
while(inames.hasNext()){
if( graphName.equals( inames.next() )) {
isCached = true;
}
}
}
finally {
dataset.end();
}
return isCached;
}
示例10: queryData
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
private int queryData(Dataset dataset, String toponym){
int addressCounter = 0;
log.info("START") ;
long startTime = System.nanoTime() ;
String pre = StrUtils.strjoinNL(
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" ,
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" ,
"PREFIX schema: <http://schema.org/>" ,
"PREFIX text: <http://jena.apache.org/text#>" ,
"PREFIX ogc: <http://www.opengis.net/ont/geosparql#>") ;
String qs = StrUtils.strjoinNL( "SELECT DISTINCT ?s ?address ?wkt " ,
" { ?s text:query (schema:streetAddress '" + toponym + "') ;" ,
" schema:streetAddress ?address ;" ,
" ogc:geometry ?geo ." ,
" ?geo ogc:asWKT ?wkt ." ,
" }") ;
dataset.begin(ReadWrite.READ) ;
try {
Query q = QueryFactory.create(pre + "\n" + qs) ;
QueryExecution qexec = QueryExecutionFactory.create(q , dataset) ;
//QueryExecUtils.executeQuery(q, qexec) ;
ResultSet results = qexec.execSelect();
for( ; results.hasNext(); ){
addressCounter++;
QuerySolution sol = results.nextSolution();
System.out.println( sol.get("s") );
}
}
finally {
dataset.end() ;
}
long finishTime = System.nanoTime() ;
double time = (finishTime-startTime)/1.0e6 ;
log.info(String.format("FINISH - %.2fms", time)) ;
return addressCounter;
}
示例11: loadDataset
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
/**
* Create a transactional in-memory dataset and load data from a given URI.
* <p/>
* The dataset returned by {@link RDFDataMgr#loadDataset(String)} is not transactional.
*
* @param uri URI of data to load
*
* @return a new dataset
*/
public static Dataset loadDataset(final String uri) {
final Dataset ds = TDBFactory.createDataset();
ds.begin(ReadWrite.WRITE);
try {
RDFDataMgr.read(ds, uri);
ds.commit();
} finally {
ds.end();
}
return ds;
}
示例12: copyFromTdb
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
private void copyFromTdb( Dataset dataset ) throws RepositoryException {
ValueFactory vf = rc.getValueFactory();
if ( dataset.supportsTransactions() ) {
dataset.begin( ReadWrite.READ );
}
// Get model inside the transaction
Model model = dataset.getDefaultModel();
StmtIterator si = model.listStatements();
try {
rc.begin();
while ( si.hasNext() ) {
Statement stmt = si.next();
com.hp.hpl.jena.rdf.model.Resource rsr = stmt.getSubject();
Property pred = stmt.getPredicate();
RDFNode val = stmt.getObject();
Node valnode = val.asNode();
Resource sub;
try {
sub = ( rsr.isAnon()
? vf.createBNode( valnode.getBlankNodeLabel() )
: vf.createURI( rsr.toString() ) );
}
catch ( UnsupportedOperationException uoo ) {
log.warn( uoo, uoo );
continue;
}
URI pred2 = vf.createURI( pred.toString() );
Value val2;
if ( val.isLiteral() ) {
Literal lit = val.asLiteral();
String dtstr = lit.getDatatypeURI();
URI dt = ( null == dtstr ? null : vf.createURI( dtstr ) );
String langstr = lit.getLanguage();
if ( null == dt ) {
if ( langstr.isEmpty() ) {
val2 = vf.createLiteral( lit.toString() );
}
else {
val2 = vf.createLiteral( lit.toString(), langstr );
}
}
else {
val2 = vf.createLiteral( lit.toString(), dt );
}
}
else {
if ( val.isAnon() ) {
val2 = vf.createBNode( valnode.getBlankNodeLabel() );
}
else {
val2 = vf.createURI( val.toString() );
}
}
rc.add( sub, pred2, val2 );
}
rc.commit();
}
catch ( RepositoryException re ) {
rc.rollback();
throw re;
}
finally {
if ( dataset.supportsTransactions() ) {
dataset.end();
}
}
}
示例13: geocodeAddress
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
/**
* Search for an address (a node in OSM).
* @param graph The input graph contains a schema:streetAddress with the name of the street, the locality and the country code .
* @return Returns the geocoordinates of the street that has been found.
*/
private TripleCollection geocodeAddress(Dataset ds, Address address){
TripleCollection geoCodeRdf = new SimpleMGraph();
String pre = StrUtils.strjoinNL(
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" ,
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" ,
"PREFIX schema: <http://schema.org/>" ,
"PREFIX text: <http://jena.apache.org/text#>" ,
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>" ,
"PREFIX ogc: <http://www.opengis.net/ont/geosparql#>") ;
String qs = StrUtils.strjoinNL( "SELECT ?s ?street ?lat ?lon" ,
" { ?s text:query (schema:streetAddress '" + address.getStreetAddress() + "') ;" ,
" schema:streetAddress ?street ;" ,
" schema:addressLocality \"" + address.getLocality() + "\" ;" ,
" schema:addressCountry \"" + address.getCountryCode() + "\" ;" ,
" geo:lat ?lat ;" ,
" geo:long ?lon ." ,
" }") ;
log.info(pre + "\n" + qs);
ds.begin(ReadWrite.READ) ;
try {
Query q = QueryFactory.create(pre + "\n" + qs) ;
QueryExecution qexec = QueryExecutionFactory.create(q , ds) ;
//QueryExecUtils.executeQuery(q, qexec) ;
ResultSet results = qexec.execSelect();
int numberOfAddresses = 0;
for( ; results.hasNext(); ){
QuerySolution sol = results.nextSolution();
String streetUriName = sol.getResource("s").getURI();
String streetName = sol.getLiteral("?street").getString();
String latitude = sol.getLiteral("?lat").getLexicalForm();
String longitude = sol.getLiteral("?lon").getLexicalForm();
UriRef addressRef = new UriRef(streetUriName);
geoCodeRdf.add(new TripleImpl(addressRef, schema_streetAddress, new PlainLiteralImpl(streetName)));
geoCodeRdf.add(new TripleImpl(addressRef, schema_addressLocality, new PlainLiteralImpl( address.getLocality())) );
geoCodeRdf.add(new TripleImpl(addressRef, schema_addressCountry, new PlainLiteralImpl( address.getCountryCode())) );
geoCodeRdf.add(new TripleImpl(addressRef, geo_lat, new PlainLiteralImpl( latitude )) );
geoCodeRdf.add(new TripleImpl(addressRef, geo_lon, new PlainLiteralImpl( longitude )) );
numberOfAddresses++;
}
log.info("Number of addresses like " + address.getStreetAddress() + " found: " + numberOfAddresses);
}
finally {
ds.end() ;
}
return geoCodeRdf;
}
示例14: geocodeStreet
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
/**
* Search for a street (way in OSM)
* @param graph The input graph contain a schema:streetAddress with the name of the street.
* @return Returns the geometry of the street that has been found with the coordinates serialized as WKT.
*/
private TripleCollection geocodeStreet(Dataset ds, Address address){
TripleCollection geoCodeRdf = new SimpleMGraph();
String pre = StrUtils.strjoinNL(
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" ,
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" ,
"PREFIX schema: <http://schema.org/>" ,
"PREFIX text: <http://jena.apache.org/text#>" ,
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>" ,
"PREFIX ogc: <http://www.opengis.net/ont/geosparql#>") ;
String qs = StrUtils.strjoinNL( "SELECT ?s ?street ?geometry ?wkt " ,
" { ?s text:query (schema:streetAddress '" + address.getStreetAddress() + "') ;" ,
" schema:streetAddress ?street ;" ,
" schema:addressLocality " + address.getLocality() + " ;" ,
" schema:addressCountry " + address.getCountryCode() + " ;" ,
" ogc:geometry ?geometry ." ,
" ?geo ogc:asWKT ?wkt ." ,
" }") ;
System.out.println(pre + "\n" + qs);
ds.begin(ReadWrite.READ) ;
try {
Query q = QueryFactory.create(pre + "\n" + qs) ;
QueryExecution qexec = QueryExecutionFactory.create(q , ds) ;
//QueryExecUtils.executeQuery(q, qexec) ;
ResultSet results = qexec.execSelect();
int numberOfToponyms = 0;
for( ; results.hasNext(); ){
QuerySolution sol = results.nextSolution();
String streetUriName = sol.getResource("s").getURI();
String streetName = sol.getLiteral("?street").getString();
Resource geo = sol.getResource("?geo");
String geoUri = geo.getURI();
String wkt = sol.getLiteral("?wkt").getString();
UriRef streetRef = new UriRef(streetUriName);
UriRef geometryRef = new UriRef(geoUri);
geoCodeRdf.add(new TripleImpl(streetRef, schema_streetAddress, new PlainLiteralImpl(streetName) ));
geoCodeRdf.add(new TripleImpl(streetRef, schema_addressLocality, new PlainLiteralImpl( address.getLocality())) );
geoCodeRdf.add(new TripleImpl(streetRef, schema_addressCountry, new PlainLiteralImpl( address.getCountryCode())) );
geoCodeRdf.add(new TripleImpl(streetRef, new UriRef("http://www.opengis.net/ont/geosparql#geometry"), geometryRef));
geoCodeRdf.add(new TripleImpl(geometryRef, new UriRef("http://www.opengis.net/ont/geosparql#asWKT"), new PlainLiteralImpl(wkt)));
numberOfToponyms++;
}
log.info("Number of toponymis like " + address.getStreetAddress() + " found: " + numberOfToponyms);
}
finally {
ds.end() ;
}
return geoCodeRdf;
}
示例15: onCreate
import com.hp.hpl.jena.query.Dataset; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rdfread_write);
TextView textView = (TextView) findViewById(R.id.rdfTextView);
textView.setMovementMethod(new ScrollingMovementMethod());
// create tdb dataset
File directory = new File(getFilesDir(), "tdb_dataset");
directory.delete();
directory.mkdir();
Dataset dataset = TDBFactory.createDataset(directory.getAbsolutePath()) ;
dataset.begin(ReadWrite.WRITE);
Model model = dataset.getDefaultModel();
// read a turtle file from assets and write them into a tdb backed model
try {
InputStream skos_ttl = getAssets().open("skos.ttl");
RDFDataMgr.read(model, skos_ttl, Lang.TURTLE);
skos_ttl.close();
InputStream geosparql_rdf_xml = getAssets().open("geosparql_vocab_all.rdf");
RDFDataMgr.read(model, geosparql_rdf_xml, Lang.RDFXML);
geosparql_rdf_xml.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
// add some example data
String personURI = "http://somewhere/JohnSmith";
String fullName = "John Smith";
Resource johnSmith = model.createResource(personURI);
johnSmith.addProperty(VCARD.FN, fullName);
dataset.commit();
dataset.end();
// read from tdb and print triples
dataset.begin(ReadWrite.READ);
Query query = QueryFactory.create("SELECT * WHERE { ?s ?p ?o } LIMIT 2");
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
String resultString = ResultSetFormatter.asText(results);
Log.d(TAG, resultString);
StringWriter dump = new StringWriter();
RDFDataMgr.write(dump, dataset, RDFFormat.JSONLD_PRETTY);
dataset.end();
dataset.close();
textView.setText(dump.toString());
}