use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserResource method deleteRole.
/**
* Deletes a named user
*
* @param userName
*/
private void deleteRole(String roleName) {
RDFTerm roleResource = getNamedRole(roleName).getNode();
Iterator<Triple> roleTriples = systemGraph.filter((BlankNodeOrIRI) roleResource, null, null);
ArrayList<Triple> buffer = new ArrayList<Triple>();
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
while (roleTriples.hasNext()) {
Triple triple = roleTriples.next();
buffer.add(triple);
}
} finally {
readLock.unlock();
}
// is lock needed?
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
try {
systemGraph.removeAll(buffer);
} finally {
writeLock.unlock();
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserResource method changeLiteral.
/**
* Replaces/inserts literal value for predicate assumes there is only one
* triple for the given predicate new value is added before deleting old one
* in case user is modifying their own data in which case they need triples
* in place for rights etc.
*
* @param userNode node in systemGraph corresponding to the user to change
* @param predicate property of the triple to change
* @param newValue new value for given predicate
*/
private void changeLiteral(GraphNode userNode, IRI predicate, String newValue) {
Iterator<Triple> oldTriples = systemGraph.filter((BlankNodeOrIRI) userNode.getNode(), predicate, null);
ArrayList<Triple> oldBuffer = new ArrayList<Triple>();
RDFTerm oldObject = null;
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
while (oldTriples.hasNext()) {
Triple triple = oldTriples.next();
oldObject = triple.getObject();
oldBuffer.add(triple);
}
} finally {
readLock.unlock();
}
// filter appears to see plain literals and xsd:strings as differerent
// so not
// userNode.addPropertyValue(predicate, newValue);
Literal newObject = new PlainLiteralImpl(newValue);
userNode.addProperty(predicate, newObject);
if (newObject.equals(oldObject)) {
return;
}
systemGraph.removeAll(oldBuffer);
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class UserResource method remove.
/**
* Deletes a named user
*
* @param userName
*/
private void remove(String userName) {
RDFTerm userResource = getNamedUser(userName).getNode();
Iterator<Triple> userTriples = systemGraph.filter((BlankNodeOrIRI) userResource, null, null);
ArrayList<Triple> buffer = new ArrayList<Triple>();
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
while (userTriples.hasNext()) {
Triple triple = userTriples.next();
buffer.add(triple);
}
} finally {
readLock.unlock();
}
// ImmutableGraph context = getNamedUser(userName).getNodeContext();
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
try {
systemGraph.removeAll(buffer);
} finally {
writeLock.unlock();
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class OWLAPIToClerezzaConverterTest method testOWLOntologyToGraph.
@Test
public void testOWLOntologyToGraph() {
/*
* Transform the OWLOntology into a Clerezza Graph.
*/
Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
/*
* Print all the triples contained in the Clerezza Graph.
*/
Iterator<Triple> tripleIt = mGraph.iterator();
while (tripleIt.hasNext()) {
Triple triple = tripleIt.next();
log.info(triple.toString());
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class EnhancementStructureHelper method validateTopicAnnotation.
/**
* Checks if a fise:TopicAnnotation is valid as defined by
* <a herf="https://issues.apache.org/jira/browse/STANBOL-617">STANBOL-617</a>.
* NOTE that this also validates all fise:Enhancement related requirements by
* calling {@link #validateEnhancement(Graph, IRI, Map)}
* @param enhancements the enhancements graph
* @param topicAnnotation the topic annotation to validate
* @param expectedValues expected values (properties for the values are used as keys)
*/
public static void validateTopicAnnotation(Graph enhancements, IRI topicAnnotation, Map<IRI, RDFTerm> expectedValues) {
//validate the rdf:type
Iterator<Triple> rdfTypeIterator = enhancements.filter(topicAnnotation, RDF_TYPE, ENHANCER_TOPICANNOTATION);
assertTrue("Parsed Enhancement " + topicAnnotation + " is missing the fise:TopicAnnotation type ", rdfTypeIterator.hasNext());
//TopicAnnotations need to be linked to TextAnnotations describing the
//section of the text that has a specific Topic.
//If the topic is for the whole text the TextAnnotation will have no
//selected-text value
Iterator<Triple> relationToTextAnnotationIterator = enhancements.filter(topicAnnotation, DC_RELATION, null);
// check if the relation to the text annotation is set
assertTrue(relationToTextAnnotationIterator.hasNext());
while (relationToTextAnnotationIterator.hasNext()) {
// test if the referred annotations are text annotations
IRI referredTextAnnotation = (IRI) relationToTextAnnotationIterator.next().getObject();
assertTrue(enhancements.filter(referredTextAnnotation, RDF_TYPE, ENHANCER_TEXTANNOTATION).hasNext());
}
// test if an entity (the topic) is referred (NOTE: in contrast to
// fise:EntityAnnotations this property is NOT required - cardinality [0..*]
Iterator<Triple> entityReferenceIterator = enhancements.filter(topicAnnotation, ENHANCER_ENTITY_REFERENCE, null);
RDFTerm expectedReferencedEntity = expectedValues.get(ENHANCER_ENTITY_REFERENCE);
while (entityReferenceIterator.hasNext()) {
//check possible multiple references
RDFTerm entityReferenceResource = entityReferenceIterator.next().getObject();
// test if the reference is an URI
assertTrue("fise:entity-reference value MUST BE of URIs", entityReferenceResource instanceof IRI);
if (expectedReferencedEntity != null && expectedReferencedEntity.equals(entityReferenceResource)) {
//found
expectedReferencedEntity = null;
}
}
assertNull("EntityAnnotation " + topicAnnotation + "fise:entity-reference has not the expected value " + expectedReferencedEntity + "!", expectedReferencedEntity);
//test if the entity label is set (required)
Iterator<Triple> entityLabelIterator = enhancements.filter(topicAnnotation, ENHANCER_ENTITY_LABEL, null);
assertTrue(entityLabelIterator.hasNext());
RDFTerm expectedEntityLabel = expectedValues.get(ENHANCER_ENTITY_LABEL);
while (entityLabelIterator.hasNext()) {
RDFTerm entityLabelResource = entityLabelIterator.next().getObject();
assertTrue("fise:entity-label values MUST BE PlainLiterals (EntityAnnotation: " + topicAnnotation + ")!", entityLabelResource instanceof Literal);
if (expectedEntityLabel != null && expectedEntityLabel.equals(entityLabelResource)) {
expectedEntityLabel = null;
}
}
assertNull("The expected EntityLabel " + expectedEntityLabel + " was not found", expectedEntityLabel);
// test fise:entity-type(s). NOTE: this is not required - cardinality [0..*]
Iterator<Triple> entityTypeIterator = enhancements.filter(topicAnnotation, Properties.ENHANCER_ENTITY_TYPE, null);
RDFTerm expectedEntityType = expectedValues.get(Properties.ENHANCER_ENTITY_TYPE);
if (entityTypeIterator.hasNext()) {
RDFTerm entityTypeResource = entityTypeIterator.next().getObject();
assertTrue("fise:entity-type values MUST BE URIs", entityTypeResource instanceof IRI);
if (expectedEntityType != null && expectedEntityType.equals(entityTypeResource)) {
//found
expectedEntityType = null;
}
}
assertNull("The expected fise:entity-type value " + expectedEntityType + " was not found!", expectedEntityType);
//test all properties required by fise:Enhancement
validateEnhancement(enhancements, topicAnnotation, expectedValues);
}
Aggregations