You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2022/04/28 11:29:57 UTC

[jena] branch main updated: fix for custom builtins not being used when parsing rules

This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/main by this push:
     new b6651e6567 fix for custom builtins not being used when parsing rules
     new f456247c40 Merge pull request #1268 from loticlabs/bugfix/rule-parse-builtins
b6651e6567 is described below

commit b6651e65674c1ac7480085499ae293c460f8f716
Author: Matt Marshall <ma...@users.noreply.github.com>
AuthorDate: Thu Apr 21 18:16:07 2022 -0400

    fix for custom builtins not being used when parsing rules
---
 .../org/apache/jena/reasoner/rulesys/Rule.java     |  2 +-
 .../jena/reasoner/rulesys/test/TestRuleLoader.java | 47 +++++++++++++++++++---
 .../testing/reasoners/bugs/custom-builtins.rules   |  4 ++
 .../reasoners/rules/include-test-not-found.rules   |  8 ++++
 jena-core/testing/reasoners/rules/ruleset1.rules   |  3 ++
 jena-core/testing/reasoners/rules/ruleset2.rules   |  3 ++
 6 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Rule.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Rule.java
index df3f29d8e5..f661e7080b 100755
--- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Rule.java
+++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Rule.java
@@ -510,7 +510,7 @@ public class Rule implements ClauseEntry {
             InputStream in = FileManager.getInternal().open(uri);
             if (in == null) throw new RulesetNotFoundException( uri );
             br = FileUtils.asBufferedUTF8( in );
-            return parseRules( Rule.rulesParserFromReader( br ) );
+            return parseRules( Rule.rulesParserFromReader( br, registry ) );
         } finally {
             if (br != null) try { br.close(); } catch (IOException e2) {}
         }
diff --git a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestRuleLoader.java b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestRuleLoader.java
index 77042fc762..530e9ba889 100644
--- a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestRuleLoader.java
+++ b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestRuleLoader.java
@@ -18,23 +18,60 @@
 
 package org.apache.jena.reasoner.rulesys.test;
 
-import org.apache.jena.reasoner.rulesys.Rule ;
-import org.apache.jena.shared.RulesetNotFoundException ;
-import org.apache.jena.shared.WrappedIOException ;
+import org.apache.jena.reasoner.rulesys.BuiltinRegistry;
+import org.apache.jena.reasoner.rulesys.MapBuiltinRegistry;
+import org.apache.jena.reasoner.rulesys.Rule;
+import org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin;
+import org.apache.jena.shared.RulesetNotFoundException;
+import org.apache.jena.shared.WrappedIOException;
 import org.junit.Test;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
 /**
  * Tests for the rule loader
  */
 public class TestRuleLoader  {
-    
+
+    private static BuiltinRegistry createBuiltinRegistry() {
+        BuiltinRegistry br = new MapBuiltinRegistry();
+        br.register(new BaseBuiltin() {
+            @Override
+            public String getName() {
+                return "customBuiltin";
+            }
+        });
+        return br;
+    }
+
     @Test(expected=RulesetNotFoundException.class)
     public void load_from_file_uri_non_existent() {
         Rule.rulesFromURL("file:///no-such-file.txt");
     }
-    
+
+    @Test
+    public void load_from_file_with_include_uri_non_existent() {
+        RulesetNotFoundException e = assertThrows(RulesetNotFoundException.class,
+                () -> Rule.rulesFromURL("testing/reasoners/rules/include-test-not-found.rules"));
+        assertEquals("file:testing/reasoners/includeAlt.rules", e.getURI());
+    }
+
     @Test(expected=WrappedIOException.class)
     public void load_from_file_bad_encoding() {
         Rule.rulesFromURL("testing/reasoners/bugs/bad-encoding.rules");
     }
+
+    /**
+     * Test that {@link Rule#rulesFromURL(String, BuiltinRegistry)} uses the builtin registry argument given.
+     */
+    @Test
+    public void load_from_file_with_custom_builtins() {
+        BuiltinRegistry br = createBuiltinRegistry();
+        List<Rule> rules = Rule.rulesFromURL("testing/reasoners/bugs/custom-builtins.rules", br);
+        assertEquals(List.of("ruleWithBuiltin"), rules.stream().map(Rule::getName).collect(Collectors.toList()));
+    }
 }
diff --git a/jena-core/testing/reasoners/bugs/custom-builtins.rules b/jena-core/testing/reasoners/bugs/custom-builtins.rules
new file mode 100644
index 0000000000..ab34a72b26
--- /dev/null
+++ b/jena-core/testing/reasoners/bugs/custom-builtins.rules
@@ -0,0 +1,4 @@
+@prefix ex: <https://example.com/>
+
+# rule uses a custom builtin
+ruleWithBuiltin: (?s ?p ?o) -> customBuiltin(?s, ?p, ?o) .
\ No newline at end of file
diff --git a/jena-core/testing/reasoners/rules/include-test-not-found.rules b/jena-core/testing/reasoners/rules/include-test-not-found.rules
new file mode 100644
index 0000000000..daeef74aea
--- /dev/null
+++ b/jena-core/testing/reasoners/rules/include-test-not-found.rules
@@ -0,0 +1,8 @@
+# Test file used to check @import redirection
+@prefix p1: <http://jena.hpl.hp.com/newprefix#>
+
+-> (p1:A p1:p p1:foo).
+
+@include <file:testing/reasoners/includeAlt.rules>.
+
+ 
\ No newline at end of file
diff --git a/jena-core/testing/reasoners/rules/ruleset1.rules b/jena-core/testing/reasoners/rules/ruleset1.rules
new file mode 100644
index 0000000000..8635e9ec1a
--- /dev/null
+++ b/jena-core/testing/reasoners/rules/ruleset1.rules
@@ -0,0 +1,3 @@
+# Used to test inclusion of multiple rule sets
+
+rule1: -> (eg:a rdf:type eg:C) .
diff --git a/jena-core/testing/reasoners/rules/ruleset2.rules b/jena-core/testing/reasoners/rules/ruleset2.rules
new file mode 100644
index 0000000000..d71329648e
--- /dev/null
+++ b/jena-core/testing/reasoners/rules/ruleset2.rules
@@ -0,0 +1,3 @@
+# Used to test inclusion of multiple rule sets
+
+rule2: -> (eg:a rdf:type eg:D) .