org.apache.jena.util.FileUtils Java Examples

The following examples show how to use org.apache.jena.util.FileUtils. 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: AWGUtil.java    From wings with Apache License 2.0 6 votes vote down vote up
public static void testTemplateElaboration(String domdir, String template)
		throws FileNotFoundException, IOException {
	String conf_path = domdir + "/wings.properties";
	String requestid = UUID.randomUUID().toString();
	Wings wings = new Wings(template, requestid, conf_path, true);

	wings.initializePC();
   wings.initializeRC();
	wings.initializeWorkflowGenerator();
	wings.setDC(wings.initializeDC());
	wings.initializeItem();

	Template it = wings.getWG().getInferredTemplate(wings.getTemplate());
	Assert.assertNotNull(it);

	String[] arr1 = FileUtils.readWholeFileAsUTF8(domdir + "/test1.output").split(
			"\\s*[\\[,\\]]\\s*");
	String[] arr2 = it.getConstraintEngine().getConstraints().toString()
			.split("\\s*[\\[,\\]]\\s*");
	Assert.assertEquals(arr1.length, arr2.length);

	java.util.Arrays.sort(arr1);
	java.util.Arrays.sort(arr2);
	Assert.assertArrayEquals(arr1, arr2);
}
 
Example #2
Source File: TestDASHTestCases.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static void collectTestCases(File folder, List<TestCase> testCases) throws Exception {
	for(File f : folder.listFiles()) {
		if(f.isDirectory()) {
			collectTestCases(f, testCases);
		}
		else if(f.isFile() && f.getName().endsWith(".ttl")) {
			Model testModel = JenaUtil.createDefaultModel();
			InputStream is = new FileInputStream(f);
			testModel.read(is, "urn:dummy", FileUtils.langTurtle);
			testModel.add(SHACLSystemModel.getSHACLModel());
			Resource ontology = testModel.listStatements(null, OWL.imports, ResourceFactory.createResource(DASH.BASE_URI)).next().getSubject();
			for(TestCaseType type : TestCaseTypes.getTypes()) {
				testCases.addAll(type.getTestCases(testModel, ontology));
			}
		}
	}
}
 
Example #3
Source File: RuleExample.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Loads an example SHACL-AF (rules) file and execute it against the data.
 */
public static void main(String[] args) throws Exception {

    // Load the main data model that contains rule(s)
    Model dataModel = JenaUtil.createMemoryModel();
    dataModel.read(RuleExample.class.getResourceAsStream("sh/tests/rules/triple/rectangle.test.ttl"), "urn:dummy",
            FileUtils.langTurtle);

    // Perform the rule calculation, using the data model
    // also as the rule model - you may have them separated
    Model result = RuleUtil.executeRules(dataModel, dataModel, null, null);

    // you may want to add the original data, to make sense of the rule results
    result.add(dataModel);

    // Print rule calculation results
    System.out.println(ModelPrinter.get().print(result));
}
 
Example #4
Source File: SHACLCTestRunner.java    From shacl with Apache License 2.0 6 votes vote down vote up
private void runFile(File compactFile) {
	try {
		Model compactModel = JenaUtil.createMemoryModel();
		compactModel.read(new FileReader(compactFile), "urn:x-base:default", SHACLC.langName);
		compactModel.removeAll(null, OWL.imports, ResourceFactory.createResource(DASH.BASE_URI));

		File turtleFile = new File(compactFile.getParentFile(), compactFile.getName().replaceAll(".shaclc", ".ttl"));
		Model turtleModel = JenaUtil.createMemoryModel();
		turtleModel.read(new FileReader(turtleFile), "urn:x-base:default", FileUtils.langTurtle);
		
		if(compactModel.getGraph().isIsomorphicWith(turtleModel.getGraph())) {
			System.out.println("Passed test " + compactFile);
		}
		else {
			System.err.println("Failed test " + compactFile);
			System.err.println("Turtle:  " + ModelPrinter.get().print(turtleModel));
			System.err.println("Compact: " + ModelPrinter.get().print(compactModel));
		}
	}
	catch(Exception ex) {
		System.err.println("Exception during test " + compactFile + ": " + ex);
		ex.printStackTrace();
	}
}
 
