org.eclipse.rdf4j.sail.SailException Java Examples

The following examples show how to use org.eclipse.rdf4j.sail.SailException. 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: AccumuloIndexSet.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
    *
    * @param accCon
    *            - connection to a valid Accumulo instance
    * @param tablename
    *            - name of an existing PCJ table
    * @throws MalformedQueryException
    * @throws SailException
    * @throws QueryEvaluationException
    * @throws TableNotFoundException
    * @throws AccumuloSecurityException
    * @throws AccumuloException
    * @throws PCJStorageException
    */
public AccumuloIndexSet(final Configuration conf, final String tablename)
		throws MalformedQueryException, SailException,
                   QueryEvaluationException, TableNotFoundException, AccumuloException, AccumuloSecurityException, PCJStorageException {
       this.tablename = tablename;
	this.accCon = ConfigUtils.getConnector(conf);
	this.auths = getAuthorizations(conf);
       PcjMetadata meta = pcj.getPcjMetadata(accCon, tablename);
	final SPARQLParser sp = new SPARQLParser();
	final ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(meta.getSparql(), null);

	setProjectionExpr((Projection) pq.getTupleExpr());
	final Set<VariableOrder> orders = meta.getVarOrders();

	varOrder = Lists.newArrayList();
	for (final VariableOrder var : orders) {
		varOrder.add(var.toString());
	}
	setLocalityGroups(tablename, accCon, varOrder);
	this.setSupportedVariableOrderMap(varOrder);
}
 
Example #2
Source File: FileIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void writeStatement(CloseableIteration<? extends Statement, SailException> stIter, int tripleMarker,
		int quadMarker, DataOutputStream dataOut) throws IOException, SailException {
	try {
		while (stIter.hasNext()) {
			Statement st = stIter.next();
			Resource context = st.getContext();
			if (context == null) {
				dataOut.writeByte(tripleMarker);
			} else {
				dataOut.writeByte(quadMarker);
			}
			writeValue(st.getSubject(), dataOut);
			writeValue(st.getPredicate(), dataOut);
			writeValue(st.getObject(), dataOut);
			if (context != null) {
				writeValue(context, dataOut);
			}
		}
	} finally {
		stIter.close();
	}
}
 
Example #3
Source File: MemoryBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Benchmark
public long singleTransactionGetFirstStatement() {

	MemoryStore memoryStore = new MemoryStore();
	memoryStore.initialize();

	try (NotifyingSailConnection connection = memoryStore.getConnection()) {
		connection.begin(IsolationLevels.valueOf(isolationLevel));

		statementList.forEach(
				st -> connection.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext()));

		long count = 0;
		for (int i = 0; i < 10; i++) {
			try (CloseableIteration<? extends Statement, SailException> statements = connection.getStatements(null,
					null, null, false)) {
				count += statements.next().toString().length();
			}
		}

		connection.commit();

		return count;
	}

}
 
Example #4
Source File: TripleStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void checkVersion() throws SailException {
	// Check version number
	String versionStr = properties.getProperty(VERSION_KEY);
	if (versionStr == null) {
		logger.warn("{} missing in TripleStore's properties file", VERSION_KEY);
	} else {
		try {
			int version = Integer.parseInt(versionStr);
			if (version < 10) {
				throw new SailException("Directory contains incompatible triple data");
			} else if (version > SCHEME_VERSION) {
				throw new SailException("Directory contains data that uses a newer data format");
			}
		} catch (NumberFormatException e) {
			logger.warn("Malformed version number in TripleStore's properties file");
		}
	}
}
 
Example #5
Source File: AbstractSail.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public SailConnection getConnection() throws SailException {
	if (!isInitialized()) {
		init();
	}
	initializationLock.readLock().lock();
	try {
		SailConnection connection = getConnectionInternal();

		Throwable stackTrace = debugEnabled() ? new Throwable() : null;
		synchronized (activeConnections) {
			activeConnections.put(connection, stackTrace);
		}

		return connection;
	} finally {
		initializationLock.readLock().unlock();
	}
}
 
