Java Code Examples for com.hp.hpl.jena.rdf.model.ModelFactory#createModelForGraph()

The following examples show how to use com.hp.hpl.jena.rdf.model.ModelFactory#createModelForGraph() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoReloadableDataset.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
private void reload() {
	loader.getMapping().connect();
	Graph graph = loader.getGraphD2RQ();
	
	datasetGraph = DatasetGraphFactory.createOneGraph(graph);
	defaultModel = ModelFactory.createModelForGraph(datasetGraph.getDefaultGraph());		

	hasTruncatedResults = false;
	for (SQLConnection db: loader.getMapping().getSQLConnections()) {
		if (db.limit() != Database.NO_LIMIT) {
			hasTruncatedResults = true;
		}
	}

	if (autoReload) {
		lastModified = watchedFile.lastModified();
		lastReload = System.currentTimeMillis();
	}
}
 
Example 2
Source File: LUBM.java    From neo4jena with Apache License 2.0 6 votes vote down vote up
public static void write(GraphDatabaseService njgraph) {
	Logger log= Logger.getLogger(Wine.class);
	InputStream in = FileManager.get().open( inputFileName );
	if (in == null) {
           throw new IllegalArgumentException( "File: " + inputFileName + " not found");
       }
       
	Model model = ModelFactory.createDefaultModel();
       model.read(in,"","RDF");
       double triples = model.size();
       log.info("Model loaded with " +  triples + " triples");
       System.out.println("Model loaded with " +  triples + " triples");
       
	NeoGraph graph = new NeoGraph(njgraph);
	graph.startBulkLoad();
	log.info("Connection created");
	Model njmodel = ModelFactory.createModelForGraph(graph);
	log.info("NeoGraph Model initiated");
	System.out.println("NeoGraph Model initiated");
	StopWatch watch = new StopWatch();
	//log.info(njmodel.add(model));
	njmodel.add(model);
	log.info("Storing completed (ms): " + watch.stop());
	graph.stopBulkLoad();
	System.out.println("Storing completed (ms): " + watch.stop());
}
 
Example 3
Source File: Course_Test.java    From neo4jena with Apache License 2.0 6 votes vote down vote up
public static void insertData(GraphDatabaseService njgraph){
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	
	String s2 = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
           	"PREFIX uni: <http://seecs.edu.pk/db885#>" +
           	"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
           "INSERT DATA "+
           "{ " +
           " <http://seecs.edu.pk/db885#KhalidLatif> rdf:type uni:Professor ."+
           "}"; 
	StopWatch watch = new StopWatch();
	UpdateAction.parseExecute(s2, njmodel);
	System.out.println("Insert query took: " + watch.stop() + " ms");
	log.info("Data inserted");
	System.out.println("Data inserted");
}
 
Example 4
Source File: Course_Test.java    From neo4jena with Apache License 2.0 6 votes vote down vote up
public static void getJob(GraphDatabaseService njgraph)
{
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	
	ST descJob = TemplateLoader.getQueriesGroup().getInstanceOf("getGraph");
	String queryASString = Constants.QUERY_PREFIX+ descJob.render();
	
	Query query = QueryFactory.create(queryASString, Syntax.syntaxSPARQL_11);
	QueryExecution qexec = QueryExecutionFactory.create(query, njmodel);
	ResultSet res = qexec.execSelect();
	
	int count=0;
       while(res.hasNext()){
       	//System.out.println("in while"+count);
       	QuerySolution sol = res.next();
       	System.out.println(sol.get("?Z"));
       	count++;
       }
      
      //log.info("Record fetched:"+ count);
      System.out.println("Record fetched:"+ count);
}
 
Example 5
Source File: Wine.java    From neo4jena with Apache License 2.0 6 votes vote down vote up
public static void write(GraphDatabaseService njgraph) {
	InputStream in = FileManager.get().open( inputFileName );
	if (in == null) {
           throw new IllegalArgumentException( "File: " + inputFileName + " not found");
       }
       
	Model model = ModelFactory.createDefaultModel();
       model.read(in,"","RDF");
       double triples = model.size();
       System.out.println("Model loaded with " +  triples + " triples");
       
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	graph.startBulkLoad();
	System.out.println("NeoGraph Model initiated");
	StopWatch watch = new StopWatch();
	//log.info(njmodel.add(model));
	njmodel.add(model);
	System.out.println("Storing completed (ms): " + watch.stop());
	graph.stopBulkLoad();
}
 
