Search in sources :

Example 21 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class ClerezzaBackend method listObjects.

@Override
public Collection<RDFTerm> listObjects(RDFTerm subject, RDFTerm property) {
    if (!(property instanceof IRI) || !(subject instanceof BlankNodeOrIRI)) {
        throw new IllegalArgumentException("Subject needs to be a URI or blank node, property a URI node");
    }
    Collection<RDFTerm> result = new ArrayList<RDFTerm>();
    Lock readLock = readLockGraph();
    try {
        Iterator<Triple> triples = graph.filter((BlankNodeOrIRI) subject, (IRI) property, null);
        while (triples.hasNext()) {
            result.add(triples.next().getObject());
        }
    } finally {
        if (readLock != null) {
            //will be null if #graph is a read-only graph instance
            readLock.unlock();
        }
    }
    return result;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Lock(java.util.concurrent.locks.Lock)

Example 22 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class IndexedGraphTest method createGraph.

private static void createGraph(Collection<Triple> tc, int triples, Long seed) {
    Random rnd = new Random();
    if (seed != null) {
        rnd.setSeed(seed);
    }
    LiteralFactory lf = LiteralFactory.getInstance();
    //randoms are in the range [0..3]
    //literal
    double l = 1.0;
    //int
    double i = l / 3;
    //double
    double d = l * 2 / 3;
    //bNode
    double b = 2.0;
    //create new bNode
    double nb = b - (l * 2 / 3);
    double random;
    BlankNodeOrIRI subject = null;
    IRI predicate = null;
    List<IRI> predicateList = new ArrayList<IRI>();
    predicateList.add(RDF.first);
    predicateList.add(RDF.rest);
    predicateList.add(RDF.type);
    predicateList.add(RDFS.label);
    predicateList.add(RDFS.comment);
    predicateList.add(RDFS.range);
    predicateList.add(RDFS.domain);
    predicateList.add(FOAF.name);
    predicateList.add(FOAF.nick);
    predicateList.add(FOAF.homepage);
    predicateList.add(FOAF.age);
    predicateList.add(FOAF.depiction);
    String URI_PREFIX = "http://www.test.org/bigGraph/ref";
    Language DE = new Language("de");
    Language EN = new Language("en");
    Iterator<IRI> predicates = predicateList.iterator();
    List<BlankNode> bNodes = new ArrayList<BlankNode>();
    bNodes.add(new BlankNode());
    for (int count = 0; tc.size() < triples; count++) {
        random = rnd.nextDouble() * 3;
        if (random >= 2.5 || count == 0) {
            if (random <= 2.75) {
                subject = new IRI(URI_PREFIX + count);
            } else {
                int rndIndex = (int) ((random - 2.75) * bNodes.size() / (3.0 - 2.75));
                subject = bNodes.get(rndIndex);
            }
        }
        if (random > 2.0 || count == 0) {
            if (!predicates.hasNext()) {
                Collections.shuffle(predicateList, rnd);
                predicates = predicateList.iterator();
            }
            predicate = predicates.next();
        }
        if (random <= l) {
            //literal
            if (random <= i) {
                tc.add(new TripleImpl(subject, predicate, lf.createTypedLiteral(count)));
            } else if (random <= d) {
                tc.add(new TripleImpl(subject, predicate, lf.createTypedLiteral(random)));
            } else {
                Literal text;
                if (random <= i) {
                    text = new PlainLiteralImpl("Literal for " + count);
                } else if (random <= d) {
                    text = new PlainLiteralImpl("An English literal for " + count, EN);
                } else {
                    text = new PlainLiteralImpl("Ein Deutsches Literal für " + count, DE);
                }
                tc.add(new TripleImpl(subject, predicate, text));
            }
        } else if (random <= b) {
            //bnode
            BlankNode bnode;
            if (random <= nb) {
                bnode = new BlankNode();
                bNodes.add(bnode);
            } else {
                //>nb <b
                int rndIndex = (int) ((random - nb) * bNodes.size() / (b - nb));
                bnode = bNodes.get(rndIndex);
            }
            tc.add(new TripleImpl(subject, predicate, bnode));
        } else {
            //IRI
            tc.add(new TripleImpl(subject, predicate, new IRI(URI_PREFIX + count * random)));
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) ArrayList(java.util.ArrayList) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Random(java.util.Random) Language(org.apache.clerezza.commons.rdf.Language) Literal(org.apache.clerezza.commons.rdf.Literal) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 23 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class IndexedGraphTest method bNodeConsitency.

@Test
public void bNodeConsitency() {
    Graph mGraph = getEmptyGraph();
    final BlankNode bNode = new BlankNode() {

        @Override
        public int hashCode() {
            return -1;
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof BlankNode;
        }
    };
    final BlankNode bNodeClone = new BlankNode() {

        @Override
        public int hashCode() {
            return -1;
        }

        @Override
        public boolean equals(Object o) {
            return o instanceof BlankNode;
        }
    };
    mGraph.add(new TripleImpl(bNode, uriRef1, uriRef2));
    mGraph.add(new TripleImpl(bNodeClone, uriRef2, uriRef3));
    BlankNodeOrIRI bNodeBack = mGraph.filter(null, uriRef1, uriRef2).next().getSubject();
    Assert.assertEquals("The bnode we get back is not equals to the one we added", bNode, bNodeBack);
    BlankNodeOrIRI bNodeBack2 = mGraph.filter(null, uriRef2, uriRef3).next().getSubject();
    Assert.assertEquals("The returnned bnodes are no longer equals", bNodeBack, bNodeBack2);
    Assert.assertTrue("Not finding a triple when searching with equal bNode", mGraph.filter(bNodeBack, uriRef2, null).hasNext());
}
Also used : SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Test(org.junit.Test) GraphTest(org.apache.clerezza.rdf.core.test.GraphTest)

Example 24 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class TripleMatcherGroupImpl method getMatchingSubjects.

@Override
public Set<IRI> getMatchingSubjects(ImmutableGraph g) {
    if (matchers.isEmpty()) {
        return new HashSet<IRI>();
    }
    // For all matchers, find the set of subjects that match
    // and compute the intersection of those sets
    Set<IRI> intersection = null;
    for (TripleMatcher m : matchers) {
        final Set<IRI> s = new HashSet<IRI>();
        final Iterator<Triple> it = g.iterator();
        while (it.hasNext()) {
            final Triple t = it.next();
            if (m.matches(t)) {
                final BlankNodeOrIRI n = t.getSubject();
                if (n instanceof IRI) {
                    s.add((IRI) n);
                } else {
                // TODO do we need to handle non-IRI subjects?
                }
            }
        }
        if (intersection == null) {
            intersection = s;
        } else {
            intersection.retainAll(s);
        }
    }
    return intersection;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) IRI(org.apache.clerezza.commons.rdf.IRI) TripleMatcher(org.apache.stanbol.enhancer.benchmark.TripleMatcher) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashSet(java.util.HashSet)

Example 25 with BlankNodeOrIRI

use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.

the class ListChain method activate.

@Override
protected void activate(ComponentContext ctx) throws ConfigurationException {
    super.activate(ctx);
    Object value = ctx.getProperties().get(PROPERTY_ENGINE_LIST);
    List<String> configuredChain = new ArrayList<String>();
    if (value instanceof String[]) {
        configuredChain.addAll(Arrays.asList((String[]) value));
    } else if (value instanceof List<?>) {
        for (Object o : (List<?>) value) {
            if (o != null) {
                configuredChain.add(o.toString());
            }
        }
    } else {
        throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The engines of a List Chain MUST BE configured as Array/List of " + "Strings (parsed: " + (value != null ? value.getClass() : "null") + ")");
    }
    Set<String> engineNames = new HashSet<String>(configuredChain.size());
    BlankNodeOrIRI last = null;
    Graph ep = new SimpleGraph();
    BlankNodeOrIRI epNode = createExecutionPlan(ep, getName(), getChainProperties());
    log.debug("Parse ListChain config:");
    for (String line : configuredChain) {
        try {
            Entry<String, Map<String, List<String>>> parsed = ConfigUtils.parseConfigEntry(line);
            if (!engineNames.add(parsed.getKey())) {
                throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The EnhancementEngine '" + parsed.getKey() + "' is mentioned" + "twice in the configured list!");
            }
            boolean optional = getState(parsed.getValue(), "optional");
            log.debug(" > Engine: {} ({})", parsed.getKey(), optional ? "optional" : "required");
            last = writeExecutionNode(ep, epNode, parsed.getKey(), optional, last == null ? null : Collections.singleton(last), getEnhancementProperties(parsed.getValue()));
        } catch (IllegalArgumentException e) {
            throw new ConfigurationException(PROPERTY_ENGINE_LIST, "Unable to parse Chain Configuraiton (message: '" + e.getMessage() + "')!", e);
        }
    }
    if (engineNames.isEmpty()) {
        throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The configured chain MUST at least contain a single valid entry!");
    }
    this.engineNames = Collections.unmodifiableSet(engineNames);
    this.executionPlan = ep.getImmutableGraph();
}
Also used : ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ConfigurationException(org.osgi.service.cm.ConfigurationException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)89 Triple (org.apache.clerezza.commons.rdf.Triple)52 IRI (org.apache.clerezza.commons.rdf.IRI)41 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)30 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)27 HashSet (java.util.HashSet)24 Graph (org.apache.clerezza.commons.rdf.Graph)22 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)14 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)14 Literal (org.apache.clerezza.commons.rdf.Literal)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Lock (java.util.concurrent.locks.Lock)10 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)10 EnhancementEngineHelper.getString (org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)7 Language (org.apache.clerezza.commons.rdf.Language)6 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)6