org.openrdf.model.Model Java Examples

The following examples show how to use org.openrdf.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: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDescribeA()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ex:a");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory f = conn.getValueFactory();
	URI a = f.createURI("http://example.org/a");
	URI p = f.createURI("http://example.org/p");
	Model result = QueryResults.asModel(gq.evaluate());
	Set<Value> objects = result.filter(a, p, null).objects();
	assertNotNull(objects);
	for (Value object : objects) {
		if (object instanceof BNode) {
			assertTrue(result.contains((Resource)object, null, null));
			assertEquals(2, result.filter((Resource)object, null, null).size());
		}
	}
}
 
Example #2
Source File: JavaNameResolver.java    From anno4j with Apache License 2.0 6 votes vote down vote up
public void setModel(Model model) {
	this.model = model;
	if (nouns != null) {
		Set<String> localNames = new HashSet<String>();
		for (Resource subj : model.filter(null, RDF.TYPE, null).subjects()) {
			if (subj instanceof URI) {
				localNames.add(((URI) subj).getLocalName());
			}
		}
		for (String name : localNames) {
			if (name.matches("^[a-zA-Z][a-z]+$")) {
				nouns.add(name.toLowerCase());
			}
		}
	}
}
 
Example #3
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDescribeAWhere()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ?x WHERE {?x rdfs:label \"a\". } ");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory f = conn.getValueFactory();
	URI a = f.createURI("http://example.org/a");
	URI p = f.createURI("http://example.org/p");
	Model result = QueryResults.asModel(gq.evaluate());
	Set<Value> objects = result.filter(a, p, null).objects();
	assertNotNull(objects);
	for (Value object : objects) {
		if (object instanceof BNode) {
			assertTrue(result.contains((Resource)object, null, null));
			assertEquals(2, result.filter((Resource)object, null, null).size());
		}
	}
}
 
Example #4
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDescribeB()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ex:b");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory f = conn.getValueFactory();
	URI b = f.createURI("http://example.org/b");
	URI p = f.createURI("http://example.org/p");
	Model result = QueryResults.asModel(gq.evaluate());
	Set<Resource> subjects = result.filter(null, p, b).subjects();
	assertNotNull(subjects);
	for (Value subject : subjects) {
		if (subject instanceof BNode) {
			assertTrue(result.contains(null, null, subject));
		}
	}
}
 
Example #5
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private static void write(Model statements, OutputStream out) {
    try (PrintStream pout = new PrintStream(out)) {
        RDFWriter w = new RDFXMLPrettyWriter(pout);
        Rio.write(statements, w);
    } catch (Exception x) {
        throw new TripleStoreException("Writing model statements", x);
    }
}
 
Example #6
Source File: CodeGenTestCase.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new File object in the <code>targetDir</code> folder.
 * 
 * @param filename
 * @return
 * @throws StoreConfigException
 * @throws RepositoryException
 */
protected File createJar(String filename) throws Exception {
	Model schema = new TreeModel();
	OntologyLoader ontologies = new OntologyLoader(schema);
	ontologies.loadOntologies(imports);
	OWLCompiler compiler = new OWLCompiler();
	compiler.setModel(schema);
	compiler.setPrefixNamespaces(ontologies.getNamespaces());
	File concepts = getConceptJar(targetDir, filename);
	compiler.createJar(concepts);
	return concepts;
}
 
Example #7
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDescribeWhere()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ?x WHERE {?x rdfs:label ?y . } ");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory vf = conn.getValueFactory();
	URI a = vf.createURI("http://example.org/a");
	URI b = vf.createURI("http://example.org/b");
	URI c = vf.createURI("http://example.org/c");
	URI e = vf.createURI("http://example.org/e");
	URI f = vf.createURI("http://example.org/f");
	URI p = vf.createURI("http://example.org/p");

	Model result = QueryResults.asModel(gq.evaluate());
	assertTrue(result.contains(a, p, null));
	assertTrue(result.contains(b, RDFS.LABEL, null));
	assertTrue(result.contains(c, RDFS.LABEL, null));
	assertTrue(result.contains(null, p, b));
	assertTrue(result.contains(e, RDFS.LABEL, null));
	assertTrue(result.contains(null, p, e));
	assertFalse(result.contains(f, null, null));
	Set<Value> objects = result.filter(a, p, null).objects();
	assertNotNull(objects);
	for (Value object : objects) {
		if (object instanceof BNode) {
			assertTrue(result.contains((Resource)object, null, null));
			assertEquals(2, result.filter((Resource)object, null, null).size());
		}
	}
}
 