Example #5
Source File: AbstractTool.java    From shacl with Apache License 2.0 6 votes vote down vote up
AbstractTool() {
	
	InputStream shaclTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/shacl.ttl");
	Model shacl = JenaUtil.createMemoryModel();
	shacl.read(shaclTTL, SH.BASE_URI, FileUtils.langTurtle);
	shacl.add(SystemTriples.getVocabularyModel());
	dm.addModel(SH.BASE_URI, shacl);
	
	InputStream dashTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/dash.ttl");
	Model dash = JenaUtil.createMemoryModel();
	dash.read(dashTTL, SH.BASE_URI, FileUtils.langTurtle);
	dm.addModel(DASH.BASE_URI, dash);
	
	InputStream toshTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/tosh.ttl");
	Model tosh = JenaUtil.createMemoryModel();
	tosh.read(toshTTL, SH.BASE_URI, FileUtils.langTurtle);
	dm.addModel(TOSH.BASE_URI, tosh);
	
	spec.setDocumentManager(dm);
	
	SHACLC.install();
}
 
Example #6
Source File: SHACLSystemModel.java    From shacl with Apache License 2.0 6 votes vote down vote up
public static synchronized Model getSHACLModel() {
	if(shaclModel == null) {
		
		shaclModel = JenaUtil.createDefaultModel();
		
		InputStream shaclTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/shacl.ttl");
		shaclModel.read(shaclTTL, SH.BASE_URI, FileUtils.langTurtle);
		
		InputStream dashTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/dash.ttl");
		shaclModel.read(dashTTL, SH.BASE_URI, FileUtils.langTurtle);
		
		InputStream toshTTL = SHACLSystemModel.class.getResourceAsStream("/rdf/tosh.ttl");
		shaclModel.read(toshTTL, SH.BASE_URI, FileUtils.langTurtle);
		
		shaclModel.add(SystemTriples.getVocabularyModel());
		
		SHACLFunctions.registerFunctions(shaclModel);
	}
	return shaclModel;
}
 
Example #7
Source File: TestFileStore.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Test
public void fs_write_01() throws IOException {
    FileStore fs = FileStore.attach(STORE, "FILE");
    assertEquals(0, fs.getCurrentIndex());
    FileEntry entry = fs.writeNewFile(out->{
        try(AWriter aw = IO .wrapUTF8(out)) {
          aw.write("abc");
        }
    }) ;
    assertNotNull(entry);
    assertNotNull(entry.datafile);
    int idx = checkFilename(entry.datafile);
    assertEquals(1, idx);
    // Read it back in again.
    String s = FileUtils.readWholeFileAsUTF8(entry.getDatafileName());
    assertEquals("abc", s);
}
 
Example #8
Source File: Infer.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void run(String[] args) throws IOException {
	Model dataModel = getDataModel(args);
	Model shapesModel = getShapesModel(args);
	if(shapesModel == null) {
		shapesModel = dataModel;
	}
	Model results = RuleUtil.executeRules(dataModel, shapesModel, null, null);
	results.write(System.out, FileUtils.langTurtle);
}
 
Example #9
Source File: TemplateController.java    From wings with Apache License 2.0 5 votes vote down vote up
public String getBeamerMappingsJSON() {
    try {
        String beamerDir =
                config.getDomain().getDomainDirectory() + File.separator + "beamer";
        return FileUtils.readWholeFileAsUTF8(beamerDir + File.separator + "mappings.json");
    } catch (Exception e) {
        //e.printStackTrace();
    }
    return "{}";
}
 
Example #10
Source File: TemplateController.java    From wings with Apache License 2.0 5 votes vote down vote up
public String getBeamerParaphrasesJSON() {
    try {
        String beamerDir =
                config.getDomain().getDomainDirectory() + File.separator + "beamer";
        return FileUtils.readWholeFileAsUTF8(beamerDir + File.separator + "paraphrases.json");
    } catch (Exception e) {
        //e.printStackTrace();
    }
    return "{}";
}
 
