Java Code Examples for org.openrdf.rio.RDFFormat#forFileName()

The following examples show how to use org.openrdf.rio.RDFFormat#forFileName() . 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
Source File: AST2BOpUpdate.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 
 * 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
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private RDFFormat forFileName(String path, RDFFormat fallback) {
	RDFFormat format = RDFFormat.forFileName(path);
	RDFParserRegistry registry = RDFParserRegistry.getInstance();
	if (format != null && registry.has(format))
		return format;
	return fallback;
}
 
Example 3
Source File: AbstractDataDrivenSPARQLTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load some RDF data.
 * 
 * @param resource
 *            The resource whose data will be loaded.
 * 
 * @return The #of statements parsed from the source. If there are
 *         duplicate told statements, then there may be fewer statements
 *         written onto the KB.
 */
protected long loadData(final String resource) {

    if (log.isInfoEnabled())
        log.info("Loading " + resource);
    
    final String baseURL = new File(resource).toURI().toString();

    InputStream is = null;
    try {

        is = getResourceAsStream(resource);

        final RDFFormat rdfFormat = RDFFormat.forFileName(resource);

        if (rdfFormat == null)
            throw new RuntimeException("Unknown format: resource="
                    + resource);

        // final RDFFormat rdfFormat = guessFormat(new File(resource),
        // null/* default */);

        return loadData(is, rdfFormat, baseURL);

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error("Could not close: resource=" + resource, e);
            }
            is = null;
        }
    }


}
 
Example 4
Source File: AbstractDataDrivenSPARQLTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load some RDF data.
 * 
 * @param resource
 *            The resource whose data will be loaded.
 * 
 * @return The #of statements parsed from the source. If there are
 *         duplicate told statements, then there may be fewer statements
 *         written onto the KB.
 */
protected long loadData(final String resource) {

    if (log.isInfoEnabled())
        log.info("Loading " + resource);
    
    final String baseURL = new File(resource).toURI().toString();

    InputStream is = null;
    try {

        is = getResourceAsStream(resource);

        final RDFFormat rdfFormat = RDFFormat.forFileName(resource);

        if (rdfFormat == null)
            throw new RuntimeException("Unknown format: resource="
                    + resource);

        // final RDFFormat rdfFormat = guessFormat(new File(resource),
        // null/* default */);

        return loadData(is, rdfFormat, baseURL);

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error("Could not close: resource=" + resource, e);
            }
            is = null;
        }
    }


}
 
