DEV Community

Wenqi Jiang
Wenqi Jiang

Posted on

Jena Custom Functions

https://github.com/oeg-upm/loom-ld

1. Write Functions

public abstract class AbstractSimilarityFunction extends FunctionBase2 implements Similarity {

  private SimilarityAlgorithm algorithm;

  public AbstractSimilarityFunction(SimilarityAlgorithm algorithm) {
    this.algorithm = algorithm;
  }

  @Override
  public NodeValue exec(NodeValue v1, NodeValue v2) {
    String element1 = v1.asString();
    String element2 = v2.asString();
    Double score = similarity(element1, element2);
    return NodeValue.makeDouble(score);
  }

  public SimilarityAlgorithm getAlgorithm() {
    return algorithm;
  }

  public void setAlgorithm(SimilarityAlgorithm algorithm) {
    this.algorithm = algorithm;
  }
}

Enter fullscreen mode Exit fullscreen mode

2. Registration Functions

FunctionRegistry.get().put(uri, funcClass);
Enter fullscreen mode Exit fullscreen mode

3. Use Custom Functions in SPARQL

PREFIX loom:     <http://oeg.upm.es/loom-ld/functions/link#>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)