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

The following examples show how to use org.eclipse.rdf4j.model.Model#add() . 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: ConfigViewTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRender() throws Exception {

	ConfigView configView = ConfigView.getInstance();

	Model configData = new LinkedHashModelFactory().createEmptyModel();
	configData.add(RDF.ALT, RDF.TYPE, RDFS.CLASS);

	Map<Object, Object> map = new LinkedHashMap<>();
	map.put(ConfigView.HEADERS_ONLY, false);
	map.put(ConfigView.CONFIG_DATA_KEY, configData);
	map.put(ConfigView.FORMAT_KEY, RDFFormat.NTRIPLES);

	final MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.GET.name());
	request.addHeader("Accept", RDFFormat.NTRIPLES.getDefaultMIMEType());

	MockHttpServletResponse response = new MockHttpServletResponse();

	configView.render(map, request, response);

	String ntriplesData = response.getContentAsString();
	Model renderedData = Rio.parse(new StringReader(ntriplesData), "", RDFFormat.NTRIPLES);
	assertThat(renderedData).isNotEmpty();
}
 
Example 2
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 3
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 4
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 5
Source File: RyaAccumuloSailConfig.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public Resource export(final Model model) {
    final Resource implNode = super.export(model);

    @SuppressWarnings("deprecation")
    final
    ValueFactory v = model.getValueFactory();

    model.add(implNode, USER, v.createLiteral(user));
    model.add(implNode, PASSWORD, v.createLiteral(password));
    model.add(implNode, INSTANCE, v.createLiteral(instance));
    model.add(implNode, ZOOKEEPERS, v.createLiteral(zookeepers));
    model.add(implNode, IS_MOCK, v.createLiteral(isMock));

    return implNode;
}
 
Example 6
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 7
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 8
Source File: HBaseSailConfig.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Stores configuration into the given Model
 * @param graph Model to store configuration into
 * @return Resource node with the configuration within the Model
 */
@Override
public Resource export(Model graph) {
    Resource implNode = super.export(graph);
    ValueFactory vf = SimpleValueFactory.getInstance();
    if (tablespace != null) graph.add(implNode, HALYARD.TABLE_NAME_PROPERTY, vf.createLiteral(tablespace));
    graph.add(implNode, HALYARD.SPLITBITS_PROPERTY, vf.createLiteral(splitBits));
    graph.add(implNode, HALYARD.CREATE_TABLE_PROPERTY, vf.createLiteral(create));
    graph.add(implNode, HALYARD.PUSH_STRATEGY_PROPERTY, vf.createLiteral(push));
    graph.add(implNode, HALYARD.EVALUATION_TIMEOUT_PROPERTY, vf.createLiteral(evaluationTimeout));
    graph.add(implNode, HALYARD.ELASTIC_INDEX_URL_PROPERTY, vf.createLiteral(elasticIndexURL));
    return implNode;
}
 
Example 9
Source File: EntityModelWriter.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addRelationForMrefTypeAttribute(
    Model model, Resource subject, IRI predicate, Iterable<Entity> objectEntities) {
  for (Entity objectEntity : objectEntities) {
    model.add(
        subject,
        predicate,
        valueFactory.createIRI(subject.stringValue() + '/' + objectEntity.getIdValue()));
  }
}
 
Example 10
Source File: SemagrowRepositoryConfig.java    From semagrow with Apache License 2.0 5 votes vote down vote up
@Override
public Resource export(Model graph) {
    Resource repImplNode = super.export(graph);

    if (sailConfig != null) {
        Resource sailImplNode = sailConfig.export(graph);
        graph.add(repImplNode, SAILIMPL, sailImplNode);
    }

    return repImplNode;
}
 
Example 11
Source File: AbstractDelegatingSailImplConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resource export(Model m) {
	Resource implNode = super.export(m);

	if (delegate != null) {
		Resource delegateNode = delegate.export(m);
		m.add(implNode, DELEGATE, delegateNode);
	}

	return implNode;
}
 
Example 12
Source File: CustomGraphQueryInferencerConfig.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Resource export(Model m) {
	Resource implNode = super.export(m);
	m.setNamespace("cgqi", CustomGraphQueryInferencerSchema.NAMESPACE);
	if (null != language) {
		m.add(implNode, QUERY_LANGUAGE, SimpleValueFactory.getInstance().createLiteral(language.getName()));
	}
	addQueryNode(m, implNode, RULE_QUERY, ruleQuery);
	addQueryNode(m, implNode, MATCHER_QUERY, matcherQuery);
	return implNode;
}
 
Example 13
Source File: BaseSailConfig.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);

	if (evalStratFactoryClassName != null) {
		graph.setNamespace("sb", NAMESPACE);
		graph.add(implNode, EVALUATION_STRATEGY_FACTORY,
				SimpleValueFactory.getInstance().createLiteral(evalStratFactoryClassName));
	}

	return implNode;
}
 
