Search in sources :

Example 31 with URI

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;
}
Also used : Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) RdfDAOException(org.apache.rya.api.persist.RdfDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) Value(org.apache.accumulo.core.data.Value) ArrayList(java.util.ArrayList) List(java.util.List)

Example 32 with URI

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();
}
Also used : Resource(org.openrdf.model.Resource) Value(org.openrdf.model.Value) URI(org.openrdf.model.URI)

Example 33 with URI

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();
}
Also used : Value(org.openrdf.model.Value) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI)

Example 34 with URI

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());
}
Also used : Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 35 with URI

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());
}
Also used : URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

URI (org.openrdf.model.URI)368 Test (org.junit.Test)138 Value (org.openrdf.model.Value)122 Resource (org.openrdf.model.Resource)110 Statement (org.openrdf.model.Statement)96 ValueFactory (org.openrdf.model.ValueFactory)83 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)67 URIImpl (org.openrdf.model.impl.URIImpl)58 HashSet (java.util.HashSet)36 RyaURI (org.apache.rya.api.domain.RyaURI)34 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)34 Literal (org.openrdf.model.Literal)34 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)34 StatementImpl (org.openrdf.model.impl.StatementImpl)32 LiteralImpl (org.openrdf.model.impl.LiteralImpl)29 Var (org.openrdf.query.algebra.Var)28 ArrayList (java.util.ArrayList)25 SailConnection (org.openrdf.sail.SailConnection)25 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24