use of org.openrdf.model.URI in project incubator-rya by apache.
the class MongoFreeTextIndexerIT method testSearch.
@Test
public void testSearch() throws Exception {
try (MongoFreeTextIndexer f = new MongoFreeTextIndexer()) {
f.setConf(conf);
f.init();
final ValueFactory vf = new ValueFactoryImpl();
final URI subject = new URIImpl("foo:subj");
final URI predicate = RDFS.LABEL;
final Value object = vf.createLiteral("this is a new hat");
final URI context = new URIImpl("foo:context");
final Statement statement = vf.createStatement(subject, predicate, object, context);
f.storeStatement(RdfToRyaConversions.convertStatement(statement));
f.flush();
assertEquals(Sets.newHashSet(), getSet(f.queryText("asdf", EMPTY_CONSTRAINTS)));
assertEquals(Sets.newHashSet(statement), getSet(f.queryText("new", EMPTY_CONSTRAINTS)));
assertEquals(Sets.newHashSet(statement), getSet(f.queryText("hat new", EMPTY_CONSTRAINTS)));
}
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class MongoFreeTextIndexerIT method testContextSearch.
@Test
public void testContextSearch() throws Exception {
try (MongoFreeTextIndexer f = new MongoFreeTextIndexer()) {
f.setConf(conf);
f.init();
final ValueFactory vf = new ValueFactoryImpl();
final URI subject = new URIImpl("foo:subj");
final URI predicate = new URIImpl(RDFS.COMMENT.toString());
final Value object = vf.createLiteral("this is a new hat");
final URI context = new URIImpl("foo:context");
final Statement statement = vf.createStatement(subject, predicate, object, context);
f.storeStatement(RdfToRyaConversions.convertStatement(statement));
f.flush();
assertEquals(Sets.newHashSet(statement), getSet(f.queryText("hat", EMPTY_CONSTRAINTS)));
assertEquals(Sets.newHashSet(statement), getSet(f.queryText("hat", new StatementConstraints().setContext(context))));
assertEquals(Sets.newHashSet(), getSet(f.queryText("hat", new StatementConstraints().setContext(vf.createURI("foo:context2")))));
}
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class RyaDirectExample method testAddAndFreeTextSearchWithPCJ.
private static void testAddAndFreeTextSearchWithPCJ(final SailRepositoryConnection conn) throws Exception {
// add data to the repository using the SailRepository add methods
final ValueFactory f = conn.getValueFactory();
final URI person = f.createURI("http://example.org/ontology/Person");
String uuid;
uuid = "urn:people:alice";
conn.add(f.createURI(uuid), RDF.TYPE, person);
conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
uuid = "urn:people:bobss";
conn.add(f.createURI(uuid), RDF.TYPE, person);
conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Bob Snob Hose", "en"));
String queryString;
TupleQuery tupleQuery;
CountingResultHandler tupleHandler;
// ///////////// search for alice
queryString = //
"PREFIX fts: <http://rdf.useekm.com/fts#> " + //
"SELECT ?person ?match ?e ?c ?l ?o " + //
"{" + //
" ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + //
" FILTER(fts:text(?match, \"pal*\")) " + //
"}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 1);
// ///////////// search for alice and bob
queryString = //
"PREFIX fts: <http://rdf.useekm.com/fts#> " + //
"SELECT ?person ?match " + //
"{" + //
" ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + //
" ?person a <http://example.org/ontology/Person> . " + //
" FILTER(fts:text(?match, \"(alice | bob) *SE\")) " + //
"}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 2);
// ///////////// search for alice and bob
queryString = //
"PREFIX fts: <http://rdf.useekm.com/fts#> " + //
"SELECT ?person ?match " + //
"{" + //
" ?person a <http://example.org/ontology/Person> . " + //
" ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + //
" FILTER(fts:text(?match, \"(alice | bob) *SE\")) " + //
" FILTER(fts:text(?match, \"pal*\")) " + //
"}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 1);
// ///////////// search for bob
queryString = //
"PREFIX fts: <http://rdf.useekm.com/fts#> " + //
"SELECT ?person ?match ?e ?c ?l ?o " + //
"{" + //
" ?e a ?c . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + //
" ?e <uri:talksTo> ?o . " + //
" ?person a <http://example.org/ontology/Person> . " + //
" ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . " + //
" FILTER(fts:text(?match, \"!alice & hose\")) " + //
"}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
tupleHandler = new CountingResultHandler();
tupleQuery.evaluate(tupleHandler);
log.info("Result count : " + tupleHandler.getCount());
Validate.isTrue(tupleHandler.getCount() == 1);
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class RyaDirectExample method createPCJ.
// private static void testDeleteGeoData(final SailRepositoryConnection conn)
// throws Exception {
// // Delete all stored points
// final String sparqlDelete = "PREFIX geo: <http://www.opengis.net/ont/geosparql#> "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> "//
// + "DELETE {\n" //
// + " ?feature a geo:Feature . "//
// + " ?feature geo:hasGeometry ?point . "//
// + " ?point a geo:Point . "//
// + " ?point geo:asWKT ?wkt . "//
// + "}\n" + "WHERE { \n" + " ?feature a geo:Feature . "//
// + " ?feature geo:hasGeometry ?point . "//
// + " ?point a geo:Point . "//
// + " ?point geo:asWKT ?wkt . "//
// + "}";//
//
// final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL,
// sparqlDelete);
// deleteUpdate.execute();
//
// String queryString;
// TupleQuery tupleQuery;
// CountingResultHandler tupleHandler;
//
// // Find all stored points
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#> "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> "//
// + "SELECT ?feature ?point ?wkt " //
// + "{" //
// + " ?feature a geo:Feature . "//
// + " ?feature geo:hasGeometry ?point . "//
// + " ?point a geo:Point . "//
// + " ?point geo:asWKT ?wkt . "//
// + "}";//
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 0);
// }
private static void createPCJ(final Configuration conf) throws RepositoryException, AccumuloException, AccumuloSecurityException, TableExistsException, PcjException, InferenceEngineException, NumberFormatException, UnknownHostException, SailException, TableNotFoundException {
final Configuration config = new AccumuloRdfConfiguration(conf);
config.set(ConfigUtils.USE_PCJ, "false");
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
final Sail extSail = RyaSailFactory.getInstance(config);
repository = new SailRepository(extSail);
conn = repository.getConnection();
final String queryString1 = //
"" + //
"SELECT ?e ?c ?l ?o " + //
"{" + //
" ?c a ?e . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + //
" ?e <uri:talksTo> ?o . " + //
"}";
final String queryString2 = //
"" + //
"SELECT ?e ?c ?l ?o " + //
"{" + //
" ?e a ?c . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + //
" ?e <uri:talksTo> ?o . " + //
"}";
URI obj, subclass, talksTo;
final URI person = new URIImpl("urn:people:alice");
final URI feature = new URIImpl("urn:feature");
final URI sub = new URIImpl("uri:entity");
subclass = new URIImpl("uri:class");
obj = new URIImpl("uri:obj");
talksTo = new URIImpl("uri:talksTo");
conn.add(person, RDF.TYPE, sub);
conn.add(feature, RDF.TYPE, sub);
conn.add(sub, RDF.TYPE, subclass);
conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
conn.add(sub, talksTo, obj);
final String tablename1 = RYA_TABLE_PREFIX + "INDEX_1";
final String tablename2 = RYA_TABLE_PREFIX + "INDEX_2";
final Connector accCon = new MockInstance(INSTANCE).getConnector("root", new PasswordToken("".getBytes(StandardCharsets.UTF_8)));
new PcjTables().createAndPopulatePcj(conn, accCon, tablename1, queryString1, new String[] { "e", "c", "l", "o" }, Optional.<PcjVarOrderFactory>absent());
new PcjTables().createAndPopulatePcj(conn, accCon, tablename2, queryString2, new String[] { "e", "c", "l", "o" }, Optional.<PcjVarOrderFactory>absent());
} catch (final RyaDAOException e) {
throw new Error("While creating PCJ tables.", e);
} finally {
closeQuietly(conn);
closeQuietly(repository);
}
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class LocalReasoner method relevantJoinRule.
/**
* Determine whether a fact is a triple which might be used in some join
* rule for its subject and/or object.
*/
public static Relevance relevantJoinRule(Fact fact, Schema schema) {
// contained in the schema object.
if (Schema.isSchemaTriple(fact.getTriple())) {
return Relevance.NONE;
}
// Otherwise, consider the semantics of the statement:
URI predURI = fact.getPredicate();
Value object = fact.getObject();
boolean relevantToSubject = false;
boolean relevantToObject = false;
// Literals don't get reasoners, so determine whether object is a uri:
boolean literalObject = object instanceof Literal;
// Type statements can be joined if...
if (predURI.equals(RDF.TYPE)) {
Resource typeURI = (Resource) fact.getObject();
if (schema.hasClass(typeURI)) {
OwlClass c = schema.getClass(typeURI);
// 1. the type is a property restriction
if (!c.getOnProperty().isEmpty() || // 2. the type is relevant to a property restriction
!c.getSvfRestrictions().isEmpty() || !c.getAvfRestrictions().isEmpty() || !c.getQCRestrictions().isEmpty() || // 3. the type has complementary/disjoint types
!c.getDisjointClasses().isEmpty() || !c.getComplementaryClasses().isEmpty()) {
relevantToSubject = true;
}
}
}
// If the schema knows about the property:
if (schema.hasProperty(predURI)) {
OwlProperty prop = schema.getProperty(predURI);
// transitivity: relevant to both
if (prop.isTransitive()) {
relevantToSubject = true;
relevantToObject = !literalObject;
} else {
// disjoint properties: relevant to subject
if (!prop.getDisjointProperties().isEmpty()) {
relevantToSubject = true;
}
// Property restrictions: possibly relevant to either
for (Resource rURI : prop.getRestrictions()) {
OwlClass r = schema.getClass(rURI);
// (if <subject type rURI>, infer object's type)
if (!r.allValuesFrom().isEmpty()) {
relevantToSubject = true;
}
// max cardinality requires a join on the subject
if (!literalObject && (r.getMaxCardinality() >= 0 || r.getMaxQualifiedCardinality() >= 0 || !r.someValuesFrom().isEmpty())) {
relevantToObject = true;
}
if (relevantToSubject && (relevantToObject || literalObject)) {
break;
}
}
}
}
return Relevance.get(relevantToSubject, relevantToObject);
}
Aggregations