Search in sources :

Example 71 with RDFTerm

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

the class IndexedGraph method resolveBlankNodeHashConflict.

//    /**
//     * Compares two resources with special support for {@link #MIN} and
//     * {@link #MAX} to allow building {@link SortedSet#subSet(Object, Object)}
//     * for <code>null</code> values parsed to 
//     * {@link #filter(BlankNodeOrIRI, IRI, RDFTerm)}
//     * @param a
//     * @param b
//     * @return
//     */
//    protected static int compareHash(RDFTerm a, RDFTerm b, Map<Integer,List<RDFTerm>> confictsMap) {
//        int hashA = a.hashCode();
//        int hashB = b.hashCode();
//        if (hashA != hashB) {
//            return hashA > hashB ? 1 : -1;
//        }
//        //those resources might be equals
//        //(1) Check for MIN, MAX (used to build sub-sets). Other resources might
//        //    have a similar hasCode
//        int state = a == MIN || b == MAX ? -1 :
//            a == MAX || b == MIN ? 1 : 0;
//        if(state == 0){
//            if(a.equals(b)){ //check of the resources are equals
//                return 0; //return zero
//            } else if(//we need to care about HashCode conflicts 
//                a instanceof BlankNode && b instanceof BlankNode){ // of BlankNodes
//                log.info("HashCode conflict for {} and {}",a,b); //we have a conflict
//                return resolveBlankNodeHashConflict(a, b, confictsMap);
//            } else { //same hashCode but not equals
//                //use the String representation of the Resources to sort them
//                String as = resource2String(a);
//                String bs = resource2String(b);
//                log.info("same hash code {} - compare Strings a: {}, b: {}",
//                    new Object[]{a.hashCode(),as,bs});
//                return as.compareTo(bs);
//            }
//        }
//       return state;
//    }
/**
     * Resolved BlankNode hasConflics, by storing the correct order for the affected
     * {@link Integer} in a {@link List} of RDFTerm instances.
     * @param a the first {@link BlankNode}
     * @param b the second {@link BlankNode}
     * @param confictsMap the Map used to store the order of BlankNodes with conflicts
     * @return the decision taken based on the confictsMap.
     */
private static int resolveBlankNodeHashConflict(RDFTerm a, RDFTerm b, Map<Integer, List<RDFTerm>> confictsMap) {
    //This is not a bad thing. We need just to ensure constant ordering
    //and as there is nothing we can use to distinguish we need to keep
    //this information in a list.
    Integer hash = Integer.valueOf(a.hashCode());
    List<RDFTerm> resources = confictsMap.get(hash);
    if (resources == null) {
        //new conflict ... just add and return
        resources = new ArrayList<RDFTerm>(2);
        confictsMap.put(hash, resources);
        resources.add(a);
        resources.add(b);
        return -1;
    }
    //already conflicting resource for this hash present
    int aIndex = -1;
    int bIndex = -1;
    for (int i = 0; i < resources.size() && (aIndex < 0 || bIndex < 0); i++) {
        RDFTerm r = resources.get(i);
        if (aIndex < 0 && r.equals(a)) {
            aIndex = i;
        }
        if (bIndex < 0 && r.equals(b)) {
            bIndex = i;
        }
    }
    if (aIndex < 0) {
        //a not found
        aIndex = resources.size();
        resources.add(a);
    }
    if (bIndex < 0) {
        //b not found
        bIndex = resources.size();
        resources.add(b);
    }
    return aIndex < bIndex ? -1 : 1;
}
Also used : RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 72 with RDFTerm

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

the class RdfSerializingWriter method getAdditionalExpansionResources.