Example 6
Source File: LUBM.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void search(GraphDatabaseService njgraph) {
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	
	//long start = System.currentTimeMillis();
	String s2 = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
               	"PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>" +
               "SELECT ?X ?name "+
               "WHERE" +
               "{ ?X ub:name ?name ." +
                "FILTER regex(?name,\"^Publication\") ."+
               "}"; 
      	          
       Query query = QueryFactory.create(s2);
       QueryExecution qExe = QueryExecutionFactory.create(query, njmodel);
       StopWatch watch = new StopWatch();
       ResultSet results = qExe.execSelect();
       log.info("Query took (ms): "+ watch.stop());
       System.out.println("Query took (ms): "+ watch.stop());
       //ResultSetFormatter.out(System.out, results);
       
       int count=0;
       while(results.hasNext()){
       	//System.out.println("in while"+count);
       	QuerySolution sol = results.next();
       	System.out.println(sol.get("name"));
       	count++;
       }
      
      log.info("Record fetched:"+ count);
      System.out.println("Record fetched:"+ count);
      
}
 
Example 7
Source File: Course_Test.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void search(GraphDatabaseService njgraph) {
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	
	String s2 = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
               	"PREFIX uni: <http://seecs.edu.pk/db885#>" +
               	"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
               "SELECT ?X ?Z ?Y "+
               "WHERE" +
               "{ ?X ?Z ?Y ." +
               "}"; 
      	          
       Query query = QueryFactory.create(s2); 
       QueryExecution qExe = QueryExecutionFactory.create(query, njmodel);
       StopWatch watch = new StopWatch();
       ResultSet results = qExe.execSelect();
       long endTime = watch.stop();
       log.info("Query took (ms): "+endTime);
       System.out.println("Query took (ms): "+ endTime);
      // ResultSetFormatter.out(System.out, results);
       
       int count=0;
       while(results.hasNext()){
       	//System.out.println("in while"+count);
       	QuerySolution sol = results.next();
       	System.out.println(sol.get("?Z"));
       	count++;
       }
      
      log.info("Record fetched:"+ count);
      System.out.println("Record fetched:"+ count);
}
 
Example 8
Source File: Wine.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void search(GraphDatabaseService njgraph) {
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	      
	String s2 = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
				"PREFIX food: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/food#>"+
				"PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>" +
				"PREFIX owl: <http://www.w3.org/2002/07/owl#>"+
				"SELECT ?X WHERE {"+
				"?X food:SweetFruit ?Z . }";

       Query query = QueryFactory.create(s2);
       QueryExecution qExe = QueryExecutionFactory.create(query, njmodel);
       StopWatch watch = new StopWatch();
       ResultSet results = qExe.execSelect();
       System.out.println("Query took (ms): "+ watch.stop());
       //ResultSetFormatter.out(System.out, results);
       
       int count=0;
       while(results.hasNext()){
       	//System.out.println("in while"+count);
       	QuerySolution sol = results.next();
       	System.out.print(sol.get("X"));
       	count++;
       }
      System.out.println("Record fetched:"+ count);
      
}
 
Example 9
Source File: Course.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void search(GraphDatabaseService njgraph) {
	NeoGraph graph = new NeoGraph(njgraph);
	Model njmodel = ModelFactory.createModelForGraph(graph);
	
	String s2 = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
               	"PREFIX uni: <http://seecs.edu.pk/db885#>" +
               	"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
               "SELECT ?X ?Z ?Y "+
               "WHERE" +
               "{ ?X ?Z ?Y ." +
               "}"; 
      	          
       Query query = QueryFactory.create(s2); 
       QueryExecution qExe = QueryExecutionFactory.create(query, njmodel);
       StopWatch watch = new StopWatch();
       ResultSet results = qExe.execSelect();
       log.info("Query took (ms): "+ watch.stop());
       System.out.println("Query took (ms): "+ watch.stop());
      // ResultSetFormatter.out(System.out, results);
       
       int count=0;
       while(results.hasNext()){
       	//System.out.println("in while"+count);
       	QuerySolution sol = results.next();
       	System.out.println(sol.get("?Z"));
       	count++;
       }
      
      log.info("Record fetched:"+ count);
      System.out.println("Record fetched:"+ count);
}
 
Example 10
Source File: Course.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void write(GraphDatabaseService njgraph) {
	InputStream in = FileManager.get().open( inputFileName );
	if (in == null) {
           throw new IllegalArgumentException( "File: " + inputFileName + " not found");
       }
       
	Model model = ModelFactory.createDefaultModel();
       model.read(in,"","TTL");
       double triples = model.size();
       log.info("Model loaded with " +  triples + " triples");
       System.out.println("Model loaded with " +  triples + " triples");
       Map<String, String> prefixMap = model.getNsPrefixMap();
      // System.out.println("Prefix Mapping: " + prefixMap);
       
	NeoGraph graph = new NeoGraph(njgraph);
	graph.getPrefixMapping().setNsPrefixes(prefixMap);
	graph.startBulkLoad();
	log.info("Connection created");
	Model njmodel = ModelFactory.createModelForGraph(graph);
	log.info("NeoGraph Model initiated");
	System.out.println("NeoGraph Model initiated");
	
	//log.info(njmodel.add(model));
	//njmodel.add(model);
	StmtIterator iterator = model.listStatements();
	StopWatch watch = new StopWatch();
	int count = 0;
	while(iterator.hasNext()){
		njmodel.add(iterator.next());
		count++;
	}
	System.out.println("Total triples loaded are:"+ count);
	graph.stopBulkLoad();
	//log.info("Storing completed (ms): " + watch.stop());
	System.out.println("Storing completed (ms): " + watch.stop());
}
 
