use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class RyaDirectExample method main.
public static void main(final String[] args) throws Exception {
final Configuration conf = getConf();
conf.set(PrecomputedJoinIndexerConfig.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.name());
conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
log.info("Creating the tables as root.");
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
log.info("Creating PCJ Tables");
createPCJ(conf);
log.info("Connecting to Indexing Sail Repository.");
final Sail extSail = RyaSailFactory.getInstance(conf);
repository = new SailRepository(extSail);
conn = repository.getConnection();
final long start = System.currentTimeMillis();
log.info("Running SPARQL Example: Add and Delete");
testAddAndDelete(conn);
log.info("Running SAIL/SPARQL Example: PCJ Search");
testPCJSearch(conn);
log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
testAddAndTemporalSearchWithPCJ(conn);
log.info("Running SAIL/SPARQL Example: Add and Free Text Search with PCJ");
testAddAndFreeTextSearchWithPCJ(conn);
// log.info("Running SPARQL Example: Add Point and Geo Search with PCJ");
// // testAddPointAndWithinSearchWithPCJ(conn);
// log.info("Running SPARQL Example: Temporal, Freetext, and Geo Search");
// testTemporalFreeGeoSearch(conn);
// log.info("Running SPARQL Example: Geo, Freetext, and PCJ Search");
// testGeoFreetextWithPCJSearch(conn);
log.info("Running SPARQL Example: Delete Temporal Data");
testDeleteTemporalData(conn);
log.info("Running SPARQL Example: Delete Free Text Data");
testDeleteFreeTextData(conn);
// log.info("Running SPARQL Example: Delete Geo Data");
// testDeleteGeoData(conn);
log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
} finally {
log.info("Shutting down");
closeQuietly(conn);
closeQuietly(repository);
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class InferenceExamples method main.
public static void main(final String[] args) throws Exception {
if (IS_DETAILED_LOGGING_ENABLED) {
setupLogging();
}
final Configuration conf = getConf();
conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
log.info("Connecting to Indexing Sail Repository.");
final Sail sail = RyaSailFactory.getInstance(conf);
repository = new SailRepository(sail);
conn = repository.getConnection();
final long start = System.currentTimeMillis();
testInfer(conn, sail);
testPropertyChainInference(conn, sail);
testPropertyChainInferenceAltRepresentation(conn, sail);
testSomeValuesFromInference(conn, sail);
testAllValuesFromInference(conn, sail);
testIntersectionOfInference(conn, sail);
testOneOfInference(conn, sail);
log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
} finally {
log.info("Shutting down");
closeQuietly(conn);
closeQuietly(repository);
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class SpinConstructRule method loadSpinRules.
/**
* Load a set of SPIN rules from a data store.
* @param conf Contains the connection information. Not null.
* @return A map of rule identifiers to rule objects.
* @throws ForwardChainException if connecting, querying for rules, or
* parsing rules fails.
*/
public static Ruleset loadSpinRules(RdfCloudTripleStoreConfiguration conf) throws ForwardChainException {
Preconditions.checkNotNull(conf);
Map<Resource, Rule> rules = new ConcurrentHashMap<>();
// Connect to Rya
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
repository = new SailRepository(RyaSailFactory.getInstance(conf));
} catch (Exception e) {
throw new ForwardChainException("Couldn't initialize SAIL from configuration", e);
}
// Load and parse the individual SPIN rules from the data store
String ruleQueryString = "SELECT ?type ?rule ?text WHERE {\n" + " ?type <" + SPIN.RULE_PROPERTY.stringValue() + "> ?rule .\n" + " {\n" + " ?rule a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + " ?rule <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + " } UNION {\n" + " ?rule a ?template .\n" + " ?template <" + SPIN.BODY_PROPERTY + ">? ?body .\n" + " ?body a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + " ?body <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + " }\n" + "}";
SPARQLParser parser = new SPARQLParser();
try {
conn = repository.getConnection();
TupleQuery ruleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, ruleQueryString);
ruleQuery.evaluate(new AbstractTupleQueryResultHandler() {
@Override
public void handleSolution(BindingSet bs) throws TupleQueryResultHandlerException {
// For each rule identifier found, instantiate a SpinRule
Value requiredType = bs.getValue("type");
Value ruleIdentifier = bs.getValue("rule");
Value ruleText = bs.getValue("text");
if (requiredType instanceof Resource && ruleIdentifier instanceof Resource && ruleText instanceof Literal) {
ParsedQuery parsedRule;
try {
parsedRule = parser.parseQuery(ruleText.stringValue(), null);
if (parsedRule instanceof ParsedGraphQuery) {
SpinConstructRule rule = new SpinConstructRule((Resource) requiredType, (Resource) ruleIdentifier, (ParsedGraphQuery) parsedRule);
if (rule.hasAnonymousConsequent()) {
logger.error("Skipping unsupported rule " + ruleIdentifier + " -- consequent refers to bnode, which is not" + " currently supported (creating new bnodes at each" + " application could lead to infinite recursion).");
} else {
rules.put((Resource) ruleIdentifier, rule);
}
}
} catch (Exception e) {
throw new TupleQueryResultHandlerException(e);
}
}
}
});
} catch (TupleQueryResultHandlerException | QueryEvaluationException | MalformedQueryException | RepositoryException e) {
throw new ForwardChainException("Couldn't retrieve SPIN rules", e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (RepositoryException e) {
logger.warn("Error closing repository connection", e);
}
}
if (repository.isInitialized()) {
try {
repository.shutDown();
} catch (RepositoryException e) {
logger.warn("Error shutting down repository", e);
}
}
}
return new Ruleset(rules.values());
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project RDFConverter by Mat-O-Lab.
the class RDFValidator method validateRDF.
protected static String validateRDF(String rdf, String shaclShapes) throws UnsupportedEncodingException {
String ret = null;
boolean bValid = true;
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try {
SailRepositoryConnection connection = sailRepository.getConnection();
connection.begin();
StringReader shaclRules = new StringReader(shaclShapes);
connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
connection.begin();
StringReader rdfData = new StringReader(rdf);
connection.add(rdfData, "", RDFFormat.TURTLE);
connection.commit();
} catch (Exception exception) {
Throwable cause = exception.getCause();
bValid = false;
if (cause instanceof ShaclSailValidationException) {
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
ret = baos.toString(String.valueOf(StandardCharsets.UTF_8));
}
if (cause == null) {
ret = "fail: " + exception.getMessage();
} else {
ret = "fail: " + ret;
}
}
if (bValid) {
ret = "VALID";
}
return ret;
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project RDFConverter by Mat-O-Lab.
the class ShaclSampleCode method main.
public static void main(String[] args) throws IOException {
boolean bValid = true;
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
// Logger root = (Logger) LoggerFactory.getLogger(ShaclSail.class.getName());
// root.setLevel(Level.INFO);
// shaclSail.setLogValidationPlans(true);
// shaclSail.setGlobalLogValidationExecution(true);
// shaclSail.setLogValidationViolations(true);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
connection.begin();
StringReader shaclRules = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix sh: <http://www.w3.org/ns/shacl#> .", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "ex:PersonShape", " a sh:NodeShape ;", " sh:targetClass foaf:Person ;", " sh:property ex:PersonShapeProperty .", "ex:PersonShapeProperty ", " sh:path foaf:age ;", " sh:datatype xsd:int ;", " sh:maxCount 1 ;", " sh:minCount 1 ."));
connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
connection.begin();
StringReader invalidSampleData = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "ex:peter a foaf:Person ;", " foaf:age 20, \"30\"^^xsd:int ."));
connection.add(invalidSampleData, "", RDFFormat.TURTLE);
try {
connection.commit();
} catch (Exception exception) {
Throwable cause = exception.getCause();
if (cause instanceof ShaclSailValidationException) {
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
// Rio.write(validationReportModel, System.out, RDFFormat.TURTLE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
System.err.println(baos.toString(StandardCharsets.UTF_8.toString()));
bValid = false;
}
if (!bValid) {
System.err.println("RDF NOT VALID !!! Throw Exception !!!");
}
throw exception;
}
if (bValid) {
System.out.println("RDF VALID !!!");
}
}
}
Aggregations