All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.sparqlify.update.UpdateGraph Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package org.aksw.sparqlify.update;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.aksw.commons.util.reflect.MultiMethod;
import org.aksw.jena_sparql_api.utils.QueryUtils;

import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.sparql.modify.request.UpdateDataDelete;
import org.apache.jena.sparql.modify.request.UpdateDataInsert;
import org.apache.jena.sparql.modify.request.UpdateModify;
import org.apache.jena.sparql.syntax.Element;
import org.apache.jena.update.Update;
import org.apache.jena.update.UpdateFactory;
import org.apache.jena.update.UpdateRequest;


public class UpdateGraph
{
	private Set graphListeners = new HashSet();

	public Set getPreUpdateListeners()
	{
		return graphListeners;
	}


	private ModelSparqlEndpoint endpoint;
	
	private int batchSize = 128;
	
	public UpdateGraph(ModelSparqlEndpoint endpoint)
	{
		this.endpoint = endpoint;
	}
	
    public static Map quadsToGraphs(Collection quads) {
    	Map result = new HashMap();
    	
    	
    	for(Quad quad : quads) {
    		Triple triple = quad.asTriple();

    		Node graphName = quad.getGraph();
    		
    		Graph graph = result.get(graphName);
    		if(graph == null) {
    			graph = GraphFactory.createDefaultGraph();
    			result.put(graphName, graph);
    		}
    		
    		graph.add(triple);
    	}
    	
    	return result;
    }
    
    private void insert(Collection quads) {
    	if(quads.isEmpty()) {
    		return;
    	}
    	
    	notifyPreUpdate(quads, null);
    	
    	for(Entry entry : quadsToGraphs(quads).entrySet()) {
    		Model model = ModelFactory.createModelForGraph(entry.getValue());
    		endpoint.insert(model, entry.getKey().toString());
    	}

    	notifyPostUpdate(quads, null);
    }

    private void notifyPreUpdate(Collection inserts, Collection deletes) {
    	
    	for(GraphListener graphListener : graphListeners) {
    		graphListener.onPreBatchStart();

    		if(inserts != null) {
	    		for(Quad quad : inserts) {
	    			graphListener.onPreInsert(quad);
	    		}
    		}
    		
    		if(deletes != null) {
	    		for(Quad quad : deletes) {
	    			graphListener.onPreDelete(quad);
	    		}
    		}
    		
    		graphListener.onPreBatchEnd();
    	}
    }

    private void notifyPostUpdate(Collection inserts, Collection deletes) {
    	
    	for(GraphListener graphListener : graphListeners) {
    		graphListener.onPostBatchStart();

    		if(inserts != null) {
	    		for(Quad quad : inserts) {
	    			graphListener.onPostInsert(quad);
	    		}
    		}
    		
    		if(deletes != null) {
	    		for(Quad quad : deletes) {
	    			graphListener.onPostDelete(quad);
	    		}
    		}
    		
    		graphListener.onPostBatchEnd();
    	}
    }
    
    
    
    private void delete(Collection quads) {
    	if(quads.isEmpty()) {
    		return;
    	}
    	
    	notifyPreUpdate(null, quads);

    	for(Entry entry : quadsToGraphs(quads).entrySet()) {
    		Model model = ModelFactory.createModelForGraph(entry.getValue());
    		endpoint.remove(model, entry.getKey().toString());
    	}

    	notifyPostUpdate(null, quads);
    }

    /*
    protected void startBatch()
    {
    	for(GraphListener graphListener : preChangeGraphListeners) {
    		graphListener.onBatchStart();
    	}
    }

    protected void endBatch()
    {
    	for(GraphListener graphListener : preChangeGraphListeners) {
    		graphListener.onBatchEnd();
    	}
    }*/

    
	@SuppressWarnings("unused")
	private void _update(UpdateModify update)
	{
		Element wherePattern = update.getWherePattern();
		Query query = QueryUtils.elementToQuery(wherePattern);
		
		
		String queryStr = query.toString();
		
		// TODO Limit and offset...
		ResultSet rs = endpoint.createQueryExecution(queryStr).execSelect();
	
		Set inserts = new HashSet();
		Set deletes = new HashSet();
		
		while(rs.hasNext()) {
			Binding binding = rs.nextBinding();

			Set i = QueryUtils.instanciate(update.getInsertQuads(), binding);
			Set d = QueryUtils.instanciate(update.getDeleteQuads(), binding);

			inserts.addAll(i);
			deletes.addAll(d);

	    	if(inserts.size() > batchSize) {
	    		insert(inserts);
	    		inserts.clear();
	    	}

	    	if(deletes.size() > batchSize) {
	    		delete(deletes);
	    		deletes.clear();
	    	}
		}
		insert(inserts);
		delete(deletes);
	}
	
	@SuppressWarnings("unused")
	private void _update(UpdateDataInsert update)
	{
		insert(update.getQuads());
	}
	
	@SuppressWarnings("unused")
	private void _update(UpdateDataDelete update)
	{
		delete(update.getQuads());
	}
	
	public void executeUpdate(String queryStr)
	{
		UpdateRequest request = new UpdateRequest();
		UpdateFactory.parse(request, queryStr);
	
		for(Update update : request.getOperations()) {
			MultiMethod.invoke(this, "_update", update);
		}
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy