Java Code Examples for org.eclipse.rdf4j.model.Model#setNamespace()

The following examples show how to use org.eclipse.rdf4j.model.Model#setNamespace() . 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: RepositoryConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Exports the configuration into RDF using the given repositoryNode
 *
 * @param model          target RDF collection
 * @param repositoryNode
 * @since 2.3
 */
public void export(Model model, Resource repositoryNode) {
	ValueFactory vf = SimpleValueFactory.getInstance();
	model.setNamespace(RDFS.NS);
	model.setNamespace(XMLSchema.NS);
	model.setNamespace("rep", NAMESPACE);
	model.add(repositoryNode, RDF.TYPE, REPOSITORY);

	if (id != null) {
		model.add(repositoryNode, REPOSITORYID, vf.createLiteral(id));
	}
	if (title != null) {
		model.add(repositoryNode, RDFS.LABEL, vf.createLiteral(title));
	}
	if (implConfig != null) {
		Resource implNode = implConfig.export(model);
		model.add(repositoryNode, REPOSITORYIMPL, implNode);
	}
}
 
Example 2
Source File: FedXRepositoryConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Resource export(Model m) {

	Resource implNode = super.export(m);

	m.setNamespace("fedx", NAMESPACE);
	if (getDataConfig() != null) {
		m.add(implNode, DATA_CONFIG, vf.createLiteral(getDataConfig()));
	}

	if (getMembers() != null) {

		Model members = getMembers();
		Set<Resource> memberNodes = members.subjects();
		for (Resource memberNode : memberNodes) {
			m.add(implNode, MEMBER, memberNode);
			m.addAll(members.filter(memberNode, null, null));
		}
	}

	return implNode;
}
 
Example 3
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextIRIWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, uri1));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, uri1));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1, uri1).size());
		assertEquals(1, parsedOutput.filter(null, uri1, uri2, uri1).size());
	} else {
		assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
		assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example 4
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateNoContextWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example 5
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteSingleStatementSubjectBNodeSingleContextBNodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example 6
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteSingleStatementNoBNodesSingleContextBnodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, uri1, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(uri1, uri1, uri1).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
}
 
Example 7
Source File: AbstractSailImplConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Resource export(Model m) {
	ValueFactory vf = SimpleValueFactory.getInstance();
	BNode implNode = vf.createBNode();

	m.setNamespace("sail", SailConfigSchema.NAMESPACE);
	if (type != null) {
		m.add(implNode, SAILTYPE, vf.createLiteral(type));
	}

	if (iterationCacheSyncThreshold > 0) {
		m.add(implNode, SailConfigSchema.ITERATION_CACHE_SYNC_THRESHOLD,
				vf.createLiteral(iterationCacheSyncThreshold));
	}

	if (connectionTimeOut > 0) {
		m.add(implNode, SailConfigSchema.CONNECTION_TIME_OUT, vf.createLiteral(connectionTimeOut));
	}
	return implNode;
}
 
Example 8
Source File: ElasticsearchStoreConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Resource export(Model graph) {
	Resource implNode = super.export(graph);

	graph.setNamespace(ElasticsearchStoreSchema.PREFIX, NAMESPACE);
	if (hostname != null) {
		graph.add(implNode, ElasticsearchStoreSchema.hostname, vf.createLiteral(hostname));
	}
	if (clusterName != null) {
		graph.add(implNode, ElasticsearchStoreSchema.clusterName, vf.createLiteral(clusterName));
	}
	if (index != null) {
		graph.add(implNode, ElasticsearchStoreSchema.index, vf.createLiteral(index));
	}
	if (port != -1) {
		graph.add(implNode, ElasticsearchStoreSchema.port, vf.createLiteral(port));
	}

	return implNode;
}
 
Example 9
Source File: ArrangedWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteSingleStatementWithSubjectNamespace() {
	String otherNs = "http://example.net/";
	IRI uri0 = vf.createIRI(otherNs, "uri0");

	Model input = new LinkedHashModel();
	input.add(vf.createStatement(uri0, uri1, uri2));

	input.setNamespace("org", exNs);
	input.setNamespace("net", otherNs);

	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);

	String sep = System.lineSeparator();
	String expectedResult = "@prefix net: <http://example.net/> ." + sep +
			"@prefix org: <http://example.org/> ." + sep + sep +
			"net:uri0 org:uri1 org:uri2 ." + sep;

	assertEquals(expectedResult, outputWriter.toString());
}
 
