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;
}
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)));
}
}
}
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());
}
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;
}
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();
}
Aggregations