Example 11
Source File: SystemLoader.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public Model getModelD2RQ() {
	if (dataModel == null) {
		dataModel = ModelFactory.createModelForGraph(getGraphD2RQ());
	}
	return dataModel;
}
 
Example 12
Source File: PageServlet.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
	D2RServer server = D2RServer.fromServletContext(getServletContext());
	server.checkMappingFileChanged();
	String relativeResourceURI = request.getRequestURI().substring(
			request.getContextPath().length()
					+ request.getServletPath().length());
	// Some servlet containers keep the leading slash, some don't
	if (!"".equals(relativeResourceURI)
			&& "/".equals(relativeResourceURI.substring(0, 1))) {
		relativeResourceURI = relativeResourceURI.substring(1);
	}
	if (request.getQueryString() != null) {
		relativeResourceURI = relativeResourceURI + "?"
				+ request.getQueryString();
	}

	/* Determine service stem, i.e. vocab/ in /[vocab/]page */
	int servicePos;
	if (-1 == (servicePos = request.getServletPath().indexOf(
			"/" + D2RServer.getPageServiceName())))
		throw new ServletException("Expected to find service path /"
				+ D2RServer.getPageServiceName());
	String serviceStem = request.getServletPath().substring(1,
			servicePos + 1);

	String resourceURI = server.resourceBaseURI(serviceStem)
			+ relativeResourceURI;
	String documentURL = server.dataURL(serviceStem, relativeResourceURI);
	String pageURL = server.pageURL(serviceStem, relativeResourceURI);

	VelocityWrapper velocity = new VelocityWrapper(this, request, response);
	Context context = velocity.getContext();
	context.put("uri", resourceURI);

	// Build resource description
	Resource resource = ResourceFactory.createResource(resourceURI);
	boolean outgoingTriplesOnly = server.isVocabularyResource(resource)
			&& !server.getConfig().getVocabularyIncludeInstances();
	int limit = server.getConfig().getLimitPerPropertyBridge();
	Model description = null;
	try {
		ResourceDescriber describer = new ResourceDescriber(
				server.getMapping(), resource.asNode(), outgoingTriplesOnly,
				limit, Math.round(server.getConfig().getPageTimeout() * 1000));
		description = ModelFactory.createModelForGraph(describer.description());
	} catch (QueryCancelledException ex) {
		velocity.reportError(
				504, "504 Gateway Timeout", "The operation timed out.");
		return;
	}
	if (description.size() == 0) {
		velocity.reportError(404, "404 Not Found", "No resource with this identifier exists in the database.");
		return;
	}
	// Get a Resource that is attached to the description model
	resource = description.getResource(resourceURI);

	this.prefixes = server.getPrefixes(); // model();

	if (server.getConfig().serveMetadata()) {
		// create and add metadata to context
		MetadataCreator resourceMetadataCreator = new MetadataCreator(
				server, server.getConfig().getResourceMetadataTemplate(
						server, getServletContext()));

		Model metadata = resourceMetadataCreator.addMetadataFromTemplate(
				resourceURI, documentURL, pageURL);
		if (!metadata.isEmpty()) {
			List<Statement> mList = metadata.getResource(documentURL)
					.listProperties().toList();
			Collections.sort(mList, MetadataCreator.subjectSorter);
			context.put("metadata", mList);

			context.put("metadataroot", metadata.getResource(documentURL));

			// add prefixes to context
			Map<String, String> nsSet = metadata.getNsPrefixMap();
			nsSet.putAll(description.getNsPrefixMap());

			context.put("prefixes", nsSet.entrySet());
			context.put("renderedNodesMap",
					new HashMap<Resource, Boolean>());

			// add a empty map for keeping track of blank nodes aliases
			context.put("blankNodesMap", new HashMap<Resource, String>());
		} else {
			context.put("metadata", Boolean.FALSE);
		}
	} else {
		context.put("metadata", Boolean.FALSE);
	}

	context.put("rdf_link", documentURL);
	context.put("label", getBestLabel(resource));
	context.put("properties", collectProperties(description, resource));
	context.put("classmap_links", classmapLinks(resource));
	context.put("limit_per_property_bridge", limit > 0 ? limit : null);
	velocity.mergeTemplateXHTML("resource_page.vm");
}