You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2012/11/19 21:42:24 UTC

svn commit: r1411398 - /jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java

Author: rvesse
Date: Mon Nov 19 20:42:24 2012
New Revision: 1411398

URL: http://svn.apache.org/viewvc?rev=1411398&view=rev
Log:
Adding missing file from previous commit

Added:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java

Added: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java?rev=1411398&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java (added)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/expr/ExprTransformSubstitute.java Mon Nov 19 20:42:24 2012
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2012 YarcData LLC All Rights Reserved.
+ */ 
+
+package com.hp.hpl.jena.sparql.expr;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.hp.hpl.jena.sparql.core.Var;
+import com.hp.hpl.jena.sparql.function.user.UserDefinedFunction;
+
+/**
+ * An expression transformer that substitutes another expression in place of variables
+ * <p>
+ * Primarily introduced in order to support the new {@link UserDefinedFunction} capabilities
+ * </p>
+ * @author rvesse
+ *
+ */
+public class ExprTransformSubstitute extends ExprTransformCopy {
+    
+    private Map<String, Expr> replacements = new HashMap<String, Expr>();
+    
+    public ExprTransformSubstitute(Var find, Expr replace) {
+        if (find == null) throw new IllegalArgumentException("find cannot be null");
+        if (replace == null) throw new IllegalArgumentException("replace cannot be null");
+        this.replacements.put(find.getVarName(), replace);
+    }
+    
+    public ExprTransformSubstitute(Map<String, Expr> substitutions) {
+        if (substitutions == null) throw new IllegalArgumentException("replacements cannot be null");
+        this.replacements.putAll(substitutions);
+        
+        for (String key : this.replacements.keySet()) {
+            if (this.replacements.get(key) == null) throw new IllegalArgumentException("Variable ?" + key + " cannot be mapped to a null expression");
+        }
+    }
+    
+    @Override
+    public Expr transform(ExprVar exprVar) {
+        //If variable matches replace with the chosen expression
+        if (this.replacements.containsKey(exprVar.getVarName())) return this.replacements.get(exprVar.getVarName());
+        //Otherwise leave as is
+        return super.transform(exprVar);
+    }
+
+}