Java Code Examples for org.openrdf.rio.RDFFormat#RDFXML

The following examples show how to use org.openrdf.rio.RDFFormat#RDFXML . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 
 * Utility method to get the {@link RDFFormat} for filename.
 * 
 * It checks for compressed endings and is provided as a utility.
 * 
 * @param fileName
 * @return
 */
public static RDFFormat rdfFormatForFile(final String fileName) {
	/*
     * Try to get the RDFFormat from the URL's file path.
     */

    RDFFormat fmt = RDFFormat.forFileName(fileName);

    if (fmt == null && fileName.endsWith(".zip")) {
        fmt = RDFFormat.forFileName(fileName.substring(0, fileName.length() - 4));
    }

    if (fmt == null && fileName.endsWith(".gz")) {
        fmt = RDFFormat.forFileName(fileName.substring(0, fileName.length() - 3));
    }

    if (fmt == null) {
        // Default format.
        fmt = RDFFormat.RDFXML;
    }
    
    return fmt;
}
 
Example 2
/**
 * Loads all triples found in the datafile associated with the given name.
 * 
 * @param datafileName the name of the datafile.
 * @param graphs an optional set of target graph URIs. 
 * @throws Exception hopefully never, otherwise the test fails.
 */
protected void load(final MisteryGuest data) throws Exception {
	if (data.datasets == null || data.datasets.length == 0) {
		return;
	}
	
	for (final String datafileName : data.datasets) {
		final RDFFormat format = datafileName.endsWith(".ttl") ? RDFFormat.TURTLE : RDFFormat.RDFXML;
		if (data.graphURI != null) {
			localConnection.add(source(datafileName), DUMMY_BASE_URI, format, localConnection.getValueFactory().createURI(data.graphURI));
			cumulusConnection.add(source(datafileName), DUMMY_BASE_URI, format, cumulusConnection.getValueFactory().createURI(data.graphURI));
		} else {
			localConnection.add(source(datafileName), DUMMY_BASE_URI, format);
			cumulusConnection.add(source(datafileName), DUMMY_BASE_URI, format);				
		}
	}  
}
 
Example 3
private static RDFFormat guessFormatFromName(String name) {
    if (name.endsWith(".ttl")) {
        return RDFFormat.TURTLE;
    } else if (name.endsWith(".xml")) {
        return RDFFormat.RDFXML;
    }
    return RDFFormat.RDFXML;
}
 
Example 4
public void test_compareEntailments() throws Exception {
    
    // String[] resource = new String[]{"/com/bigdata/rdf/rules/testOwlSameAs.rdf"};
    // String[] resource = new String[]{"/com/bigdata/rdf/rules/testOwlSameAs.rdf"};
    String[] resource = new String[]{"com/bigdata/rdf/rules/small.rdf"};
    String[] baseURL = new String[]{""};
    RDFFormat[] format = new RDFFormat[]{RDFFormat.RDFXML};

    doCompareEntailments(resource, baseURL, format);
    
}
 
Example 5
/**
 * test get Statemetns.
 * @throws Exception The test would fails if Exception occurs
 */
@Test
public void testGetStatements() throws Exception {
	//insert some statments first
	for (final String tripleAsString : _data) {
		final Statement triple = parseNX(tripleAsString).iterator().next();
		TRIPLE_STORE.addData(triple);
	}
	//test get the statements
	String uri = BASE_URI + Protocol.REPOSITORIES + "/" + REPO_ID + "/" + Protocol.STATEMENTS;
	File tmp = WebTestUtils.tmpFile();
	final ServletOutputStream servletOutputStream = new StubServletOutputStream(tmp);
	final HttpServletRequest request = mockHttpRequest(uri, METHOD_GET);
	when(request.getParameter(Protocol.SUBJECT_PARAM_NAME)).thenReturn(DEVICE);
	when(request.getParameter(Protocol.ACCEPT_PARAM_NAME)).thenReturn(MIMETYPE_RDF_XML);
	when(request.getCharacterEncoding()).thenReturn(UTF_8);
	final HttpServletResponse response = mock(HttpServletResponse.class);
	when(response.getOutputStream()).thenReturn(servletOutputStream);

	GetMethod method = new GetMethod(uri);
	RDFFormat format = RDFFormat.RDFXML;
	RDFParser parser = Rio.createParser(format, valueFactory);
	parser.setParserConfig(parser.getParserConfig());
	StatementCollector collector = new StatementCollector();
	parser.setRDFHandler(collector);
	_classUnderTest.service(request, response);

	parser.parse(new FileInputStream(tmp), method.getURI().getURI());
	assertTrue(!collector.getStatements().isEmpty());
	verify(response).setStatus(HttpServletResponse.SC_OK);

	servletOutputStream.close();
}
 
Example 6
/**
 * Returns an instance of {@link org.openrdf.rio.RDFFormat} for a
 * given MIME-Type string.
 *
 * @param mimetype the MIME-Type as string
 * @return the corresponding RDF-Format
 */
protected RDFFormat getRDFFormat(String mimetype) {
	switch (mimetype) {
		default:
		case RDFMediaType.RDF_TURTLE:
			return RDFFormat.TURTLE;
		case RDFMediaType.RDF_XML:
			return RDFFormat.RDFXML;
		case RDFMediaType.RDF_NTRIPLES:
			return RDFFormat.NTRIPLES;
		case RDFMediaType.RDF_JSON:
			return RDFFormat.RDFJSON;
	}
}
 
Example 7
/**
 * @param args
 */