Example #8
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDescribeD()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ex:d");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory f = conn.getValueFactory();
	URI d = f.createURI("http://example.org/d");
	URI p = f.createURI("http://example.org/p");
	URI e = f.createURI("http://example.org/e");
	Model result = QueryResults.asModel(gq.evaluate());

	assertNotNull(result);
	assertTrue(result.contains(null, p, e));
	assertFalse(result.contains(e, null, null));
	Set<Value> objects = result.filter(d, p, null).objects();
	assertNotNull(objects);
	for (Value object : objects) {
		if (object instanceof BNode) {
			Set<Value> childObjects = result.filter((BNode)object, null, null).objects();
			assertNotNull(childObjects);
			for (Value childObject : childObjects) {
				if (childObject instanceof BNode) {
					assertTrue(result.contains((BNode)childObject, null, null));
				}
			}
		}
	}
}
 
Example #9
Source File: ComplexSPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDescribeF()
	throws Exception
{
	loadTestData("/testdata-query/dataset-describe.trig");
	StringBuilder query = new StringBuilder();
	query.append(getNamespaceDeclarations());
	query.append("DESCRIBE ex:f");

	GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString());

	ValueFactory vf = conn.getValueFactory();
	URI f = vf.createURI("http://example.org/f");
	URI p = vf.createURI("http://example.org/p");
	Model result = QueryResults.asModel(gq.evaluate());

	assertNotNull(result);
	assertEquals(4, result.size());
	Set<Value> objects = result.filter(f, p, null).objects();
	assertNotNull(objects);
	for (Value object : objects) {
		if (object instanceof BNode) {
			Set<Value> childObjects = result.filter((BNode)object, null, null).objects();
			assertNotNull(childObjects);
			for (Value childObject : childObjects) {
				if (childObject instanceof BNode) {
					assertTrue(result.contains((BNode)childObject, null, null));
				}
			}
		}
	}
}
 
Example #10
Source File: OWLCompiler.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private Set<String> findUndefinedNamespaces(Model model, ClassLoader cl) {
	Set<String> unknown = new HashSet<String>();
	for (Resource subj : model.filter(null, RDF.TYPE, null).subjects()) {
		if (subj instanceof URI) {
			URI uri = (URI) subj;
			String ns = uri.getNamespace();
			if (!mapper.isRecordedConcept(uri, cl)
					&& !literals.isRecordedeType(uri)
					&& !mapper.isRecordedAnnotation(uri)) {
				unknown.add(ns);
			}
		}
	}
	return unknown;
}
 
Example #11
Source File: OWLCompiler.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private String findPrefix(String ns, Model model) {
	for (Namespace e : model.getNamespaces()) {
		if (ns.equals(e.getName()) && e.getPrefix().length() > 0) {
			return e.getPrefix();
		}
	}
	if (resolvingPrefix) {
		String prefix = NamespacePrefixService.getInstance().prefix(ns);
		if (prefix != null && model.getNamespace(prefix) == null) {
			model.setNamespace(prefix, ns);
			return prefix;
		}
	}
	return "ns" + Integer.toHexString(ns.hashCode());
}
 
Example #12
Source File: OWLCompiler.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * Assigns the schema that will be compiled.
 * 
 * @param model contains all relevant triples
 */
public void setModel(Model model) {
	assert model != null;
	this.model = model;
	normalizer = new OwlNormalizer(new RDFDataSource(model));
	normalizer.normalize();
}
 