Example 10
Source File: ShaclSailConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Resource export(Model m) {
	Resource implNode = super.export(m);

	m.setNamespace("sail-shacl", NAMESPACE);
	m.add(implNode, PARALLEL_VALIDATION, BooleanLiteral.valueOf(isParallelValidation()));
	m.add(implNode, UNDEFINED_TARGET_VALIDATES_ALL_SUBJECTS,
			BooleanLiteral.valueOf(isUndefinedTargetValidatesAllSubjects()));
	m.add(implNode, LOG_VALIDATION_PLANS, BooleanLiteral.valueOf(isLogValidationPlans()));
	m.add(implNode, LOG_VALIDATION_VIOLATIONS, BooleanLiteral.valueOf(isLogValidationViolations()));
	m.add(implNode, IGNORE_NO_SHAPES_LOADED_EXCEPTION, BooleanLiteral.valueOf(isIgnoreNoShapesLoadedException()));
	m.add(implNode, VALIDATION_ENABLED, BooleanLiteral.valueOf(isValidationEnabled()));
	m.add(implNode, CACHE_SELECT_NODES, BooleanLiteral.valueOf(isCacheSelectNodes()));
	m.add(implNode, GLOBAL_LOG_VALIDATION_EXECUTION, BooleanLiteral.valueOf(isGlobalLogValidationExecution()));
	m.add(implNode, RDFS_SUB_CLASS_REASONING, BooleanLiteral.valueOf(isRdfsSubClassReasoning()));
	m.add(implNode, PERFORMANCE_LOGGING, BooleanLiteral.valueOf(isPerformanceLogging()));
	m.add(implNode, SERIALIZABLE_VALIDATION, BooleanLiteral.valueOf(isSerializableValidation()));
	return implNode;
}
 
Example 11
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteOneStatementsObjectBNodeSinglePredicateSingleContextBNodeReusedWithNamespace()
		throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, bnode, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	Model doubleBNodeStatement = parsedOutput.filter(uri1, uri1, null);
	assertEquals(1, doubleBNodeStatement.size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.objects().size());
	assertTrue(parsedOutput.objects().iterator().next() instanceof BNode);
	assertTrue(doubleBNodeStatement.objects().iterator().next() instanceof BNode);
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertEquals(doubleBNodeStatement.objects().iterator().next(),
				doubleBNodeStatement.contexts().iterator().next());
	}
}
 
Example 12
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsSubjectBNodeSinglePredicateSingleContextBNodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1, bnode));
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri2, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri1).size());
	assertEquals(1, parsedOutput.filter(null, uri1, uri2).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example 13
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, bnodeSingleUseObject, bnode));
	input.add(vf.createStatement(uri1, uri2, bnodeSingleUseObject, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertEquals(1, parsedOutput.filter(uri1, uri1, null).size());
	assertEquals(1, parsedOutput.filter(uri1, uri2, null).size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.objects().size());
	assertTrue(parsedOutput.objects().iterator().next() instanceof BNode);
}
 
Example 14
Source File: ShaclSailValidationException.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @return A Model containing the validation report as specified by the SHACL Recommendation
 */
@Override
public Model validationReportAsModel() {

	ValidationReport validationReport = getValidationReport();

	Model model = validationReport.asModel();
	model.setNamespace(SHACL.PREFIX, SHACL.NAMESPACE);
	return model;

}
 
Example 15
Source File: ProxyRepositoryConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resource export(Model model) {
	Resource implNode = super.export(model);
	if (null != this.proxiedID) {
		model.setNamespace("proxy", ProxyRepositorySchema.NAMESPACE);
		model.add(implNode, ProxyRepositorySchema.PROXIED_ID,
				SimpleValueFactory.getInstance().createLiteral(this.proxiedID));
	}
	return implNode;
}
 
Example 16
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteSingleStatementNoBNodesNoContextWithNamespace() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, uri1));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(vf.createStatement(uri1, uri1, uri1)));
}
 
