org.eclipse.rdf4j.model.Model Java Examples

The following examples show how to use org.eclipse.rdf4j.model.Model. 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: ModelsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSetPropertyWithContext2() {
	Literal lit1 = VF.createLiteral(1.0);
	IRI graph1 = VF.createIRI("urn:g1");
	IRI graph2 = VF.createIRI("urn:g2");
	model1.add(foo, bar, lit1, graph1);
	model1.add(foo, bar, bar);
	model1.add(foo, bar, foo, graph2);

	Literal lit2 = VF.createLiteral(2.0);

	Model m = Models.setProperty(model1, foo, bar, lit2);

	assertNotNull(m);
	assertEquals(model1, m);
	assertFalse(model1.contains(foo, bar, lit1));
	assertFalse(model1.contains(foo, bar, lit1, graph1));
	assertFalse(model1.contains(foo, bar, foo));
	assertFalse(model1.contains(foo, bar, bar));
	assertFalse(model1.contains(foo, bar, foo, graph2));
	assertTrue(model1.contains(foo, bar, lit2));
}
 
Example #2
Source File: ModelsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testConvertRDFStarToReification() {
	Model rdfStarModel = RDFStarTestHelper.createRDFStarModel();
	Model referenceModel = RDFStarTestHelper.createRDFReificationModel();

	Model reificationModel1 = Models.convertRDFStarToReification(VF, rdfStarModel);
	assertTrue("RDF* conversion to reification with explicit VF, model-to-model",
			Models.isomorphic(reificationModel1, referenceModel));

	Model reificationModel2 = Models.convertRDFStarToReification(rdfStarModel);
	assertTrue("RDF* conversion to reification with implicit VF, model-to-model",
			Models.isomorphic(reificationModel2, referenceModel));

	Model reificationModel3 = new TreeModel();
	Models.convertRDFStarToReification(VF, rdfStarModel, (Consumer<Statement>) reificationModel3::add);
	assertTrue("RDF* conversion to reification with explicit VF, model-to-consumer",
			Models.isomorphic(reificationModel3, referenceModel));

	Model reificationModel4 = new TreeModel();
	Models.convertRDFStarToReification(rdfStarModel, reificationModel4::add);
	assertTrue("RDF* conversion to reification with explicit VF, model-to-consumer",
			Models.isomorphic(reificationModel4, referenceModel));
}
 
Example #3
Source File: ConfigController.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private ModelAndView handleQuery(HttpServletRequest request, HttpServletResponse response)
		throws ClientHTTPException {

	RDFWriterFactory rdfWriterFactory = ProtocolUtil.getAcceptableService(request, response,
			RDFWriterRegistry.getInstance());
	String repId = RepositoryInterceptor.getRepositoryID(request);
	RepositoryConfig repositoryConfig = repositoryManager.getRepositoryConfig(repId);

	Model configData = modelFactory.createEmptyModel();
	String baseURI = request.getRequestURL().toString();
	Resource ctx = SimpleValueFactory.getInstance().createIRI(baseURI + "#" + repositoryConfig.getID());

	repositoryConfig.export(configData, ctx);
	Map<String, Object> model = new HashMap<>();
	model.put(ConfigView.FORMAT_KEY, rdfWriterFactory.getRDFFormat());
	model.put(ConfigView.CONFIG_DATA_KEY, configData);
	model.put(ConfigView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));
	return new ModelAndView(ConfigView.getInstance(), model);
}
 
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 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 #5
Source File: ValidationResult.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Model asModel(Model model) {

		model.add(getId(), RDF.TYPE, SHACL.VALIDATION_RESULT);

		model.add(getId(), SHACL.FOCUS_NODE, getFocusNode());
		model.add(getId(), SHACL.SOURCE_CONSTRAINT_COMPONENT, getSourceConstraintComponent().getIri());
		model.add(getId(), SHACL.SOURCE_SHAPE, getSourceShapeResource());

		if (getPath() != null) {
			model.add(getId(), SHACL.RESULT_PATH, ((SimplePath) getPath()).getPath());
		}

		if (detail != null) {
			model.add(getId(), SHACL.DETAIL, detail.getId());
			detail.asModel(model);
		}

		return model;
	}
 
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 testSuccessBNodeParsesAreDistinct() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, bnode));
	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, "");
	assertEquals(1, parsedOutput.size());
	ByteArrayInputStream inputReader2 = new ByteArrayInputStream(outputWriter.toByteArray());
	rdfParser.parse(inputReader2, "");
	assertEquals(2, parsedOutput.size());
}
 
