/** * 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; }
if (body[i] instanceof TriplePattern) { TriplePattern clause = (TriplePattern) body[i]; int score = scoreNodeBoundness(clause.getSubject(), env) * 3 + scoreNodeBoundness(clause.getPredicate(), env) * 2 + scoreNodeBoundness(clause.getObject(), env) * 3;
/** * 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); } }
@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 ); }
/** * 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; }
emitBodyPut(goal.getSubject(), 0, false); emitBodyPut(goal.getPredicate(), 1, false); emitBodyPut(goal.getObject(), 2, false);
/** * 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); }
Node[] hEnv = new Node[numRuleVars]; if (!unify(goal.getSubject(), head.getSubject(), gEnv, hEnv)) { return null;
envFrame.pVars[0] = argVars[0] = standardize(goal.getSubject(), mappedVars); envFrame.pVars[1] = argVars[1] = standardize(goal.getPredicate(), mappedVars); envFrame.pVars[2] = argVars[2] = standardize(goal.getObject(), mappedVars);
Node s = pattern.getSubject(); Node p = pattern.getPredicate(); Node o = pattern.getObject();