Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A05BF200B47 for ; Sun, 17 Jul 2016 13:26:45 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9F1C8160A5C; Sun, 17 Jul 2016 11:26:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 24703160A6B for ; Sun, 17 Jul 2016 13:26:43 +0200 (CEST) Received: (qmail 60063 invoked by uid 500); 17 Jul 2016 11:26:43 -0000 Mailing-List: contact commits-help@jena.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jena.apache.org Delivered-To: mailing list commits@jena.apache.org Received: (qmail 60044 invoked by uid 99); 17 Jul 2016 11:26:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 17 Jul 2016 11:26:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1A5F2E08BD; Sun, 17 Jul 2016 11:26:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: der@apache.org To: commits@jena.apache.org Date: Sun, 17 Jul 2016 11:26:43 -0000 Message-Id: <8b22a4e7a4fc47bdb34e8921fdf61f37@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/3] jena git commit: [JENA-1204] support for multiple BuiltinRegistry for different rule sets archived-at: Sun, 17 Jul 2016 11:26:45 -0000 Repository: jena Updated Branches: refs/heads/master b6ee59d06 -> b722e943c [JENA-1204] support for multiple BuiltinRegistry for different rule sets Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/60f4a0a0 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/60f4a0a0 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/60f4a0a0 Branch: refs/heads/master Commit: 60f4a0a0f22b25e2ed167f3a4625d037c0d82c2c Parents: dea868f Author: Paul Houle Authored: Wed Jul 6 09:56:04 2016 -0400 Committer: Paul Houle Committed: Wed Jul 6 09:56:04 2016 -0400 ---------------------------------------------------------------------- .../apache/jena/reasoner/rulesys/Builtin.java | 2 +- .../jena/reasoner/rulesys/BuiltinRegistry.java | 88 +++++++------------- .../jena/reasoner/rulesys/FBRuleReasoner.java | 2 +- .../apache/jena/reasoner/rulesys/Functor.java | 57 +++++++++---- .../reasoner/rulesys/MapBuiltinRegistry.java | 69 +++++++++++++++ .../rulesys/OverrideBuiltinRegistry.java | 61 ++++++++++++++ .../org/apache/jena/reasoner/rulesys/Rule.java | 72 ++++++++++++---- .../reasoner/rulesys/impl/BindingStack.java | 2 +- .../reasoner/rulesys/impl/BindingVector.java | 2 +- .../jena/reasoner/rulesys/test/TestBugs.java | 4 +- .../test/TestGenericRuleReasonerConfig.java | 46 ++++++++++ 11 files changed, 309 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Builtin.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Builtin.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Builtin.java index eea2590..592f519 100755 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Builtin.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Builtin.java @@ -31,7 +31,7 @@ import org.apache.jena.graph.* ; *