Example #7
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContext() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	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, "");
	assertEquals(1, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
}
 
Example #8
Source File: ModelsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSetProperty() {
	Literal lit1 = VF.createLiteral(1.0);
	model1.add(foo, bar, lit1);
	model1.add(foo, bar, foo);

	Literal lit2 = VF.createLiteral(2.0);

	Model m = Models.setProperty(model1, foo, bar, lit2);

	assertNotNull(m);
	assertEquals(model1, m);
	assertFalse(model1.contains(foo, bar, lit1));
	assertFalse(model1.contains(foo, bar, foo));
	assertTrue(model1.contains(foo, bar, lit2));

}
 
Example #9
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentURIContextWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, uri1));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	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, "");
	assertEquals(1, parsedOutput.size());
	if (rdfWriterFactory.getRDFFormat().supportsContexts()) {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1, uri1));
	} else {
		assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	}
}
 
Example #10
Source File: LocalRepositoryManagerIntegrationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
@Deprecated
public void testModifySystemRepository() {
	RepositoryConfig config = subject.getRepositoryConfig(TEST_REPO);
	subject.addRepositoryConfig(new RepositoryConfig(SystemRepository.ID, new SystemRepositoryConfig()));
	subject.shutDown();
	subject = new LocalRepositoryManager(datadir);
	subject.initialize();
	try (RepositoryConnection con = subject.getSystemRepository().getConnection()) {
		Model model = new TreeModel();
		config.setTitle("Changed");
		config.export(model, con.getValueFactory().createBNode());
		Resource ctx = RepositoryConfigUtil.getContext(con, config.getID());
		con.begin();
		con.clear(ctx);
		con.add(model, ctx == null ? con.getValueFactory().createBNode() : ctx);
		con.commit();
	}
	assertEquals("Changed", subject.getRepositoryConfig(TEST_REPO).getTitle());
}
 
Example #11
Source File: ElasticsearchStoreConfigTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void exportAddsAllConfigData() {

	mb
			.add(ElasticsearchStoreSchema.hostname, "host1")
			.add(ElasticsearchStoreSchema.clusterName, "cluster1")
			.add(ElasticsearchStoreSchema.index, "index1")
			.add(ElasticsearchStoreSchema.port, 9300);
	// @formatter:on

	subject.parse(mb.build(), implNode);

	Model m = new TreeModel();
	Resource node = subject.export(m);

	assertThat(m.contains(node, ElasticsearchStoreSchema.hostname, null)).isTrue();
	assertThat(m.contains(node, ElasticsearchStoreSchema.clusterName, null)).isTrue();
	assertThat(m.contains(node, ElasticsearchStoreSchema.index, null)).isTrue();
	assertThat(m.contains(node, ElasticsearchStoreSchema.port, null)).isTrue();

}
 
Example #12
Source File: SemagrowRepositoryConfig.java    From semagrow with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(Model graph, Resource node) throws RepositoryConfigException {

    try {
        Optional<Resource> sailImplNode = Models.objectResource(graph.filter(node, SAILIMPL,null));

        if (sailImplNode.isPresent()) {

                sailConfig  = new SemagrowSailConfig();
                sailConfig.parse(graph, sailImplNode.get());
        }
    }
    catch (SailConfigException e) {
        throw new RepositoryConfigException(e.getMessage(), e);
    }
}
 
