use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.
the class ClerezzaRDFUtils method urifyBlankNodes.
public static void urifyBlankNodes(Graph model) {
HashMap<BlankNode, IRI> blankNodeMap = new HashMap<BlankNode, IRI>();
Graph remove = new SimpleGraph();
Graph add = new SimpleGraph();
for (Triple t : model) {
BlankNodeOrIRI subj = t.getSubject();
RDFTerm obj = t.getObject();
IRI pred = t.getPredicate();
boolean match = false;
if (subj instanceof BlankNode) {
match = true;
IRI ru = blankNodeMap.get(subj);
if (ru == null) {
ru = createRandomUri();
blankNodeMap.put((BlankNode) subj, ru);
}
subj = ru;
}
if (obj instanceof BlankNode) {
match = true;
IRI ru = blankNodeMap.get(obj);
if (ru == null) {
ru = createRandomUri();
blankNodeMap.put((BlankNode) obj, ru);
}
obj = ru;
}
if (match) {
remove.add(t);
add.add(new TripleImpl(subj, pred, obj));
}
}
model.removeAll(remove);
model.addAll(add);
}
use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.
the class ClerezzaRDFUtils method findRoot.
private static void findRoot(Graph model, BlankNodeOrIRI node, Set<BlankNodeOrIRI> roots, Set<BlankNodeOrIRI> visited) {
if (visited.contains(node)) {
return;
}
visited.add(node);
Iterator<Triple> it = model.filter(null, null, node);
// something that is not the object of some stement is a root
if (!it.hasNext()) {
roots.add(node);
LOG.debug("Root found: {}", node);
return;
}
while (it.hasNext()) {
Triple t = it.next();
BlankNodeOrIRI subj = t.getSubject();
findRoot(model, subj, roots, visited);
}
}
use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.
the class TestHtmlExtractor method testRootExtraction.
/**
* This tests the merging of disconnected graphs under a single root
*
* @throws Exception
*/
@Test
public void testRootExtraction() throws Exception {
HtmlExtractor extractor = new HtmlExtractor(registry, parser);
Graph model = new SimpleGraph();
String testFile = "test-MultiRoot.html";
// extract text from RDFa annotated html
InputStream in = getResourceAsStream(testFile);
assertNotNull("failed to load resource " + testFile, in);
extractor.extract("file://" + testFile, in, null, "text/html", model);
// show triples
int tripleCounter = model.size();
LOG.debug("Triples: {}", tripleCounter);
printTriples(model);
Set<BlankNodeOrIRI> roots = ClerezzaRDFUtils.findRoots(model);
assertTrue(roots.size() > 1);
ClerezzaRDFUtils.makeConnected(model, new IRI("file://" + testFile), new IRI(NIE_NS + "contains"));
roots = ClerezzaRDFUtils.findRoots(model);
assertEquals(1, roots.size());
}
use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.
the class UserResource method store.
// **********************************
// ****** ADD PERMISSION TO USER ****
// **********************************
// **************************************
// ****** REMOVE PERMISSION FROM USER ***
// **************************************
// ************************************
// ****** ADD PERMISSION TO ROLE ******
// ************************************
// **************************************
// ****** REMOVE PERMISSION FROM ROLE ***
// **************************************
// //////////////////////////////////////////////////////////////
/**
* Pushes user data into system graph
*
* @param userNode
* @param uriInfo
* @param currentUserName
* @param newUserName
* @param fullName
* @param email
* @param password
* @param roles
* @param permissions
* @return
*/
private Response store(GraphNode userNode, UriInfo uriInfo, String currentUserName, String newUserName, String fullName, String email, String password, List<String> roles, List<String> permissions) {
if (newUserName != null && !newUserName.equals("")) {
changeLiteral(userNode, PLATFORM.userName, newUserName);
}
if (fullName != null && !fullName.equals("")) {
changeLiteral(userNode, FOAF.name, fullName);
}
if (password != null && !password.equals("")) {
String passwordSha1 = PasswordUtil.convertPassword(password);
changeLiteral(userNode, PERMISSION.passwordSha1, passwordSha1);
}
if (email != null && !email.equals("")) {
changeResource(userNode, FOAF.mbox, new IRI("mailto:" + email));
}
BlankNodeOrIRI userResource = (BlankNodeOrIRI) userNode.getNode();
if (roles != null) {
clearRoles(userResource);
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
try {
for (int i = 0; i < roles.size(); i++) {
roles.set(i, roles.get(i).trim());
if (!roles.get(i).equals("")) {
addRole(userNode, roles.get(i));
}
}
} finally {
writeLock.unlock();
}
}
if (permissions != null) {
clearPermissions(userResource);
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
try {
for (int i = 0; i < permissions.size(); i++) {
permissions.set(i, permissions.get(i).trim());
if (!permissions.get(i).equals("")) {
addPermission(userNode, permissions.get(i));
}
}
} finally {
writeLock.unlock();
}
}
URI pageUri = uriInfo.getBaseUriBuilder().path("system/console/usermanagement").build();
// header Cache-control: no-cache, just in case intermediaries are
// holding onto old stuff
CacheControl cc = new CacheControl();
cc.setNoCache(true);
// the jax-rs things available
return Response.seeOther(pageUri).cacheControl(cc).build();
}
use of org.apache.clerezza.commons.rdf.BlankNodeOrIRI in project stanbol by apache.
the class UserResource method changeUser.
/**
* Modify user given a graph describing the change.
*
* @param inputGraph change graph
* @return HTTP response
*/
@POST
@Consumes(SupportedFormat.TURTLE)
@Path("change-user")
public Response changeUser(ImmutableGraph inputGraph) {
Lock readLock = systemGraph.getLock().readLock();
readLock.lock();
Iterator<Triple> changes = inputGraph.filter(null, null, Ontology.Change);
Triple oldTriple = null;
Triple newTriple = null;
if (changes.hasNext()) {
Triple changeTriple = changes.next();
BlankNodeOrIRI changeNode = changeTriple.getSubject();
Literal userName = (Literal) inputGraph.filter(changeNode, PLATFORM.userName, null).next().getObject();
Iterator<Triple> userTriples = systemGraph.filter(null, PLATFORM.userName, userName);
// if (userTriples.hasNext()) {
BlankNodeOrIRI userNode = userTriples.next().getSubject();
IRI predicateIRI = (IRI) inputGraph.filter(changeNode, Ontology.predicate, null).next().getObject();
// handle old value (if it exists)
Iterator<Triple> iterator = inputGraph.filter(changeNode, Ontology.oldValue, null);
RDFTerm oldValue = null;
if (iterator.hasNext()) {
oldValue = iterator.next().getObject();
// Triple oldTriple = systemGraph.filter(null, predicateIRI,
// oldValue).next();
Iterator<Triple> oldTriples = systemGraph.filter(userNode, predicateIRI, oldValue);
if (oldTriples.hasNext()) {
oldTriple = oldTriples.next();
}
}
RDFTerm newValue = inputGraph.filter(changeNode, Ontology.newValue, null).next().getObject();
newTriple = new TripleImpl(userNode, predicateIRI, newValue);
// }
}
readLock.unlock();
Lock writeLock = systemGraph.getLock().writeLock();
writeLock.lock();
if (oldTriple != null) {
systemGraph.remove(oldTriple);
}
systemGraph.add(newTriple);
writeLock.unlock();
// seems the most appropriate response
return Response.noContent().build();
}
Aggregations