org.apache.commons.rdf.api.RDF类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(201)

本文整理了Java中org.apache.commons.rdf.api.RDF类的一些代码示例,展示了RDF类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RDF类的具体详情如下:
包路径:org.apache.commons.rdf.api.RDF
类名称:RDF

RDF介绍

[英]A RDF implementation.

A RDF implementation can create instances of the RDFTermtypes IRI, BlankNode and Literal, as well as creating instances of the types Triple, Quad, Graph or Dataset.

A partial RDF implementation should be clearly documented as such, and may throw UnsupportedOperationException where applicable, e.g. if it does not support creating Datasets or Quads.

Instances of RDF work like a factory for creating Commons RDF instances. spezializations of this interface may also provide methods for conversions from/to their underlying RDF framework.

If a factory method of a particular implementation does not allow or support a provided parameter, e.g. because an IRI is considered invalid, then it SHOULD throw IllegalArgumentException.
[中]RDF实现。
RDF实现可以创建RDFTermtypes IRI、BlankNode和Literal的实例,以及创建Triple、Quad、Graph或Dataset类型的实例。
部分RDF实现应明确记录,并在适用的情况下可能引发不支持的操作异常,例如,如果它不支持创建数据集或四边形。
RDF的实例与创建Commons RDF实例的工厂类似。这个接口的特殊化还可以提供从底层RDF框架到底层RDF框架的转换方法。
如果特定实现的工厂方法不允许或不支持提供的参数,例如,因为IRI被认为无效,那么它应该抛出IllegalArgumentException。

代码示例

代码示例来源:origin: eu.optique-project/r2rml-api-core

@Override
public Set<Triple> serialize() {
  Set<Triple> stmtSet = new HashSet<Triple>();
  stmtSet.add(getRDF().createTriple(node, getRDF().createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), getRDF().createIRI(R2RMLVocabulary.TYPE_JOIN)));
  stmtSet.add(getRDF().createTriple(node, getRDF().createIRI(R2RMLVocabulary.PROP_CHILD),
      getRDF().createLiteral(child)));
  stmtSet.add(getRDF().createTriple(node, getRDF().createIRI(R2RMLVocabulary.PROP_PARENT),
      getRDF().createLiteral(parent)));
  return stmtSet;
}

代码示例来源:origin: trellis-ldp/trellis

private Stream<Quad> getLdfQuads() {
  return Stream.of(
    rdf.createQuad(PreferUserManaged, identifier, DC.creator, rdf.createLiteral("User")),
    rdf.createQuad(PreferUserManaged, rdf.createIRI("ex:foo"), DC.title, rdf.createIRI("ex:title")),
    rdf.createQuad(PreferUserManaged, identifier, DC.title, rdf.createLiteral("A title")),
    rdf.createQuad(PreferUserManaged, identifier, DC.title, rdf.createLiteral("Other title")),
    rdf.createQuad(PreferUserManaged, identifier, type, rdf.createIRI("ex:Type")),
    rdf.createQuad(PreferUserManaged, rdf.createIRI("ex:foo"), type, rdf.createIRI("ex:Type")),
    rdf.createQuad(PreferUserManaged, rdf.createIRI("ex:foo"), type, rdf.createIRI("ex:Other")),
    rdf.createQuad(PreferServerManaged, identifier, DC.created,
      rdf.createLiteral("2017-04-01T10:15:00Z", XSD.dateTime)),
    rdf.createQuad(PreferAccessControl, identifier, type, ACL.Authorization),
    rdf.createQuad(PreferAccessControl, identifier, ACL.mode, ACL.Control));
}

代码示例来源:origin: org.trellisldp/trellis-test

/**
   * Build a dataset.
   * @param resource the resource IRI
   * @param title a title
   * @param subject a subject
   * @return a new dataset
   */
  default Dataset buildDataset(final IRI resource, final String title, final String subject) {
    final Dataset dataset = getInstance().createDataset();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, getInstance().createLiteral(title));
    dataset.add(Trellis.PreferUserManaged, resource, DC.subject, getInstance().createIRI(subject));
    dataset.add(Trellis.PreferUserManaged, resource, type, SKOS.Concept);
    return dataset;
  }
}

代码示例来源:origin: org.trellisldp.ext/trellis-db

private static RDFTerm getObject(final String value, final String lang, final String datatype) {
    if (nonNull(lang)) {
      return rdf.createLiteral(value, lang);
    } else if (nonNull(datatype)) {
      return rdf.createLiteral(value, rdf.createIRI(datatype));
    }
    return rdf.createIRI(value);
  }
}

代码示例来源:origin: trellis-ldp/trellis

