Search in sources :

Example 1 with BlankNodeOrIRI

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);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) HashMap(java.util.HashMap) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 2 with BlankNodeOrIRI

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);
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI)

Example 3 with BlankNodeOrIRI

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());
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) InputStream(java.io.InputStream) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HtmlExtractor(org.apache.stanbol.enhancer.engines.htmlextractor.impl.HtmlExtractor) Test(org.junit.Test)

Example 4 with BlankNodeOrIRI

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();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) CacheControl(javax.ws.rs.core.CacheControl) URI(java.net.URI) Lock(java.util.concurrent.locks.Lock)

Example 5 with BlankNodeOrIRI

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();
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Literal(org.apache.clerezza.commons.rdf.Literal) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

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