Example #11
Source File: ValidationExample.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Loads an example SHACL file and validates all focus nodes against all shapes.
 */
public static void main(String[] args) throws Exception {
	
	// Load the main data model
	Model dataModel = JenaUtil.createMemoryModel();
	dataModel.read(ValidationExample.class.getResourceAsStream("/sh/tests/core/property/class-001.test.ttl"), "urn:dummy", FileUtils.langTurtle);
	
	// Perform the validation of everything, using the data model
	// also as the shapes model - you may have them separated
	Resource report = ValidationUtil.validateModel(dataModel, dataModel, true);
	
	// Print violations
	System.out.println(ModelPrinter.get().print(report.getModel()));
}
 
Example #12
Source File: Validate.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void run(String[] args) throws IOException {
	Model dataModel = getDataModel(args);
	Model shapesModel = getShapesModel(args);
	if(shapesModel == null) {
		shapesModel = dataModel;
	}
	Resource report = ValidationUtil.validateModel(dataModel, shapesModel, true);
	report.getModel().write(System.out, FileUtils.langTurtle);

	if(report.hasProperty(SH.conforms, JenaDatatypes.FALSE)) {
		// See https://github.com/TopQuadrant/shacl/issues/56
		System.exit(1);
	}
}
 
Example #13
Source File: AbstractTool.java    From shacl with Apache License 2.0 5 votes vote down vote up
protected Model getShapesModel(String[] args) throws IOException {
	for(int i = 0; i < args.length - 1; i++) {
		if(SHAPES_FILE.equals(args[i])) {
			String fileName = args[i + 1];
			OntModel model = ModelFactory.createOntologyModel(spec);
			File file = new File(fileName);
			String lang = file.getName().endsWith(".shaclc") ? SHACLC.langName : FileUtils.langTurtle;
			model.read(new FileInputStream(file), "urn:x:base", lang);
			return model;
		}
	}
	return null;
}
 
Example #14
Source File: AbstractTool.java    From shacl with Apache License 2.0 5 votes vote down vote up
protected Model getDataModel(String[] args) throws IOException {
	for(int i = 0; i < args.length - 1; i++) {
		if(DATA_FILE.equals(args[i])) {
			String dataFileName = args[i + 1];
			OntModel dataModel = ModelFactory.createOntologyModel(spec);
			File file = new File(dataFileName);
			String lang = file.getName().endsWith(".shaclc") ? SHACLC.langName : FileUtils.langTurtle;
			dataModel.read(new FileInputStream(file), "urn:x:base", lang);
			return dataModel;
		}
	}
	System.err.println("Missing -datafile, e.g.: -datafile myfile.ttl");
	System.exit(0);
	return null;
}
 
Example #15
Source File: W3CTestRunner.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void collectItems(File manifestFile, String baseURI) throws IOException {
	
	String filePath = manifestFile.getAbsolutePath().replaceAll("\\\\", "/");
	int coreIndex = filePath.indexOf("core/");
	if(coreIndex > 0 && !filePath.contains("sparql/core")) {
		filePath = filePath.substring(coreIndex);
	}
	else {
		int sindex = filePath.indexOf("sparql/");
		if(sindex > 0) {
			filePath = filePath.substring(sindex);
		}
	}
	
	Model model = JenaUtil.createMemoryModel();
	model.read(new FileInputStream(manifestFile), baseURI, FileUtils.langTurtle);
	
	for(Resource manifest : model.listSubjectsWithProperty(RDF.type, MF.Manifest).toList()) {
		for(Resource include : JenaUtil.getResourceProperties(manifest, MF.include)) {
			String path = include.getURI().substring(baseURI.length());
			File includeFile = new File(manifestFile.getParentFile(), path);
			if(path.contains("/")) {
				String addURI = path.substring(0, path.indexOf('/'));
				collectItems(includeFile, baseURI + addURI + "/");
			}
			else {
				collectItems(includeFile, baseURI + path);
			}
		}
		for(Resource entries : JenaUtil.getResourceProperties(manifest, MF.entries)) {
			for(RDFNode entry : entries.as(RDFList.class).iterator().toList()) {
				items.add(new Item(entry.asResource(), filePath, manifestFile));
			}
		}
	}
}
 