Example #13
Source File: JSONParserParseTest.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Test
public void testParse() throws Exception {
    Model transformedModel = new LinkedHashModel();
    RDFParser parser = new JSONParser();
    parser.setValueFactory(SimpleValueFactory.getInstance());
    parser.set(JSONParser.GENERATE_ONTOLOGY, true);
    parser.setRDFHandler(new ContextStatementCollector(transformedModel, SimpleValueFactory.getInstance()));
    parser.parse(JSONParserParseTest.class.getResourceAsStream(parameter + ".json"), "http://testParse/"+parameter + "/");

    WriterConfig wc = new WriterConfig();
    wc.set(BasicWriterSettings.PRETTY_PRINT, true);
    System.out.println("-------------- " + parameter + " ------------------");
    Rio.write(transformedModel, System.out, RDFFormat.TURTLE, wc);

    Model expectedModel = Rio.parse(JSONParserParseTest.class.getResourceAsStream(parameter + ".ttl"), "http://testParse/" + parameter + "/", RDFFormat.TURTLE);

    JSONParserParseTest.assertEquals(expectedModel, transformedModel);
}
 
Example #14
Source File: MultithreadedTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void add(String turtle, IRI graph) {
	turtle = String.join("\n", "",
			"@prefix ex: <http://example.com/ns#> .",
			"@prefix sh: <http://www.w3.org/ns/shacl#> .",
			"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
			"@prefix foaf: <http://xmlns.com/foaf/0.1/>.") + turtle;

	StringReader shaclRules = new StringReader(turtle);

	try {
		Model parse = Rio.parse(shaclRules, "", RDFFormat.TURTLE);
		parse.stream()
				.map(statement -> {
					if (graph != null) {
						return vf.createStatement(statement.getSubject(), statement.getPredicate(),
								statement.getObject(), graph);
					}

					return statement;
				})
				.forEach(statement -> addedStatements.add(statement));
	} catch (IOException e) {
		throw new RuntimeException(e);
	}

}
 
Example #15
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	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, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #16
Source File: RemoteRepositoryGraphRepositoryInformation.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void initialize(Model graph, Resource repNode) {
	
	// name: the node's value
	setProperty("name", repNode.stringValue());

	// repositoryServer / location
	Model repositoryServer = graph.filter(repNode, SimpleValueFactory.getInstance().createIRI("http://fluidops.org/config#repositoryServer"), null);
	String repoLocation = repositoryServer.iterator().next().getObject().stringValue();
	setProperty("location", repoLocation);
	setProperty("repositoryServer", repoLocation);
	
	// repositoryName
	Model repositoryName = graph.filter(repNode, SimpleValueFactory.getInstance().createIRI("http://fluidops.org/config#repositoryName"), null);
	String repoName = repositoryName.iterator().next().getObject().stringValue();
	setProperty("repositoryName", repoName);
	
	// id: the name of the location
	String id = repNode.stringValue().replace("http://", "");
	id = "remote_" + id.replace("/", "_");
	setProperty("id", id);
}
 
Example #17
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testWriteCommentBNodeContextBNodeWithNamespaceBeforeNamespace() throws Exception {
	ByteArrayOutputStream outputWriter = new ByteArrayOutputStream();
	RDFWriter rdfWriter = rdfWriterFactory.getWriter(outputWriter);
	setupWriterConfig(rdfWriter.getWriterConfig());
	rdfWriter.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri1, bnode));
	rdfWriter.handleComment("This comment should not screw up parsing");
	rdfWriter.handleNamespace("ex1", exNs);
	rdfWriter.handleStatement(vf.createStatement(uri1, uri1, uri2, bnode));
	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, "");
	assertEquals(2, parsedOutput.size());
	assertTrue(parsedOutput.contains(uri1, uri1, uri1));
	assertTrue(parsedOutput.contains(uri1, uri1, uri2));
	assertEquals(1, parsedOutput.contexts().size());
}
 