public static void main(String[] args) {
	// load all the RDF data
	RDFDataSet tripleStore = new RDFFileDataSet(SteveExperiment.STEVE_FOLDER, RDFFormat.RDFXML);
	LargeClassificationDataSet ds = new SteveDataSet(tripleStore, 10);
	ds.create();

	double[] cs = { 1, 10, 100, 1000 };

	LibLINEARParameters svmParms = new LibLINEARParameters(LibLINEARParameters.SVC_DUAL, cs);
	svmParms.setNumFolds(5);

	boolean reverseWL = true; // WL should be in reverse mode, which means
								// regular subtrees
	boolean[] inference = { false };

	int[] depths = { 3 };

	int maxFeatures = 100;

	RDFData data = ds.getRDFData();
	List<Double> target = ds.getTarget();

	for (boolean inf : inference) {
		for (int d : depths) {
			List<RDFWLSubTreeKernel> kernels = new ArrayList<RDFWLSubTreeKernel>();
			kernels.add(new RDFWLSubTreeKernel(d * 2, d, inf, reverseWL, true, true));

			FeatureInspectionExperiment<RDFData, RDFWLSubTreeKernel> exp = new FeatureInspectionExperiment<RDFData, RDFWLSubTreeKernel>(
					kernels, data, target, svmParms, maxFeatures);
			exp.run();
		}
	}
}
 
Example 8
/**
     * Load using {@link AsynchronousStatementBufferFactory}.
     */
    protected AsynchronousStatementBufferFactory<BigdataStatement,File> doLoad2(
            final AbstractTripleStore store, final File resource,
            final boolean parallel) throws Exception {

        final RDFParserOptions parserOptions = new RDFParserOptions();
        parserOptions.setVerifyData(false);
        
        final AsynchronousStatementBufferFactory<BigdataStatement,File> statementBufferFactory = new AsynchronousStatementBufferFactory<BigdataStatement,File>(
                (ScaleOutTripleStore) store,//
                chunkSize, //
                valuesInitialCapacity,//
                bnodesInitialCapacity,//
                RDFFormat.RDFXML, // defaultFormat
                null, // defaultGraph
                parserOptions, // 
                false, // deleteAfter
                parallel?5:1,  // parserPoolSize,
                20, // parserQueueCapacity
                parallel?5:1,  // term2IdWriterPoolSize,
                parallel?5:1,  // otherWriterPoolSize
                parallel?5:1,  // notifyPoolSize
                unbufferedStatementThreshold
                );

//        final AsynchronousWriteBufferFactoryWithoutSids2<BigdataStatement, File> statementBufferFactory = new AsynchronousWriteBufferFactoryWithoutSids2<BigdataStatement, File>(
//                (ScaleOutTripleStore) store, chunkSize, valuesInitialCapacity,
//                bnodesInitialCapacity);

        try {

            // tasks to load the resource or file(s)
            if (resource.isDirectory()) {

                statementBufferFactory.submitAll(resource,
                        new com.bigdata.rdf.load.RDFFilenameFilter(),
                        rejectedExecutionDelay);
                
            } else {
                
                statementBufferFactory.submitOne(resource);
                
            }

            // wait for the async writes to complete.
            statementBufferFactory.awaitAll();

            // dump write statistics for indices used by kb.
//            System.err.println(((AbstractFederation) store.getIndexManager())
//                    .getServiceCounterSet().getPath("Indices").toString());

            // dump factory specific counters.
            System.err.println(statementBufferFactory.getCounters().toString());
            
        } catch (Throwable t) {

            statementBufferFactory.cancelAll(true/* mayInterruptIfRunning */);

            // rethrow
            throw new RuntimeException(t);

        }

        return statementBufferFactory;
        
    }
 
Example 9
/**
     * Send an RDF Graph as a response using content negotiation.
     * 
     * @param req
     * @param resp
     */
    static public void sendGraph(final HttpServletRequest req,
            final HttpServletResponse resp, final Graph g) throws IOException {
        /*
         * CONNEG for the MIME type.
         */
        final List<String> acceptHeaders = Collections.list(req.getHeaders("Accept"));
		final String acceptStr = ConnegUtil.getMimeTypeForQueryParameterQueryRequest(req
				.getParameter(BigdataRDFServlet.OUTPUT_FORMAT_QUERY_PARAMETER),
				acceptHeaders.toArray(new String[acceptHeaders.size()])); 
		
        final ConnegUtil util = new ConnegUtil(acceptStr);

        // The best RDFFormat for that Accept header.
        RDFFormat format = util.getRDFFormat();

        if (format == null)
            format = RDFFormat.RDFXML;

		RDFWriterFactory writerFactory = RDFWriterRegistry.getInstance().get(
				format);

		if (writerFactory == null) {

			if (log.isDebugEnabled()) {
				log.debug("No writer for format: format=" + format
						+ ", Accept=\"" + acceptStr + "\"");
			}

			format = RDFFormat.RDFXML;
			
			writerFactory = RDFWriterRegistry.getInstance().get(format);
			
		}

//        if (writerFactory == null) {
//
//			buildResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
//					"No writer for format: Accept=\"" + acceptStr
//							+ "\", format=" + format);
//			
//			return;
//
//		}
		
        resp.setStatus(HTTP_OK);

        resp.setContentType(format.getDefaultMIMEType());

        final OutputStream os = resp.getOutputStream();
        try {
            final RDFWriter writer = writerFactory.getWriter(os);
            writer.startRDF();
            final Iterator<Statement> itr = g.iterator();
            while (itr.hasNext()) {
                final Statement stmt = itr.next();
                writer.handleStatement(stmt);
            }
            writer.endRDF();
            os.flush();
        } catch (RDFHandlerException e) {
        	// wrap and rethrow. will be handled by launderThrowable().
            throw new IOException(e);
        } finally {
            os.close();
        }
    }