Package org.apache.stanbol.commons.indexedgraph

Examples of org.apache.stanbol.commons.indexedgraph.IndexedMGraph


    /**
     * @return an RDF/JSON descriptions of places for the word map widget
     */
    public String getPlacesAsJSON() throws ParseException, UnsupportedEncodingException {
        MGraph g = new IndexedMGraph();
        LiteralFactory lf = LiteralFactory.getInstance();
        MGraph metadata = contentItem.getMetadata();
        for (EntityExtractionSummary p : getPlaceOccurrences()) {
            EntitySuggestion bestGuess = p.getBestGuess();
            if (bestGuess == null) {
                continue;
            }
            UriRef uri = new UriRef(bestGuess.getUri());
            Iterator<Triple> latitudes = metadata.filter(uri, GEO_LAT, null);
            if (latitudes.hasNext()) {
                g.add(latitudes.next());
            }
            Iterator<Triple> longitutes = metadata.filter(uri, GEO_LONG, null);
            if (longitutes.hasNext()) {
                g.add(longitutes.next());
                g.add(new TripleImpl(uri, Properties.RDFS_LABEL, lf.createTypedLiteral(bestGuess.getLabel())));
            }
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.serialize(out, g, SupportedFormat.RDF_JSON);

View Full Code Here


    static MGraph toRDF(QueryResultList<?> resultList) {
        final MGraph resultGraph;
        Class<?> type = resultList.getType();
        if (String.class.isAssignableFrom(type)) {
            resultGraph = new IndexedMGraph(); //create a new Graph
            for (Object result : resultList) {
                //add a triple to each reference in the result set
                resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new UriRef(result.toString())));
            }
        } else {
            //first determine the type of the resultList
            final boolean isSignType;
            if (Representation.class.isAssignableFrom(type)) {
                isSignType = false;
            } else if (Representation.class.isAssignableFrom(type)) {
                isSignType = true;
            } else {
                //incompatible type -> throw an Exception
                throw new IllegalArgumentException("Parsed type " + type + " is not supported");
            }
            //special treatment for RdfQueryResultList for increased performance
            if (resultList instanceof RdfQueryResultList) {
                resultGraph = ((RdfQueryResultList) resultList).getResultGraph();
                if (isSignType) { //if we build a ResultList for Signs, that we need to do more things
                    //first remove all triples representing results
                    Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null);
                    while (resultTripleIt.hasNext()) {
                        resultTripleIt.next();
                        resultTripleIt.remove();
                    }
                    //now add the Sign specific triples and add result triples
                    //to the Sign IDs
                    for (Object result : resultList) {
                        UriRef signId = new UriRef(((Entity) result).getId());
                        EntityToRDF.addEntityTriplesToGraph(resultGraph, (Entity) result);
                        resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId));
                    }
                }
            } else { //any other implementation of the QueryResultList interface
                resultGraph = new IndexedMGraph(); //create a new graph
                if (Representation.class.isAssignableFrom(type)) {
                    for (Object result : resultList) {
                        UriRef resultId;
                        if (!isSignType) {
                            EntityToRDF.addRDFTo(resultGraph, (Representation) result);
View Full Code Here

     * @param headers the http headers of the request
     * @return the response
     */
    private Response executeLDPathQuery(Entityhub entityhub,FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
        QueryResultList<Representation> result;
        ValueFactory vf = new RdfValueFactory(new IndexedMGraph());
        EntityhubBackend backend = new EntityhubBackend(entityhub);
        EntityhubLDPath ldPath = new EntityhubLDPath(backend,vf);
        //copy the selected fields, because we might need to delete some during
        //the preparation phase
        Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
View Full Code Here

     * have it as a field
     */
    static final LiteralFactory literalFactory = LiteralFactory.getInstance();

    static MGraph toRDF(Representation representation) {
        MGraph graph = new IndexedMGraph();
        addRDFTo(graph, representation);
        return graph;
    }
View Full Code Here

    static void addRDFTo(MGraph graph, Representation representation) {
        graph.addAll(valueFactory.toRdfRepresentation(representation).getRdfGraph());
    }

    static TripleCollection toRDF(Entity entity) {
        MGraph graph = new IndexedMGraph();
        addRDFTo(graph, entity);
        return graph;
    }
View Full Code Here

        String format = SupportedFormat.RDF_XML;
        InputStream in = dereference(uri, format);
        long queryEnd = System.currentTimeMillis();
        log.debug("  > DereferenceTime: "+(queryEnd-start));
        if(in != null){
            MGraph rdfData = new IndexedMGraph(parser.parse(in, format,new UriRef(getBaseUri())));
            long parseEnd = System.currentTimeMillis();
            log.debug("  > ParseTime: "+(parseEnd-queryEnd));
            return valueFactory.createRdfRepresentation(new UriRef(uri), rdfData);
        } else {
            return null;
View Full Code Here

    /**
     * Test related to STANBOL-698
     */
    @Test
    public void testDouble(){
        MGraph graph = new IndexedMGraph();
        UriRef id = new UriRef("http://www.example.org/test");
        UriRef doubleTestField = new UriRef("http://www.example.org/field/double");
        LiteralFactory lf = LiteralFactory.getInstance();
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.NaN)));
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.POSITIVE_INFINITY)));
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Double.NEGATIVE_INFINITY)));
       
        RdfValueFactory vf = new RdfValueFactory(graph);
        Representation r = vf.createRepresentation(id.getUnicodeString());
        Set<Double> expected = new HashSet<Double>(Arrays.asList(
            Double.NaN, Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY));