Example #16
Source File: FunctionTestCaseType.java    From shacl with Apache License 2.0 5 votes vote down vote up
private Graph parseGraph(RDFNode node) {
	Model model = JenaUtil.createDefaultModel();
	if(node.isLiteral()) {
		String str = node.asLiteral().getLexicalForm();
		model.read(new ByteArrayInputStream(str.getBytes()), "urn:x:dummy", FileUtils.langTurtle);
	}
	return model.getGraph();
}
 
Example #17
Source File: LocatorFileAccept.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * Processing the filename for file: or relative filename and return a
 * filename suitable for file operations.
 */
public String toFileName(String filenameIRI) {
    // Do not use directly : it will ignore the directory. 
    //IRILib.filenameToIRI

    String scheme = FileUtils.getScheme(filenameIRI);
    String fn = filenameIRI;
    // Windows : C:\\ is not a scheme name!
    if (scheme != null) {
        if (scheme.length() == 1) {
            // Not perfect for MS Windows but if thisDir is set then
            // the main use case is resolving relative (no drive)
            // filenames against thisDir. Treat the presence of a
            // drive letter as making this a JVM relative filename. 
            return fn;
        } else if (scheme.length() > 1) {
            if (!scheme.equalsIgnoreCase("file")) // Not file: IRI
            {
                return null;
            }
            fn = IRILib.IRIToFilename(filenameIRI);
            // fall through
        }
    }
    // fn is the file name to use.
    return absolute(fn);
}
 
Example #18
Source File: ModelPrinter.java    From shacl with Apache License 2.0 4 votes vote down vote up
protected RDFWriter createRDFWriter(Model model) {
	return model.getWriter(FileUtils.langTurtle);
}
 
Example #19
Source File: TestValidatorConfiguration.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Test
public void testMaxErrors() {
    Model dataModel = JenaUtil.createMemoryModel();
    dataModel.read(ValidationExample.class.getResourceAsStream("/sh/tests/core/property/class-001.test.ttl"), "urn:dummy", FileUtils.langTurtle);

    ValidationEngineConfiguration configuration = new ValidationEngineConfiguration();
    configuration.setValidationErrorBatch(-1);

    Resource reportNoMaximum = ValidationUtil.validateModel(dataModel, dataModel, configuration);

    Model resultModel = reportNoMaximum.getModel();
    assert(resultModel.listStatements(null, SH.resultSeverity, SH.Violation).toList().size() == 2);


    configuration.setValidationErrorBatch(1);
    Resource reportMaximum = ValidationUtil.validateModel(dataModel, dataModel, configuration);

    resultModel = reportMaximum.getModel();
    System.out.println("FOUND " + resultModel.listStatements(null, SH.resultSeverity, SH.Violation).toList().size() );
    assert(resultModel.listStatements(null, SH.resultSeverity, SH.Violation).toList().size() == 1);

}
 
Example #20
Source File: SystemTriples.java    From shacl with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the system ontology (a shared copy).
 * @return the system ontology
 */