Example #18
Source File: RDFWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void testSES2030BNodeCollisionsInternal(boolean preserveBNodeIDs) throws Exception {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		RDFWriter rdfWriter = rdfWriterFactory.getWriter(output);
		setupWriterConfig(rdfWriter.getWriterConfig());
		rdfWriter.startRDF();
		int count = 18;
		for (int i = 0; i < count; i++) {
			BNode bNode2 = vf.createBNode("a" + Integer.toHexString(i).toUpperCase());
			// System.out.println(bNode2.getID());
			rdfWriter.handleStatement(vf.createStatement(uri1, uri2, bNode2));
		}
		rdfWriter.endRDF();
		RDFParser rdfParser = rdfParserFactory.getParser();
		setupParserConfig(rdfParser.getParserConfig());
		if (preserveBNodeIDs) {
			rdfParser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
		}
		Model parsedModel = new LinkedHashModel();
		rdfParser.setRDFHandler(new StatementCollector(parsedModel));
		rdfParser.parse(new ByteArrayInputStream(output.toByteArray()), "");
//		if (count != parsedModel.size()) {
//			Rio.write(parsedModel, System.out, RDFFormat.NQUADS);
//		}
		assertEquals(count, parsedModel.size());
	}
 
Example #19
Source File: RepositoryConnectionTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testGraphSerialization() throws Exception {
	testCon.add(bob, name, nameBob);
	testCon.add(alice, name, nameAlice);

	try (RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, true);) {
		Model graph = Iterations.addAll(statements, new LinkedHashModel());

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(baos);
		out.writeObject(graph);
		out.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		ObjectInputStream in = new ObjectInputStream(bais);
		Model deserializedGraph = (Model) in.readObject();
		in.close();

		assertThat(deserializedGraph.isEmpty()).isFalse();
		assertThat(deserializedGraph).hasSameSizeAs(graph);
		for (Statement st : deserializedGraph) {
			assertThat(graph).contains(st);
			assertThat(testCon.hasStatement(st, true)).isTrue();
		}
	}
}
 
Example #20
Source File: EntityModelWriter.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void addStatementsForAttributeTags(
    Entity objectEntity, Model model, Resource subject, EntityType entityType) {
  for (Attribute objectAttribute : entityType.getAtomicAttributes()) {
    Object value = objectEntity.get(objectAttribute.getName());
    if (value == null) {
      continue;
    }
    for (LabeledResource tag :
        tagService
            .getTagsForAttribute(entityType, objectAttribute)
            .get(Relation.isAssociatedWith)) {
      IRI predicate = valueFactory.createIRI(tag.getIri());
      addRelationForAttribute(model, subject, predicate, objectEntity, objectAttribute);
    }
  }
}
 
Example #21
Source File: SemagrowRepositoryResolver.java    From semagrow with Apache License 2.0 6 votes vote down vote up
private Model parseConfig(File file) throws SailConfigException, IOException
{
    RDFFormat format = Rio.getParserFormatForFileName(file.getAbsolutePath()).get();
    if (format==null)
        throw new SailConfigException("Unsupported file format: " + file.getAbsolutePath());
    RDFParser parser = Rio.createParser(format);
    Model model = new LinkedHashModel();
    parser.setRDFHandler(new StatementCollector(model));
    InputStream stream = new FileInputStream(file);

    try {
        parser.parse(stream, file.getAbsolutePath());
    } catch (Exception e) {
        throw new SailConfigException("Error parsing file!");
    }

    stream.close();
    return model;
}
 
Example #22
Source File: SourceGenerator.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private String getFieldComment(final IRI propertyIri) {
    final StringBuilder builder = new StringBuilder();
    final Model propertyStuff = model.filter(propertyIri, null, null);
    propertyStuff.filter(propertyIri, RDFS.COMMENT, null).forEach(stmt -> {
        builder.append(stmt.getObject().stringValue());
        builder.append("\n\n");
    });
    return builder.toString().trim();
}
 
Example #23
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineEmptyCommentWithSpaceBefore() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(new StringReader("<urn:test:subject> <urn:test:predicate> <urn:test:object> . #\n"),
			"http://example/");
	assertEquals(1, model.size());
	assertEquals(Collections.singleton("urn:test:object"), Models.objectStrings(model));
}
 