Example #13
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private void write(DataSource ds, RepositoryConnection conn, Resource context) throws RepositoryException {
    LOG.info("Writing context {}", context);

    RepositoryResult<Statement> statements = conn.getStatements(null, null, null, true, context);
    Model model = new LinkedHashModel();
    QueryResults.addAll(statements, model);
    copyNamespacesToModel(conn, model);

    String outname = context.toString();
    write(model, outputStream(ds, outname));
}
 
Example #14
Source File: NamedQueryTest.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public void testModel() throws Exception {
	Model result = me.loadAllPeopleInModel();
	assertFalse(result.isEmpty());
}
 
Example #15
Source File: NamedQueryTest.java    From anno4j with Apache License 2.0 4 votes vote down vote up
@Sparql(PREFIX + "CONSTRUCT { ?person a :Person; :name ?name } "
		+ "WHERE { ?person :name ?name } ORDER BY ?name")
Model loadAllPeopleInModel();
 
Example #16
Source File: LinkedDataServletTest.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
@Test
public void post() throws IOException, CumulusStoreException, RDFHandlerException, ServletException {

	for (int i = 0; i < TRIPLES_NT.size(); i++) {

		for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) {

			/*
			 * clear data ...
			 */
			TRIPLE_STORE.clear();
			assertTrue("store should be empty", !TRIPLE_STORE.query(new Value[] { null, null, null }).hasNext());

			/*
			 * prepare data in desired RDF serialization
			 */

			Model model = new LinkedHashModel(parseNX(TRIPLES_NT.get(i)));
			final ByteArrayOutputStream out = new ByteArrayOutputStream();
			Rio.write(model, out, RDFFormat.forMIMEType(mime_type));

			/*
			 * prepare mock ...
			 */
			when(_request.getHeader(Headers.CONTENT_TYPE)).thenReturn(mime_type);
			when(_request.getInputStream()).thenReturn(new ServletInputStream() {
				final InputStream _inputStream = new ByteArrayInputStream(out.toByteArray());

				@Override
				public int read() throws IOException {
					return _inputStream.read();
				}
			});

			/*
			 * HTTP POST 
			 */
			_ld_servlet.doPost(_request, _response);

			/*
			 * verify the HTTP POST ...
			 */
			verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_CREATED);

			for (Statement stmt : model) {
				assertTrue("statement '" + stmt + "' has not been been added correctly for serialization '" + mime_type + "'", TRIPLE_STORE.query(Util.toValueArray(stmt))
						.hasNext());
			}
		}
	}
}
 