private List<Quad> auditData(final IRI subject, final Session session, final List<IRI> types) {
    final List<Quad> data = new ArrayList<>();
    final BlankNode bnode = rdf.createBlankNode();
    data.add(rdf.createQuad(PreferAudit, subject, PROV.wasGeneratedBy, bnode));
    types.forEach(t -> data.add(rdf.createQuad(PreferAudit, bnode, type, t)));
    data.add(rdf.createQuad(PreferAudit, bnode, PROV.wasAssociatedWith, session.getAgent()));
    data.add(rdf.createQuad(PreferAudit, bnode, PROV.atTime,
          rdf.createLiteral(session.getCreated().toString(), XSD.dateTime)));
    session.getDelegatedBy().ifPresent(delegate ->
        data.add(rdf.createQuad(PreferAudit, bnode, PROV.actedOnBehalfOf, delegate)));
    return data;
  }
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test
public void removeLanguageTagsCaseInsensitive() throws Exception {
  // COMMONSRDF-51: Ensure we can remove with any casing
  // of literal language tag
  final Literal lower = factory.createLiteral("Hello", "en-gb");
  final Literal upper = factory.createLiteral("Hello", "EN-GB");
  final Literal mixed = factory.createLiteral("Hello", "en-GB");
  final IRI example1 = factory.createIRI("http://example.com/s1");
  final IRI greeting = factory.createIRI("http://example.com/greeting");
  try (final Graph graph = factory.createGraph()) {
    graph.add(example1, greeting, upper);
    // Remove should also honour any case
    graph.remove(example1, null, mixed);
    assertFalse(graph.contains(null, greeting, null));
    graph.add(example1, greeting, lower);
    graph.remove(example1, null, upper);
    // Check with Triple
    graph.add(factory.createTriple(example1, greeting, mixed));
    graph.remove(factory.createTriple(example1, greeting, upper));
    assertFalse(graph.contains(null, greeting, null));
  }
}

代码示例来源:origin: org.trellisldp.ext/trellis-db

private Stream<Quad> fetchQuadsFromTable(final String tableName, final IRI graphName) {
  final String query = "SELECT subject, predicate, object, lang, datatype "
            + "FROM " + tableName + " WHERE resource_id = ?";
  return jdbi.withHandle(handle -> handle.select(query, data.getId())
      .map((rs, ctx) -> rdf.createQuad(graphName, rdf.createIRI(rs.getString(SUBJECT)),
          rdf.createIRI(rs.getString(PREDICATE)),
          getObject(rs.getString(OBJECT), rs.getString(LANG), rs.getString(DATATYPE))))
      .stream());
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Override
public IRI getDatatype() {
  return factory.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
}
@Override

代码示例来源:origin: eu.optique-project/r2rml-api-core

@Override
public Set<Triple> serialize() {
  Set<Triple> stmtSet = new HashSet<Triple>();
  stmtSet.add(getRDF().createTriple(node, getRDF().createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), getRDF().createIRI(R2RMLVocabulary.TYPE_REF_OBJECT_MAP)));
  stmtSet.add(getRDF().createTriple(node, getRDF().createIRI(R2RMLVocabulary.PROP_PARENT_TRIPLES_MAP), parent.getNode()));
  for (Join j : joinList) {
    stmtSet.add(getRDF().createTriple(node, getRDF().createIRI(R2RMLVocabulary.PROP_JOIN_CONDITION), j.getNode()));
    stmtSet.addAll(j.serialize());
  }
  return stmtSet;
}

代码示例来源:origin: trellis-ldp/trellis

