Search in sources :

Example 26 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class ZemantaEnhancementEngine method processCategories.

protected void processCategories(Graph results, Graph enhancements, IRI ciId) {
    Iterator<Triple> categories = results.filter(null, RDF_TYPE, ZemantaOntologyEnum.Category.getUri());
    //add the root Text annotation as soon as the first TopicAnnotation is added.
    IRI textAnnotation = null;
    while (categories.hasNext()) {
        BlankNodeOrIRI category = categories.next().getSubject();
        log.debug("process category " + category);
        Double confidence = parseConfidence(results, category);
        log.debug(" > confidence :" + confidence);
        //now we need to follow the Target link
        IRI target = EnhancementEngineHelper.getReference(results, category, ZemantaOntologyEnum.target.getUri());
        if (target != null) {
            //first check the used categorisation
            IRI categorisationScheme = EnhancementEngineHelper.getReference(results, target, ZemantaOntologyEnum.categorization.getUri());
            if (categorisationScheme != null && categorisationScheme.equals(ZemantaOntologyEnum.categorization_DMOZ.getUri())) {
                String categoryTitle = EnhancementEngineHelper.getString(results, target, ZemantaOntologyEnum.title.getUri());
                if (categoryTitle != null) {
                    if (textAnnotation == null) {
                        //this is the first category ... create the TextAnnotation used
                        //to link all fise:TopicAnnotations
                        textAnnotation = createTextEnhancement(enhancements, this, ciId);
                        enhancements.add(new TripleImpl(textAnnotation, DC_TYPE, SKOS_CONCEPT));
                    }
                    //now write the TopicAnnotation
                    IRI categoryEnhancement = createTopicEnhancement(enhancements, this, ciId);
                    //make related to the EntityAnnotation
                    enhancements.add(new TripleImpl(categoryEnhancement, DC_RELATION, textAnnotation));
                    //write the title
                    enhancements.add(new TripleImpl(categoryEnhancement, ENHANCER_ENTITY_LABEL, new PlainLiteralImpl(categoryTitle)));
                    //write the reference
                    if (categoryTitle.startsWith(ZEMANTA_DMOZ_PREFIX)) {
                        enhancements.add(new TripleImpl(categoryEnhancement, ENHANCER_ENTITY_REFERENCE, new IRI(DMOZ_BASE_URL + categoryTitle.substring(ZEMANTA_DMOZ_PREFIX.length()))));
                    }
                    //write the confidence
                    if (confidence != null) {
                        enhancements.add(new TripleImpl(categoryEnhancement, ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(confidence)));
                    }
                    //we need to write the fise:entity-type
                    //as of STANBOL-617 we use now both the zemanta:Category AND the skos:Concept
                    //type. dc:type is no longer used as this is only used by fise:TextAnnotations
                    // see http://wiki.iks-project.eu/index.php/ZemantaEnhancementEngine#Mapping_of_Categories
                    // for more Information
                    enhancements.add(new TripleImpl(categoryEnhancement, ENHANCER_ENTITY_TYPE, SKOS_CONCEPT));
                    //Use also Zemanta Category as type for the referred Entity
                    enhancements.add(new TripleImpl(categoryEnhancement, ENHANCER_ENTITY_TYPE, ZemantaOntologyEnum.Category.getUri()));
                } else {
                    log.warn("Unable to process category " + category + " because no title is present");
                }
            } else {
                log.warn("Unable to process category " + category + " because categorisation scheme != DMOZ (" + categorisationScheme + " != " + ZemantaOntologyEnum.categorization_DMOZ.getUri() + ")");
            }
        } else {
            log.warn("Unable to process category " + category + " because no target node was found");
        }
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 27 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class ZemantaEnhancementEngine method searchExistingTextAnnotations.

/**
     * Search for existing TextAnnotations for an given selected text and
     * returns an Map that uses the start position as an key and a list of
     * text annotations as an value.
     *
     * @param enhancements  the graph containing the enhancements to be searched
     * @param anchorLiteral the Literal representing the selected text
     *
     * @return Map that uses the start position as an key and a list of
     *         text annotations as an value.
     */
private Map<Integer, Collection<BlankNodeOrIRI>> searchExistingTextAnnotations(Graph enhancements, Literal anchorLiteral) {
    Iterator<Triple> textAnnotationsIterator = enhancements.filter(null, ENHANCER_SELECTED_TEXT, anchorLiteral);
    Map<Integer, Collection<BlankNodeOrIRI>> existingTextAnnotationsMap = new HashMap<Integer, Collection<BlankNodeOrIRI>>();
    while (textAnnotationsIterator.hasNext()) {
        BlankNodeOrIRI subject = textAnnotationsIterator.next().getSubject();
        //test rdfType
        if (enhancements.contains(new TripleImpl(subject, RDF_TYPE, ENHANCER_TEXTANNOTATION))) {
            Integer start = EnhancementEngineHelper.get(enhancements, subject, ENHANCER_START, Integer.class, literalFactory);
            if (start != null) {
                Collection<BlankNodeOrIRI> textAnnotationList = existingTextAnnotationsMap.get(start);
                if (textAnnotationList == null) {
                    textAnnotationList = new ArrayList<BlankNodeOrIRI>();
                    existingTextAnnotationsMap.put(start, textAnnotationList);
                }
                textAnnotationList.add(subject);
            }
        }
    }
    return existingTextAnnotationsMap;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) HashMap(java.util.HashMap) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Collection(java.util.Collection) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 28 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class AuthenticationCheckerImpl method getAgentFromGraph.

private BlankNodeOrIRI getAgentFromGraph(String userName) throws NoSuchAgent {
    BlankNodeOrIRI agent;
    Lock l = systemGraph.getLock().readLock();
    l.lock();
    try {
        Iterator<Triple> agents = systemGraph.filter(null, PLATFORM.userName, new PlainLiteralImpl(userName));
        if (agents.hasNext()) {
            agent = agents.next().getSubject();
        } else {
            logger.debug("unsuccessful authentication attempt as non-existent user {}", userName);
            throw new NoSuchAgent();
        }
    } finally {
        l.unlock();
    }
    return agent;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Lock(java.util.concurrent.locks.Lock)

Example 29 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class UserResource method rolesCheckboxes.

/**
     * Provides HTML corresponding to a user's roles
     *
     * all roles are listed with checkboxes, the roles this user has are checked
     *
     * (isn't very pretty but is just a one-off)
     *
     * @param userName the user in question
     * @return HTML checkboxes as HTTP response
     */
@GET
@Path("users/{username}/rolesCheckboxes")
@Produces(MediaType.TEXT_HTML)
public Response rolesCheckboxes(@PathParam("username") String userName) {
    // return new RdfViewable("rolesCheckboxes", getRoleType(), this.getClass());
    StringBuffer html = new StringBuffer();
    Iterator<Triple> allRoleTriples = systemGraph.filter(null, RDF.type, PERMISSION.Role);
    ArrayList<String> allRoleNames = new ArrayList<String>();
    Lock readLock = systemGraph.getLock().readLock();
    readLock.lock();
    try {
        // pulls out all role names
        while (allRoleTriples.hasNext()) {
            Triple triple = allRoleTriples.next();
            GraphNode roleNode = new GraphNode(triple.getSubject(), systemGraph);
            Iterator<Literal> titlesIterator = roleNode.getLiterals(DC.title);
            while (titlesIterator.hasNext()) {
                allRoleNames.add(titlesIterator.next().getLexicalForm());
            }
        }
    } finally {
        readLock.unlock();
    }
    Graph rolesGraph = getUserRolesGraph(userName);
    ArrayList<String> userRoleNames = new ArrayList<String>();
    Iterator<Triple> userRoleTriples = rolesGraph.filter(null, RDF.type, PERMISSION.Role);
    while (userRoleTriples.hasNext()) {
        Triple triple = userRoleTriples.next();
        GraphNode roleNode = new GraphNode(triple.getSubject(), rolesGraph);
        Iterator<Literal> titlesIterator = roleNode.getLiterals(DC.title);
        while (titlesIterator.hasNext()) {
            userRoleNames.add(titlesIterator.next().getLexicalForm());
        }
    }
    for (int i = 0; i < allRoleNames.size(); i++) {
        String role = allRoleNames.get(i);
        if (role.equals("BasePermissionsRole")) {
            // filter out
            continue;
        }
        if (userRoleNames.contains(role)) {
            html.append("<input class=\"role\" type=\"checkbox\" id=\"").append(role).append("\" name=\"").append(role).append("\" value=\"").append(role).append("\" checked=\"checked\" />");
        } else {
            html.append("<input class=\"role\" type=\"checkbox\" id=\"").append(role).append("\" name=\"").append(role).append("\" value=\"").append(role).append("\" />");
        }
        html.append("<label for=\"").append(role).append("\">").append(role).append("</label>");
        html.append("<br />");
    }
    return Response.ok(html.toString()).build();
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Literal(org.apache.clerezza.commons.rdf.Literal) ArrayList(java.util.ArrayList) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 30 with Triple

use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.

the class OWLUtils method extractOntologyID.

/**
     * Returns the logical identifier of the supplied RDF graph, which is interpreted as an OWL ontology.
     * 
     * @param graph
     *            the RDF graph
     * @return the OWL ontology ID of the supplied graph, or null if it denotes an anonymous ontology.
     */
public static OWLOntologyID extractOntologyID(Graph graph) {
    IRI ontologyIri = null, versionIri = null;
    Iterator<Triple> it = graph.filter(null, RDF.type, OWL.Ontology);
    if (it.hasNext()) {
        BlankNodeOrIRI subj = it.next().getSubject();
        if (it.hasNext()) {
            log.warn("Multiple OWL ontology definitions found.");
            log.warn("Ignoring all but {}", subj);
        }
        if (subj instanceof org.apache.clerezza.commons.rdf.IRI) {
            ontologyIri = IRI.create(((org.apache.clerezza.commons.rdf.IRI) subj).getUnicodeString());
            Iterator<Triple> it2 = graph.filter(subj, new org.apache.clerezza.commons.rdf.IRI(OWL2Constants.OWL_VERSION_IRI), null);
            if (it2.hasNext())
                versionIri = IRI.create(((org.apache.clerezza.commons.rdf.IRI) it2.next().getObject()).getUnicodeString());
        }
    }
    if (ontologyIri == null) {
        // Note that OWL 2 does not allow ontologies with a version IRI and no ontology IRI.
        log.debug("Ontology is anonymous. Returning null ID.");
        return null;
    }
    if (versionIri == null)
        return new OWLOntologyID(ontologyIri);
    else
        return new OWLOntologyID(ontologyIri, versionIri);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.semanticweb.owlapi.model.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI)

Aggregations

Triple (org.apache.clerezza.commons.rdf.Triple)151 IRI (org.apache.clerezza.commons.rdf.IRI)88 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)84 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)70 Graph (org.apache.clerezza.commons.rdf.Graph)45 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)41 HashSet (java.util.HashSet)34 Literal (org.apache.clerezza.commons.rdf.Literal)30 ArrayList (java.util.ArrayList)27 Lock (java.util.concurrent.locks.Lock)21 HashMap (java.util.HashMap)20 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)19 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)19 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)15 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)12 Test (org.junit.Test)12 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)10 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)9 GraphNode (org.apache.clerezza.rdf.utils.GraphNode)8 IOException (java.io.IOException)7