Example #17
Source File: SparqlAdvice.java    From anno4j with Apache License 2.0 4 votes vote down vote up
private Object cast(SparqlBuilder result, Class<?> rclass,
		Class<?> componentClass) throws OpenRDFException,
		TransformerException, IOException, ParserConfigurationException,
		SAXException, XMLStreamException {
	if (TupleQueryResult.class.equals(rclass)) {
		return result.asTupleQueryResult();
	} else if (GraphQueryResult.class.equals(rclass)) {
		return result.asGraphQueryResult();
	} else if (Result.class.equals(rclass)) {
		return result.asResult(componentClass);
	} else if (Set.class.equals(rclass)) {
		return result.asSet(componentClass);
	} else if (List.class.equals(rclass)) {
		return result.asList(componentClass);

	} else if (byte[].class.equals(rclass)) {
		return result.asByteArray();
	} else if (CharSequence.class.equals(rclass)) {
		return result.asCharSequence();
	} else if (Readable.class.equals(rclass)) {
		return result.asReadable();
	} else if (String.class.equals(rclass)) {
		return result.asString();

	} else if (Void.class.equals(rclass) || Void.TYPE.equals(rclass)) {
		result.asUpdate();
		return null;
	} else if (Boolean.class.equals(rclass) || Boolean.TYPE.equals(rclass)) {
		return result.asBoolean();
	} else if (Byte.class.equals(rclass) || Byte.TYPE.equals(rclass)) {
		return result.asByte();
	} else if (Character.class.equals(rclass)
			|| Character.TYPE.equals(rclass)) {
		return result.asChar();
	} else if (Double.class.equals(rclass) || Double.TYPE.equals(rclass)) {
		return result.asDouble();
	} else if (Float.class.equals(rclass) || Float.TYPE.equals(rclass)) {
		return result.asFloat();
	} else if (Integer.class.equals(rclass) || Integer.TYPE.equals(rclass)) {
		return result.asInt();
	} else if (Long.class.equals(rclass) || Long.TYPE.equals(rclass)) {
		return result.asLong();
	} else if (Short.class.equals(rclass) || Short.TYPE.equals(rclass)) {
		return result.asShort();

	} else if (Model.class.equals(rclass)) {
		return result.asModel();
	} else if (Statement.class.equals(rclass)) {
		return result.asStatement();
	} else if (BindingSet.class.equals(rclass)) {
		return result.asBindingSet();
	} else if (URI.class.equals(rclass)) {
		return result.asURI();
	} else if (BNode.class.equals(rclass)) {
		return result.asBNode();
	} else if (Literal.class.equals(rclass)) {
		return result.asLiteral();
	} else if (Resource.class.equals(rclass)) {
		return result.asResource();
	} else if (Value.class.equals(rclass)) {
		return result.asValue();

	} else if (Document.class.equals(rclass)) {
		return result.asDocument();
	} else if (DocumentFragment.class.equals(rclass)) {
		return result.asDocumentFragment();
	} else if (Element.class.equals(rclass)) {
		return result.asElement();
	} else if (Node.class.equals(rclass)) {
		return result.asNode();

	} else if (Reader.class.equals(rclass)) {
		return result.asReader();
	} else if (CharArrayWriter.class.equals(rclass)) {
		return result.asCharArrayWriter();
	} else if (ByteArrayOutputStream.class.equals(rclass)) {
		return result.asByteArrayOutputStream();
	} else if (ReadableByteChannel.class.equals(rclass)) {
		return result.asReadableByteChannel();
	} else if (InputStream.class.equals(rclass)) {
		return result.asInputStream();
	} else if (XMLEventReader.class.equals(rclass)) {
		return result.asXMLEventReader();

	} else {
		return result.as(rclass);
	}
}
 
Example #18
Source File: SparqlEvaluator.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public Model asModel() throws OpenRDFException {
	GraphQuery qry = prepareGraphQuery();
	Model model = new LinkedHashModel();
	qry.evaluate(new StatementCollector(model));
	return model;
}
 
Example #19
Source File: RDFList.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFList(Model model, Value start) {
	this(new RDFDataSource(model), start);
}
 
Example #20
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public Model getModel() {
	return model;
}
 
Example #21
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public OntologyLoader(Model model) {
	this.model = model;
}
 
Example #22
Source File: RDFProperty.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFProperty(Model model, Resource self) {
	super(model, self);
}
 
Example #23
Source File: RDFOntology.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFOntology(Model model, Resource self) {
	super(model, self);
}
 
Example #24
Source File: RDFClass.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFClass(Model model, Resource self) {
	super(model, self);
}
 
Example #25
Source File: RDFEntity.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public Model getModel() {
	return model;
}
 
Example #26
Source File: RDFEntity.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFEntity(Model model, Resource self) {
	this.model = model;
	this.self = self;
}
 
Example #27
Source File: RDFDataSource.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public Model match(Value subj, URI pred, Value obj, URI graph) {
	if (subj == null || subj instanceof Resource)
		return new LinkedHashModel(model.filter((Resource) subj, pred, obj, graph));
	return new LinkedHashModel();
}
 
Example #28
Source File: RDFDataSource.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public Model match(Value subj, URI pred, Value obj) {
	if (subj == null || subj instanceof Resource)
		return new LinkedHashModel(model.filter((Resource) subj, pred, obj));
	return new LinkedHashModel();
}
 
Example #29
Source File: RDFDataSource.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public RDFDataSource(Model model) {
	this.model = model;
}
 
Example #30
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
private static void copyNamespacesToModel(RepositoryConnection conn, Model m) throws RepositoryException {
    RepositoryResult<Namespace> ns = conn.getNamespaces();
    while (ns.hasNext()) {
        m.setNamespace(ns.next());
    }
}