@Test
public void testToQuad() {
  final IRI subject = rdf.createIRI("http://example.com/resource");
  final Literal object = rdf.createLiteral("title");
  final Triple triple = rdf.createTriple(subject, DC.title, object);
  final Quad quad = rdf.createQuad(Trellis.PreferUserManaged, subject, DC.title, object);
  assertEquals(quad, toQuad(Trellis.PreferUserManaged).apply(triple), "Incorrect quad from triple!");
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

private Graph createGraph2() {
  final RDF factory2 = createFactory();
  final IRI name = factory2.createIRI("http://xmlns.com/foaf/0.1/name");
  final Graph g2 = factory2.createGraph();
  final BlankNode b1 = createOwnBlankNode("b1", "bc8d3e45-a08f-421d-85b3-c25b373abf87");
  g2.add(b1, name, factory2.createLiteral("Charlie"));
  final BlankNode b2 = createOwnBlankNode("b2", "2209097a-5078-4b03-801a-6a2d2f50d739");
  g2.add(b2, name, factory2.createLiteral("Dave"));
  final IRI hasChild = factory2.createIRI("http://example.com/hasChild");
  // NOTE: Opposite direction of loadGraph1
  g2.add(b2, hasChild, b1);
  return g2;
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test
public void testCreateTripleBnodeTriple() {
  final BlankNode subject = factory.createBlankNode();
  final IRI predicate = factory.createIRI("http://example.com/pred");
  final Literal object = factory.createLiteral("Example", "en");
  final Triple triple = factory.createTriple(subject, predicate, object);
  // bnode equivalence should be OK as we used the same
  // factory and have not yet inserted Triple into a Graph
  assertEquals(subject, triple.getSubject());
  assertEquals(predicate, triple.getPredicate());
  assertEquals(object, triple.getObject());
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test
public void testCreateTripleBnodeBnode() {
  final BlankNode subject = factory.createBlankNode("b1");
  final IRI predicate = factory.createIRI("http://example.com/pred");
  final BlankNode object = factory.createBlankNode("b2");
  final Triple triple = factory.createTriple(subject, predicate, object);
  // bnode equivalence should be OK as we used the same
  // factory and have not yet inserted Triple into a Graph
  assertEquals(subject, triple.getSubject());
  assertEquals(predicate, triple.getPredicate());
  assertEquals(object, triple.getObject());
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test
public void testCreateGraph() throws Exception {
  try (final Graph graph = factory.createGraph(); final Graph graph2 = factory.createGraph()) {
    assertEquals("Graph was not empty", 0, graph.size());
    graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode());
    assertNotSame(graph, graph2);
    assertEquals("Graph was empty after adding", 1, graph.size());
    assertEquals("New graph was not empty", 0, graph2.size());
  }
}

代码示例来源:origin: eu.optique-project/r2rml-api-core

TermMapImpl(RDF rdf, RDFTerm constant) {
  super(rdf);
  this.constVal = requireNonNull(constant);
  if(constant instanceof BlankNode){
    throw new IllegalArgumentException("BlankNode cannot be used as constant");
  } else if (constant instanceof IRI){
    this.termTypeIRI = getRDF().createIRI(R2RMLVocabulary.TERM_IRI);
  } else {// if (constant instanceof Literal)
    this.termTypeIRI = getRDF().createIRI(R2RMLVocabulary.TERM_LITERAL);
  }
  this.termMapType = TermMapType.CONSTANT_VALUED;
  setNode(getRDF().createBlankNode());
}

代码示例来源:origin: trellis-ldp/trellis

@Test
public void testDefaultCreate() {
  final IRI root = rdf.createIRI("trellis:data/");
  final Dataset dataset = rdf.createDataset();
  final Metadata metadata = Metadata.builder(existing).container(root).interactionModel(LDP.RDFSource).build();
  when(mockResourceService.replace(eq(metadata), eq(dataset))).thenReturn(completedFuture(null));
  assertDoesNotThrow(() -> mockResourceService.create(metadata, dataset).toCompletableFuture().join());
  verify(mockResourceService).replace(eq(metadata), eq(dataset));
}

代码示例来源:origin: org.apache.commons/commons-rdf-api

@Test(expected = IllegalArgumentException.class)
public void testInvalidLiteralLang() throws Exception {
  factory.createLiteral("Example", "with space");
}

代码示例来源:origin: org.trellisldp/trellis-api

/**
 * Un-skolemize a blank node.
 *
 * @param term the RDF term
 * @return a blank node, if a previously-skolemized node; otherwise the original term
 */
default RDFTerm unskolemize(final RDFTerm term) {
  if (term instanceof IRI) {
    final String iri = ((IRI) term).getIRIString();
    if (iri.startsWith(TRELLIS_BNODE_PREFIX)) {
      return getInstance().createBlankNode(iri.substring(TRELLIS_BNODE_PREFIX.length()));
    }
  }
  return term;
}

代码示例来源:origin: org.trellisldp/trellis-api

/**
 * Get a mapping function to turn a triple into a quad.
 *
 * @param graphName the graph name
 * @return the mapping function
 */
public static Function<Triple, Quad> toQuad(final IRI graphName) {
  return triple -> rdf.createQuad(graphName, triple.getSubject(), triple.getPredicate(), triple.getObject());
}

代码示例来源:origin: trellis-ldp/trellis

@Test
public void testDatasetConversion() {
  final Dataset dataset = simpleRdf.createDataset();
  dataset.add(simpleRdf.createQuad(PreferUserManaged, subject, SKOS.prefLabel, literal));
  dataset.add(simpleRdf.createQuad(PreferUserManaged, subject, type, SKOS.Concept));
  dataset.add(simpleRdf.createQuad(PreferUserManaged, subject, DC.subject, AS.Activity));
  assertEquals(3L, dataset.size(), "Confirm dataset size");
  assertTrue(TriplestoreUtils.asJenaDataset(dataset).containsNamedModel(PreferUserManaged.getIRIString()),
      "Confirm presence of trellis:PreferUserManaged named graph");
  assertNotEquals(TriplestoreUtils.asJenaDataset(dataset).asDatasetGraph(),
      TriplestoreUtils.asJenaDataset(dataset).asDatasetGraph(), "Confirm dataset has been converted");
}

相关文章