Example 5
Source File: AbstractRDFTaskFactory.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Guess at the {@link RDFFormat}.
 * 
 * @param filename
 *            Some filename.
 * 
 * @return The {@link RDFFormat} -or- <code>null</code> iff
 *         {@link #fallback} is <code>null</code> and the no format
 *         was recognized for the <i>filename</i>
 */
public RDFFormat getRDFFormat(String filename) {

    final RDFFormat rdfFormat = //
    fallback == null //
    ? RDFFormat.forFileName(filename) //
            : RDFFormat.forFileName(filename, fallback)//
    ;

    return rdfFormat;

}
 
Example 6
Source File: VocabBuilder.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public boolean accept(final File dir, final String name) {

            if (new File(dir, name).isDirectory()) {

                if(dir.isHidden()) {
                    
                    // Skip hidden files.
                    return false;
                    
                }
                
//                if(dir.getName().equals(".svn")) {
//                    
//                    // Skip .svn files.
//                    return false;
//                    
//                }
                
                // visit subdirectories.
                return true;
                
            }

            // if recognizable as RDF.
            boolean isRDF = RDFFormat.forFileName(name) != null
                    || (name.endsWith(".zip") && RDFFormat.forFileName(name
                            .substring(0, name.length() - 4)) != null)
                    || (name.endsWith(".gz") && RDFFormat.forFileName(name
                            .substring(0, name.length() - 3)) != null);

			if (log.isDebugEnabled())
				log.debug("dir=" + dir + ", name=" + name + " : isRDF=" + isRDF);

            return isRDF;

        }
 
Example 7
Source File: FileSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
@Override
public void loadDataFromURL(String stringURL) throws RepositoryException, RDFParseException, IOException {

    URL url = new URL(stringURL);

    RDFFormat format = RDFFormat.forFileName(stringURL);

    if (this.format.equals(format)) {
        //append to file
    } else {
        //convert, then append to file
    }

}
 
Example 8
Source File: MutagDataSetTest.java    From mustard with MIT License 5 votes vote down vote up
@Test
public void test() {
	RDFDataSet ts = new RDFFileDataSet("datasets/carcinogenesis.owl", RDFFormat.forFileName("datasets/carcinogenesis.owl"));
	
	MutagDataSet ds = new MutagDataSet(ts);
	ds.create();
}
 
Example 9
Source File: GraphLoader.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the best guess at the {@link RDFFormat} for a resource.
 * <p>
 * Note: This handles the .gz and .zip extensions.
 * 
 * @param n
 *            The name of the resource.
 * @param rdfFormat
 *            The fallback format (optional).
 *            
 * @return The best guess format.
 */
private RDFFormat guessRDFFormat(final String n, final RDFFormat rdfFormat) {

    RDFFormat fmt = RDFFormat.forFileName(n);

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

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

    if (fmt == null) // fallback
        fmt = rdfFormat;

    return fmt;

}
 
Example 10
Source File: GraphStatisticsExperiment.java    From mustard with MIT License 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {

	//RDFDataSet tripleStore = new RDFFileDataSet(AIFB_FILE, RDFFormat.N3);
	//ClassificationDataSet ds = new AIFBDataSet(tripleStore);

	//RDFDataSet tripleStore = new RDFFileDataSet(BGS_FOLDER, RDFFormat.NTRIPLES);
	//ClassificationDataSet ds = new BGSLithoDataSet(tripleStore);
	
	RDFDataSet tripleStore = new RDFFileDataSet("datasets/carcinogenesis.owl", RDFFormat.forFileName("datasets/carcinogenesis.owl"));
	ClassificationDataSet ds = new MutagDataSet(tripleStore);

	ds.create();



	boolean[] inference = {false, true};
	int[] depths = {1};


	
	for (boolean inf : inference) {
		for (int depth : depths) {

			Set<Statement> st = RDFUtils.getStatements4Depth(tripleStore, ds.getRDFData().getInstances(), depth, inf);
			st.removeAll(ds.getRDFData().getBlackList());
			SingleDTGraph graph = RDFUtils.statements2Graph(st, RDFUtils.REGULAR_LITERALS, ds.getRDFData().getInstances(), true);


			GraphList<DTGraph<String,String>> graphs = RDFUtils.getSubGraphs(graph.getGraph(), graph.getInstances(), depth);
			System.out.println("Graph, inf: " + inf + ", depth: " + depth + ", " + computeGraphStatistics(graphs));

			graphs = RDFUtils.getSubTrees(graph.getGraph(), graph.getInstances(), depth);
			System.out.println("Tree,  inf: " + inf + ", depth: " + depth + ", " + computeGraphStatistics(graphs));
		}

	}
	
	/*
	int[] depths = {1};

	String[] subsets = {"datasets/AMsubset1false", "datasets/AMsubset2false", "datasets/AMsubset3false", "datasets/AMsubset4false", 
			"datasets/AMsubset5false", "datasets/AMsubset6false", "datasets/AMsubset7false",
			"datasets/AMsubset8false", "datasets/AMsubset9false", "datasets/AMsubset10false"};
	
	*/
	
	/*
	String[] subsets = {"datasets/AMsubset1true", "datasets/AMsubset2true", "datasets/AMsubset3true", "datasets/AMsubset4true", 
			"datasets/AMsubset5true", "datasets/AMsubset6true", "datasets/AMsubset7true",
			"datasets/AMsubset8true", "datasets/AMsubset9true", "datasets/AMsubset10true"};
	
	String[] subsets = {"datasets/BGSsubset1false", "datasets/BGSsubset2false", "datasets/BGSsubset3false", "datasets/BGSsubset4false", 
			"datasets/BGSsubset5false", "datasets/BGSsubset6false", "datasets/BGSsubset7false",
			"datasets/BGSsubset8false", "datasets/BGSsubset9false", "datasets/BGSsubset10false"};
	
	String[] subsets = {"datasets/BGSsubset1true", "datasets/BGSsubset2true", "datasets/BGSsubset3true", "datasets/BGSsubset4true", 
			"datasets/BGSsubset5true", "datasets/BGSsubset6true", "datasets/BGSsubset7true",
			"datasets/BGSsubset8true", "datasets/BGSsubset9true", "datasets/BGSsubset10true"};
			
			//*/
	

	/*
	for (int depth : depths) {
		GraphList<DTGraph<String,String>> graphs = new GraphList<DTGraph<String,String>>(new ArrayList<DTGraph<String,String>>());
		GraphList<DTGraph<String,String>> trees = new GraphList<DTGraph<String,String>>(new ArrayList<DTGraph<String,String>>());
		
		for (String subset : subsets) {
			ClassificationDataSet ds = new SubsetDataSet(subset);
			ds.create();
			Set<Statement> st = RDFUtils.getStatements4Depth(ds.getRDFData().getDataset(), ds.getRDFData().getInstances(), depth, false);
			st.removeAll(ds.getRDFData().getBlackList());
			SingleDTGraph graph = RDFUtils.statements2Graph(st, RDFUtils.REGULAR_LITERALS, ds.getRDFData().getInstances(), true);

			graphs.getGraphs().addAll(RDFUtils.getSubGraphs(graph.getGraph(), graph.getInstances(), depth).getGraphs());
			trees.getGraphs().addAll(RDFUtils.getSubTrees(graph.getGraph(), graph.getInstances(), depth).getGraphs());
		}
		
		System.out.println("Graph, depth: " + depth + ", " + computeGraphStatistics(graphs));		
		System.out.println("Tree, depth: " + depth + ", " + computeGraphStatistics(trees));
	}
	*/
}
 
Example 11
Source File: AbstractTestNanoSparqlClient.java    From database with GNU General Public License v2.0 2 votes vote down vote up
protected static Graph readGraphFromFile(final File file) throws RDFParseException, RDFHandlerException, IOException {
    
    final RDFFormat format = RDFFormat.forFileName(file.getName());
    
    final RDFParserFactory rdfParserFactory = RDFParserRegistry
            .getInstance().get(format);

    if (rdfParserFactory == null) {
        throw new RuntimeException("Parser not found: file=" + file
                + ", format=" + format);
    }

    final RDFParser rdfParser = rdfParserFactory
            .getParser();

    rdfParser.setValueFactory(new ValueFactoryImpl());

    rdfParser.setVerifyData(true);

    rdfParser.setStopAtFirstError(true);

    rdfParser
            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

    final StatementCollector rdfHandler = new StatementCollector();
    
    rdfParser.setRDFHandler(rdfHandler);

    /*
     * Run the parser, which will cause statements to be
     * inserted.
     */

    final FileReader r = new FileReader(file);
    try {
        rdfParser.parse(r, file.toURI().toString()/* baseURL */);
    } finally {
        r.close();
    }
    
    final Graph g = new LinkedHashModel();
    
    g.addAll(rdfHandler.getStatements());

    return g;

}
 
Example 12
Source File: AsynchronousStatementBufferFactory.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return a task to parse the document. The task should allocate an
 * {@link AsynchronousStatementBufferImpl} for the document. When
 * that buffer is flushed, the document will be queued for further
 * processing.
 * 
 * @param resource
 *            The resource to be parsed.
 * @return The task to execute.
 * 
 * @throws Exception
 */
protected Callable<?> newParserTask(final R resource) throws Exception {

    final String resourceStr = resource.toString();
    
    if (log.isInfoEnabled())
        log.info("resource=" + resourceStr);
    
    final RDFFormat defaultFormat = getDefaultRDFFormat();
    /* @todo This might be ignorant of .gz and .zip extensions.
     * @todo when resource is URL use reported MimeTYPE also.
     */
    final RDFFormat rdfFormat = (defaultFormat == null //
            ? RDFFormat.forFileName(resourceStr) //
            : RDFFormat.forFileName(resourceStr, defaultFormat)//
    );
    
    if (rdfFormat == null) {
        
        final String msg = "Could not determine interchange syntax - skipping : file="
                + resource;

        log.error(msg);
        
        throw new RuntimeException(msg);

    }

    // Convert the resource identifier to a URL.
    final String baseURI; ;
    if (getClass().getResource(resourceStr) != null) {
        
        baseURI = getClass().getResource(resourceStr).toURI()
                .toString();
        
    } else {
        
        baseURI = new File(resourceStr).toURI().toString();
        
    }

    return new ParserTask(resource, baseURI, rdfFormat);

}