Example #6
Source File: HBaseSail.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize() throws SailException { //initialize the SAIL
    try {
    	    //get or create and get the HBase table
        table = HalyardTableUtils.getTable(config, tableName, create, splitBits);

        //Iterate over statements relating to namespaces and add them to the namespace map.
        try (CloseableIteration<? extends Statement, SailException> nsIter = getStatements(null, HALYARD.NAMESPACE_PREFIX_PROPERTY, null, true)) {
            while (nsIter.hasNext()) {
                Statement st = nsIter.next();
                if (st.getObject() instanceof Literal) {
                    String prefix = st.getObject().stringValue();
                    String name = st.getSubject().stringValue();
                    namespaces.put(prefix, new SimpleNamespace(prefix, name));
                }
            }
        }
    } catch (IOException ex) {
        throw new SailException(ex);
    }
}
 
Example #7
Source File: AbstractSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public final void endUpdate(UpdateContext op) throws SailException {
	connectionLock.readLock().lock();
	try {
		verifyIsOpen();

		updateLock.lock();
		try {
			verifyIsActive();
			endUpdateInternal(op);
		} finally {
			updateLock.unlock();
		}
	} finally {
		connectionLock.readLock().unlock();
		if (op != null) {
			flush();
		}
	}
}
 
Example #8
Source File: SailSourceConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean removeInferredStatement(Resource subj, IRI pred, Value obj, Resource... contexts)
		throws SailException {
	verifyIsOpen();
	verifyIsActive();
	synchronized (datasets) {
		IsolationLevel level = getIsolationLevel();
		if (inferredOnlySink == null) {
			SailSource branch = branch(IncludeInferred.inferredOnly);
			inferredOnlyDataset = branch.dataset(level);
			inferredOnlySink = branch.sink(level);
			explicitOnlyDataset = branch(IncludeInferred.explicitOnly).dataset(level);
		}
		removeStatementsInternal(subj, pred, obj, contexts);
		return remove(subj, pred, obj, inferredOnlyDataset, inferredOnlySink, contexts);
	}
}
 
Example #9
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int applyRuleRdfs4b() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> iter = newThisIteration.getStatements(null, null, null);

	for (Statement st : iter) {
		Value uuu = st.getObject();
		if (uuu instanceof Resource) {
			boolean added = addInferredStatement((Resource) uuu, RDF.TYPE, RDFS.RESOURCE);
			if (added) {
				nofInferred++;
			}
		}
	}

	return nofInferred;
}
 
Example #10
Source File: VisulizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = SailException.class)
public void minCount() throws Exception {

	ShaclSail shaclSail = Utils.getInitializedShaclSail("shacl.ttl");

	try (NotifyingSailConnection connection = shaclSail.getConnection()) {
		SimpleValueFactory vf = SimpleValueFactory.getInstance();
		connection.begin();
		BNode bNode = vf.createBNode();
		connection.addStatement(bNode, RDF.TYPE, RDFS.RESOURCE);
		connection.addStatement(bNode, RDFS.LABEL, vf.createLiteral(""));
		connection.commit();

		shaclSail.setLogValidationPlans(true);

		connection.begin();
		BNode bNode2 = vf.createBNode();
		connection.addStatement(bNode2, RDF.TYPE, RDFS.RESOURCE);
		connection.removeStatement(null, bNode, RDFS.LABEL, vf.createLiteral(""));

		connection.commit();
	}

}
 
Example #11
Source File: AbstractSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public final void removeNamespace(String prefix) throws SailException {
	if (prefix == null) {
		throw new NullPointerException("prefix must not be null");
	}
	connectionLock.readLock().lock();
	try {
		verifyIsOpen();

		updateLock.lock();
		try {
			verifyIsActive();
			removeNamespaceInternal(prefix);
		} finally {
			updateLock.unlock();
		}
	} finally {
		connectionLock.readLock().unlock();
	}
}
 
Example #12
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void doInferencing() throws SailException {
	// All rules need to be checked:
	for (int i = 0; i < RDFSRules.RULECOUNT; i++) {
		ruleCount[i] = 0;
		checkRuleNextIter[i] = true;
	}

	super.doInferencing();

	// Print some statistics
	logger.debug("---RdfMTInferencer statistics:---");
	logger.debug("total statements inferred = " + totalInferred);
	for (int i = 0; i < RDFSRules.RULECOUNT; i++) {
		logger.debug("rule " + RDFSRules.RULENAMES[i] + ":\t#inferred=" + ruleCount[i]);
	}
	logger.debug("---end of statistics:---");
}
 
Example #13
Source File: FedX.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void shutDown() throws SailException {
	try {
	    log.info("Shutting down federation and all underlying repositories ...");
	    
	    scheduler.shutdown();
	    scheduler = null;
        // Abort all running queries
        shutDownInternal();
        cache.persist();
        
        monitoring = null;
        
	} catch (FedXException e) {
		throw new SailException(e);
	}		
}
 