* The mapping from the rule definition (which uses functors to hold the parsed call) * to the java implementation of the builtin is done via the - * {@link BuiltinRegistry BuiltinRegistry} which can + * {@link MapBuiltinRegistry BuiltinRegistry} which can * be user extended. */ public interface Builtin { http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/BuiltinRegistry.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/BuiltinRegistry.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/BuiltinRegistry.java index d37b574..b6ed580 100755 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/BuiltinRegistry.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/BuiltinRegistry.java @@ -1,3 +1,7 @@ +package org.apache.jena.reasoner.rulesys; + +import org.apache.jena.reasoner.rulesys.builtins.*; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -16,35 +20,11 @@ * limitations under the License. */ -package org.apache.jena.reasoner.rulesys; - -import java.util.*; - -import org.apache.jena.reasoner.rulesys.builtins.* ; - -/** * A registry for mapping functor names on java objects (instances - * of subclasses of Builtin) which implement their behvaiour. - *

- * This is currently implemented as a singleton to simply any future - * move to support different sets of builtins. - * - * @see Builtin - */ -public class BuiltinRegistry { - - /** The single global static registry */ +public abstract class BuiltinRegistry { + /** The default base registry */ public static BuiltinRegistry theRegistry; - - /** Mapping from functor name to Builtin implementing it */ - protected Map builtins = new HashMap<>(); - - /** Mapping from URI of builtin to implementation */ - protected Map builtinsByURI = new HashMap<>(); - - // Static initilizer for the singleton instance static { - theRegistry = new BuiltinRegistry(); - + theRegistry=new MapBuiltinRegistry(); theRegistry.register(new Print()); theRegistry.register(new AddOne()); theRegistry.register(new LessThan()); @@ -82,66 +62,54 @@ public class BuiltinRegistry { theRegistry.register(new ListNotContains()); theRegistry.register(new ListMapAsSubject()); theRegistry.register(new ListMapAsObject()); - + theRegistry.register(new MakeInstance()); theRegistry.register(new Table()); theRegistry.register(new TableAll()); - + theRegistry.register(new MakeSkolem()); - + theRegistry.register(new Hide()); - + theRegistry.register(new StrConcat()); theRegistry.register(new UriConcat()); theRegistry.register(new Regex()); - + theRegistry.register(new Now()); - + // Special purposes support functions for OWL theRegistry.register(new AssertDisjointPairs()); + } - + /** - * Construct an empty registry + * Register an implementation for a given builtin using its default name. + * @param impl the implementation of the builtin */ - public BuiltinRegistry() { - } - + + public abstract void register(Builtin impl); /** * Register an implementation for a given builtin functor. * @param functor the name of the functor used to invoke the builtin * @param impl the implementation of the builtin */ - public void register(String functor, Builtin impl) { - builtins.put(functor, impl); - builtinsByURI.put(impl.getURI(), impl); - } - - /** - * Register an implementation for a given builtin using its default name. - * @param impl the implementation of the builtin - */ - public void register(Builtin impl) { - builtins.put(impl.getName(), impl); - builtinsByURI.put(impl.getURI(), impl); - } - + + public abstract void register(String functor, Builtin impl); /** * Find the implementation of the given builtin functor. * @param functor the name of the functor being invoked. * @return a Builtin or null if there is none registered under that name */ - public Builtin getImplementation(String functor) { - return builtins.get(functor); - } - + + public abstract Builtin getImplementation(String functor); + /** * Find the implementation of the given builtin functor. * @param uri the URI of the builtin to be retrieved * @return a Builtin or null if there is none registered under that name */ - public Builtin getImplementationByURI(String uri) { - return builtinsByURI.get(uri); - } - + + public abstract Builtin getImplementationByURI(String uri); + + } http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/FBRuleReasoner.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/FBRuleReasoner.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/FBRuleReasoner.java index 1f067c8..51ac06f 100644 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/FBRuleReasoner.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/FBRuleReasoner.java @@ -280,7 +280,7 @@ public class FBRuleReasoner implements RuleReasoner { // Create a dummy rule which tables the predicate ... Rule tablePredicateRule = new Rule("", new ClauseEntry[]{ - new Functor("table", new Node[] { predicate }) + new Functor("table", new Node[] { predicate },BuiltinRegistry.theRegistry) }, new ClauseEntry[]{}); // ... end append the rule to the ruleset http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Functor.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Functor.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Functor.java index 2eb22e5..ee0ea90 100755 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Functor.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Functor.java @@ -54,17 +54,7 @@ public class Functor implements ClauseEntry { }; protected static Logger logger = LoggerFactory.getLogger(Functor.class); - - /** - * Constructor. - * @param name the name of the functor - * @param args a list of nodes defining the arguments - */ - public Functor(String name, List args) { - this.name = name; - this.args = args.toArray(new Node[]{}); - } - + /** * Constructor. * @param name the name of the functor @@ -80,13 +70,48 @@ public class Functor implements ClauseEntry { * Constructor * @param name the name of the functor * @param args a list of nodes defining the arguments - * @param registry a table of builtins to consult to check for + * @param registry a table of builtins to consult to check for * implementations of this functor when used as a rule clause */ public Functor(String name, List args, BuiltinRegistry registry) { + this(name, args,registry.getImplementation(name)); + } + + /** + * Constructor + * @param name the name of the functor + * @param args an array of nodes defining the arguments + * @param registry a table of builtins to consult to check for + * implementations of this functor when used as a rule clause + */ + public Functor(String name,Node[] args, BuiltinRegistry registry) { + this(name, args,registry.getImplementation(name)); + } + + /** + * Constructor + * @param name the name of the functor + * @param args a list of nodes defining the arguments + * @param impl a specific builtin implementation of this functor + * + */ + public Functor(String name, List args, Builtin impl) { this.name = name; this.args = args.toArray(new Node[]{}); - this.implementor = registry.getImplementation(name); + this.implementor = impl; + } + + /** + * Constructor + * @param name the name of the functor + * @param args a list of nodes defining the arguments + * @param impl a specific builtin implementation of this functor + * + */ + public Functor(String name, Node[] args, Builtin impl) { + this.name = name; + this.args = args; + this.implementor = impl; } /** @@ -262,7 +287,7 @@ public class Functor implements ClauseEntry { } return false; } - + /** * Create a functor and wrap it up as a Literal node * @param name the name of the functor @@ -270,9 +295,9 @@ public class Functor implements ClauseEntry { * accidental structure sharing */ public static Node makeFunctorNode(String name, Node[] args) { - return makeFunctorNode( new Functor( name, args ) ); + return makeFunctorNode( new Functor( name, args, BuiltinRegistry.theRegistry ) ); } - + /** * Wrap a functor as a Literal node * @param f the functor data structure to be wrapped in a node. http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/MapBuiltinRegistry.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/MapBuiltinRegistry.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/MapBuiltinRegistry.java new file mode 100644 index 0000000..fd542ad --- /dev/null +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/MapBuiltinRegistry.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.reasoner.rulesys; + +import java.util.*; + +import org.apache.jena.reasoner.rulesys.builtins.* ; + +/** * A registry for mapping functor names on java objects (instances + * of subclasses of Builtin) which implement their behvaiour. + *

+ * This is currently implemented as a singleton to simply any future + * move to support different sets of builtins. + * + * @see Builtin + */ +public class MapBuiltinRegistry extends BuiltinRegistry { + + /** Mapping from functor name to Builtin implementing it */ + protected Map builtins = new HashMap<>(); + + /** Mapping from URI of builtin to implementation */ + protected Map builtinsByURI = new HashMap<>(); + + /** + * Construct an empty registry + */ + public MapBuiltinRegistry() { + } + + @Override + public void register(String functor, Builtin impl) { + builtins.put(functor, impl); + builtinsByURI.put(impl.getURI(), impl); + } + + @Override + public void register(Builtin impl) { + builtins.put(impl.getName(), impl); + builtinsByURI.put(impl.getURI(), impl); + } + + @Override + public Builtin getImplementation(String functor) { + return builtins.get(functor); + } + + @Override + public Builtin getImplementationByURI(String uri) { + return builtinsByURI.get(uri); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/OverrideBuiltinRegistry.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/OverrideBuiltinRegistry.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/OverrideBuiltinRegistry.java new file mode 100644 index 0000000..e8e39f0 --- /dev/null +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/OverrideBuiltinRegistry.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.reasoner.rulesys; + +import java.util.HashMap; +import java.util.Map; + +public class OverrideBuiltinRegistry extends BuiltinRegistry { + + /** Mapping from functor name to Builtin implementing it */ + final protected Map builtins = new HashMap<>(); + + /** Mapping from URI of builtin to implementation */ + final protected Map builtinsByURI = new HashMap<>(); + + final protected BuiltinRegistry innerRegistry; + + public OverrideBuiltinRegistry(BuiltinRegistry innerRegistry) { + this.innerRegistry = innerRegistry; + } + + @Override + public void register(String functor, Builtin impl) { + builtins.put(functor, impl); + builtinsByURI.put(impl.getURI(), impl); + } + + @Override + public void register(Builtin impl) { + builtins.put(impl.getName(), impl); + builtinsByURI.put(impl.getURI(), impl); + } + + @Override + public Builtin getImplementation(String functor) { + Builtin that=builtins.get(functor); + return that==null ? innerRegistry.getImplementation(functor) : that; + } + + @Override + public Builtin getImplementationByURI(String uri) { + Builtin that=builtinsByURI.get(uri); + return that==null ? innerRegistry.getImplementationByURI(uri) : that; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Rule.java ---------------------------------------------------------------------- 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 c0ad540..d38b6fb 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 @@ -158,7 +158,7 @@ public class Rule implements ClauseEntry { else { throw new ReasonerException( - "Undefined Functor " + ( (Functor) elt ).getName() + " in " + toShortString() ); + "(allMonotonic) Undefined Functor " + ( (Functor) elt ).getName() + " in " + toShortString() ); } } } @@ -376,7 +376,7 @@ public class Rule implements ClauseEntry { for (int i = 0; i < args.length; i++) { cargs[i] = cloneNode(args[i], vmap, env); } - Functor fn = new Functor(f.getName(), cargs); + Functor fn = new Functor(f.getName(), cargs, f.getImplementor()); fn.setImplementor(f.getImplementor()); return fn; } @@ -482,19 +482,31 @@ public class Rule implements ClauseEntry { // parser access /** - * Parse a string as a rule. + * Parse a string as a rule, using the default BuiltinRegistry to resolve functor names + * * @throws ParserException if there is a problem */ public static Rule parseRule(String source) throws ParserException { - Parser parser = new Parser(source); + return parseRule(source,BuiltinRegistry.theRegistry); + } + + /** + * Parse a string as a rule using a specified BuiltinRegistry to resolve functor names + * + * @param source the source string for the rule + * @param registry the BuiltinRegistry used to resolve functor names + * @throws ParserException if there is a problem + */ + public static Rule parseRule(String source,BuiltinRegistry registry) throws ParserException { + Parser parser = new Parser(source,registry); return parser.parseRule(); } - + /** * Answer the list of rules parsed from the given URL. * @throws RulesetNotFoundException */ - public static List rulesFromURL( String uri ) { + public static List rulesFromURL( String uri,BuiltinRegistry registry) { BufferedReader br = null; try { InputStream in = FileManager.get().open(uri); @@ -505,6 +517,14 @@ public class Rule implements ClauseEntry { if (br != null) try { br.close(); } catch (IOException e2) {} } } + + /** + * Answer the list of rules parsed from the given URL. + * @throws RulesetNotFoundException + */ + public static List rulesFromURL( String uri) { + return rulesFromURL(uri,BuiltinRegistry.theRegistry); + } /** * Processes the source reader stripping off comment lines and noting prefix @@ -512,7 +532,7 @@ public class Rule implements ClauseEntry { * Returns a parser which is bound to the stripped source text with * associated prefix and rule inclusion definitions. */ - public static Parser rulesParserFromReader( BufferedReader src ) { + public static Parser rulesParserFromReader( BufferedReader src, BuiltinRegistry registry ) { try { StringBuilder result = new StringBuilder(); String line; @@ -557,7 +577,7 @@ public class Rule implements ClauseEntry { result.append("\n"); } } - Parser parser = new Parser(result.toString()); + Parser parser = new Parser(result.toString(),registry); parser.registerPrefixMap(prefixes); parser.addRulesPreload(preloadedRules); return parser; @@ -566,6 +586,16 @@ public class Rule implements ClauseEntry { { throw new WrappedIOException( e ); } } + /** + * Processes the source reader stripping off comment lines and noting prefix + * definitions (@prefix) and rule inclusion commands (@include). + * Returns a parser which is bound to the stripped source text with + * associated prefix and rule inclusion definitions. + */ + public static Parser rulesParserFromReader( BufferedReader src) { + return rulesParserFromReader(src,BuiltinRegistry.theRegistry); + } + /** * Helper function find a URI argument in the current string, * optionally surrounded by matching <>. @@ -644,8 +674,17 @@ public class Rule implements ClauseEntry { * @return a list of rules * @throws ParserException if there is a problem */ + public static List parseRules(String source,BuiltinRegistry registry) throws ParserException { + return parseRules(new Parser(source,registry)); + } + + /** + * Parse a string as a list a rules. + * @return a list of rules + * @throws ParserException if there is a problem + */ public static List parseRules(String source) throws ParserException { - return parseRules(new Parser(source)); + return parseRules(source,BuiltinRegistry.theRegistry); } @@ -658,13 +697,17 @@ public class Rule implements ClauseEntry { * No embedded spaces supported. */ public static class Parser { - + + /** Tokenizer */ private Tokenizer stream; /** Look ahead, null if none */ private String lookahead; - + + // registry for builtin name lookup + private final BuiltinRegistry registry; + // Literal parse state flags private static final int NORMAL = 0; private static final int STARTED_LITERAL = 1; @@ -691,9 +734,10 @@ public class Rule implements ClauseEntry { * Constructor * @param source the string to be parsed */ - Parser(String source) { + Parser(String source,BuiltinRegistry registry) { stream = new Tokenizer(source, "()[], \t\n\r", "'\"", true); lookahead = null; + this.registry=registry; } /** @@ -859,7 +903,7 @@ public class Rule implements ClauseEntry { } return NodeFactory.createURI(exp); } else if (peekToken().equals("(")) { - Functor f = new Functor(token, parseNodeList(), BuiltinRegistry.theRegistry); + Functor f = new Functor(token, parseNodeList(), registry); return Functor.makeFunctorNode( f ); } else if (token.equals("'") || token.equals("\"")) { // A plain literal @@ -968,7 +1012,7 @@ public class Rule implements ClauseEntry { } else { String name = nextToken(); List args = parseNodeList(); - Functor clause = new Functor(name, args, BuiltinRegistry.theRegistry); + Functor clause = new Functor(name, args, registry); if (clause.getImplementor() == null) { // Not a.error error becase later processing can add this // implementation to the registry http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingStack.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingStack.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingStack.java index d4e883f..03fae6a 100644 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingStack.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingStack.java @@ -137,7 +137,7 @@ public class BindingStack implements BindingEnvironment { } boundargs.add( binding ); } - Functor newf = new Functor(functor.getName(), boundargs); + Functor newf = new Functor(functor.getName(), boundargs, functor.getImplementor()); return Functor.makeFunctorNode( newf ); } else { return node; http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingVector.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingVector.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingVector.java index 70a20d1..eaca73e 100644 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingVector.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/impl/BindingVector.java @@ -98,7 +98,7 @@ public class BindingVector implements BindingEnvironment { } boundargs.add( binding ); } - Functor newf = new Functor( functor.getName(), boundargs ); + Functor newf = new Functor( functor.getName(), boundargs, functor.getImplementor() ); return Functor.makeFunctorNode( newf ); } else { return node; http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestBugs.java ---------------------------------------------------------------------- diff --git a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestBugs.java b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestBugs.java index dd14c03..5194f85 100644 --- a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestBugs.java +++ b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestBugs.java @@ -635,7 +635,7 @@ public class TestBugs extends TestCase { */ public void testGroundClosure2() { Flag myFlag = new Flag(); - BuiltinRegistry.theRegistry.register(myFlag); + MapBuiltinRegistry.theRegistry.register(myFlag); List rules = Rule.rulesFromURL("file:testing/reasoners/bugs/groundClosure2.rules"); GenericRuleReasoner reasoner = new GenericRuleReasoner( rules ); InfModel inf = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel()); @@ -792,7 +792,7 @@ public class TestBugs extends TestCase { assertEquals( culprit, ResourceFactory.createTypedLiteral( new Integer(42) )); culprit = doTestLiteralsInErrorReports("-> (eg:a eg:p 'foo'). (?X rb:violation error('test', 'arg')) <- (?S eg:p ?X)."); assertEquals( culprit, ResourceFactory.createPlainLiteral("foo")); - BuiltinRegistry.theRegistry.register( new SomeTriple() ); + MapBuiltinRegistry.theRegistry.register( new SomeTriple() ); culprit = doTestLiteralsInErrorReports("-> (eg:a eg:p 42). (?X rb:violation error('test', 'arg')) <- (?S eg:p ?Y), someTriple(?X)."); assertTrue( culprit.isLiteral() ); Object val = ((Literal)culprit).getValue(); http://git-wip-us.apache.org/repos/asf/jena/blob/60f4a0a0/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestGenericRuleReasonerConfig.java ---------------------------------------------------------------------- diff --git a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestGenericRuleReasonerConfig.java b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestGenericRuleReasonerConfig.java index 75e5841..2c96559 100644 --- a/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestGenericRuleReasonerConfig.java +++ b/jena-core/src/test/java/org/apache/jena/reasoner/rulesys/test/TestGenericRuleReasonerConfig.java @@ -21,10 +21,17 @@ package org.apache.jena.reasoner.rulesys.test; import java.util.*; import org.apache.jena.assembler.test.AssemblerTestBase ; +import org.apache.jena.graph.Node; import org.apache.jena.rdf.model.* ; +import org.apache.jena.reasoner.Reasoner; import org.apache.jena.reasoner.rulesys.* ; +import org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin; +import org.apache.jena.vocabulary.RDF; import org.apache.jena.vocabulary.ReasonerVocabulary ; +import static org.apache.jena.reasoner.rulesys.Rule.parseRule; + + /** Your eyes will bleed with the number of backslashes required in the substitute strings. @@ -121,4 +128,43 @@ public class TestGenericRuleReasonerConfig extends AssemblerTestBase rules.addAll( Rule.rulesFromURL( whereB ) ); return rules; } + + public void testRuleLoadingWithOverridenBuiltins() { + List savedNode=new ArrayList<>(); + Builtin b= new BaseBuiltin() { + @Override + public String getName() { + return "groo"; + } + + @Override + public int getArgLength() { + return 1; + } + + @Override + public void headAction(Node[] args, int length, RuleContext context) { + savedNode.add(getArg(0,args,context)); + } + + + }; + BuiltinRegistry r=new OverrideBuiltinRegistry(BuiltinRegistry.theRegistry); + r.register(b); + assertEquals(b,r.getImplementation("groo")); + List rules=new ArrayList<>(); + // + // note that the head action does not appear to fire unless we put a triple in the head as well.. is + // this expected? + // + rules.add(parseRule("[ (?instance rdf:type ?type) -> groo(?type) ]",r)); + GenericRuleReasoner article=new GenericRuleReasoner(rules); + article.setMode(GenericRuleReasoner.FORWARD_RETE); + Model input=ModelFactory.createDefaultModel(); + input.add(input.createResource(), RDF.type,input.createResource("http://example.com/Renegade")); + InfModel output=ModelFactory.createInfModel(article,input); + output.size(); // not optional, inferences are not run if we don't trigger them + assertEquals(1,savedNode.size()); + assertEquals("http://example.com/Renegade",savedNode.get(0).getURI()); } +}