View Full Code Here

            TripleCollection rdfData = parser.parse(in, SparqlSearcher.DEFAULT_RDF_CONTENT_TYPE,
                new UriRef(getBaseUri()));
            if(rdfData instanceof MGraph){
                graph = (MGraph) rdfData;
            } else {
                graph = new IndexedMGraph(rdfData);
            }
            long parseEnd = System.currentTimeMillis();
            log.debug("  > ParseTime: "+(parseEnd-queryEnd));
            return new RdfQueryResultList(query, graph);
        } else {
View Full Code Here

        Assert.assertTrue(expected.isEmpty());
    }
   
    @Test
    public void testFloat(){
        MGraph graph = new IndexedMGraph();
        UriRef id = new UriRef("http://www.example.org/test");
        UriRef doubleTestField = new UriRef("http://www.example.org/field/double");
        LiteralFactory lf = LiteralFactory.getInstance();
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Float.NaN)));
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Float.POSITIVE_INFINITY)));
        graph.add(new TripleImpl(id, doubleTestField, lf.createTypedLiteral(Float.NEGATIVE_INFINITY)));
       
        RdfValueFactory vf = new RdfValueFactory(graph);
        Representation r = vf.createRepresentation(id.getUnicodeString());
        Set<Float> expected = new HashSet<Float>(Arrays.asList(
            Float.NaN, Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY));
View Full Code Here

            throw new IllegalArgumentException("The parsed ChainName MUST NOT be empty!");
        }
        Collections.sort(availableEngines,EXECUTION_ORDER_COMPARATOR);
        //now we have all required and possible also optional engines
        //  -> build the execution plan
        MGraph ep = new IndexedMGraph();
        NonLiteral epNode = createExecutionPlan(ep, chainName);
        Integer prevOrder = null;
        Set<NonLiteral> prev = null;
        Set<NonLiteral> current = new HashSet<NonLiteral>();
        for(String name : missing){
            boolean optionalMissing = optional.contains(name);
            NonLiteral node = writeExecutionNode(ep, epNode, name, optionalMissing, null);
            if(!optionalMissing){
                current.add(node);
            } // else add missing optional engines without any dependsOn restrictions
        }
        for(EnhancementEngine engine : availableEngines){
            String name = engine.getName();
            Integer order = getEngineOrder(engine);
            if(prevOrder == null || !prevOrder.equals(order)){
                prev = current;
                current = new HashSet<NonLiteral>();
                prevOrder = order;
            }
            try {
                current.add(writeExecutionNode(ep, epNode, name, optional.contains(name), prev));
            } catch (RuntimeException e){
                //add the engine and class to ease debugging in such cases
                log.error("Exception while writing ExecutionNode for Enhancement Eninge: "
                    + engine +"(class: "+engine.getClass()+")",e);
                throw e; //rethrow it
            }
        }
        return ep.getGraph();
    }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.commons.indexedgraph.IndexedMGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.