public static synchronized Model getVocabularyModel() {
	if (vocabulary == null) {
		vocabulary = JenaUtil.createDefaultModel();
		org.topbraid.jenax.util.JenaUtil.initNamespaces(vocabulary.getGraph());
		vocabulary.setNsPrefix("xsd", XSD.getURI());
		InputStream ttl = SystemTriples.class.getResourceAsStream("/rdf/system-triples.ttl");
		vocabulary.read(ttl, "urn:x:dummy", FileUtils.langTurtle);
		ensureSuperClasses(RDFS.Class, RDFS.Resource);
		ensureSuperClasses(OWL.Class, OWL.Thing);
		
		// Remove owl imports rdfs which only causes trouble
		vocabulary.removeAll(null, OWL.imports, null); 
		
		vocabulary.add(OWL.Thing, RDFS.subClassOf, RDFS.Resource);
		vocabulary.add(OWL.inverseOf, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentClass, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentProperty, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentProperty, RDFS.range, RDF.Property);
		vocabulary.add(OWL.differentFrom, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.sameAs, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.disjointWith, RDF.type, OWL.SymmetricProperty);
		Resource xml = vocabulary.getResource(XMLLiteralType.theXMLLiteralType.getURI());
		vocabulary.add(xml, RDFS.subClassOf, RDFS.Resource);
		for(String uri : JenaDatatypes.getDatatypeURIs()) {
			Resource r = vocabulary.getResource(uri);
			if (r.getProperty(RDF.type) == null) {
				vocabulary.add(r, RDF.type, RDFS.Datatype);
				vocabulary.add(r, RDFS.subClassOf, RDFS.Literal);
			}
		}
		
		vocabulary.add(RDF.HTML, RDFS.label, "HTML");
		
		// Triples were formally in OWL 1, but dropped from OWL 2
		vocabulary.add(RDFS.comment, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.label, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.isDefinedBy, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.seeAlso, RDF.type, OWL.AnnotationProperty);
		
		// Add rdfs:labels for XSD types
		for(Resource datatype : vocabulary.listSubjectsWithProperty(RDF.type, RDFS.Datatype).toList()) {
			datatype.addProperty(RDFS.label, datatype.getLocalName());
           }
		vocabulary = JenaUtil.asReadOnlyModel(vocabulary);
	}
	return vocabulary;
}
 
Example #21
Source File: AssemblerFileLog.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Override
    public Object open(Assembler a, Resource root, Mode mode) {
        if ( !exactlyOneProperty(root, VocabPatch.pDataset) )
            throw new AssemblerException(root, "No dataset to be logged");
        if ( !root.hasProperty(VocabPatch.pLogFile) )
            throw new AssemblerException(root, "No log file");

        Resource dataset = GraphUtils.getResourceValue(root, VocabPatch.pDataset);
        List<String> destLogs =  GraphUtils.multiValueAsString(root, VocabPatch.pLogFile);

        String logPolicy = GraphUtils.getStringValue(root, VocabPatch.pLogPolicy);
        FilePolicy policy = logPolicy == null ? FilePolicy.FIXED : FilePolicy.policy(logPolicy);

        DatasetGraph dsgBase;;
        try {
            Dataset dsBase = (Dataset)a.open(dataset);
            dsgBase = dsBase.asDatasetGraph();
        } catch (Exception ex) {
            FmtLog.error(this.getClass(), "Failed to build the dataset to adding change logging to: %s",dataset);
            throw ex;
        }

        RDFChanges changes = null;

        // It would be better if each file had a policy.
        //   patch:logFile [ patch:filename ; patch:policy ];
        //   patch:logFile ("FILE" "FIXED");
        for ( String x : destLogs ) {
            FmtLog.info(LOG, "Log file: '%s'", x);
            if ( x.startsWith("file:") )
                x = IRILib.IRIToFilename(x);

            ManagedOutput output = OutputMgr.create(x, policy);
            // --------------
            String ext = FileUtils.getFilenameExt(x);
//            if ( ext.equals("gz") ) {
//                String fn2 = x.substring(0, ".gz".length());
//                ext = FileUtils.getFilenameExt(fn2);
//            }
//            OutputStream out = IO.openOutputFile(x);

            boolean binaryPatches = ext.equalsIgnoreCase(RDFPatchConst.EXT_B);
            RDFChanges sc = binaryPatches
                ? null //RDFPatchOps.binaryWriter(out);
                //: RDFPatchOps.textWriter(out);
                : new RDFChangesManagedOutput(output);

            if ( sc == null )
                throw new AssemblerException(root, "Failed to build the output destination: "+x);
            changes = RDFChangesN.multi(changes, sc);
        }
        DatasetGraph dsg = RDFPatchOps.changes(dsgBase, changes);
        Dataset ds = DatasetFactory.wrap(dsg);
        return ds;
    }