private Set<RDFTerm> getAdditionalExpansionResources(Graph tc, GraphNode recipe) {
    final Set<IRI> subjectExpansionProperties = getSubjectExpansionProperties(recipe);
    final Set<IRI> objectExpansionProperties = getObjectExpansionProperties(recipe);
    final Set<RDFTerm> result = new HashSet<RDFTerm>();
    if ((subjectExpansionProperties.size() > 0) || (objectExpansionProperties.size() > 0)) {
        for (Triple triple : tc) {
            final IRI predicate = triple.getPredicate();
            if (subjectExpansionProperties.contains(predicate)) {
                result.add(triple.getSubject());
            }
            if (objectExpansionProperties.contains(predicate)) {
                result.add(triple.getObject());
            }
        }
    }
    return result;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 73 with RDFTerm

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

the class RdfSerializingWriter method getExpandedContext.

private Graph getExpandedContext(GraphNode node, GraphNode recipe) {
    final Graph result = new SimpleGraph(node.getNodeContext());
    final Set<RDFTerm> expandedResources = new HashSet<RDFTerm>();
    expandedResources.add(node.getNode());
    while (true) {
        Set<RDFTerm> additionalExpansionRes = getAdditionalExpansionResources(result, recipe);
        additionalExpansionRes.removeAll(expandedResources);
        if (additionalExpansionRes.size() == 0) {
            return result;
        }
        for (RDFTerm resource : additionalExpansionRes) {
            final GraphNode additionalNode = new GraphNode(resource, node.getGraph());
            result.addAll(additionalNode.getNodeContext());
            expandedResources.add(resource);
        }
    }
}
Also used : SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) HashSet(java.util.HashSet)

Example 74 with RDFTerm

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

the class RdfSerializingWriter method getSubjectExpansionProperties.

private Set<IRI> getSubjectExpansionProperties(GraphNode recipe) {
    final MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true);
    final List<String> paramValues = queryParams.get(SUBJ_EXP_PARAM);
    final Set<IRI> result = new HashSet<IRI>();
    if (paramValues != null) {
        for (String uriString : paramValues) {
            result.add(new IRI(uriString));
        }
    }
    if (recipe != null) {
        Iterator<GraphNode> ingredients = recipe.getObjectNodes(RECIPES.ingredient);
        while (ingredients.hasNext()) {
            Iterator<RDFTerm> properties = ingredients.next().getObjects(RECIPES.ingredientInverseProperty);
            while (properties.hasNext()) {
                result.add((IRI) properties.next());
            }
        }
    }
    return result;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 75 with RDFTerm

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

the class ZemantaEnhancementEngineTest method tesetBioText.

@Test
public void tesetBioText() throws EngineException, IOException {
    ContentItem ci = wrapAsContentItem(BIO_DOMAIN_TEXT);
    try {
        zemantaEngine.computeEnhancements(ci);
    } catch (EngineException e) {
        RemoteServiceHelper.checkServiceUnavailable(e);
        return;
    }
    JenaSerializerProvider serializer = new JenaSerializerProvider();
    serializer.serialize(System.out, ci.getMetadata(), TURTLE);
    Map<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(zemantaEngine.getClass().getName()));
    //deactivate require fise:confidence values for fise:TextAnnotations, because
    //the one used to group the TopicAnnotations does not have a confidence value
    int textAnnoNum = validateAllTextAnnotations(ci.getMetadata(), BIO_DOMAIN_TEXT, expectedValues);
    log.info(textAnnoNum + " TextAnnotations found ...");
    //adding null as expected for confidence makes it a required property
    expectedValues.put(Properties.ENHANCER_CONFIDENCE, null);
    int entityAnnoNum = EnhancementStructureHelper.validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    log.info(entityAnnoNum + " EntityAnnotations found ...");
    int topicAnnoNum = EnhancementStructureHelper.validateAllTopicAnnotations(ci.getMetadata(), expectedValues);
    log.info(topicAnnoNum + " TopicAnnotations found ...");
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) HashMap(java.util.HashMap) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Aggregations

RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)126 IRI (org.apache.clerezza.commons.rdf.IRI)84 Triple (org.apache.clerezza.commons.rdf.Triple)70 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)48 Literal (org.apache.clerezza.commons.rdf.Literal)35 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 HashMap (java.util.HashMap)28 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)26 Graph (org.apache.clerezza.commons.rdf.Graph)24 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)18 ArrayList (java.util.ArrayList)17 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)16 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)13 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Collection (java.util.Collection)10 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)10 Lock (java.util.concurrent.locks.Lock)9 IOException (java.io.IOException)5