Example #14
Source File: MemoryStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Synchronizes the contents of this repository with the data that is stored on disk. Data will only be written when
 * the contents of the repository and data in the file are out of sync.
 */
public void sync() throws SailException {
	// syncSemaphore prevents concurrent file synchronizations
	synchronized (syncSemaphore) {
		if (persist && contentsChanged) {
			logger.debug("syncing data to file...");
			try {
				IsolationLevels level = IsolationLevels.SNAPSHOT;
				try (SailDataset explicit = store.getExplicitSailSource().dataset(level);
						SailDataset inferred = store.getInferredSailSource().dataset(level)) {
					new FileIO(store.getValueFactory()).write(explicit, inferred, syncFile, dataFile);
				}
				contentsChanged = false;
				logger.debug("Data synced to file");
			} catch (IOException e) {
				logger.error("Failed to sync to file", e);
				throw new SailException(e);
			}
		}
	}
}
 
Example #15
Source File: AbstractSearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * To be removed, prefer {@link #evaluate(SearchQueryEvaluator)}.
 */
@Override
@Deprecated
public Collection<BindingSet> evaluate(QuerySpec query) throws SailException {
	Iterable<? extends DocumentScore> result = evaluateQuery(query);
	return generateBindingSets(query, result);
}
 
Example #16
Source File: MemoryStoreConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void commitInternal() throws SailException {
	super.commitInternal();

	sail.notifySailChanged(sailChangedEvent);
	sail.scheduleSyncTask();

	// create a fresh event object.
	sailChangedEvent = new DefaultSailChangedEvent(sail);
}
 
Example #17
Source File: AbstractSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * If there are no open operations.
 *
 * @throws SailException
 */
synchronized private void flushPendingUpdates() throws SailException {
	if (!isActiveOperation()
			|| isActive() && !getTransactionIsolation().isCompatibleWith(IsolationLevels.SNAPSHOT_READ)) {
		flush();
		pendingAdds = false;
		pendingRemovals = false;
	}
}
 
Example #18
Source File: FedXConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void clearInternal(Resource... contexts) throws SailException {
	try {
		getWriteStrategyInternal().clear(contexts);
	} catch (RepositoryException e) {
		throw new SailException(e);
	}
}
 
Example #19
Source File: ForwardChainingRDFSInferencerConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int applyRuleRdfs9_1() throws SailException {
	int nofInferred = 0;

	Iterable<Statement> ntIter = newThisIteration.getStatements(null, RDFS.SUBCLASSOF, null);

	for (Statement nt : ntIter) {
		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (yyy instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDF.TYPE, xxx, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource aaa = t1.getSubject();

				boolean added = addInferredStatement(aaa, RDF.TYPE, yyy);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example #20
Source File: InferencerConnectionWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calls {@link #flushUpdates()} before forwarding the call to the wrapped connection.
 */
@Override
public CloseableIteration<? extends Statement, SailException> getStatements(Resource subj, IRI pred, Value obj,
		boolean includeInferred, Resource... contexts) throws SailException {
	flushUpdates();
	return super.getStatements(subj, pred, obj, includeInferred, contexts);
}
 
Example #21
Source File: AbstractSailTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setUp() throws Exception {

	subject = new AbstractSail() {

		@Override
		public boolean isWritable() throws SailException {
			return false;
		}

		@Override
		public ValueFactory getValueFactory() {
			return SimpleValueFactory.getInstance();
		}

		@Override
		protected void shutDownInternal() throws SailException {
			// TODO Auto-generated method stub

		}

		@Override
		protected SailConnection getConnectionInternal() throws SailException {
			SailConnection connDouble = mock(SailConnection.class);
			doAnswer(f -> {
				subject.connectionClosed(connDouble);
				return null;
			}).when(connDouble).close();
			return connDouble;
		}

	};
}
 
Example #22
Source File: NativeStoreConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void commitInternal() throws SailException {
	try {
		super.commitInternal();
	} finally {
		if (txnLock != null) {
			txnLock.release();
		}
	}

	nativeStore.notifySailChanged(sailChangedEvent);

	// create a fresh event object.
	sailChangedEvent = new DefaultSailChangedEvent(nativeStore);
}
 
Example #23
Source File: FedXConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void rollbackInternal() throws SailException {
	try {
		getWriteStrategyInternal().rollback();
	} catch (RepositoryException e) {
		throw new SailException(e);
	}
}
 
Example #24
Source File: SailRepository.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public SailRepositoryConnection getConnection() throws RepositoryException {
	if (!isInitialized()) {
		init();
	}
	try {
		return new SailRepositoryConnection(this, sail.getConnection());
	} catch (SailException e) {
		throw new RepositoryException(e);
	}
}
 
Example #25
Source File: SemagrowSailBooleanQuery.java    From semagrow with Apache License 2.0 5 votes vote down vote up
public boolean evaluate() throws QueryEvaluationException {

        ParsedBooleanQuery parsedBooleanQuery = getParsedQuery();
        TupleExpr tupleExpr = parsedBooleanQuery.getTupleExpr();
        Dataset dataset = getDataset();
        if (dataset == null) {
            // No external dataset specified, use query's own dataset (if any)
            dataset = parsedBooleanQuery.getDataset();
        }

        try {
            SemagrowSailConnection sailCon = (SemagrowSailConnection) getConnection().getSailConnection();

            CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter;
            bindingsIter = sailCon.evaluate(tupleExpr, dataset, getBindings(), getIncludeInferred(), false,
                    getIncludedSources(), getExcludedSources());

            bindingsIter = enforceMaxQueryTime(bindingsIter);

            try {
                return bindingsIter.hasNext();
            }
            finally {
                bindingsIter.close();
            }
        }
        catch (SailException e) {
            throw new QueryEvaluationException(e.getMessage(), e);
        }
    }
 
Example #26
Source File: FileIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeStatements(final SailDataset explicit, SailDataset inferred, DataOutputStream dataOut)
		throws IOException, SailException {
	// write explicit only statements
	writeStatement(explicit.getStatements(null, null, null), EXPL_TRIPLE_MARKER, EXPL_QUAD_MARKER, dataOut);
	// write inferred only statements
	writeStatement(inferred.getStatements(null, null, null), INF_TRIPLE_MARKER, INF_QUAD_MARKER, dataOut);
}
 
Example #27
Source File: CustomFederationConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void assertHasStatement(String message, Resource subject, URI predicate, Value object,
		SailConnection connection) throws SailException {
	try (CloseableIteration<? extends Statement, SailException> statements = connection.getStatements(subject,
			(IRI) predicate, object, true)) {
		assertTrue(message, statements.hasNext());
	}
}
 
Example #28
Source File: QuerySpecBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a set of QuerySpecs embodying all necessary information to perform the Lucene query embedded in a
 * TupleExpr. To be removed, prefer {@link #process(TupleExpr, BindingSet, Collection<SearchQueryEvaluator>)}.
 */
@SuppressWarnings("unchecked")
@Deprecated
public Set<QuerySpec> process(TupleExpr tupleExpr, BindingSet bindings) throws SailException {
	HashSet<QuerySpec> result = new HashSet<>();
	process(tupleExpr, bindings, (Collection<SearchQueryEvaluator>) (Collection<?>) result);
	return result;
}
 
Example #29
Source File: MongoBatchUpdatePCJ.java    From rya with Apache License 2.0 5 votes vote down vote up
private Sail connectToRya(final String ryaInstanceName) throws RyaClientException {
    try {
        final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName);
        return RyaSailFactory.getInstance(ryaConf);
    } catch (SailException | AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
        throw new RyaClientException("Could not connect to the Rya instance named '" + ryaInstanceName + "'.", e);
    }
}
 
Example #30
Source File: HBaseSail.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean hasNext() throws SailException {
    if (evaluationTimeout > 0 && System.currentTimeMillis() > endTime) {
        throw new SailException("Statements scanning exceeded specified timeout " + evaluationTimeout + "s");
    }
    if (next == null) try { //try and find the next result
        while (true) {
            if (iter == null) {
                Result res = nextResult();
                if (res == null) {
                    return false; //no more Results
                } else {
                    iter = HalyardTableUtils.parseStatements(res, getValueFactory()).iterator();
                }
            }
            while (iter.hasNext()) {
                Statement s = iter.next();
                if ((subj == null || subj.equals(s.getSubject())) && (pred == null || pred.equals(s.getPredicate())) && (obj == null || obj.equals(s.getObject()))) {
                    next = s;  //cache the next statement which will be returned with a call to next().
                    return true; //there is another statement
                }
            }
            iter = null;
        }
    } catch (IOException e) {
        throw new SailException(e);
    } else {
        return true;
    }
}