Skip to content

Functions

An RML function (RML-FNML) computes a term value: upper-casing a string, testing a condition, looking something up. The mapping says which function to call and with which inputs. At run time CARML needs a Java implementation for it.

Calling a function in a mapping

rml:functionExecution goes on a term map or on an rml:ExpressionField.

@prefix rml:  <http://w3id.org/rml/> .
@prefix grel: <http://users.ugent.be/~bjdmeest/function/grel.ttl#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<TriplesMap1>
  rml:logicalSource [ rml:source [ a rml:RelativePathSource ; rml:path "student.csv" ] ;
      rml:referenceFormulation rml:CSV ] ;
  rml:subjectMap [ rml:template "http://example.com/{Name}" ] ;
  rml:predicateObjectMap [
      rml:predicate foaf:name ;
      rml:objectMap [
          rml:functionExecution <#Execution> ;
          rml:return grel:stringOut ] ] .

<#Execution>
  rml:function grel:toUpperCase ;
  rml:input [
      rml:parameter grel:valueParam ;
      rml:inputValueMap [ rml:reference "Name" ] ] .

rml:return names the function output to use. It must be an output the function declares.

Each property here is short for a map with a constant:

Short form Long form
rml:function <iri> rml:functionMap [ rml:constant <iri> ]
rml:parameter <iri> rml:parameterMap [ rml:constant <iri> ]
rml:inputValue <v> rml:inputValueMap [ rml:constant <v> ]
rml:return <iri> rml:returnMap [ rml:constant <iri> ]

Providing the implementation

There are five ways, listed from most to least preferred.

1. An existing library with FnO descriptions

Libraries such as GREL and IDLab come with FnO descriptions in RDF. CARML needs two kinds of description for a function:

  • the declaration (fno:Function), with its parameters and outputs,
  • the binding (fno:Mapping) of the function IRI to a Java class and method.

Register them with addFunctionDescriptions, once per file:

RdfRmlMapper mapper = RdfRmlMapper.builder()
    .mapping(mapping)
    .addFunctionDescriptions(resource("/fno/functions_idlab.ttl"), RDFFormat.TURTLE)
    .addFunctionDescriptions(resource("/fno/functions_idlab_classes_java_mapping.ttl"), RDFFormat.TURTLE)
    .build();

Here resource(...) stands for getResourceAsStream on one of your classes. addFunctionDescriptions also accepts an RDF4J Model.

What the two common libraries ship:

Library Coordinates Descriptions in the jar
IDLab be.ugent.idlab.knows:idlab-functions-java Declarations in /fno/functions_idlab.ttl, bindings in /fno/functions_idlab_classes_java_mapping.ttl. Needs Java 21.
GREL com.github.fnoio:grel-functions-java, from JitPack Bindings only, in /grel_java_mapping.ttl.

The GREL jar has no declarations, so rml:return grel:stringOut fails until you register them from another file. CARML's own tests use the functions.ttl of the RML-FNML test cases.

2. The built-in condition functions

CARML always registers five functions: idlab-fn:isNull, idlab-fn:isNotNull, idlab-fn:equal, idlab-fn:notEqual and grel:controls_if. They are meant for rml:condition, which makes a term map produce a value only when the condition holds:

rml:subjectMap [
  rml:template "http://example.com/{Name}" ;
  rml:condition [
    rml:functionExecution [
      rml:function idlab-fn:equal ;
      rml:input [ rml:parameter grel:valueParam ;  rml:inputValueMap [ rml:reference "Class" ] ] ,
                [ rml:parameter grel:valueParam2 ; rml:inputValue "A" ] ] ] ] .

3. A function provider

To have your own functions registered whenever their jar is on the classpath, implement FunctionProvider and register it as a service:

@AutoService(FunctionProvider.class)
public class MyFunctionProvider implements FunctionProvider {
  @Override
  public Collection<FunctionDescriptor> getFunctions() {
    return List.of(/* descriptors */);
  }
}

If you want to add and remove functions yourself, build a FunctionRegistry and pass it to .functionRegistry(registry).

4. A lambda

For a single function, define it on the builder:

RdfRmlMapper.builder()
    .mapping(mapping)
    .function("http://example.org/fn#toUpperCase")
        .param("http://example.org/fn#value", String.class)
        .returns("http://example.org/fn#out", String.class)
        .execute(args -> ((String) args.get(Values.iri("http://example.org/fn#value"))).toUpperCase(Locale.ROOT))
    .build();

The arguments arrive in a map keyed by parameter IRI. Use returns(outputIri, type) when the mapping has an rml:return, since the IRIs must match. returns(type) declares no output IRI.

5. Annotated classes

Annotate methods with @RmlFunction and their parameters with @RmlParam, then register an instance:

public class MyFunctions {
  @RmlFunction("http://example.org/fn#toUpperCase")
  public String toUpperCase(@RmlParam("http://example.org/fn#value") String value) {
    return value.toUpperCase(Locale.ROOT);
  }
}
RdfRmlMapper.builder().mapping(mapping).addFunctions(new MyFunctions()).build();

addFunctionClasses("com.example.MyFunctions") does the same from a class name. This only works for classes you can annotate.

When two registrations use the same function IRI, the later one in this order wins: built-ins, function providers, FnO descriptions, annotated classes, lambdas.

Legacy forms

  • @FnoFunction and @FnoParam are deprecated names for @RmlFunction and @RmlParam. They still work.
  • fnml:functionValue, from before RML-FNML, still runs in subject, predicate, object and graph maps. It does not run in join conditions or expression fields. There CARML logs a warning and produces no value. Use rml:functionExecution in new mappings.