Example 14
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testOneCollectionWithType() throws Exception {
	Model input = new LinkedHashModel();
	Resource _1 = vf.createBNode();
	Resource _2 = vf.createBNode();
	Resource _3 = vf.createBNode();
	input.add(uri1, uri2, _1);
	input.add(_1, RDF.TYPE, RDF.LIST);
	input.add(_1, RDF.FIRST, uri3);
	input.add(_1, RDF.REST, _2);
	input.add(_2, RDF.FIRST, uri4);
	input.add(_2, RDF.REST, _3);
	input.add(_3, RDF.FIRST, uri5);
	input.add(_3, RDF.REST, RDF.NIL);
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	for (Statement st : input) {
		rdfWriter.handleStatement(st);
	}
	rdfWriter.endRDF();
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	RDFParser rdfParser = rdfParserFactory.getParser();
	setupParserConfig(rdfParser.getParserConfig());
	Model parsedOutput = new LinkedHashModel();
	rdfParser.setRDFHandler(new StatementCollector(parsedOutput));
	rdfParser.parse(inputReader, "");
	// ignore rdf:List
	input.remove(null, RDF.TYPE, RDF.LIST);
	parsedOutput.remove(null, RDF.TYPE, RDF.LIST);
	assertSameModel(input, parsedOutput);
}
 
Example 15
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteTwoStatementsWithDifferentLanguage() throws Exception {
	Model input = new LinkedHashModel();
	input.setNamespace("ex", exNs);
	input.add(vf.createStatement(uri1, uri1, vf.createLiteral("hello", "nl")));
	input.add(vf.createStatement(uri1, uri1, vf.createLiteral("hello", "en")));
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	write(input, outputWriter);
	ByteArrayInputStream inputReader = new ByteArrayInputStream(outputWriter.toByteArray());
	Model parsedOutput = parse(inputReader, "");
	assertEquals(2, parsedOutput.size());
	assertSameModel(input, parsedOutput);
}
 
Example 16
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public final void testRDFStarCompatibility() throws Exception {
	Model expectedModel = new LinkedHashModel();
	Triple t1 = vf.createTriple(vf.createIRI("http://example.com/1"), vf.createIRI("http://example.com/2"),
			vf.createLiteral("example", vf.createIRI("http://example.com/3")));
	expectedModel.add(vf.createStatement(t1, DC.SOURCE, vf.createIRI("http://example.com/4")));
	Triple t2 = vf.createTriple(t1, DC.DATE, vf.createLiteral(new Date()));
	expectedModel.add(vf.createStatement(vf.createIRI("http://example.com/5"), DC.RELATION, t2));
	Triple t3 = vf.createTriple(vf.createTriple(vf.createTriple(vf.createIRI("urn:a"), RDF.TYPE,
			vf.createIRI("urn:b")), vf.createIRI("urn:c"), vf.createIRI("urn:d")), vf.createIRI("urn:e"),
			vf.createIRI("urn:f"));
	expectedModel.add(vf.createStatement(t3, vf.createIRI("urn:same"), t3));

	// Default: formats with RDF* support handle it natively and non-RDF* use a compatibility encoding
	InputStream input1 = serialize(expectedModel);
	testParser.parse(input1, BASE_URI);
	assertErrorListener(0, 0, 0);
	assertModel(expectedModel);

	testListener.reset();
	testStatements.clear();

	// Turn off compatibility on parsing: formats with RDF* support will produce RDF* triples,
	// non-RDF* formats will produce IRIs of the kind urn:rdf4j:triple:xxx
	InputStream input2 = serialize(expectedModel);
	testParser.getParserConfig().set(BasicParserSettings.PROCESS_ENCODED_RDF_STAR, false);
	testParser.parse(input2, BASE_URI);
	assertErrorListener(0, 0, 0);
	if (testParser.getRDFFormat().supportsRDFStar()) {
		assertModel(expectedModel);
	} else {
		assertTrue(testStatements.contains(RDFStarUtil.toRDFEncodedValue(t1), DC.SOURCE,
				vf.createIRI("http://example.com/4")));
		assertTrue(testStatements.contains(vf.createIRI("http://example.com/5"), DC.RELATION,
				RDFStarUtil.toRDFEncodedValue(t2)));
		assertTrue(testStatements.contains(RDFStarUtil.toRDFEncodedValue(t3), vf.createIRI("urn:same"),
				RDFStarUtil.toRDFEncodedValue(t3)));
		assertEquals(3, testStatements.size());
	}
}
 
Example 17
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 18
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWriteSingleStatementSubjectBNodeNoContext() throws Exception {
	Model input = new LinkedHashModel();
	input.add(vf.createStatement(bnodeSingleUseSubject, uri1, uri1));
	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.subjects().size());
	assertTrue(parsedOutput.subjects().iterator().next() instanceof BNode);
}
 
Example 19
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private final Model getTestModel(String languageValue, String languageTag) {
	Model result = new LinkedHashModel();
	result.add(vf.createStatement(vf.createBNode(), RDFS.COMMENT, vf.createLiteral(languageValue, languageTag)));
	return result;
}
 
Example 20
Source File: RepositoryManagerFederator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void addToGraph(Model graph, Resource subject, IRI predicate, Value object) {
	if (LOGGER.isDebugEnabled()) {
		LOGGER.debug(subject + " " + predicate + " " + object);
	}
	graph.add(subject, predicate, object);
}