Example 17
Source File: ArrangedWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteRepeatedInlineBlankNode() {
	Model model = new ModelBuilder().subject(exNs + "subject")
			.add(vf.createIRI(exNs, "rel1"), bnode1)
			.add(vf.createIRI(exNs, "rel2"), bnode1)
			.add(vf.createIRI(exNs, "rel3"), bnode2)
			.subject(bnode1)
			.add(RDFS.LABEL, "the bnode1")
			.subject(bnode2)
			.add(RDFS.LABEL, "the bnode2")
			.build();

	model.setNamespace(RDFS.NS);
	model.setNamespace("ex", exNs);

	StringWriter stringWriter = new StringWriter();
	BufferedWriter writer = new BufferedWriter(stringWriter);
	WriterConfig config = new WriterConfig();
	config.set(BasicWriterSettings.INLINE_BLANK_NODES, true);
	Rio.write(model, writer, RDFFormat.TURTLE, config);

	String sep = System.lineSeparator();
	String expectedResult = "@prefix ex: <http://example.org/> ." + sep +
			"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ." + sep +
			"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> ." + sep + sep +
			"ex:subject ex:rel1 _:bnode1 ." + sep + sep +
			"_:bnode1 rdfs:label \"the bnode1\" ." + sep + sep +
			"ex:subject ex:rel2 _:bnode1;" + sep +
			"  ex:rel3 [" + sep +
			"      rdfs:label \"the bnode2\"" + sep +
			"    ] ." + sep;

	assertEquals(expectedResult, stringWriter.toString());
}
 
Example 18
Source File: MemoryStoreConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resource export(Model graph) {
	Resource implNode = super.export(graph);

	graph.setNamespace("ms", NAMESPACE);
	if (persist) {
		graph.add(implNode, PERSIST, BooleanLiteral.TRUE);
	}

	if (syncDelay != 0) {
		graph.add(implNode, SYNC_DELAY, SimpleValueFactory.getInstance().createLiteral(syncDelay));
	}

	return implNode;
}
 
Example 19
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeReusedWithNamespace()
		throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, bnodeSingleUseObject, bnode));
	input.add(vf.createStatement(bnode, uri2, bnodeSingleUseObject, bnode));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	Model doubleBNodeStatement = parsedOutput.filter(uri1, uri1, null);
	assertEquals(1, doubleBNodeStatement.size());
	Model tripleBNodeStatement = parsedOutput.filter(null, uri2, null);
	assertEquals(1, tripleBNodeStatement.size());
	assertEquals(1, parsedOutput.contexts().size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contexts().iterator().next() instanceof BNode);
	}
	assertEquals(1, parsedOutput.objects().size());
	assertTrue(parsedOutput.objects().iterator().next() instanceof BNode);
	assertTrue(tripleBNodeStatement.subjects().iterator().next() instanceof BNode);
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertEquals(tripleBNodeStatement.subjects().iterator().next(),
				doubleBNodeStatement.contexts().iterator().next());
	}
}
 
Example 20
Source File: SHACLManifestTestSuiteFactory.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private IRI readTurtle(Model manifests, URL url, String baseURI, String... excludedSubdirs)
		throws IOException, RDFParseException {
	if (baseURI == null) {
		baseURI = url.toExternalForm();
	}
	ParsedIRI baseIRI = ParsedIRI.create(baseURI);
	SimpleValueFactory vf = SimpleValueFactory.getInstance();
	IRI manifest = vf.createIRI(baseIRI.toString());
	int before = manifests.size();

	try (InputStream in = url.openStream()) {
		RDFParser rdfParser = new TurtleParser();

		ContextStatementCollector collection = new ContextStatementCollector(manifests, vf, manifest);
		rdfParser.setRDFHandler(collection);

		rdfParser.parse(in, baseIRI.toString());
		for (Map.Entry<String, String> e : collection.getNamespaces().entrySet()) {
			manifests.setNamespace(e.getKey(), e.getValue());
		}
	}
	if (before < manifests.size()) {
		for (Value included : new LinkedHashSet<>(
				manifests.filter(manifest, vf.createIRI(MF_INCLUDE), null).objects())) {
			String subManifestFile = included.stringValue();
			if (includeSubManifest(subManifestFile, excludedSubdirs)) {
				readTurtle(manifests, new URL(subManifestFile), subManifestFile, excludedSubdirs);
			}
		}
	}
	return manifest;
}