clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From enrid...@apache.org
Subject [3/5] CLEREZZA-810 Packages renamed
Date Sun, 16 Mar 2014 18:51:37 GMT
http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/DataAccess.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/DataAccess.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/DataAccess.java
deleted file mode 100644
index f420f99..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/DataAccess.java
+++ /dev/null
@@ -1,860 +0,0 @@
-package rdf.virtuoso.storage.access;
-
-import java.sql.DriverManager;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.Language;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.PlainLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.TypedLiteral;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.wymiwyg.commons.util.collections.BidiMap;
-import org.wymiwyg.commons.util.collections.BidiMapImpl;
-
-import rdf.virtuoso.storage.VirtuosoBNode;
-import virtuoso.jdbc4.VirtuosoConnection;
-import virtuoso.jdbc4.VirtuosoException;
-import virtuoso.jdbc4.VirtuosoExtendedString;
-import virtuoso.jdbc4.VirtuosoPreparedStatement;
-import virtuoso.jdbc4.VirtuosoRdfBox;
-import virtuoso.jdbc4.VirtuosoResultSet;
-
-/**
- * 
- * @author enridaga
- *
- */
-public class DataAccess {
-	private Logger logger = LoggerFactory.getLogger(DataAccess.class);
-
-	final static String DRIVER = "virtuoso.jdbc4.Driver";
-
-	// XXX This is only used to create a new bnode identifier in virtuoso
-	final static String INSERT_NEW_BNODE = "SPARQL INSERT INTO iri(??) { [] `iri(??)` "
-			+ "`iri(??)`}";
-
-	// XXX This is only used to delete a new bnode identifier in virtuoso
-	final static String DELETE_NEW_BNODE = "SPARQL DELETE FROM iri(??) { ?s ?p ?o } WHERE { ?s ?p ?o . filter( ?p = iri(??) && "
-			+ " ?o = iri(??) ) }";
-
-	final static String INSERT_QUAD = "SPARQL INSERT INTO iri(??) {`iri(??)` `iri(??)` "
-			+ "`bif:__rdf_long_from_batch_params(??,??,??)`}";
-	final static String DELETE_QUAD = "SPARQL DELETE FROM iri(??) { ?s ?p ?o } WHERE { ?s ?p ?o . filter( ?s = iri(??) && ?p = iri(??) && "
-			+ " ?o = bif:__rdf_long_from_batch_params(??,??,??) ) } ";
-	final static String LIST_GRAPHS = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {[] [] []} }";
-	final static String CLEAR_GRAPH = "SPARQL CLEAR GRAPH iri(??)";
-	final static String COUNT_TRIPLES_OF_GRAPH = "SPARQL SELECT COUNT(*) WHERE { bind( iri(??) as ?graph ) . graph ?graph { [] [] [] } }";
-	final static String SELECT__ = "SPARQL SELECT ?subject ?predicate ?object WHERE { bind( iri(??) as ?graph ) . GRAPH ?graph { ?subject ?predicate ?object ";
-	final static String SELECT_TRIPLES_NULL_NULL_NULL = SELECT__ + " } }";
-	final static String SELECT_TRIPLES_S_NULL_NULL = SELECT__ + " . FILTER( ?subject = iri(??) ) } }";
-	final static String SELECT_TRIPLES_S_P_NULL = SELECT__ + " . FILTER( ?subject = iri(??) && ?predicate = iri(??) ) } }";
-	final static String SELECT_TRIPLES_S_P_O = SELECT__ + " . FILTER( ?subject = iri(??) && ?predicate = iri(??) && ?object = bif:__rdf_long_from_batch_params(??,??,??) ) } }";
-	final static String SELECT_TRIPLES_NULL_P_NULL = SELECT__ + " . FILTER( ?predicate = iri(??) ) } }";
-	final static String SELECT_TRIPLES_NULL_P_O = SELECT__ + " . FILTER( ?predicate = iri(??) && ?object = bif:__rdf_long_from_batch_params(??,??,??) ) } }";
-	final static String SELECT_TRIPLES_NULL_NULL_O = SELECT__ + " . FILTER( ?object = bif:__rdf_long_from_batch_params(??,??,??) ) } }";
-	final static String SELECT_TRIPLES_S_NULL_O = SELECT__ + " . FILTER( ?subject = iri(??) && ?object = bif:__rdf_long_from_batch_params(??,??,??) ) } }";
-
-	private final static String[] filterQueries = new String[] {
-			SELECT_TRIPLES_NULL_NULL_NULL, SELECT_TRIPLES_S_NULL_NULL,
-			SELECT_TRIPLES_S_P_O, SELECT_TRIPLES_NULL_NULL_O,
-			SELECT_TRIPLES_NULL_P_NULL, SELECT_TRIPLES_S_P_NULL,
-			SELECT_TRIPLES_NULL_P_O, SELECT_TRIPLES_S_NULL_O };
-
-	/**
-	 * Bidirectional map for managing the conversion from virtuoso blank nodes
-	 * (strings) to clerezza blank nodes and vice versa.
-	 */
-	private final BidiMap<VirtuosoBNode, BNode> bnodesMap;
-
-	private Map<String, VirtuosoPreparedStatement> preparedStatements = null;
-	private VirtuosoConnection connection = null;
-	private String connectionString;
-	private String user;
-	private String pwd;
-
-	// We protect the constructor from outside the package...
-	DataAccess(String connectionString, String user, String pwd) {
-		this.connectionString = connectionString;
-		this.user = user;
-		this.pwd = pwd;
-		connection = createConnection();
-		
-		// Init collections
-		this.preparedStatements = new HashMap<String,VirtuosoPreparedStatement>();
-		this.bnodesMap = new BidiMapImpl<VirtuosoBNode, BNode>();
-
-	}
-
-	private VirtuosoConnection createConnection() {
-		try {
-			Class.forName(VirtuosoWeightedProvider.DRIVER, true, this
-					.getClass().getClassLoader());
-			VirtuosoConnection c = (VirtuosoConnection) DriverManager
-					.getConnection(connectionString, user, pwd);
-			c.setAutoCommit(true);
-			return c;
-		} catch (ClassNotFoundException e) {
-			throw new RuntimeException(e);
-		} catch (SQLException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	// A simple renewal policy
-	private int statementCalls = 0;
-	protected PreparedStatement getStatement(String query)
-			throws VirtuosoException {
-		if(statementCalls >= 10000){
-			statementCalls=0; 
-			renew();
-		}else{
-			statementCalls++;
-		}
-		if (!preparedStatements.containsKey(query)) {
-			VirtuosoPreparedStatement ps = (VirtuosoPreparedStatement) connection
-					.prepareStatement(query);
-			preparedStatements.put(query, ps);
-		}
-		return preparedStatements.get(query);
-	}
-
-	private VirtuosoBNode toVirtBnode(BNode bnode) {
-		logger.debug("toVirtBnode(BNode {})", bnode);
-		if (bnode instanceof VirtuosoBNode) {
-			return ((VirtuosoBNode) bnode);
-		} else {
-			VirtuosoBNode virtBnode = bnodesMap.getKey(bnode);
-			if (virtBnode == null) {
-				// We create a local bnode mapped to the BNode given
-				virtBnode = nextVirtBnode(bnode);
-				bnodesMap.put(virtBnode, bnode);
-			}
-			return virtBnode;
-		}
-	}
-
-	public void renew() {
-		logger.trace("renewing...");
-		close();
-		connection = createConnection();
-	}
-
-	public void close() {
-		logger.trace("closing resources...");
-		Collection<VirtuosoPreparedStatement> pss = preparedStatements.values();
-		for (VirtuosoPreparedStatement ps : pss) {
-			try {
-				logger.trace("Closing prepared statement {}", ps);
-				ps.close();
-			} catch (Exception e) {
-				logger.error("Cannot close statement", e);
-			}
-		}
-		logger.trace("closed {} statements.", pss.size());
-		preparedStatements.clear();
-		try {
-			connection.close();
-			logger.trace("Connection closed");
-		} catch (Exception e) {
-			logger.error("Cannot close connection", e);
-		}
-	}
-
-	private void bindValue(PreparedStatement st, int i, Resource object)
-			throws SQLException {
-		if (object instanceof UriRef) {
-			st.setInt(i, 1);
-			st.setString(i + 1, ((UriRef) object).getUnicodeString());
-			st.setNull(i + 2, java.sql.Types.VARCHAR);
-		} else if (object instanceof BNode) {
-			st.setInt(i, 1);
-			st.setString(i + 1, ((VirtuosoBNode) object).getSkolemId());
-			st.setNull(i + 2, java.sql.Types.VARCHAR);
-		} else if (object instanceof TypedLiteral) {
-			TypedLiteral tl = ((TypedLiteral) object);
-			st.setInt(i, 4);
-			st.setString(i + 1, tl.getLexicalForm());
-			st.setString(i + 2, tl.getDataType().getUnicodeString());
-		} else if (object instanceof PlainLiteral) {
-			PlainLiteral pl = (PlainLiteral) object;
-			if (pl.getLanguage() != null) {
-				st.setInt(i, 5);
-				st.setString(i + 1, pl.getLexicalForm());
-				st.setString(i + 2, pl.getLanguage().toString());
-			} else {
-				st.setInt(i, 3);
-				st.setString(i + 1, pl.getLexicalForm());
-				st.setNull(i + 2, java.sql.Types.VARCHAR);
-			}
-		} else
-			throw new IllegalArgumentException(object.toString());
-	}
-
-	private void bindPredicate(PreparedStatement st, int i, UriRef predicate)
-			throws SQLException {
-		st.setString(i, predicate.getUnicodeString());
-	}
-
-	private void bindSubject(PreparedStatement st, int i, NonLiteral subject)
-			throws SQLException {
-		if (subject instanceof UriRef) {
-			st.setString(i, ((UriRef) subject).getUnicodeString());
-		} else {
-			st.setString(i, ((VirtuosoBNode) subject).getSkolemId());
-		}
-	}
-
-	private void bindGraph(PreparedStatement st, int i, UriRef uriRef)
-			throws SQLException {
-		st.setString(i, uriRef.getUnicodeString());
-	}
-	
-
-	private void bindGraph(PreparedStatement st, int i, String uri)
-			throws SQLException {
-		st.setString(i, uri);
-	}
-
-	/**
-	 * Generate a new local bnode to be used in virtuoso queries
-	 * 
-	 * @return
-	 */
-	private VirtuosoBNode nextVirtBnode(BNode bn) {
-		logger.debug("nextVirtBnode(BNode)");
-		/**
-		 * XXX Here we force virtuoso to generate a valid skolem uri for a blank
-		 * node we are going to insert for the first time.
-		 * 
-		 * All this process should be more efficient, possibly invoking a native
-		 * procedure, instead of insert/select/delete a fake triple as it is
-		 * now.
-		 */
-		UriRef g = new UriRef("urn:x-virtuoso:bnode-tmp");
-		UriRef p = new UriRef("urn:x-virtuoso:bnode:object");
-		UriRef o = new UriRef(new StringBuilder()
-				.append("urn:x-virtuoso:bnode:").append(bn).toString());
-
-		Exception e = null;
-		VirtuosoResultSet rs = null;
-
-		String bnodeId = null;
-		// insert
-		try {
-			PreparedStatement insert = getStatement(INSERT_NEW_BNODE);
-			bindGraph(insert, 1, g);
-			bindPredicate(insert, 2, p);
-			bindSubject(insert, 3, o);
-			insert.executeUpdate();
-
-			// select
-			PreparedStatement select = getStatement(SELECT_TRIPLES_NULL_P_O);
-			bindGraph(select, 1, g);
-			bindPredicate(select, 2, p);
-			bindValue(select, 3, o);
-			rs = (VirtuosoResultSet) select.executeQuery();
-			rs.next();
-			bnodeId = rs.getString(1);
-			rs.close();
-
-			// delete
-			PreparedStatement delete = getStatement(DELETE_NEW_BNODE);
-			bindGraph(delete, 1, g);
-			bindPredicate(delete, 2, p);
-			bindSubject(delete, 3, o); // It is a IRI
-			delete.executeUpdate();
-
-		} catch (VirtuosoException ve) {
-			logger.error("ERROR while executing statement", ve);
-			e = ve;
-		} catch (SQLException se) {
-			logger.error("ERROR while executing statement", se);
-			e = se;
-		} finally {
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Exception ex) {
-				logger.error("Error attempting to close result set", ex);
-			}
-		}
-		if (e != null) {
-			throw new RuntimeException(e);
-		}
-		return new VirtuosoBNode(bnodeId);
-
-	}
-	public void insertQuad(String graph, Triple triple) {
-		NonLiteral s = triple.getSubject(); 
-		UriRef p = triple.getPredicate() ;
-		Resource o = triple.getObject();
-		
-		// Skolemize bnodes
-		if(s instanceof BNode){
-			s = toVirtBnode((BNode) s);
-		}
-		if(o instanceof BNode){
-			o = toVirtBnode((BNode) o);
-		}
-		
-		try {
-			PreparedStatement st = getStatement(INSERT_QUAD);
-			bindGraph(st, 1, graph);
-			bindSubject(st, 2, s);
-			bindPredicate(st, 3, p);
-			bindValue(st, 4, o);
-			st.executeUpdate();
-		} catch (VirtuosoException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		} catch (SQLException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		}
-	}
-
-	public void deleteQuad(String graph,  Triple triple) {
-		NonLiteral s = triple.getSubject(); 
-		UriRef p = triple.getPredicate() ;
-		Resource o = triple.getObject();
-
-		// Skolemize bnodes
-		if(s instanceof BNode){
-			s = toVirtBnode((BNode) s);
-		}
-		if(o instanceof BNode){
-			o = toVirtBnode((BNode) o);
-		}
-		try {
-			PreparedStatement st = getStatement(DELETE_QUAD);
-			bindGraph(st, 1, graph);
-			bindSubject(st, 2, s);
-			bindPredicate(st, 3, p);
-			bindValue(st, 4, o);
-			st.executeUpdate();
-		} catch (VirtuosoException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		} catch (SQLException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		}
-	}
-
-	public Set<UriRef> listGraphs() {
-		try {
-			PreparedStatement st = getStatement(LIST_GRAPHS);
-			ResultSet rs = st.executeQuery();
-			Set<UriRef> graphs = new HashSet<UriRef>();
-			while (rs.next()) {
-				UriRef graph = new UriRef(rs.getString(1));
-				logger.debug(" > Graph {}", graph);
-				graphs.add(graph);
-			}
-			return Collections.unmodifiableSet(graphs);
-		} catch (VirtuosoException e) {
-			logger.error("Cannot execute query", e);
-			throw new RuntimeException(e);
-		} catch (SQLException e) {
-			logger.error("Cannot execute query", e);
-			throw new RuntimeException(e);
-		}
-	}
-
-	public void clearGraph(String graph) {
-		try {
-			PreparedStatement st = getStatement(CLEAR_GRAPH);
-			bindGraph(st, 1, graph);
-			st.executeUpdate();
-			try {
-				connection.commit();
-			} catch (Exception e) {
-				logger.error("Rolling back");
-				connection.rollback();
-			}
-		} catch (VirtuosoException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		} catch (SQLException e) {
-			logger.error("Cannot execute statement", e);
-			throw new RuntimeException(e);
-		}
-	}
-
-	private VirtuosoBNode toBNode(String virtbnode) {
-		VirtuosoBNode bnode;
-		bnode = new VirtuosoBNode(virtbnode);
-		return bnode;
-	}
-
-	public Iterator<Triple> filter(String graph, NonLiteral subject,
-			UriRef predicate, Resource object) {
-		
-
-		// Override blank node object to be a skolemized IRI
-		if (object != null && object instanceof BNode) {
-			object = new UriRef(toVirtBnode((BNode) object).getSkolemId());
-		}
-
-		// Override blank node subjects to be a skolemized IRI
-		if (subject != null && subject instanceof BNode) {
-			subject = new UriRef(toVirtBnode((BNode) subject).getSkolemId());
-		}
-		
-		if (logger.isDebugEnabled()) {
-			logger.debug("performFilter(UriRef graph, NonLiteral s, UriRef p, Resource o)");
-			logger.debug(" > g: {}", graph);
-			logger.debug(" > s: {}", subject);
-			logger.debug(" > p: {}", predicate);
-			logger.debug(" > o: {}", object);
-		}
-
-		List<Triple> list = null;
-		Exception e = null;
-		Set<String> filters = new HashSet<String>(Arrays.asList(filterQueries));
-
-		//
-		if (subject == null) {
-			filters.remove(SELECT_TRIPLES_S_P_O);
-			filters.remove(SELECT_TRIPLES_S_NULL_NULL);
-			filters.remove(SELECT_TRIPLES_S_P_NULL);
-			filters.remove(SELECT_TRIPLES_S_NULL_O);
-		} else {
-			filters.remove(SELECT_TRIPLES_NULL_NULL_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_NULL_O);
-			filters.remove(SELECT_TRIPLES_NULL_P_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_P_O);
-		}
-		if (predicate == null) {
-			filters.remove(SELECT_TRIPLES_S_P_O);
-			filters.remove(SELECT_TRIPLES_NULL_P_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_P_O);
-			filters.remove(SELECT_TRIPLES_S_P_NULL);
-		} else {
-			filters.remove(SELECT_TRIPLES_S_NULL_O);
-			filters.remove(SELECT_TRIPLES_NULL_NULL_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_NULL_O);
-			filters.remove(SELECT_TRIPLES_S_NULL_NULL);
-		}
-		if (object == null) {
-			filters.remove(SELECT_TRIPLES_S_P_O);
-			filters.remove(SELECT_TRIPLES_S_NULL_O);
-			filters.remove(SELECT_TRIPLES_NULL_P_O);
-			filters.remove(SELECT_TRIPLES_NULL_NULL_O);
-		} else {
-			filters.remove(SELECT_TRIPLES_S_P_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_NULL_NULL);
-			filters.remove(SELECT_TRIPLES_NULL_P_NULL);
-			filters.remove(SELECT_TRIPLES_S_NULL_NULL);
-		}
-
-		// There must be only 1 boss
-		String filter = filters.iterator().next();
-		PreparedStatement ps = null;
-		VirtuosoResultSet rs = null;
-		try {
-			logger.debug("query: {}", filter);
-			ps = getStatement(filter);
-			// In any case the first binding is the graph
-			bindGraph(ps, 1, graph);
-
-			int index = 2;
-			if (subject != null) {
-				bindSubject(ps, index, subject);
-				index++;
-			}
-			if (predicate != null) {
-				bindPredicate(ps, index, predicate);
-				index++;
-			}
-			if (object != null) {
-				bindValue(ps, index, object);
-			}
-
-			rs = (VirtuosoResultSet) ps.executeQuery();
-			list = new ArrayList<Triple>();
-
-			while (rs.next()) {
-				list.add(new TripleBuilder(rs.getObject(1), rs.getObject(2), rs
-						.getObject(3)).build());
-			}
-		} catch (VirtuosoException e1) {
-			logger.error("ERROR while executing statement", ps);
-			e = e1;
-		} catch (SQLException e1) {
-			logger.error("ERROR while executing statement", ps);
-			e = e1;
-		} finally {
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Throwable ex) {
-				logger.error("Cannot close result set", ex);
-			}
-		}
-
-		if (list == null || e != null) {
-			throw new RuntimeException(e);
-		}
-		return list.iterator();
-	}
-
-	public int size(String graph){
-		Exception e = null;
-		PreparedStatement ps = null;
-		VirtuosoResultSet rs = null;
-		int size = -1;
-		try {
-			ps = getStatement(COUNT_TRIPLES_OF_GRAPH);
-			// In any case the first binding is the graph
-			bindGraph(ps, 1, graph);
-			ps.execute();
-			rs = (VirtuosoResultSet) ps.getResultSet();
-			rs.next();
-			size = rs.getInt(1);
-		} catch (VirtuosoException e1) {
-			logger.error("ERROR while executing statement", ps);
-			e = e1;
-		} catch (SQLException e1) {
-			logger.error("ERROR while executing statement", ps);
-			e = e1;
-		} finally {
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Throwable ex) {
-				logger.error("Cannot close result set", ex);
-			}
-		}
-		if (size == -1 || e != null) {
-			throw new RuntimeException(e);
-		}
-		return size;	
-	}
-	
-	/**
-	 * Builds a clerezza Triple from Virtuoso result types
-	 * 
-	 */
-	private class TripleBuilder {
-
-		Object s = null;
-		Object p = null;
-		Object o = null;
-
-		public TripleBuilder(Object s, Object p, Object o) {
-			if (logger.isDebugEnabled()) {
-				logger.debug("TripleBuilder(Object s, Object p, Object o)");
-				logger.debug("> s: {}", s);
-				logger.debug("> p: {}", p);
-				logger.debug("> o: {}", o);
-			}
-			this.s = s;
-			this.p = p;
-			this.o = o;
-		}
-
-		private NonLiteral buildSubject() {
-			logger.debug("TripleBuilder.getSubject() : {}", s);
-			if (s instanceof VirtuosoExtendedString) {
-				VirtuosoExtendedString vs = (VirtuosoExtendedString) s;
-				if (vs.iriType == VirtuosoExtendedString.IRI
-						&& (vs.strType & 0x01) == 0x01) {
-					// Subject is IRI
-					return new UriRef(vs.str);
-				} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
-					return DataAccess.this.toBNode(vs.str);
-				} else {
-					// !Cannot happen
-					throw new IllegalStateException(
-							"Subject must be an IRI or a BNODE");
-				}
-			} else {
-				throw new IllegalStateException(
-						"Subject must be an instance of VirtuosoExtendedString");
-			}
-		}
-
-		private UriRef buildPredicate() {
-			logger.debug("TripleBuilder.getPredicate() : {}", p);
-			if (p instanceof VirtuosoExtendedString) {
-				VirtuosoExtendedString vs = (VirtuosoExtendedString) p;
-				if (vs.iriType == VirtuosoExtendedString.IRI
-						&& (vs.strType & 0x01) == 0x01) {
-					// Subject is IRI
-					return new UriRef(vs.str);
-				} else {
-					// !Cannot happen
-					throw new IllegalStateException("Predicate must be an IRI ");
-				}
-			} else {
-				throw new IllegalStateException("Predicate must be an IRI");
-			}
-		}
-
-		Resource buildObject() {
-			logger.debug("TripleBuilder.getObject() : {}", o);
-
-			if (o instanceof VirtuosoExtendedString) {
-				// In case is IRI
-				VirtuosoExtendedString vs = (VirtuosoExtendedString) o;
-				if (vs.iriType == VirtuosoExtendedString.IRI
-						&& (vs.strType & 0x01) == 0x01) {
-					// Is IRI
-					return new UriRef(vs.str);
-				} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
-					//
-					return DataAccess.this.toBNode(vs.str);
-				} else {
-					// Is a plain literal
-					return new PlainLiteralImpl(vs.str);
-				}
-			} else if (o instanceof VirtuosoRdfBox) {
-				// In case is typed literal
-				VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
-
-				String value;
-				if (rb.rb_box.getClass().isAssignableFrom(String.class)) {
-					value = (String) rb.rb_box;
-					String lang = rb.getLang();
-					String type = rb.getType();
-					if (type == null) {
-						Language language = lang == null ? null : new Language(
-								lang);
-						return new PlainLiteralImpl(value, language);
-					} else {
-						return new TypedLiteralImpl(value, new UriRef(type));
-					}
-				} else if (rb.rb_box instanceof VirtuosoExtendedString) {
-					VirtuosoExtendedString vs = (VirtuosoExtendedString) rb.rb_box;
-
-					if (vs.iriType == VirtuosoExtendedString.IRI
-							&& (vs.strType & 0x01) == 0x01) {
-						// Is IRI
-						return new UriRef(vs.str);
-					} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
-						//
-						return DataAccess.this.toBNode(vs.str);
-					} else {
-						String type = rb.getType();
-						if (type == null) {
-							String lang = rb.getLang();
-							if (lang != null) {
-								return new PlainLiteralImpl(vs.str,
-										new Language(lang));
-							}
-							// Is a plain literal
-							return new PlainLiteralImpl(vs.str);
-						} else {
-							return new TypedLiteralImpl(vs.str,
-									new UriRef(type));
-						}
-					}
-				}
-			} else if (o == null) {
-				// Raise an exception
-				throw new IllegalStateException("Object cannot be NULL!");
-			}
-
-			// FIXME (not clear this...)
-			return new PlainLiteralImpl(o.toString());
-		}
-
-		public Triple build() {
-			logger.debug("TripleBuilder.build()");
-			return new TripleImpl(buildSubject(), buildPredicate(),
-					buildObject());
-		}
-	}
-
-	/**
-	 * The following private methods are used to support the triple addition
-	 * plan B
-	 */
-	
-	public boolean performAddPlanB(String graph, Triple triple) {
-
-		StringBuilder b = new StringBuilder();
-		b.append(toVirtSubject(triple.getSubject())).append(" ")
-				.append(toVirtPredicate(triple.getPredicate())).append(" ")
-				.append(toVirtObject(triple.getObject())).append(" . ");
-		String sql = new StringBuilder().append("db.dba.ttlp(?, '', '").append(graph).append("', 0)").toString();
-		logger.debug("Exec Plan B: {}", sql);
-		Exception e = null;
-		PreparedStatement st = null;
-		try {
-			st = getStatement(sql);
-			String s = b.toString();
-			logger.trace(" TTL is \n{}\n", s);
-			st.setNString(1, b.toString());
-			st.execute();
-		} catch (VirtuosoException ve) {
-			logger.error("ERROR while executing statement", ve);
-			e = ve;
-		} catch (SQLException se) {
-			logger.error("ERROR while executing statement", se);
-			e = se;
-		}
-		if (e != null) {
-			logger.error("S {}", triple.getSubject());
-			logger.error("P {}", triple.getPredicate());
-			logger.error("O {}", triple.getObject());
-			logger.error(" O length: {}", triple.getObject().toString()
-					.length());
-			logger.error("Sql: {}", sql);
-			throw new RuntimeException(e);
-		}
-		return true;
-	}
-
-	/**
-	 * Returns a string to be used inline in SQL statements as Object of a
-	 * triple.
-	 * 
-	 * @param object
-	 * @return
-	 */
-	private String toVirtObject(Resource object) {
-		logger.debug("toVirtObject(Resource {})", object);
-		if (object == null)
-			return null;
-		if (object instanceof UriRef) {
-			return toVirtIri((UriRef) object);
-		} else if (object instanceof BNode) {
-			return toVirtBnode((BNode) object).asSkolemIri();
-		} else if (object instanceof PlainLiteral) {
-			return toVirtPlainLiteral((PlainLiteral) object);
-		} else if (object instanceof TypedLiteral) {
-			return toVirtTypedLiteral((TypedLiteral) object);
-		}
-		// XXX throw exception here?
-		return null;
-	}
-
-	/**
-	 * Returns a string to be used in SQL statements.
-	 * 
-	 * @param object
-	 * @return
-	 */
-	private String toVirtTypedLiteral(TypedLiteral object) {
-		logger.debug("toVirtTypedLiteral(TypedLiteral {})", object);
-		UriRef dt = object.getDataType();
-		String literal = object.getLexicalForm();// .replaceAll("\"", "\\\\\"");
-		StringBuilder prepared;
-		// If XMLLiteral, prepare XML entities
-		prepared = prepareString(
-				literal,
-				dt.getUnicodeString()
-						.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"));
-		return new StringBuilder().append('"').append('"').append('"')
-				.append(prepared).append('"').append('"').append('"')
-				.append("^^").append(toVirtIri(dt)).toString();
-	}
-
-	private StringBuilder prepareString(String str, boolean xml) {
-		StringBuilder retStr = new StringBuilder();
-		for (int i = 0; i < str.length(); i++) {
-			int cp = Character.codePointAt(str, i);
-			int charCount = Character.charCount(cp);
-			if (charCount > 1) {
-				i += charCount - 1; // 2.
-				if (i >= str.length()) {
-					throw new IllegalArgumentException("truncated unexpectedly");
-				}
-			}
-
-			if (cp < 128) {
-				retStr.appendCodePoint(cp);
-			} else {
-				if (xml) {
-					retStr.append(String.format("&#x%04x;", cp));
-				} else {
-					retStr.append(String.format("\\u%04x", cp));
-				}
-			}
-		}
-		return retStr;
-	}
-
-	/**
-	 * Returns a string to be used in SQL statements.
-	 * 
-	 * @param object
-	 * @return
-	 */
-	private String toVirtPlainLiteral(PlainLiteral object) {
-		logger.debug("toVirtPlainLiteral(PlainLiteral {})", object);
-		Language lang = object.getLanguage();
-		String literal = object.getLexicalForm();
-		StringBuilder sb = new StringBuilder().append('"').append('"')
-				.append('"').append(prepareString(literal, false)).append('"')
-				.append('"').append('"');
-		if (lang == null) {
-			return sb.toString();
-		} else {
-			return sb.append("@").append(lang).toString();
-		}
-	}
-
-	/**
-	 * Returns a string to be used in SQL statements as Predicate of a triple.
-	 * 
-	 * @param predicate
-	 * @return
-	 */
-	private String toVirtPredicate(UriRef predicate) {
-		logger.debug("toVirtPredicate(UriRef {}) ", predicate);
-		if (predicate == null)
-			return null;
-		return toVirtIri(predicate);
-	}
-
-	private String toVirtIri(UriRef ur) {
-		logger.debug("toVirtIri(UriRef {})", ur);
-		return "<" + ur.getUnicodeString() + ">";
-	}
-
-	/**
-	 * Returns a string to be used in SQL statements as Subject of a triple.
-	 * 
-	 * @param subject
-	 * @return
-	 */
-	private String toVirtSubject(NonLiteral subject) {
-		logger.debug("toVirtSubject(NonLiteral {})", subject);
-		if (subject == null) {
-			return null;
-		}
-		if (subject instanceof UriRef) {
-			return toVirtIri((UriRef) subject);
-		} else if (subject instanceof BNode) {
-			return toVirtBnode((BNode) subject).asSkolemIri();
-		} else {
-			// These should be the only 2 implementations
-			throw new IllegalArgumentException(
-					"subject must be BNode or UriRef");
-		}
-	}
-
-}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
deleted file mode 100644
index 0908d73..0000000
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
+++ /dev/null
@@ -1,923 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package rdf.virtuoso.storage.access;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.Writer;
-import java.math.BigInteger;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.clerezza.rdf.core.Graph;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.TripleCollection;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException;
-import org.apache.clerezza.rdf.core.access.EntityUndeletableException;
-import org.apache.clerezza.rdf.core.access.NoSuchEntityException;
-import org.apache.clerezza.rdf.core.access.TcManager;
-import org.apache.clerezza.rdf.core.access.WeightedTcProvider;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Properties;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Service;
-import org.osgi.framework.Constants;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.component.ComponentContext;
-import org.osgi.service.component.ComponentException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import rdf.virtuoso.storage.VirtuosoGraph;
-import rdf.virtuoso.storage.VirtuosoMGraph;
-import virtuoso.jdbc4.VirtuosoConnection;
-import virtuoso.jdbc4.VirtuosoException;
-import virtuoso.jdbc4.VirtuosoPreparedStatement;
-import virtuoso.jdbc4.VirtuosoResultSet;
-import virtuoso.jdbc4.VirtuosoStatement;
-
-/**
- * A {@link org.apache.clerezza.rdf.core.access.WeightedTcProvider} for
- * Virtuoso.
- * 
- * @author enridaga
- * 
- */
-@Component(metatype = true, immediate = true)
-@Service(WeightedTcProvider.class)
-@Properties({
-		@Property(name = "password", value = "dba", description = "User password"),
-		@Property(name = "host", value = "localhost", description = "The host running the Virtuoso server"),
-		@Property(name = "port", intValue = 1111, description = "The port number"),
-		@Property(name = "user", value = "dba", description = "User name"),
-		@Property(name = "weight", intValue = 110, description = "Weight assigned to this provider"),
-		@Property(name = TcManager.GENERAL_PURPOSE_TC, boolValue = true) })
-public class VirtuosoWeightedProvider implements WeightedTcProvider {
-
-	// JDBC driver class (XXX move to DataAccess?)
-	public static final String DRIVER = "virtuoso.jdbc4.Driver";
-
-	// Default value for the property "weight"
-	public static final int DEFAULT_WEIGHT = 110;
-
-	// Names of properties in OSGi configuration
-	public static final String HOST = "host";
-	public static final String PORT = "port";
-	public static final String USER = "user";
-	public static final String PASSWORD = "password";
-	public static final String WEIGHT = "weight";
-
-	// Name of the graph used to contain the registry of the created graphs
-	public static final String ACTIVE_GRAPHS_GRAPH = "urn:x-virtuoso:active-graphs";
-
-	// Loaded graphs
-	private Map<UriRef, VirtuosoMGraph> graphs = new HashMap<UriRef, VirtuosoMGraph>();
-
-	// DataAccess registry
-	private Set<DataAccess> dataAccessSet = new HashSet<DataAccess>();
-
-	// Logger
-	private Logger logger = LoggerFactory
-			.getLogger(VirtuosoWeightedProvider.class);
-
-	// Fields
-	private String host;
-	private Integer port;
-	private String user;
-	private String pwd;
-	private String connStr;
-	private int weight = DEFAULT_WEIGHT;
-
-	/**
-	 * Creates a new {@link VirtuosoWeightedProvider}.
-	 * 
-	 * Before the weighted provider can be used, the method
-	 * <code>activate</code> has to be called.
-	 */
-	public VirtuosoWeightedProvider() {
-		logger.debug("Created VirtuosoWeightedProvider.");
-	}
-
-	public VirtuosoWeightedProvider(String jdbcConnectionString,
-			String jdbcUser, String jdbcPassword) {
-		connStr = jdbcConnectionString;
-		user = jdbcUser;
-		pwd = jdbcPassword;
-	}
-
-	/**
-	 * Activates this component.<br />
-	 * 
-	 * @param cCtx
-	 *            Execution context of this component. A value of null is
-	 *            acceptable when you set the property connection
-	 * @throws ConfigurationException
-	 * @throws IllegalArgumentException
-	 *             No component context given and connection was not set.
-	 */
-	@Activate
-	public void activate(ComponentContext cCtx) {
-		logger.trace("activate(ComponentContext {})", cCtx);
-		logger.info("Activating VirtuosoWeightedProvider...");
-
-		if (cCtx == null) {
-			logger.error("No component context given and connection was not set");
-			throw new IllegalArgumentException(
-					"No component context given and connection was not set");
-		} else if (cCtx != null) {
-			logger.debug("Context is given: {}", cCtx);
-			String pid = (String) cCtx.getProperties().get(
-					Constants.SERVICE_PID);
-			try {
-
-				// Bind logging of DriverManager
-				if (logger.isDebugEnabled()) {
-					logger.debug("Activating logging for DriverManager");
-					// DriverManager.setLogWriter(new PrintWriter(System.err));
-					DriverManager.setLogWriter(new PrintWriter(new Writer() {
-						private Logger l = LoggerFactory
-								.getLogger(DriverManager.class);
-						private StringBuilder b = new StringBuilder();
-
-						@Override
-						public void write(char[] cbuf, int off, int len)
-								throws IOException {
-							b.append(cbuf, off, len);
-						}
-
-						@Override
-						public void flush() throws IOException {
-							l.debug("{}", b.toString());
-							b = new StringBuilder();
-						}
-
-						@Override
-						public void close() throws IOException {
-							l.debug("{}", b.toString());
-							l.debug("Log PrintWriter closed");
-						}
-					}));
-				}
-
-				// FIXME The following should not be needed...
-				try {
-					this.weight = (Integer) cCtx.getProperties().get(WEIGHT);
-				} catch (NumberFormatException nfe) {
-					logger.warn(nfe.toString());
-					logger.warn("Setting weight to defaults");
-					this.weight = DEFAULT_WEIGHT;
-				}
-
-				/**
-				 * Retrieve connection properties
-				 */
-				host = (String) cCtx.getProperties().get(HOST);
-				port = (Integer) cCtx.getProperties().get(PORT);
-				user = (String) cCtx.getProperties().get(USER);
-				pwd = (String) cCtx.getProperties().get(PASSWORD);
-
-				// Build connection string
-				connStr = getConnectionString(host, port);
-
-				// Check connection
-				VirtuosoConnection connection = getConnection(connStr, user,
-						pwd);
-
-				// Debug activation
-				if (logger.isDebugEnabled()) {
-					logger.debug("Component context properties: ");
-					logger.debug("> host: {}", host);
-					logger.debug("> port: {}", port);
-					logger.debug("> user: {}", user);
-					// We hide the password in log files:
-					MessageDigest algorithm;
-					try {
-						algorithm = MessageDigest.getInstance("MD5");
-					} catch (NoSuchAlgorithmException e) {
-						throw new RuntimeException(e);
-					}
-					algorithm.reset();
-					algorithm.update(pwd.getBytes());
-					byte messageDigest[] = algorithm.digest();
-
-					StringBuffer hexString = new StringBuffer();
-					for (int i = 0; i < messageDigest.length; i++) {
-						hexString.append(Integer
-								.toHexString(0xFF & messageDigest[i]));
-					}
-					String foo = messageDigest.toString();
-					logger.debug("> password: {}", foo);
-				}
-				logger.info("Connection to {} initialized. User is {}",
-						connStr, user);
-
-				// everything went ok
-				connection.close();
-			} catch (VirtuosoException e) {
-				logger.error(
-						"A problem occurred while intializing connection to Virtuoso",
-						e);
-				logger.error("Be sure you have configured the connection parameters correctly in the OSGi/SCR configuration");
-				cCtx.disableComponent(pid);
-				throw new ComponentException(e.getLocalizedMessage());
-			} catch (SQLException e) {
-				logger.error(
-						"A problem occurred while intializing connection to Virtuoso",
-						e);
-				logger.error("Be sure you have configured the connection parameters correctly in the OSGi/SCR configuration");
-				cCtx.disableComponent(pid);
-				throw new ComponentException(e.getLocalizedMessage());
-			} catch (ClassNotFoundException e) {
-				logger.error(
-						"A problem occurred while intializing connection to Virtuoso",
-						e);
-				logger.error("Be sure you have configured the connection parameters correctly in the OSGi/SCR configuration");
-				cCtx.disableComponent(pid);
-				throw new ComponentException(e.getLocalizedMessage());
-			}
-		}
-		// Load remembered graphs
-		Set<UriRef> remembered = readRememberedGraphs();
-		for (UriRef name : remembered) {
-			if (canModify(name)) {
-				graphs.put(name, new VirtuosoMGraph(name.getUnicodeString(),
-						createDataAccess()));
-			} else {
-				graphs.put(name, new VirtuosoGraph(name.getUnicodeString(),
-						createDataAccess()));
-			}
-		}
-		logger.info("Activated VirtuosoWeightedProvider.");
-	}
-
-	public static final String getConnectionString(String hostName,
-			Integer portNumber) {
-		return new StringBuilder().append("jdbc:virtuoso://").append(hostName)
-				.append(":").append(portNumber).append("/CHARSET=UTF-8")
-				.toString();
-	}
-
-	private Set<UriRef> readRememberedGraphs() {
-		logger.trace(" readRememberedGraphs()");
-		String SQL = "SPARQL SELECT DISTINCT ?G FROM <" + ACTIVE_GRAPHS_GRAPH
-				+ "> WHERE { ?G a <urn:x-virtuoso/active-graph> }";
-		VirtuosoConnection connection = null;
-		Exception e = null;
-		VirtuosoStatement st = null;
-		VirtuosoResultSet rs = null;
-		Set<UriRef> remembered = new HashSet<UriRef>();
-		try {
-			connection = getConnection();
-			st = (VirtuosoStatement) connection.createStatement();
-			logger.debug("Executing SQL: {}", SQL);
-			rs = (VirtuosoResultSet) st.executeQuery(SQL);
-			while (rs.next()) {
-				UriRef name = new UriRef(rs.getString(1));
-				logger.debug(" > Graph {}", name);
-				remembered.add(name);
-			}
-		} catch (VirtuosoException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} catch (SQLException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} catch (ClassNotFoundException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} finally {
-
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Exception ex) {
-			}
-			;
-			try {
-				if (st != null)
-					st.close();
-			} catch (Exception ex) {
-			}
-			;
-			if (connection != null) {
-				try {
-					connection.close();
-				} catch (VirtuosoException e1) {
-					logger.error("Cannot close connection", e1);
-				}
-			}
-		}
-		if (e != null) {
-			throw new RuntimeException(e);
-		}
-		return remembered;
-	}
-
-	private void rememberGraphs(UriRef... graphs) {
-		logger.trace(" saveActiveGraphs()");
-		if (graphs.length > 0) {
-			// Returns the list of graphs in the virtuoso quad store
-			String SQL = "SPARQL INSERT INTO <" + ACTIVE_GRAPHS_GRAPH
-					+ "> { `iri(??)` a <urn:x-virtuoso/active-graph> }";
-			VirtuosoConnection connection = null;
-			Exception e = null;
-			VirtuosoPreparedStatement st = null;
-			VirtuosoResultSet rs = null;
-			try {
-				try {
-					connection = getConnection();
-					connection.setAutoCommit(false);
-					st = (VirtuosoPreparedStatement) connection
-							.prepareStatement(SQL);
-					logger.debug("Executing SQL: {}", SQL);
-					for (UriRef u : graphs) {
-						logger.trace(" > remembering {}", u);
-						st.setString(1, u.getUnicodeString());
-						st.executeUpdate();
-					}
-					connection.commit();
-				} catch (Exception e1) {
-					logger.error("Error while executing query/connection.", e1);
-					e = e1;
-					connection.rollback();
-				}
-			} catch (SQLException e1) {
-				logger.error("Error while executing query/connection.", e1);
-				e = e1;
-			} finally {
-				try {
-					if (rs != null)
-						rs.close();
-				} catch (Exception ex) {
-				}
-				;
-				try {
-					if (st != null)
-						st.close();
-				} catch (Exception ex) {
-				}
-				;
-				if (connection != null) {
-					try {
-						connection.close();
-					} catch (VirtuosoException e1) {
-						logger.error("Cannot close connection", e1);
-					}
-				}
-			}
-			if (e != null) {
-				throw new RuntimeException(e);
-			}
-		}
-	}
-
-	private void forgetGraphs(UriRef... graphs) {
-		logger.trace(" forgetGraphs()");
-		if (graphs.length > 0) {
-			// Returns the list of graphs in the virtuoso quad store
-			String SQL = "SPARQL WITH <"
-					+ ACTIVE_GRAPHS_GRAPH
-					+ "> DELETE { ?s ?p ?v } WHERE { ?s ?p ?v . FILTER( ?s = iri(??) ) }";
-			VirtuosoConnection connection = null;
-			Exception e = null;
-			VirtuosoPreparedStatement st = null;
-			VirtuosoResultSet rs = null;
-			try {
-				try {
-					connection = getConnection();
-					connection.setAutoCommit(false);
-					st = (VirtuosoPreparedStatement) connection
-							.prepareStatement(SQL);
-					logger.debug("Executing SQL: {}", SQL);
-					for (UriRef u : graphs) {
-						logger.trace(" > remembering {}", u);
-						st.setString(1, u.getUnicodeString());
-						st.executeUpdate();
-					}
-					connection.commit();
-				} catch (Exception e1) {
-					logger.error("Error while executing query/connection.", e1);
-					e = e1;
-					connection.rollback();
-				}
-			} catch (SQLException e1) {
-				logger.error("Error while executing query/connection.", e1);
-				e = e1;
-			} finally {
-				try {
-					if (rs != null)
-						rs.close();
-				} catch (Exception ex) {
-				}
-				;
-				try {
-					if (st != null)
-						st.close();
-				} catch (Exception ex) {
-				}
-				;
-				if (connection != null) {
-					try {
-						connection.close();
-					} catch (VirtuosoException e1) {
-						logger.error("Cannot close connection", e1);
-					}
-				}
-			}
-			if (e != null) {
-				throw new RuntimeException(e);
-			}
-		}
-	}
-
-	/**
-	 * Deactivates this component.
-	 * 
-	 * @param cCtx
-	 *            component context provided by OSGi
-	 */
-	@Deactivate
-	public void deactivate(ComponentContext cCtx) {
-		logger.debug("deactivate(ComponentContext {})", cCtx);
-		// Save active (possibly empty) graphs to a dedicated graph
-		rememberGraphs();
-		// XXX Important. Close all opened resources
-		for (DataAccess mg : dataAccessSet) {
-			mg.close();
-		}
-
-		logger.info("Shutdown complete.");
-	}
-
-	public VirtuosoConnection getConnection() throws SQLException,
-			ClassNotFoundException {
-		return getConnection(connStr, user, pwd);
-	}
-
-	private VirtuosoConnection getConnection(String connStr, String user,
-			String pwd) throws SQLException, ClassNotFoundException {
-		logger.debug("getConnection(String {}, String {}, String *******)",
-				connStr, user);
-		/**
-		 * FIXME For some reasons, it looks the DriverManager is instantiating a
-		 * new virtuoso.jdbc4.Driver instance upon any activation. (Enable DEBUG
-		 * to see this)
-		 */
-		logger.debug("Loading JDBC Driver");
-		Class.forName(VirtuosoWeightedProvider.DRIVER, true, this.getClass()
-				.getClassLoader());
-		VirtuosoConnection c = (VirtuosoConnection) DriverManager
-				.getConnection(connStr, user, pwd);
-		c.setAutoCommit(true);
-		return c;
-	}
-
-	/**
-	 * Retrieves the Graph (unmodifiable) with the given UriRef If no graph
-	 * exists with such name, throws a NoSuchEntityException
-	 */
-	@Override
-	public Graph getGraph(UriRef name) throws NoSuchEntityException {
-		logger.debug("getGraph(UriRef {}) ", name);
-		// If it is read-only, returns the Graph
-		// If it is not read-only, returns the getGraph() version of the MGraph
-		VirtuosoMGraph g = loadGraphOnce(name);
-		if (g instanceof Graph) {
-			return (Graph) g;
-		} else {
-			return g.getGraph();
-		}
-	}
-
-	/**
-	 * Retrieves the MGraph (modifiable) with the given UriRef. If no graph
-	 * exists with such name, throws a NoSuchEntityException.
-	 * 
-	 * @return mgraph
-	 */
-	@Override
-	public MGraph getMGraph(UriRef name) throws NoSuchEntityException {
-		logger.debug("getMGraph(UriRef {}) ", name);
-		VirtuosoMGraph g = loadGraphOnce(name);
-		if (g instanceof Graph) {
-			// We have this graph but only in read-only mode!
-			throw new NoSuchEntityException(name);
-		}
-		return g;
-	}
-
-	/**
-	 * Load the graph once. It check whether a graph object have been alrady
-	 * created for that UriRef, if yes returns it.
-	 * 
-	 * If not check if at least 1 triple is present in the quad for such graph
-	 * identifier. If yes, creates a new graph object and loads it in the map,
-	 * referring to it on next calls.
-	 * 
-	 * If no triples exists, the graph does not exists or it is not readable.
-	 * 
-	 * 
-	 * @param name
-	 * @return
-	 */
-	private VirtuosoMGraph loadGraphOnce(UriRef name) {
-		logger.debug("loadGraphOnce({})", name);
-
-		// Check whether the graph have been already loaded once
-		if (graphs.containsKey(name)) {
-			logger.debug("{} is already loaded", name);
-			return graphs.get(name);
-		} else {
-			VirtuosoMGraph graph = null;
-			logger.debug("Attempt to load {}", name);
-			// Let's create the graph object
-			String SQL = "SPARQL SELECT ?G WHERE { GRAPH ?G {[] [] []} . FILTER(?G = "
-					+ name + ")} LIMIT 1";
-
-			Statement st = null;
-			VirtuosoResultSet rs = null;
-			VirtuosoConnection connection = null;
-			Exception e = null;
-			try {
-				connection = getConnection(connStr, user, pwd);
-				st = connection.createStatement();
-				logger.debug("Executing SQL: {}", SQL);
-				st.execute(SQL);
-				rs = (VirtuosoResultSet) st.getResultSet();
-				if (rs.next() == false) {
-					// The graph is empty, it is not readable or does not exists
-					logger.warn("Graph does not exists: {}", name);
-					throw new NoSuchEntityException(name);
-				} else {
-					// The graph exists and it is readable ...
-					logger.debug("Graph {} is readable", name);
-					// is it writable?
-					logger.debug("Is {} writable?", name);
-					if (canModify(name)) {
-						logger.debug("Creating writable MGraph for graph {}",
-								name);
-						graphs.put(name,
-								new VirtuosoMGraph(name.getUnicodeString(),
-										createDataAccess()));
-					} else {
-						logger.debug("Creating read-only Graph for graph {}",
-								name);
-						graphs.put(name,
-								new VirtuosoMGraph(name.getUnicodeString(),
-										createDataAccess()).asVirtuosoGraph());
-					}
-					graph = graphs.get(name);
-				}
-
-			} catch (VirtuosoException ve) {
-				logger.error("Error while executing query/connection.", ve);
-				e = ve;
-			} catch (SQLException se) {
-				logger.error("Error while executing query/connection.", se);
-				e = se;
-			} catch (ClassNotFoundException ce) {
-				logger.error("Error while executing query/connection.", ce);
-				e = ce;
-			} finally {
-				try {
-					if (rs != null)
-						rs.close();
-				} catch (Exception ex) {
-				}
-				;
-				try {
-					if (st != null)
-						st.close();
-				} catch (Exception ex) {
-				}
-				;
-				if (connection != null) {
-					try {
-						connection.close();
-					} catch (VirtuosoException e1) {
-						logger.error("Cannot close connection", e1);
-					}
-				}
-			}
-			if (e != null) {
-				throw new RuntimeException(e);
-			}
-			return graph;
-		}
-
-	}
-
-	public DataAccess createDataAccess() {
-		DataAccess da = new DataAccess(connStr, user, pwd);
-		dataAccessSet.add(da);
-		// Remember all opened ones
-		return da;
-	}
-
-	/**
-	 * Generic implementation of the get(M)Graph method. If the named graph is
-	 * modifiable, behaves the same as getMGraph(UriRef name), elsewhere,
-	 * behaves as getGraph(UriRef name)
-	 */
-	@Override
-	public TripleCollection getTriples(UriRef name)
-			throws NoSuchEntityException {
-		logger.debug("getTriples(UriRef {}) ", name);
-		return loadGraphOnce(name);
-	}
-
-	/**
-	 * Returns the list of graphs in the virtuoso quad store. The returned set
-	 * is unmodifiable.
-	 * 
-	 * @return graphs
-	 */
-	@Override
-	public Set<UriRef> listGraphs() {
-		logger.debug("listGraphs()");
-		Set<UriRef> graphs = new HashSet<UriRef>();
-		// XXX Add the active (possibly empty) mgraphs
-		graphs.addAll(this.graphs.keySet());
-		// Returns the list of graphs in the virtuoso quad store
-		String SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {[] [] []} }";
-		VirtuosoConnection connection = null;
-		Exception e = null;
-		VirtuosoStatement st = null;
-		VirtuosoResultSet rs = null;
-		try {
-			connection = getConnection();
-			st = (VirtuosoStatement) connection.createStatement();
-			logger.debug("Executing SQL: {}", SQL);
-			rs = (VirtuosoResultSet) st.executeQuery(SQL);
-			while (rs.next()) {
-				UriRef graph = new UriRef(rs.getString(1));
-				logger.debug(" > Graph {}", graph);
-				graphs.add(graph);
-			}
-		} catch (VirtuosoException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} catch (SQLException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} catch (ClassNotFoundException e1) {
-			logger.error("Error while executing query/connection.", e1);
-			e = e1;
-		} finally {
-
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Exception ex) {
-			}
-			;
-			try {
-				if (st != null)
-					st.close();
-			} catch (Exception ex) {
-			}
-			;
-			if (connection != null) {
-				try {
-					connection.close();
-				} catch (VirtuosoException e1) {
-					logger.error("Cannot close connection", e1);
-				}
-			}
-		}
-		if (e != null) {
-			throw new RuntimeException(e);
-		}
-		return Collections.unmodifiableSet(graphs);
-	}
-
-	@Override
-	public Set<UriRef> listMGraphs() {
-		logger.debug("listMGraphs()");
-		Set<UriRef> graphs = listGraphs();
-		Set<UriRef> mgraphs = new HashSet<UriRef>();
-		logger.debug("Modifiable graphs:");
-		for (UriRef u : graphs) {
-			if (canModify(u)) {
-				logger.debug(" > {}", u);
-				mgraphs.add(u);
-			}
-		}
-		return Collections.unmodifiableSet(mgraphs);
-	}
-
-	private long getPermissions(String graph) {
-		VirtuosoConnection connection = null;
-		ResultSet rs = null;
-		Statement st = null;
-		logger.debug("getPermissions(String {})", graph);
-		Exception e = null;
-		Long result = null;
-		try {
-			connection = getConnection();
-			String sql = "SELECT DB.DBA.RDF_GRAPH_USER_PERMS_GET ('" + graph
-					+ "','" + connection.getMetaData().getUserName() + "') ";
-			logger.debug("Executing SQL: {}", sql);
-			st = connection.createStatement();
-			st.execute(sql);
-			rs = st.getResultSet();
-			rs.next();
-			result = rs.getLong(1);
-			logger.debug("Permission: {}", result);
-		} catch (VirtuosoException ve) {
-			logger.error("A virtuoso SQL exception occurred.");
-			e = ve;
-		} catch (SQLException se) {
-			logger.error("An SQL exception occurred.");
-			e = se;
-		} catch (ClassNotFoundException e1) {
-			logger.error("An ClassNotFoundException occurred.");
-			e = e1;
-		} finally {
-			try {
-				if (rs != null)
-					rs.close();
-			} catch (Exception ex) {
-			}
-			;
-			try {
-				if (st != null)
-					st.close();
-			} catch (Exception ex) {
-			}
-			;
-			if (connection != null) {
-				try {
-					connection.close();
-				} catch (VirtuosoException e1) {
-					logger.error("Cannot close connection", e1);
-				}
-			}
-		}
-		if (e != null) {
-			throw new RuntimeException(e);
-		}
-		return result;
-	}
-
-	public boolean canRead(UriRef graph) {
-		logger.debug("canRead(UriRef {})", graph);
-		return (isRead(getPermissions(graph.getUnicodeString())));
-	}
-
-	public boolean canModify(UriRef graph) {
-		logger.debug("canModify(UriRef {})", graph);
-		return (isWrite(getPermissions(graph.getUnicodeString())));
-	}
-
-	private boolean testPermission(long value, int bit) {
-		logger.debug("testPermission(long {},int {})", value, bit);
-		return BigInteger.valueOf(value).testBit(bit);
-	}
-
-	private boolean isRead(long permission) {
-		logger.debug("isRead(long {})", permission);
-		return testPermission(permission, 1);
-	}
-
-	private boolean isWrite(long permission) {
-		logger.debug("isWrite(long {})", permission);
-		return testPermission(permission, 2);
-	}
-
-	@Override
-	public Set<UriRef> listTripleCollections() {
-		logger.debug("listTripleCollections()");
-		// I think this should behave the same as listGraphs() in our case.
-		return listGraphs();
-	}
-
-	private VirtuosoMGraph createVirtuosoMGraph(UriRef name)
-			throws UnsupportedOperationException, EntityAlreadyExistsException {
-		logger.debug("createVirtuosoMGraph(UriRef {})", name);
-		// If the graph already exists, we throw an exception
-		try {
-			loadGraphOnce(name);
-			throw new EntityAlreadyExistsException(name);
-		} catch (NoSuchEntityException nsee) {
-			if (canModify(name)) {
-				graphs.put(name, new VirtuosoMGraph(name.getUnicodeString(),
-						createDataAccess()));
-				rememberGraphs(name);
-				return graphs.get(name);
-			} else {
-				logger.error("Cannot create MGraph {}", name);
-				throw new UnsupportedOperationException();
-			}
-		}
-	}
-
-	/**
-	 * Creates an initially empty MGraph. If the name already exists in the
-	 * store, throws an {@see EntityAlreadyExistsException}
-	 */
-	@Override
-	public MGraph createMGraph(UriRef name)
-			throws UnsupportedOperationException, EntityAlreadyExistsException {
-		logger.debug("createMGraph(UriRef {})", name);
-		return createVirtuosoMGraph(name);
-	}
-
-	/**
-	 * Creates a new graph with the given triples, then returns the readable
-	 * (not modifiable) version of the graph
-	 * 
-	 */
-	@Override
-	public Graph createGraph(UriRef name, TripleCollection triples)
-			throws UnsupportedOperationException, EntityAlreadyExistsException {
-		logger.debug("createGraph(UriRef {}, TripleCollection {})", name,
-				triples);
-		VirtuosoMGraph mgraph = createVirtuosoMGraph(name);
-		mgraph.addAll(triples);
-		return mgraph.getGraph();
-	}
-
-	/**
-	 * Clears the given graph and removes it from the loaded graphs.
-	 * 
-	 */
-	@Override
-	public void deleteTripleCollection(UriRef name)
-			throws UnsupportedOperationException, NoSuchEntityException,
-			EntityUndeletableException {
-		logger.debug("deleteTripleCollection(UriRef {})", name);
-		TripleCollection g = (VirtuosoMGraph) getTriples(name);
-		if (g instanceof Graph) {
-			throw new EntityUndeletableException(name);
-		} else {
-			((MGraph) g).clear();
-			graphs.remove(name);
-			forgetGraphs(name);
-		}
-	}
-
-	/**
-	 * Returns the names of a graph. Personally don't know why a graph should
-	 * have more then 1 identifier. Anyway, this does not happen with Virtuoso
-	 * 
-	 * @return names
-	 */
-	@Override
-	public Set<UriRef> getNames(Graph graph) {
-		logger.debug("getNames(Graph {})", graph);
-		return Collections.singleton(new UriRef(((VirtuosoMGraph) graph)
-				.getName()));
-	}
-
-	/**
-	 * Returns the weight of this provider.
-	 * 
-	 */
-	@Override
-	public int getWeight() {
-		logger.debug("getWeight()");
-		/**
-		 * The weight
-		 */
-		return this.weight;
-	}
-
-	/**
-	 * Sets the weight
-	 * 
-	 * @param weight
-	 */
-	public void setWeight(int weight) {
-		logger.debug("setWeight(int {})", weight);
-		this.weight = weight;
-	}
-}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/.DS_Store
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/.DS_Store b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/.DS_Store
new file mode 100644
index 0000000..353ff0a
Binary files /dev/null and b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/.DS_Store differ

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/.DS_Store
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/.DS_Store b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/.DS_Store
new file mode 100644
index 0000000..88ced45
Binary files /dev/null and b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/.DS_Store differ

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ConnectionTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ConnectionTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ConnectionTest.java
new file mode 100644
index 0000000..6a1e4b5
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/ConnectionTest.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.rdf.virtuoso.storage;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import virtuoso.jdbc4.VirtuosoConnection;
+import virtuoso.jdbc4.VirtuosoException;
+
+/**
+ * Tests the connection to the Virtuoso DBMS
+ * 
+ * @author enridaga
+ */
+public class ConnectionTest {
+
+	static final Logger log = LoggerFactory.getLogger(ConnectionTest.class);
+
+	private static VirtuosoConnection connection = null;
+
+	@BeforeClass
+	public static void before() throws ClassNotFoundException, SQLException {
+		org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+		connection = TestUtils.getConnection();
+	}
+
+	@Test
+	public void testIsClosed() {
+		assertFalse(connection.isClosed());
+	}
+
+	@Test
+	public void testIsConnectionLost() {
+		assertFalse(connection.isConnectionLost(0));
+	}
+
+	@Test
+	public void testIsReadOnly() throws VirtuosoException {
+		assertFalse(connection.isReadOnly());
+	}
+
+	@Test
+	public void testConnection() {
+		log.info("testConnection()");
+		try {
+
+			Statement st = connection.createStatement();
+			log.debug("Populate graph <mytest>");
+			String[] queries = {
+					"sparql clear graph <mytest>",
+					"sparql insert into graph <mytest> { <xxx> <P01> \"test1\"@en }",
+					"sparql insert into graph <mytest> { <xxx> <P01> \"test2\"@it }",
+					"sparql PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> insert into graph <mytest> { <xxx> <P01> \"test3\"^^xsd:string }",
+					"sparql insert into graph <mytest> { <xxx> <P01> \"test4\" }",
+					"sparql insert into graph <mytest> { <xxx> <P01> \"test5\" . <xxx> <P02> _:b1"
+							+ " . _:b1 <P03> <yyy> " + " . _:b3 <P05> <zzz> "
+							+ " . _:b3 <P05> <ppp> " +
+							// This is to consider that we can force it
+							" .  <nodeID://b10005> <P05> <ooo> " + " }",
+					"sparql insert into graph <mytest> { <nodeID://b10005> <property> <nodeID://b10007>}",
+					"sparql insert into graph <mytest> { <enridaga> <property> \"Literal value\"^^<http://datatype#type>}",
+					"sparql insert into graph <mytest> { <nodeID://b10005> <property> <nodeID://b10007>}" };
+			for (String q : queries) {
+				log.debug("Querying: {}", q);
+				st.execute(q);
+			}
+
+			String query = "sparql SELECT * from <mytest> WHERE {?s ?p ?o}";
+			log.debug("Querying: {}", query);
+			ResultSet rs = st.executeQuery(query);
+			TestUtils.stamp(rs);
+		} catch (SQLException e) {
+			log.error("SQL ERROR: ", e);
+			assertTrue(false);
+		} catch (Exception e) {
+			log.error("SQL ERROR: ", e);
+			assertTrue(false);
+		}
+	}
+
+	@Test
+	public void test() throws ClassNotFoundException, SQLException {
+		DatabaseMetaData dm = connection.getMetaData();
+		log.debug("Username is {}", dm.getUserName());
+		Properties p = connection.getClientInfo();
+		if (p == null) {
+			log.warn("Client info is null...");
+		} else
+			for (Entry<Object, Object> e : p.entrySet()) {
+				log.info("Client info property: {} => {}", e.getKey(),
+						e.getValue());
+			}
+		String SQL = "SELECT DISTINCT id_to_iri(G) FROM DB.DBA.RDF_QUAD quad ";
+		VirtuosoConnection cn = TestUtils.getConnection();
+		long startAt = System.currentTimeMillis();
+		// get the list of quad using SQL
+		log.debug("Executing SQL: {}", SQL);
+		cn.createStatement().executeQuery(SQL);
+		long endAt = System.currentTimeMillis();
+		log.debug("Using SQL: {}ms", endAt - startAt);
+		SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {?S ?P ?O} }";
+		startAt = System.currentTimeMillis();
+		// get the list of quad using SQL+SPARQL
+		log.debug("Executing SQL: {}", SQL);
+		cn.createStatement().executeQuery(SQL);
+		endAt = System.currentTimeMillis();
+		log.debug("Using SQL+SPARQL: {}ms", endAt - startAt);
+	}
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/RdfIOTest.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/RdfIOTest.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/RdfIOTest.java
new file mode 100644
index 0000000..7221c9b
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/RdfIOTest.java
@@ -0,0 +1,352 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.rdf.virtuoso.storage;
+
+import java.io.IOException;
+import java.net.URL;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+
+import org.apache.clerezza.platform.config.SystemConfig;
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
+import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
+import org.apache.clerezza.rdf.virtuoso.storage.VirtuosoBNode;
+import org.apache.clerezza.rdf.virtuoso.storage.VirtuosoMGraph;
+import org.apache.clerezza.rdf.virtuoso.storage.access.DataAccess;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import virtuoso.jdbc4.VirtuosoException;
+
+public class RdfIOTest {
+	static VirtuosoMGraph mgraph = null;
+	static final String TEST_GRAPH_NAME = "RdfIOTest";
+	static final String XSD = "http://www.w3.org/2001/XMLSchema#";
+	static Logger log = LoggerFactory.getLogger(RdfIOTest.class);
+	static DataAccess da;
+
+	@BeforeClass
+	public static void assume(){
+		org.junit.Assume.assumeTrue(!TestUtils.SKIP);
+	}
+	
+	/**
+	 * Clean before any test
+	 * 
+	 * @throws ClassNotFoundException
+	 * @throws SQLException
+	 */
+	@Before
+	public void before() throws ClassNotFoundException, SQLException {
+		
+		da = TestUtils.getProvider().createDataAccess();
+		mgraph = new VirtuosoMGraph(TEST_GRAPH_NAME, da);
+		mgraph.clear();
+		log.debug("Clearing graph <{}>", TEST_GRAPH_NAME);
+	}
+
+	/**
+	 * Clean after a test
+	 * 
+	 * @throws VirtuosoException
+	 * @throws ClassNotFoundException
+	 * @throws SQLException
+	 */
+	@After
+	public void clear() throws VirtuosoException, ClassNotFoundException,
+			SQLException {
+		log.debug("Clearing graph <{}> of size {}", TEST_GRAPH_NAME,
+				mgraph.size());
+		// clear all resources
+		da.close();
+		
+		da = null;
+		mgraph = null;
+		
+		log.debug("Removing graph <{}>", TEST_GRAPH_NAME);
+		Statement st = TestUtils.getConnection().createStatement();
+		st.execute("SPARQL CLEAR GRAPH <" + TEST_GRAPH_NAME + ">");
+		st.close();
+	}
+
+	@Test
+	public void xsdString() throws ClassNotFoundException, SQLException {
+		log.info("Text an xsd:string");
+		TypedLiteral object = new TypedLiteralImpl("lorem ipsum", new UriRef(
+				XSD + "string"));
+		UriRef subject = new UriRef("urn:io-test:reto");
+		UriRef predicate = new UriRef("urn:io-test:hasText");
+
+		Triple read = writeAndRead(subject, predicate, object);
+		Assert.assertTrue(read
+				.equals(new TripleImpl(subject, predicate, object)));
+		Assert.assertTrue(read.getObject() instanceof TypedLiteral);
+		TypedLiteral l = (TypedLiteral) read.getObject();
+		Assert.assertEquals(l.getLexicalForm(), "lorem ipsum");
+		Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
+
+	}
+
+	@Test
+	public void longString() throws ClassNotFoundException, SQLException, InterruptedException {
+		log.info("Test a long xsd:string");
+		StringBuilder longStr = new StringBuilder();
+		int c = 250000;
+		while (c > 0) {
+			longStr.append(" lorem ipsum ");
+			c--;
+		}
+		int size = longStr.length();
+		TypedLiteral object = new TypedLiteralImpl(longStr.toString(),
+				new UriRef(XSD + "string"));
+		UriRef subject = new UriRef("urn:io-test:reto");
+		UriRef predicate = new UriRef("urn:io-test:hasText");
+		Triple read = writeAndRead(subject, predicate, object);
+		Assert.assertTrue(read
+				.equals(new TripleImpl(subject, predicate, object)));
+		Assert.assertTrue(read.getObject() instanceof TypedLiteral);
+		TypedLiteral l = (TypedLiteral) read.getObject();
+		Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
+		Assert.assertTrue(l.getLexicalForm().length() == size);
+	}
+
+	private Triple writeAndRead(NonLiteral subject, UriRef predicate,
+			Resource object) throws ClassNotFoundException, SQLException {
+		Triple t = new TripleImpl(subject, predicate, object);
+		mgraph.add(t);
+		Triple read = mgraph.getGraph().iterator().next();
+		return read;
+	}
+
+	@Test
+	public void subjectAsUriTest() throws ClassNotFoundException, SQLException {
+		log.info("Test subject as UriRef");
+
+		NonLiteral subject;
+		UriRef predicate = new UriRef("urn:io-test:predicate");
+		UriRef object = new UriRef("urn:io-test:object");
+
+		// subject may be UriRef
+		subject = new UriRef("urn:io-test:enridaga");
+		Triple read = writeAndRead(subject, predicate, object);
+		Assert.assertTrue(read.getSubject().equals(subject));
+		Assert.assertEquals(read.getSubject(), new UriRef(
+				"urn:io-test:enridaga"));
+		Assert.assertNotSame(read.getSubject(), new UriRef(
+				"urn:io-test:alexdma"));
+		Assert.assertEquals(read, new TripleImpl(subject, predicate, object));
+
+	}
+
+	@Test
+	public void subjectAsBnodeTest() throws ClassNotFoundException,
+			SQLException {
+		log.info("Test subject as BNode");
+
+		NonLiteral subject;
+		UriRef predicate = new UriRef("urn:io-test:predicate");
+		UriRef object = new UriRef("urn:io-test:object");
+
+		// subject may be BNode
+		subject = new BNode();
+		Triple read = writeAndRead(subject, predicate, object);
+		// bnodes cannot be equals!
+		Assert.assertFalse(read.getSubject().equals(subject));
+		Assert.assertTrue(read.getSubject() instanceof BNode);
+		Assert.assertNotSame(read.getSubject(), new UriRef(
+				"urn:io-test:enridaga"));
+		Assert.assertNotSame(read.getSubject(), new UriRef(
+				"urn:io-test:alexdma"));
+		// bnodes cannot be equals!
+		Assert.assertNotSame(read, new TripleImpl(subject, predicate, object));
+	}
+
+	@Test
+	public void objectAsUriTest() throws ClassNotFoundException, SQLException {
+		log.info("Test object as UriRef");
+
+		NonLiteral subject = new UriRef("urn:io-test:enridaga");
+		UriRef predicate = new UriRef("urn:io-test:predicate");
+		UriRef object = new UriRef("urn:io-test:object");
+
+		Triple read = writeAndRead(subject, predicate, object);
+		//
+		Assert.assertTrue(read.getObject().equals(object));
+		Assert.assertTrue(read.getObject() instanceof UriRef);
+		Assert.assertEquals(read.getObject(), new UriRef("urn:io-test:object"));
+		Assert.assertNotSame(read.getSubject(), new UriRef(
+				"urn:io-test:alexdma"));
+		Assert.assertEquals(read, new TripleImpl(subject, predicate, object));
+	}
+
+	@Test
+	public void objectAsBnodeTest() throws ClassNotFoundException, SQLException {
+		log.info("Test object as Bnode");
+
+		NonLiteral subject = new UriRef("urn:io-test:subject");
+		UriRef predicate = new UriRef("urn:io-test:predicate");
+		Resource object;
+
+		// subject may be BNode
+		object = new BNode();
+		Triple read = writeAndRead(subject, predicate, object);
+		// bnodes cannot be equals!
+		Assert.assertFalse(read.getObject().equals(object));
+		Assert.assertTrue(read.getSubject().equals(subject));
+		
+		Assert.assertTrue(read.getObject() instanceof BNode);
+		Assert.assertTrue(read.getObject() instanceof VirtuosoBNode);
+		
+		Assert.assertNotSame(read.getObject(), new UriRef(
+				"urn:io-test:enridaga"));
+		Assert.assertNotSame(read.getObject(),
+				new UriRef("urn:io-test:alexdma"));
+		// these bnodes cannot be equals!
+		Assert.assertNotSame(read, new TripleImpl(subject, predicate, object));
+	}
+
+	@Test
+	public void bnodesTest() throws ClassNotFoundException, SQLException {
+		log.info("Test iterations and filter with bnode");
+
+		NonLiteral s1 = new BNode();
+		NonLiteral s2 = new BNode();
+		NonLiteral s3 = new BNode();
+		NonLiteral s4 = new BNode();
+
+		UriRef p1 = new UriRef("p1");
+		UriRef p2 = new UriRef("p2");
+		UriRef p3 = new UriRef("p3");
+
+		mgraph.add(new TripleImpl(s1, p1, s2));
+		// Get the bnode of s1
+		Triple first = mgraph.filter(null, p1, null).next();
+
+		Assert.assertTrue(first.getSubject() instanceof VirtuosoBNode);
+		Assert.assertTrue(first.getObject() instanceof VirtuosoBNode);
+		
+		BNode s1intern = (BNode) first.getSubject();
+		BNode s2intern = (BNode) first.getObject();
+		
+		mgraph.add(new TripleImpl(s2intern, p1, s3));
+		Triple second = mgraph.filter(s2intern, p1, null).next();
+		Assert.assertTrue(second.getObject() instanceof VirtuosoBNode);
+		
+		mgraph.add(new TripleImpl(s1intern, p2, s4));
+		Triple third = mgraph.filter(s1intern, p2, null).next();
+		Assert.assertTrue(third.getObject() instanceof VirtuosoBNode);
+		BNode s4intern = (BNode) third.getObject();
+		
+		mgraph.add(new TripleImpl(s1intern, p2, s4intern));
+		mgraph.add(new TripleImpl(s4intern, p3, s1intern));
+
+		Iterator<Triple> all = mgraph.iterator();
+		while(all.hasNext()){
+			Triple l = all.next();
+			log.info("{} {} {}",new Object[]{ l.getSubject(), l.getPredicate(), l.getObject()});
+		}
+		Iterator<Triple> i = mgraph.filter(null, p2, null);
+		int n = 0;
+		while (i.hasNext()) {
+			n++;
+			Triple s1t = i.next();
+			Iterator<Triple> s1i = mgraph.filter(s1t.getSubject(), null, null);
+			boolean found = false;
+			while (s1i.hasNext()) {
+				Triple s1it = s1i.next();
+				found = true;
+				log.info("{} {}",s1it.getSubject(), s1t.getSubject());
+				Assert.assertTrue(s1it.getSubject().equals(s1t.getSubject()));
+				Assert.assertTrue(s1it.getPredicate().equals(p1)
+						|| s1it.getPredicate().equals(p2));
+
+			}
+			Assert.assertTrue(found);
+			Assert.assertTrue(s1t.getObject() instanceof VirtuosoBNode);
+			Assert.assertTrue(s1t.getSubject() instanceof VirtuosoBNode);
+			Iterator<Triple> s4i = mgraph.filter((NonLiteral) s1t.getObject(),
+					null, null);
+			log.info("s4 {} ",s1t.getObject());
+			while (s4i.hasNext()) {
+				Triple s4it = s4i.next();
+				log.info("{} {}",s4it.getSubject(), s1t.getObject());
+				Assert.assertTrue(s4it.getSubject().equals(s1t.getObject()));
+				Assert.assertTrue(s4it.getPredicate().equals(p3));
+			}
+		}
+		Assert.assertEquals(n, 1);
+
+	}
+
+	@Test
+	public void sysconfigTest(){
+		
+		SystemConfig sc = new SystemConfig();
+		MGraph systemGraph = mgraph;
+		URL config = sc.getClass().getResource(SystemConfig.CONFIG_FILE);
+        if (config == null) {
+            throw new RuntimeException("no config file found");
+        }
+        ParsingProvider parser = new JenaParserProvider();
+        try {
+            parser.parse(systemGraph, config.openStream(),
+                    SupportedFormat.RDF_XML, null);
+        } catch (IOException ex) {
+            log.warn("Cannot parse coniguration at URL: {}", config);
+            throw new RuntimeException(ex);
+        }
+	}
+	
+	@Test
+	public void testUnicodeChars() throws ClassNotFoundException, SQLException {
+		log.info("Text an xsd:string");
+		
+		String s = "lorem ipsum è é £ ò ç à ù β ät ü ä";
+		TypedLiteral object = new TypedLiteralImpl(s, new UriRef(
+				XSD + "string"));
+		UriRef subject = new UriRef("urn:io-test:reto");
+		UriRef predicate = new UriRef("urn:io-test:hasText");
+
+		Triple read = writeAndRead(subject, predicate, object);
+		log.info("o: {} :: {}", object, read.getObject());
+		Assert.assertTrue(read
+				.equals(new TripleImpl(subject, predicate, object)));
+		Assert.assertTrue(read.getObject() instanceof TypedLiteral);
+		TypedLiteral l = (TypedLiteral) read.getObject();
+		Assert.assertEquals(l.getLexicalForm(), s);
+		Assert.assertEquals(l.getDataType(), new UriRef(XSD + "string"));
+
+	}
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/25694741/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/TestUtils.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/TestUtils.java b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/TestUtils.java
new file mode 100644
index 0000000..e96d62b
--- /dev/null
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/test/java/org/apache/clerezza/rdf/virtuoso/storage/TestUtils.java
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.rdf.virtuoso.storage;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.virtuoso.storage.access.VirtuosoWeightedProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import virtuoso.jdbc4.VirtuosoConnection;
+import virtuoso.jdbc4.VirtuosoExtendedString;
+import virtuoso.jdbc4.VirtuosoRdfBox;
+import virtuoso.jdbc4.VirtuosoResultSet;
+
+/**
+ * Utilities for tests
+ * 
+ * @author enridaga
+ * 
+ */
+public class TestUtils {
+
+	public static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
+
+	private static VirtuosoWeightedProvider provider = null;
+	private static String jdbcConnectionString = null;
+	private static String jdbcUser = null;
+	private static String jdbcPassword = null;
+	private static String jdbcDriver = null;
+
+	static Logger log = LoggerFactory.getLogger(TestUtils.class);
+	public static boolean SKIP = false;
+	static {
+		String skipProperty = System.getProperty("virtuoso.test");
+		if (skipProperty == null) {
+			log.info("virtuoso.skip property is not set. We skip tests by default.");
+			SKIP = true;
+		} else
+			SKIP = !skipProperty.equals("true");
+	}
+
+	public static VirtuosoConnection getConnection() throws SQLException, ClassNotFoundException{
+		return getProvider().getConnection();
+	}
+	public static VirtuosoWeightedProvider getProvider()
+			throws ClassNotFoundException, SQLException {
+		if (provider == null) {
+			initProvider();
+		}
+		return provider;
+	}
+
+	private static void initProvider() throws ClassNotFoundException,
+			SQLException {
+		if (SKIP) {
+			log.warn("Skipping tests.");
+			return;
+		}
+		String host = System.getProperty("virtuoso.host");
+		String port = System.getProperty("virtuoso.port");
+		jdbcUser = System.getProperty("virtuoso.user");
+		jdbcPassword = System.getProperty("virtuoso.password");
+		jdbcDriver = System.getProperty("virtuoso.driver");
+		if (host == null) {
+			host = "localhost";
+			log.info("Missing param 'host', setting to default: {}", host);
+		}
+		if (port == null) {
+			port = "1111";
+			log.info("Missing param 'port', setting to default: {}", port);
+		}
+		if (jdbcUser == null) {
+			jdbcUser = "dba";
+			log.info("Missing param 'user', setting to default: {}", jdbcUser);
+		}
+		if (jdbcPassword == null) {
+			jdbcPassword = "dba";
+			log.info("Missing param 'password', setting to default: {}",
+					jdbcPassword);
+		}
+		if (jdbcDriver == null) {
+			jdbcDriver = "virtuoso.jdbc4.Driver";
+			log.info("Missing param 'password', setting to default: {}",
+					jdbcDriver);
+		}
+
+		StringBuilder cb = new StringBuilder();
+		cb.append("jdbc:virtuoso://");
+		cb.append(host);
+		cb.append(":");
+		cb.append(port).append("/CHARSET=UTF-8");
+		jdbcConnectionString = cb.toString();
+//		Class.forName(VirtuosoWeightedProvider.DRIVER);
+		log.info("Create provider");
+//		connection = (VirtuosoConnection) DriverManager.getConnection(
+//				jdbcConnectionString, jdbcUser, jdbcPassword);
+		provider = new VirtuosoWeightedProvider(jdbcConnectionString, jdbcUser, jdbcPassword);
+		log.debug("Connection URL: {}", jdbcConnectionString);
+		log.debug("Connection user: {}", jdbcUser);
+	}
+
+	public static void stamp(ResultSet rs) {
+		try {
+			ResultSetMetaData rsmd;
+
+			StringBuilder output = new StringBuilder();
+			output.append(System.getProperty("line.separator"));
+			output.append("------------------------------");
+			output.append(System.getProperty("line.separator"));
+			rsmd = rs.getMetaData();
+
+			while (rs.next()) {
+				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
+					String s = rs.getString(i);
+					Object o = ((VirtuosoResultSet) rs).getObject(i);
+					if (o instanceof VirtuosoExtendedString) {
+						// In case is IRI
+						VirtuosoExtendedString vs = (VirtuosoExtendedString) o;
+						if (vs.iriType == VirtuosoExtendedString.IRI
+								&& (vs.strType & 0x01) == 0x01) {
+							output.append(" <" + vs.str + "> ");
+							log.debug(" {} is IRI", vs.str);
+						} else if (vs.iriType == VirtuosoExtendedString.BNODE) {
+							log.debug(" {} is BNODE", vs.str);
+							output.append(" <" + vs.str + "> ");
+						} else {
+							// In case is untyped literal
+							log.debug(" {} is an untyped literal", vs.str);
+							output.append(vs.str);
+						}
+					} else if (o instanceof VirtuosoRdfBox) {
+						// In case is typed literal
+						VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
+						output.append(rb.rb_box + " lang=" + rb.getLang()
+								+ " type=" + rb.getType());
+						log.debug(" {} is an typed literal", rb.rb_box);
+
+					} else if (rs.wasNull()) {
+						log.debug(" NULL ");
+						output.append("NULL");
+					} else {
+						// Is simple untyped string
+						output.append(s);
+					}
+
+				}
+				output.append("\n");
+			}
+
+			output.append(System.getProperty("line.separator"));
+			output.append("------------------------------");
+			output.append(System.getProperty("line.separator"));
+			log.info(output.toString());
+		} catch (Exception e) {
+			log.error("ERROR", e);
+		}
+	}
+
+	public static void stamp(Triple next) {
+		log.info("> TRIPLE : "+next.getSubject().toString() + " "
+				+ next.getPredicate().toString() + " "
+				+ next.getObject().toString());
+	}
+
+}


Mime
View raw message