use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class UserResource method deleteUser.
/**
* Endpoint-style user deletion takes a little bunch of Turtle describing
* the user to delete e.g. [] a foaf:Agent ; cz:userName "Hugo Ball" .
*
* @param userData
* @return HTTP/1.1 204 No Content
*/
@POST
@Consumes(SupportedFormat.TURTLE)
@Path("delete-user")
public Response deleteUser(ImmutableGraph inputGraph) {
Iterator<Triple> userNameTriples = inputGraph.filter(null, PLATFORM.userName, null);
Literal userNameNode = (Literal) userNameTriples.next().getObject();
// gives concurrent mod exception otherwise
ArrayList<Triple> tripleBuffer = new ArrayList<Triple>();
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
Iterator<Triple> userTriples = systemGraph.filter(null, null, userNameNode);
if (userTriples.hasNext()) {
Triple userTriple = userTriples.next();
Iterator<Triple> systemUserTriples = systemGraph.filter(userTriple.getSubject(), null, null);
while (systemUserTriples.hasNext()) {
tripleBuffer.add(systemUserTriples.next());
}
}
} finally {
readLock.unlock();
}
systemGraph.removeAll(tripleBuffer);
// seems the most appropriate response
return Response.noContent().build();
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class UserResource method getNamedRole.
/*
* returns an existing user node from the graph.
*/
private GraphNode getNamedRole(String roleName) {
GraphNode roleNode = null;
Iterator<Triple> roleIterator = systemGraph.filter(null, RDF.type, PERMISSION.Role);
//new PlainLiteralImpl(userName));
if (!roleIterator.hasNext()) {
return null;
}
ArrayList<Triple> tripleBuffer = new ArrayList<Triple>();
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
while (roleIterator.hasNext()) {
BlankNodeOrIRI role = roleIterator.next().getSubject();
Iterator<Triple> roleNameTriples = systemGraph.filter(role, DC.title, null);
while (roleNameTriples.hasNext()) {
Literal roleLiteral = (Literal) roleNameTriples.next().getObject();
if (roleName.equals(roleLiteral.getLexicalForm())) {
roleNode = new GraphNode(role, systemGraph);
break;
}
}
if (roleNode != null) {
break;
}
}
} finally {
readLock.unlock();
}
return roleNode;
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class UserResource method hasPermission.
private boolean hasPermission(GraphNode userNode, String permissionString) {
boolean has = false;
Iterator<Triple> existingPermissions = systemGraph.filter((BlankNodeOrIRI) userNode.getNode(), PERMISSION.hasPermission, null);
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
try {
// check to see if the user already has this permission
while (existingPermissions.hasNext()) {
BlankNodeOrIRI permissionNode = (BlankNodeOrIRI) existingPermissions.next().getObject();
Iterator<Triple> permissionTriples = systemGraph.filter(permissionNode, PERMISSION.javaPermissionEntry, null);
while (permissionTriples.hasNext()) {
Literal permission = (Literal) permissionTriples.next().getObject();
if (permissionString.equals(permission.getLexicalForm())) {
has = true;
}
}
}
} finally {
readLock.unlock();
}
return has;
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class IndexedGraph method compare.
/**
* Compares Resources to correctly sort them within the index.<p>
* Sort criteria are:<ol>
* <li> URIs are sorted by the {@link IRI#getUnicodeString()} unicode
* string)
* <li> Literals
* <ol>
* <li> sort by the {@link Literal#getLexicalForm() lixical form}
* <li> sort by {@link Literal#getLanguage() language}
* (<code>null</code> value first)
* <li> sort by {@link Literal#getDataType() type} (<code>null</code>
* value fist
* </ol>
* <li> BlankNode
* <ol>
* <li> sorted by their
* {@link System#identityHashCode(Object) Object hasCode}
* <li> on hasCode conflicts (same hasCode but not equals) a random order is
* chosen and kept in the parsed conflictsMap
* </ol>
* </ol>
* <b>NOTEs</b><ul>
* <li> parsed {@link RDFTerm} are not required to correctly implement
* {@link Object#hashCode() hashCode} and
* {@link Object#equals(Object) equals}
* <li> parsed {@link IRI} and {@link BlankNode} and {@link Literal} MUST
* NOT extend/implement any of the other classes/interfaces. This means that
* an {@link IRI} MUST NOT implement {@link BlankNode} nor {@link Literal}
* <li> parsed {@link Literal}s MAY implement PlainLiteral AND
* TypedLiteral. This allows wrappers over frameworks that do not
* distinguish between those two literal types to be used with the
* {@link IndexedGraph}.
* </ul>
*
* @param a the first resource to compare
* @param b the second resource to compare
* @param confictsMap the map used to resolve BlankNodes with hasCode
* conflicts
* @return
*/
protected static int compare(RDFTerm a, RDFTerm b, Map<Integer, List<RDFTerm>> confictsMap) {
//Handle special cases for MAX and MIN values
if (a == MIN || b == MAX) {
return -1;
} else if (a == MAX || b == MIN) {
return 1;
}
//sort (0) IRIs < (1) Literals (PlainLiterals & TypedLiterals) < (3) BlankNodes
int at = a instanceof IRI ? 0 : a instanceof Literal ? 1 : 2;
int bt = b instanceof IRI ? 0 : b instanceof Literal ? 1 : 2;
if (at == bt) {
//same type sort the different types
if (at < 2) {
//no BlankNode
//sort in alphabetic order of the string representation
String as = at == 0 ? ((IRI) a).getUnicodeString() : ((Literal) a).getLexicalForm();
String bs = bt == 0 ? ((IRI) b).getUnicodeString() : ((Literal) b).getLexicalForm();
int sc = as.compareTo(bs);
if (sc == 0 && at == 1) {
//same string value and Literals
//check if the language and types are the same
Language al = a instanceof Literal ? ((Literal) a).getLanguage() : null;
Language bl = b instanceof Literal ? ((Literal) b).getLanguage() : null;
//first try to sort by language
if (al == null) {
sc = bl == null ? 0 : -1;
} else if (bl == null) {
sc = 1;
} else {
sc = al.toString().compareTo(bl.toString());
}
if (sc == 0) {
//if still equals look at the dataType
IRI adt = a instanceof Literal ? ((Literal) a).getDataType() : null;
IRI bdt = b instanceof Literal ? ((Literal) b).getDataType() : null;
if (adt == null) {
sc = bdt == null ? 0 : -1;
} else if (bdt == null) {
sc = 1;
} else {
sc = adt.getUnicodeString().compareTo(bdt.getUnicodeString());
}
}
return sc;
} else {
//for IRIs return the string compare
return sc;
}
} else {
//handle BlankNodes
//sort BlankNodes based on hashCode
int ah = a.hashCode();
int bh = b.hashCode();
if (ah == bh) {
if (!a.equals(b)) {
//if implementations hash is the same, but the instances
//are not equals, try to sort them by identity hash code
int ash = System.identityHashCode(a);
int bsh = System.identityHashCode(b);
if (ash == bsh) {
//decision in a confilctMap
return resolveBlankNodeHashConflict(a, b, confictsMap);
} else {
return ash < bsh ? -1 : 1;
}
} else {
//same hash and equals
return 0;
}
} else {
//sort by hash
return ah < bh ? -1 : 1;
}
}
} else {
return at < bt ? -1 : 1;
}
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class ZemantaEnhancementEngine method processTextAnnotation.
/**
* This Methods searches/creates text annotations for anchor points of Zemanta
* extractions.
* <p>
* First this method searches for text annotations that do use the anchor as
* selected text. Second it searches for occurrences of the anchor within the
* content of the content and checks if there is an text annotation for that
* occurrence. If not it creates an new one.
*
* @param enhancements the graph containing the meta data
* @param text the content as string
* @param ciId the ID of the content item
* @param anchor the anchor text
* @param confidence the confidence to be used for newly created text annotations
*
* @return a collection of all existing/created text annotations for the parsed anchor
*/
private Collection<BlankNodeOrIRI> processTextAnnotation(Graph enhancements, String text, IRI ciId, String anchor, Double confidence) {
Collection<BlankNodeOrIRI> textAnnotations = new ArrayList<BlankNodeOrIRI>();
int anchorLength = anchor.length();
Literal anchorLiteral = new PlainLiteralImpl(anchor);
//first search for existing TextAnnotations for the anchor
Map<Integer, Collection<BlankNodeOrIRI>> existingTextAnnotationsMap = searchExistingTextAnnotations(enhancements, anchorLiteral);
for (int current = text.indexOf(anchor); current >= 0; current = text.indexOf(anchor, current + 1)) {
Collection<BlankNodeOrIRI> existingTextAnnotations = existingTextAnnotationsMap.get(current);
if (existingTextAnnotations != null) {
//use the existing once
textAnnotations.addAll(existingTextAnnotations);
} else {
//we need to create an new one!
IRI textAnnotation = EnhancementEngineHelper.createTextEnhancement(enhancements, this, ciId);
textAnnotations.add(textAnnotation);
//write the selection
enhancements.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(current)));
enhancements.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(current + anchorLength)));
enhancements.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT, anchorLiteral));
//extract the selection context
int beginPos;
if (current <= SELECTION_CONTEXT_PREFIX_SUFFIX_SIZE) {
beginPos = 0;
} else {
int start = current - SELECTION_CONTEXT_PREFIX_SUFFIX_SIZE;
beginPos = text.indexOf(' ', start);
if (beginPos < 0 || beginPos >= current) {
//no words
//begin within a word
beginPos = start;
}
}
int endPos;
if (current + anchorLength + SELECTION_CONTEXT_PREFIX_SUFFIX_SIZE >= text.length()) {
endPos = text.length();
} else {
int start = current + anchorLength + SELECTION_CONTEXT_PREFIX_SUFFIX_SIZE;
endPos = text.lastIndexOf(' ', start);
if (endPos <= current + anchorLength) {
//end within a word;
endPos = start;
}
}
enhancements.add(new TripleImpl(textAnnotation, ENHANCER_SELECTION_CONTEXT, new PlainLiteralImpl(text.substring(beginPos, endPos))));
// related to the annotated Entity rather to the selected text.
if (confidence != null) {
enhancements.add(new TripleImpl(textAnnotation, ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(confidence)));
}
//TODO: No idea about the type of the Annotation, because we do not have an type of the entity!
// One would need to get the types from the referred Source
}
}
return textAnnotations;
}
Aggregations