use of org.openrdf.model.URI in project incubator-rya by apache.
the class AccumuloSelectivityEvalDAO method getCardinality.
// obtains cardinality for StatementPattern. Returns cardinality of 0
// if no instances of constants occur in table.
// assumes composite cardinalities will be used.
@Override
public long getCardinality(RdfCloudTripleStoreConfiguration conf, StatementPattern sp) throws TableNotFoundException {
Var subjectVar = sp.getSubjectVar();
Resource subj = (Resource) getConstantValue(subjectVar);
Var predicateVar = sp.getPredicateVar();
URI pred = (URI) getConstantValue(predicateVar);
Var objectVar = sp.getObjectVar();
org.openrdf.model.Value obj = getConstantValue(objectVar);
Resource context = (Resource) getConstantValue(sp.getContextVar());
/**
* We put full triple scans before rdf:type because more often than not the triple scan is being joined with something else that is better than asking the
* full rdf:type of everything.
*/
double cardinality = 0;
try {
cardinality = 2 * getTableSize(conf);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
if (subj != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
CARDINALITY_OF card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECT;
values.add(subj);
if (pred != null) {
values.add(pred);
card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECTPREDICATE;
} else if (obj != null) {
values.add(obj);
card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECTOBJECT;
}
double evalCard = this.getCardinality(conf, card, values, context);
// the index does not exist)
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else if (pred != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
CARDINALITY_OF card = RdfEvalStatsDAO.CARDINALITY_OF.PREDICATE;
values.add(pred);
if (obj != null) {
values.add(obj);
card = RdfEvalStatsDAO.CARDINALITY_OF.PREDICATEOBJECT;
}
double evalCard = this.getCardinality(conf, card, values, context);
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else if (obj != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
values.add(obj);
double evalCard = this.getCardinality(conf, RdfEvalStatsDAO.CARDINALITY_OF.OBJECT, values, context);
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else {
cardinality = getTableSize(conf);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
// TODO is this okay?
return (long) cardinality;
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class Fact method explain.
/**
* Recursively generate a String to show this fact's derivation.
*/
String explain(boolean multiline, String prefix, Schema schema) {
StringBuilder sb = new StringBuilder();
String sep = " ";
if (multiline) {
sep = "\n" + prefix;
}
if (triple == null) {
sb.append("(empty)").append(sep);
} else {
Resource s = getSubject();
URI p = getPredicate();
Value o = getObject();
sb.append("<").append(s.toString()).append(">").append(sep);
sb.append("<").append(p.toString()).append(">").append(sep);
sb.append("<").append(o.toString()).append(">");
// Restrictions warrant further explanation
if (schema != null && p.equals(RDF.TYPE)) {
Resource objClass = (Resource) o;
if (schema.hasRestriction(objClass)) {
sb.append(" { ");
sb.append(schema.explainRestriction(objClass));
sb.append(" }");
}
}
sb.append(sep);
}
if (isInference()) {
sb.append(derivation.explain(multiline, prefix, schema));
} else {
sb.append("[input]");
}
return sb.toString();
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class Schema method explainRestriction.
/**
* Assuming a given resource corresponds to a property restriction,
* describe the restriction.
*/
public String explainRestriction(Resource type) {
StringBuilder sb = new StringBuilder();
if (classes.containsKey(type)) {
OwlClass pr = classes.get(type);
sb.append("owl:Restriction");
for (URI p : pr.getOnProperty()) {
sb.append(" (owl:onProperty ").append(p.toString()).append(")");
}
for (Value v : pr.hasValue()) {
sb.append(" (owl:hasValue ").append(v.toString()).append(")");
}
for (Resource c : pr.someValuesFrom()) {
sb.append(" (owl:someValuesFrom ").append(c.toString()).append(")");
}
for (Resource c : pr.allValuesFrom()) {
sb.append(" (owl:allValuesFrom ").append(c.toString()).append(")");
}
int mc = pr.getMaxCardinality();
int mqc = pr.getMaxQualifiedCardinality();
if (mc >= 0) {
sb.append(" (owl:maxCardinality ").append(mc).append(")");
}
if (mqc >= 0) {
sb.append(" (owl:maxQualifiedCardinality ").append(mqc);
}
for (Resource c : pr.onClass()) {
sb.append(" owl:onClass ").append(c.toString()).append(")");
}
}
return sb.toString();
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class LocalReasonerTest method testMaxQCardinalityZeroThings.
/**
* cls-maxqc2
*/
@Test
public void testMaxQCardinalityZeroThings() throws Exception {
Resource r = TestUtils.bnode("restriction");
URI p = TestUtils.uri("impossiblePredicate2");
schema.processTriple(TestUtils.statement(r, OWL2.MAXQUALIFIEDCARDINALITY, TestUtils.intLiteral("0")));
schema.processTriple(TestUtils.statement(r, OWL.ONPROPERTY, p));
schema.processTriple(TestUtils.statement(r, OWL2.ONCLASS, OWL.THING));
reasoner.processFact(TestUtils.fact(TestUtils.NODE, p, TestUtils.uri("y")));
reasoner.processFact(TestUtils.fact(TestUtils.NODE, RDF.TYPE, r));
Assert.assertTrue("If p has maxQualifiedCardinality of 0 on owl:Thing;" + " then any (x p y) should be found inconsistent", reasoner.hasInconsistencies());
}
use of org.openrdf.model.URI in project incubator-rya by apache.
the class SchemaTest method testInputMaxQualifiedCardinality.
@Test
public void testInputMaxQualifiedCardinality() throws Exception {
Schema schema = new Schema();
URI s = TestUtils.uri("x");
URI p = OWL2.MAXQUALIFIEDCARDINALITY;
schema.processTriple(TestUtils.statement(s, p, TestUtils.stringLiteral("-20")));
schema.processTriple(TestUtils.statement(s, p, TestUtils.stringLiteral("100")));
schema.processTriple(TestUtils.statement(s, p, TestUtils.stringLiteral("0")));
schema.processTriple(TestUtils.statement(s, p, TestUtils.stringLiteral("42")));
Assert.assertEquals("Incorrect value returned for maxQualifiedCardinality", 0, schema.getClass(s).getMaxQualifiedCardinality());
}
Aggregations