/** * Return true if the given goal is tabled, currently this is true if the * predicate is a tabled predicate or the predicate is a wildcard and some * tabled predictes exist. */ public boolean isTabled(TriplePattern goal) { return isTabled(goal.getPredicate()); }
/** * Return an ordered list of RuleClauseCode objects to implement the given * query pattern. This may use indexing to narrow the rule set more that the predicate-only case. * @param goal the triple pattern that makes up the query */ public List<RuleClauseCode> codeFor(TriplePattern goal) { List<RuleClauseCode> allRules = codeFor(goal.getPredicate()); if (allRules == null) { return allRules; } Map<Node, List<RuleClauseCode>> indexedCodeTable = indexPredicateToCodeMap.get(goal.getPredicate()); if (indexedCodeTable != null) { List<RuleClauseCode> indexedCode = indexedCodeTable.get(goal.getObject()); if (indexedCode != null) { return indexedCode; } } return allRules; }
/** * Return a Finder instance appropriate for the given query. */ public Finder getFinder(TriplePattern pattern, Finder continuation) { if (!isPrepared) prepare(); Node predicate = pattern.getPredicate(); if (predicate.isVariable()) { // Want everything in the cache, the tbox and the continuation return FinderUtil.cascade(subPropertyCache, subClassCache, continuation); } else if (subPropertyAliases.contains(predicate)) { return subPropertyCache; } else if (subClassAliases.contains(predicate)) { return subClassCache; } else { return continuation; } }
/** * Return a list of rules that match the given goal pattern * @param goal the goal being matched */ public List<Rule> rulesFor(TriplePattern goal) { List<Rule> rules = new ArrayList<Rule>(); if (goal.getPredicate().isVariable()) { checkAll(goalMap.values().iterator(), goal, rules); } else { checkAll(goalMap.getAll(goal.getPredicate()), goal, rules); checkAll(goalMap.getAll(Node.ANY), goal, rules); } return rules; }
/** * Return true if the given pattern occurs somewhere in the find sequence. */ @Override public boolean contains(TriplePattern pattern) { return graph.contains(pattern.getSubject(), pattern.getPredicate(), pattern.getObject()); }
/** Return an list of variables or nodes in a ClauseEntry, in flattened order */ private List<Node> termVars(ClauseEntry term) { List<Node> result = new ArrayList<Node>(); if (term instanceof TriplePattern) { TriplePattern goal = (TriplePattern)term; result.add(goal.getSubject()); result.add(goal.getPredicate()); Node obj = goal.getObject(); if (Functor.isFunctor(obj)) { result.add(obj); result.addAll(termVars((Functor)obj.getLiteralValue())); } else { result.add(obj); } } else if (term instanceof Functor) { Node[] args = ((Functor)term).getArgs(); for (int i = 0; i < args.length; i++) { result.add(args[i]); } } return result; } }
/** * emit the code for the head clause */ void emitHead(TriplePattern head) { if (permanentVars.size() > 0) { code[p++] = ALLOCATE; code[p++] = (byte)permanentVars.size(); } emitHeadGet(head.getSubject(), 0); emitHeadGet(head.getPredicate(), 1); emitHeadGet(head.getObject(), 2); }
/** * Return the argument index of the given variable. */ int aIndex(Node n) { TriplePattern tp = (TriplePattern)rule.getHeadElement(0); if (tp.getSubject() == n) { return 0; } else if (tp.getPredicate() == n) { return 1; } else if (tp.getObject() == n) { return 2; } else { return -1; } }
/** * Return a simplified print string for a TriplePattern */ public static String print(TriplePattern triple) { if (triple == null) return "(null)"; return "(" + print(triple.getSubject()) + " " + print(triple.getPredicate()) + " " + print(triple.getObject()) + ")"; }
/** * Return a dereferenced copy of a triple. */ public static Triple deref(TriplePattern t) { if (t == null) return null; return new Triple(deref(t.getSubject()), deref(t.getPredicate()), deref(t.getObject())); }
/** * Bind the variables in a goal pattern using the binding environment, to * generate a more specialized goal * @param goal the TriplePattern to be instantiated * @return a TriplePattern obtained from the goal by substituting current bindinds */ public TriplePattern partInstantiate(TriplePattern goal) { return new TriplePattern( getGroundVersion(goal.getSubject()), getGroundVersion(goal.getPredicate()), getGroundVersion(goal.getObject()) ); }
/** * Scan the rules for any axioms and insert those */ protected void findAndProcessAxioms() { BFRuleContext context = new BFRuleContext(infGraph); for (Iterator<Rule> i = rules.iterator(); i.hasNext(); ) { Rule r = i.next(); if (r.bodyLength() == 0) { // An axiom for (int j = 0; j < r.headLength(); j++) { Object head = r.getHeadElement(j); if (head instanceof TriplePattern) { TriplePattern h = (TriplePattern) head; Triple t = new Triple(h.getSubject(), h.getPredicate(), h.getObject()); context.addTriple(t); infGraph.getDeductionsGraph().add(t); } } } } addSet(context); processedAxioms = true; }
/** * Clone a clause, cloning any embedded variables. */ private ClauseEntry cloneClause(ClauseEntry clause, Map<Node_RuleVariable, Node> vmap, BindingEnvironment env) { if (clause instanceof TriplePattern) { TriplePattern tp = (TriplePattern)clause; return new TriplePattern ( cloneNode(tp.getSubject(), vmap, env), cloneNode(tp.getPredicate(), vmap, env), cloneNode(tp.getObject(), vmap, env) ); } else { return cloneFunctor((Functor)clause, vmap, env); } }
/** * Find all the variables in a TriplePattern. */ private int findVars(TriplePattern t, int maxIn) { int max = maxIn; max = maxVarIndex(t.getSubject(), max); max = maxVarIndex(t.getPredicate(), max); Node obj = t.getObject(); if (obj instanceof Node_RuleVariable) { max = maxVarIndex(obj, max); } else if (Functor.isFunctor(obj)) { max = findVars((Functor)obj.getLiteralValue(), max); } return max; }
@Override public ExtendedIterator<Triple> findWithContinuation(TriplePattern pattern, Finder finder) { prepare(); Node subject = pattern.getSubject(); Node predicate = pattern.getPredicate(); Node object = pattern.getObject(); ExtendedIterator<Triple> i = GraphQueryHandler.findTriple( kb, this, subject, predicate, object ); // always look at asserted triples at the end if( finder != null ) { TriplePattern tp = new TriplePattern( subject, predicate, object ); i = i.andThen( finder.find( tp ) ); } // make sure we don't have duplicates return UniqueExtendedIterator.create( i ); }
/** * Extended find interface used in situations where the implementator * may or may not be able to answer the complete query. * <p> * In this case any query on the direct or closed predicates will * be assumed complete, any other query will pass on to the continuation.</p> * @param pattern a TriplePattern to be matched against the data * @param continuation either a Finder or a normal Graph which * will be asked for additional match results if the implementor * may not have completely satisfied the query. */ @Override public ExtendedIterator<Triple> findWithContinuation(TriplePattern pattern, Finder continuation) { Node p = pattern.getPredicate(); if (p.isVariable()) { // wildcard predicate so return merge of cache and continuation return find(pattern).andThen(continuation.find(pattern)); } else if (p.equals(directPredicate) || p.equals(closedPredicate)) { // Satisfy entire query from the cache return find(pattern); } else { // No matching triples in this cache so just search the continuation return continuation.find(pattern); } }
/** * Test if a TriplePattern matches a Triple in the given binding * environment. If it does then the binding environment is modified * the reflect any additional bindings. * @return true if the pattern matches the triple */ public static boolean match(TriplePattern pattern, Triple triple, BindingStack env) { env.push(); boolean matchOK = match(pattern.getPredicate(), triple.getPredicate(), env) && match(pattern.getObject(), triple.getObject(), env) && match(pattern.getSubject(), triple.getSubject(), env); if (matchOK) { env.commit(); return true; } else { env.unwind(); return false; } }
/** * Instantiate a triple pattern against the current environment. * This version handles unbound varibles by turning them into bNodes. * @param clause the triple pattern to match * @param env the current binding environment * @return a new, instantiated triple */ @Override public Triple instantiate(TriplePattern pattern) { Node s = getGroundVersion(pattern.getSubject()); if (s.isVariable()) s = NodeFactory.createAnon(); Node p = getGroundVersion(pattern.getPredicate()); if (p.isVariable()) p = NodeFactory.createAnon(); Node o = getGroundVersion(pattern.getObject()); if (o.isVariable()) o = NodeFactory.createAnon(); return new Triple(s, p, o); }
/** * Instantiate a triple pattern against the current environment. * This version handles unbound varibles by turning them into bNodes. * @param clause the triple pattern to match * @param env the current binding environment * @return a new, instantiated triple */ @Override public Triple instantiate(TriplePattern pattern) { Node s = getGroundVersion(pattern.getSubject()); if (s.isVariable()) s = NodeFactory.createAnon(); Node p = getGroundVersion(pattern.getPredicate()); if (p.isVariable()) p = NodeFactory.createAnon(); Node o = getGroundVersion(pattern.getObject()); if (o.isVariable()) o = NodeFactory.createAnon(); return new Triple(s, p, o); }
/** * Instantiate a triple pattern against the current environment. * This version handles unbound varibles by turning them into bNodes. * @param clause the triple pattern to match * @param env the current binding environment * @return a new, instantiated triple */ @Override public Triple instantiate(TriplePattern pattern) { Node s = getGroundVersion(pattern.getSubject()); if (s.isVariable()) s = NodeFactory.createAnon(); Node p = getGroundVersion(pattern.getPredicate()); if (p.isVariable()) p = NodeFactory.createAnon(); Node o = getGroundVersion(pattern.getObject()); if (o.isVariable()) o = NodeFactory.createAnon(); return new Triple(s, p, o); }