Example #24
Source File: EntityModelWriterTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void testCreateRfdModelIntAttribute() {
  List<Attribute> attributeList = singletonList(attr2);

  when(objectEntity.getEntityType()).thenReturn(entityType);
  when(objectEntity.get("attributeName2")).thenReturn(2);
  when(objectEntity.getInt("attributeName2")).thenReturn(2);

  when(entityType.getAtomicAttributes()).thenReturn(attributeList);
  when(attr2.getName()).thenReturn("attributeName2");

  when(attr2.getDataType()).thenReturn(AttributeType.INT);

  LabeledResource tag2 = new LabeledResource("http://IRI2.nl", "tag2Label");
  Multimap<Relation, LabeledResource> tags2 =
      ImmutableMultimap.of(Relation.isAssociatedWith, tag2);
  when(tagService.getTagsForAttribute(entityType, attr2)).thenReturn(tags2);

  Model result =
      writer.createRdfModel("http://molgenis01.gcc.rug.nl/fdp/catolog/test/this", objectEntity);

  assertEquals(1, result.size());
  Iterator results = result.iterator();
  assertEquals(
      "(http://molgenis01.gcc.rug.nl/fdp/catolog/test/this, http://IRI2.nl, \"2\"^^<http://www.w3.org/2001/XMLSchema#int>) [null]",
      results.next().toString());
}
 
Example #25
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests whether an known language with the message which generates a failure if the uppercased version of the
 * language is unknown.
 */
@Test
public final void testKnownLanguageWithMessageWhereUnknownWouldFailCase2() throws Exception {
	Model expectedModel = getTestModel(KNOWN_LANGUAGE_VALUE, KNOWN_LANGUAGE_TAG.toUpperCase(Locale.ENGLISH));
	InputStream input = getKnownLanguageStream(expectedModel);

	testParser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);

	testParser.parse(input, BASE_URI);

	assertErrorListener(0, 0, 0);
	assertModel(expectedModel);
}
 
Example #26
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests whether an known datatype with the correct settings will both generate no message and not fail when
 * addNonFatalError is called with the given setting.
 */
@Test
public final void testKnownDatatypeNoMessageNoFailCase4() throws Exception {
	Model expectedModel = getTestModel(KNOWN_DATATYPE_VALUE, KNOWN_DATATYPE_URI);
	InputStream input = getKnownDatatypeStream(expectedModel);

	testParser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, false);
	testParser.getParserConfig().addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES);

	testParser.parse(input, BASE_URI);

	assertErrorListener(0, 0, 0);
	assertModel(expectedModel);
}
 
Example #27
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 #28
Source File: AbstractParserHandlingTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests whether an unknown datatype with the correct settings will both generate no message and not fail when
 * setNonFatalError is called with an empty set to reset the fatal errors
 */
@Test
public final void testUnknownDatatypeNoMessageNoFailCase5() throws Exception {
	Model expectedModel = getTestModel(UNKNOWN_DATATYPE_VALUE, UNKNOWN_DATATYPE_URI);
	InputStream input = getUnknownDatatypeStream(expectedModel);

	testParser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, false);
	testParser.getParserConfig().setNonFatalErrors(new HashSet<>());

	testParser.parse(input, BASE_URI);

	assertErrorListener(0, 0, 0);
	assertModel(expectedModel);
}
 
Example #29
Source File: SailModelNamespacesTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected Model getModelImplementation() {
	sail = new MemoryStore();
	try {
		sail.initialize();
		conn = sail.getConnection();
		conn.begin();
		return new SailModel(conn, false);
	} catch (SailException e) {
		throw new ModelException(e);
	}
}
 
Example #30
Source File: ModelCollector.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public BinaryOperator<Model> combiner() {
	return (m1, m2) -> {
		m1.addAll(m2);
		return m1;
	};
}