/** * Add a single/remove a compound rule from the store. * @param rule the rule, may have multiple heads * @param isAdd true to add, false to remove */ private void addRemoveRule(Rule rule, boolean isAdd) { if (rule.headLength() != 1) { for (int j = 0; j < rule.headLength(); j++) { Rule newRule = new Rule(rule.getName(), new ClauseEntry[] {rule.getHeadElement(j)}, rule.getBody() ); newRule.setNumVars(rule.getNumVars()); doAddRemoveRule(newRule, isAdd); } } else { doAddRemoveRule(rule, isAdd); } }
/** * Parse a string as a list a rules. * @return a list of rules * @throws ParserException if there is a problem */ public static List<Rule> parseRules(String source) throws ParserException { return parseRules(source,BuiltinRegistry.theRegistry); }
/** * Compare clause entries, taking into account variable indices. * The equality function ignores differences between variables. */ @Override public boolean sameAs(Object o) { return equals(o); }
/** * Clone a rule, cloning any embedded variables. */ public Rule cloneRule() { if (getNumVars() > 0) { HashMap<Node_RuleVariable, Node> vmap = new HashMap<>(); return new Rule(name, cloneClauseArray(head, vmap, null), cloneClauseArray(body, vmap, null)); } else { return this; } }
/** * Instantiate a rule given a variable binding environment. * This will clone any non-bound variables though that is only needed * for trail implementations. */ public Rule instantiate(BindingEnvironment env) { HashMap<Node_RuleVariable, Node> vmap = new HashMap<>(); return new Rule(name, cloneClauseArray(head, vmap, env), cloneClauseArray(body, vmap, env)); }
/** * Scan the initial rule set and pick out all the backward-only rules with non-null bodies, * and transfer these rules to the backward engine. */ private static List<Rule> extractPureBackwardRules(List<Rule> rules) { List<Rule> bRules = new ArrayList<>(); for ( Rule r : rules ) { if ( r.isBackward() && r.bodyLength() > 0 ) { bRules.add( r ); } } return bRules; }
ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI)context.getGraph(); if (infGraph.shouldTrace()) { logger.info("Fired rule: " + rule.toShortString()); if (infGraph.shouldLogDerivations() && isAdd) { matchList = new ArrayList<>(rule.bodyLength()); for (int i = 0; i < rule.bodyLength(); i++) { Object clause = rule.getBodyElement(i); if (clause instanceof TriplePattern) { matchList.add(env.instantiate((TriplePattern)clause)); for (int i = 0; i < rule.headLength(); i++) { Object hClause = rule.getHeadElement(i); if (hClause instanceof TriplePattern) { Triple t = env.instantiate((TriplePattern) hClause); imp.headAction(f.getBoundArgs(env), f.getArgLength(), context); } else { throw new ReasonerException("Invoking undefined Functor " + f.getName() +" in " + rule.toShortString()); if (r.isBackward()) { if (isAdd) { infGraph.addBRule(r.instantiate(env)); } else { infGraph.deleteBRule(r.instantiate(env));
private void addSubclassRule(final ATermAppl sub, final ATermAppl sup, final FreeVariableStore freeVar) { final List<ClauseEntry> body = new ArrayList<>(); final List<ClauseEntry> head = new ArrayList<>(); final Node var = freeVar.next(); translateSub(body, sub, freeVar, var); translateSuper(head, sup, freeVar, var); _rules.add(new Rule(head, body)); }
/** * Load a set of rule definitions including processing of * comment lines and any initial prefix definition lines. * Also notes the prefix definitions for adding to a later inf model. */ public static List<Rule> loadRules(BufferedReader src, Map<String, String> prefixes) { Rule.Parser parser = Rule.rulesParserFromReader(src); List<Rule> rules = Rule.parseRules(parser); prefixes.putAll(parser.getPrefixMap()); return rules; }
/** * Answer the list of rules parsed from the given URL. * @throws RulesetNotFoundException */ public static List<Rule> rulesFromURL( String uri) { return rulesFromURL(uri,BuiltinRegistry.theRegistry); }
/** * Test parser modes to support backarrow notation are working */ public void testParse() { List<Rule> rules = Rule.parseRules(testRules1); assertEquals("BRule parsing", "[ (?x ?q ?y) <- (?p rdfs:subPropertyOf ?q) (?x ?p ?y) ]", rules.get(0).toString()); assertEquals("BRule parsing", "[ (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b) (?b rdfs:subPropertyOf ?c) ]", rules.get(1).toString()); }
data.add(new Triple(a, r, b)); data.add(new Triple(p, ty, s)); List<Rule> rules = Rule.parseRules( "[a1: -> (a rdf:type t)]" + "[r0: (?x r ?y) -> (?x p ?y)]" + assertTrue(d.getRule().getName().equals("r1b")); TestUtil.assertIteratorValues(this, d.getMatches().iterator(), new Object[] { new Triple(a, p, b) }); assertTrue(! di.hasNext());
data.add(new Triple(C1, sC, C2)); data.add(new Triple(C2, sC, C3)); List<Rule> rules = Rule.parseRules( "-> table(rdf:type)." + "[r1: (?x p ?c) -> (?x rdf:type ?c)] " + ); if (!useTGC) { rules.add(Rule.parseRule("[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)] "));
if ( ignoreBrules && rule.isBackward() ) int numVars = rule.getNumVars(); boolean[] seenVar = new boolean[numVars]; RETESourceNode prior = null; for ( int i = 0; i < rule.bodyLength(); i++ ) Object clause = rule.getBodyElement( i ); if ( clause instanceof TriplePattern )
/** * 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 { return parseRule(source,BuiltinRegistry.theRegistry); }
/** * Check parser extension for f/b distinction. */ public void testParser() { String rf = "(?a rdf:type ?t) -> (?t rdf:type rdfs:Class)."; String rb = "(?t rdf:type rdfs:Class) <- (?a rdf:type ?t)."; assertTrue( ! Rule.parseRule(rf).isBackward() ); assertTrue( Rule.parseRule(rb).isBackward() ); }
/** * Test rule equality operations. */ public void testRuleEquality() { Rule r1 = Rule.parseRule("(?a p ?b) -> (?a q ?b)."); Rule r2 = Rule.parseRule("(?a p ?b) -> (?b q ?a)."); Rule r1b = Rule.parseRule("(?x p ?y) -> (?x q ?y)."); Rule r3 = Rule.parseRule("(?a p ?b), addOne(?a) -> (?a q ?b)."); Rule r3b = Rule.parseRule("(?c p ?d), addOne(?c) -> (?c q ?d)."); Rule r4 = Rule.parseRule("(?a p ?b), makeTemp(?a) -> (?a q ?b)."); Rule r5 = Rule.parseRule("(?a p ?b), addOne(?b) -> (?a q ?b)."); Rule r6 = Rule.parseRule("(?a p ?b), addOne(p) -> (?a q ?b)."); assertTrue(! r1.equals(r2)); assertTrue( r1.equals(r1b)); assertTrue(! r1.equals(r3)); assertTrue( r3.equals(r3b)); assertTrue(! r3.equals(r4)); assertTrue(! r3.equals(r5)); assertTrue(! r3.equals(r6)); }
if ( ignoreBrules && r.isBackward() ) Object[] body = r.getBody(); for ( int j = 0; j < body.length; j++ )
/** * 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); }