use of org.apache.rya.accumulo.documentIndex.TextColumn in project incubator-rya by apache.
the class StarQuery method nodeToTextColumn.
// assumes nodes forms valid star query with only one common variable
// assumes nodes and commonVar has been set
private TextColumn nodeToTextColumn(final StatementPattern node, final int i) throws RyaTypeResolverException {
final RyaContext rc = RyaContext.getInstance();
final Var subjVar = node.getSubjectVar();
final Var predVar = node.getPredicateVar();
final Var objVar = node.getObjectVar();
final RyaURI predURI = (RyaURI) RdfToRyaConversions.convertValue(node.getPredicateVar().getValue());
// assumes StatementPattern contains at least on variable
if (subjVar.isConstant()) {
if (commonVarConstant()) {
varPos.put(objVar.getName(), i);
return new TextColumn(new Text(predURI.getData()), new Text("object"));
} else {
return new TextColumn(new Text(predURI.getData()), new Text("subject" + "\u0000" + subjVar.getValue().stringValue()));
}
} else if (objVar.isConstant()) {
if (commonVarConstant()) {
varPos.put(subjVar.getName(), i);
return new TextColumn(new Text(predURI.getData()), new Text("subject"));
} else {
isCommonVarURI = true;
final RyaType objType = RdfToRyaConversions.convertValue(objVar.getValue());
final byte[][] b1 = rc.serializeType(objType);
final byte[] b2 = Bytes.concat("object".getBytes(StandardCharsets.UTF_8), "\u0000".getBytes(StandardCharsets.UTF_8), b1[0], b1[1]);
return new TextColumn(new Text(predURI.getData()), new Text(b2));
}
} else {
if (subjVar.getName().equals(commonVarName)) {
isCommonVarURI = true;
varPos.put(objVar.getName(), i);
final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("object"));
tc.setIsPrefix(true);
return tc;
} else {
varPos.put(subjVar.getName(), i);
final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("subject"));
tc.setIsPrefix(true);
return tc;
}
}
}
use of org.apache.rya.accumulo.documentIndex.TextColumn in project incubator-rya by apache.
the class StarQuery method toString.
// private static Set<String> getSpVariables(StatementPattern sp) {
//
// Set<String> variables = Sets.newHashSet();
// List<Var> varList = sp.getVarList();
//
// for(Var v: varList) {
// if(!v.isConstant()) {
// variables.add(v.getName());
// }
// }
//
// return variables;
//
// }
//
@Override
public String toString() {
String s = "Term conditions: " + "\n";
for (final TextColumn element : this.nodeColumnCond) {
s = s + element.toString() + "\n";
}
s = s + "Common Var: " + this.commonVar.toString() + "\n";
s = s + "Context: " + this.contextURI;
return s;
}
use of org.apache.rya.accumulo.documentIndex.TextColumn in project incubator-rya by apache.
the class StarQueryTest method testGetContrainedQuery.
@Test
public void testGetContrainedQuery() throws RyaTypeResolverException, MalformedQueryException {
String q1 = //
"" + //
"SELECT ?X ?Y1 ?Y2 " + //
"{" + //
"GRAPH <http://joe> { " + //
"?X <uri:cf1> ?Y1 ." + //
"?X <uri:cf2> ?Y2 ." + //
"?X <uri:cf3> ?Y3 ." + //
"}" + "}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = null;
pq1 = parser.parseQuery(q1, null);
TupleExpr te1 = pq1.getTupleExpr();
System.out.println(te1);
List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
StarQuery sq1 = new StarQuery(spList1);
QueryBindingSet bs1 = new QueryBindingSet();
QueryBindingSet bs2 = new QueryBindingSet();
Value v1 = vf.createURI("uri:hank");
Value v2 = vf.createURI("uri:bob");
bs1.addBinding("X", v1);
bs2.addBinding("X", v1);
bs2.addBinding("Y3", v2);
StarQuery sq2 = StarQuery.getConstrainedStarQuery(sq1, bs1);
StarQuery sq3 = StarQuery.getConstrainedStarQuery(sq1, bs2);
Assert.assertTrue(sq2.commonVarHasValue());
Assert.assertEquals(sq2.getCommonVarValue(), "uri:hank");
Assert.assertTrue(sq3.commonVarHasValue());
Assert.assertEquals(sq3.getCommonVarValue(), "uri:hank");
TextColumn[] tc1 = sq1.getColumnCond();
TextColumn[] tc2 = sq2.getColumnCond();
TextColumn[] tc3 = sq3.getColumnCond();
for (int i = 0; i < tc1.length; i++) {
Assert.assertTrue(tc1[i].equals(tc2[i]));
if (i != 2) {
Assert.assertTrue(tc1[i].equals(tc3[i]));
} else {
Assert.assertEquals(tc3[i].getColumnFamily(), new Text("uri:cf3"));
RyaType objType = RdfToRyaConversions.convertValue(v2);
byte[][] b1 = null;
b1 = RyaContext.getInstance().serializeType(objType);
byte[] b2 = Bytes.concat("object".getBytes(), "\u0000".getBytes(), b1[0], b1[1]);
Assert.assertEquals(tc3[i].getColumnQualifier(), new Text(b2));
Assert.assertTrue(!tc3[i].isPrefix());
}
}
}
use of org.apache.rya.accumulo.documentIndex.TextColumn in project incubator-rya by apache.
the class StarQueryTest method testBasicFunctionality.
@Test
public void testBasicFunctionality() throws MalformedQueryException {
String q1 = //
"" + //
"SELECT ?X ?Y1 ?Y2 " + //
"{" + //
"GRAPH <http://joe> { " + //
"?X <uri:cf1> ?Y1 ." + //
"?X <uri:cf2> ?Y2 ." + //
"?X <uri:cf3> ?Y3 ." + //
"}" + "}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = null;
pq1 = parser.parseQuery(q1, null);
TupleExpr te1 = pq1.getTupleExpr();
System.out.println(te1);
List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
Assert.assertTrue(StarQuery.isValidStarQuery(spList1));
StarQuery sq1 = new StarQuery(spList1);
Var v = sq1.getCommonVar();
Assert.assertEquals("X", v.getName());
Assert.assertEquals(null, v.getValue());
Assert.assertEquals(v.getValue(), sq1.getCommonVarValue());
Assert.assertTrue(!sq1.commonVarHasValue());
Assert.assertEquals("X", sq1.getCommonVarName());
Assert.assertTrue(sq1.isCommonVarURI());
Assert.assertTrue(sq1.hasContext());
Assert.assertEquals("http://joe", sq1.getContextURI());
TextColumn[] cond = sq1.getColumnCond();
for (int i = 0; i < cond.length; i++) {
Assert.assertEquals(cond[i].getColumnFamily().toString(), "uri:cf" + (i + 1));
Assert.assertEquals(cond[i].getColumnQualifier().toString(), "object");
}
Set<String> unCommonVars = Sets.newHashSet();
unCommonVars.add("Y1");
unCommonVars.add("Y2");
unCommonVars.add("Y3");
Assert.assertEquals(unCommonVars, sq1.getUnCommonVars());
Map<String, Integer> varPos = sq1.getVarPos();
Assert.assertEquals(0, varPos.get("Y1").intValue());
Assert.assertEquals(1, varPos.get("Y2").intValue());
Assert.assertEquals(2, varPos.get("Y3").intValue());
QueryBindingSet bs1 = new QueryBindingSet();
QueryBindingSet bs2 = new QueryBindingSet();
Value v1 = vf.createURI("uri:hank");
Value v2 = vf.createURI("uri:bob");
bs1.addBinding("X", v1);
bs2.addBinding("X", v1);
bs2.addBinding("Y3", v2);
Set<String> s1 = StarQuery.getCommonVars(sq1, bs1);
Set<String> s2 = StarQuery.getCommonVars(sq1, bs2);
Set<String> s3 = Sets.newHashSet();
Set<String> s4 = Sets.newHashSet();
s3.add("X");
s4.add("X");
s4.add("Y3");
Assert.assertEquals(s1, s3);
Assert.assertEquals(s2, s4);
}
Aggregations