private static Graph unwrap(Graph graph) { for (;;) { if ( graph instanceof InfGraph ) { graph = ((InfGraph)graph).getRawGraph(); continue; } Graph graph2 = GraphOps.unwrapOne(graph); if ( graph2 == graph ) return graph; graph = graph2; } }
/** * Get the single static precomputed rule closure. */ protected synchronized InfGraph getPreload() { if (cachePreload && preload == null) { preload = (new FBRuleInfGraph(this, rules, null)); preload.prepare(); } return preload; }
/** * <p>Returns a derivations model. The rule reasoners typically create a * graph containing those triples added to the base graph due to rule firings. * In some applications it can useful to be able to access those deductions * directly, without seeing the raw data which triggered them. In particular, * this allows the forward rules to be used as if they were rewrite transformation * rules.</p> * * @return The derivations model, if one is defined, or else null */ @Override public Model getDeductionsModel() { if (m_deductionsModel == null) { InfGraph infGraph = getInfGraph(); if (infGraph != null) { Graph deductionsGraph = infGraph.getDeductionsGraph(); if (deductionsGraph != null) { m_deductionsModel = ModelFactory.createModelForGraph( deductionsGraph ); } } } else { // ensure that the cached model sees the updated changes from the // underlying reasoner graph getInfGraph().prepare(); } return m_deductionsModel; }
public void testInfGraph() { InfGraph ig = getInfGraph(); assertSame( ig.getPrefixMapping(), ig.getRawGraph().getPrefixMapping() ); } }
data.add(new Triple(C1, q, C3)); InfGraph infgraph = makeInfGraph(ruleList, data, new Node[]{p, q}); infgraph.setDerivationLogging(true); TestUtil.assertIteratorValues(this, infgraph.find(a, null, null), new Triple[] { new Triple(a, p, C3) }); Iterator<Derivation> derivs = infgraph.getDerivation(new Triple(a, p, C3)); StringWriter outString = new StringWriter(250); PrintWriter out = new PrintWriter(outString);
/** * Test the rebind operation. */ public void testRebind() { String rules = "[rule1: (?x p ?y) -> (?x q ?y)]"; Graph data = Factory.createGraphMem(); data.add(new Triple(n1, p, n2)); InfGraph infgraph = createInfGraph(rules, data); TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null), new Triple[] { new Triple(n1, p, n2), new Triple(n1, q, n2) }); Graph ndata = Factory.createGraphMem(); ndata.add(new Triple(n1, p, n3)); infgraph.rebind(ndata); TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null), new Triple[] { new Triple(n1, p, n3), new Triple(n1, q, n3) }); }
public void headAction(Node[] args, int length, RuleContext context) { InfGraph inf = context.getGraph(); Graph raw = inf.getRawGraph(); Graph deductions = inf.getDeductionsGraph(); for (int i = 0; i < length; i++) { Node clauseN = getArg(i, args, context);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, eq, n2), new Triple(n1, ge, n2), }); TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n3), new Triple[] { new Triple(n1, ne, n3), new Triple(n1, le, n3), }); TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n1), new Triple[] { new Triple(n3, ne, n1), TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, ne, n2), new Triple(n1, lt, n2), }); TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3), new Triple[] { new Triple(n2, ne, n3), TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, ne, n2),
infgraph.setDerivationLogging(true); infgraph.add(new Triple(n1, p, n3)); infgraph.add(new Triple(n1, q, n4)); infgraph.add(new Triple(n1, q, n3)); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Triple[] { new Triple(n1, p, n3), }); Iterator<Derivation> derivs = infgraph.getDerivation(new Triple(res, p, n3)); StringWriter outString = new StringWriter(250); PrintWriter out = new PrintWriter(outString);
graph.setDerivationLogging(recordDerivations); graph.rebind(data); return graph;
ExtendedIterator<Triple> results = infgraph.find(query); assertTrue(results.hasNext()); Triple result = results.next(); Rule rule = rules.get(rulenumber); List<Triple> matchList = Arrays.asList(matches); Iterator<Derivation> derivations = infgraph.getDerivation(result); assertTrue(derivations.hasNext()); RuleDerivation derivation = (RuleDerivation) derivations.next();
/** Run a single test */ public void run() { BasicForwardRuleReasoner reasoner = new BasicForwardRuleReasoner(ruleset); InfGraph result = reasoner.bind(Factory.createGraphMem()); System.out.println("Final graph state"); for (Iterator<Triple> i = result.find(null, null, null); i.hasNext(); ) { System.out.println(PrintUtil.print(i.next())); } }
/** * Test the consistency of the underlying data. This normally tests * the validity of the bound instance data against the bound * schema data. * @return a ValidityReport structure */ @Override public ValidityReport validate() { return getInfGraph().validate(); }
/** * Return the derivation of the given statement (which should be the result of * some previous list operation). * Not all reasoneers will support derivations. * @return an iterator over Derivation records or null if there is no derivation information * available for this triple. */ @Override public Iterator<Derivation> getDerivation(Statement statement) { return getInfGraph().getDerivation(statement.asTriple()); }
/** * The the "remove" builtin */ public void testRemoveBuiltin() { String rules = "[rule1: (?x p ?y), (?x q ?y) -> remove(0)]" + ""; InfGraph infgraph = createInfGraph(rules); infgraph.add(new Triple(n1, p, Util.makeIntNode(1))); infgraph.add(new Triple(n1, p, Util.makeIntNode(2))); infgraph.add(new Triple(n1, q, Util.makeIntNode(2))); TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null), new Triple[] { new Triple(n1, p, Util.makeIntNode(1)), new Triple(n1, q, Util.makeIntNode(2)) }); }
/** * Switch on/off drivation logging. If this is switched on then every time an inference * is a made that fact is recorded and the resulting record can be access through a later * getDerivation call. This may consume a lot of space! */ @Override public void setDerivationLogging(boolean logOn) { getInfGraph().setDerivationLogging(logOn); }
/** * Cause the inference model to reconsult the underlying data to take * into account changes. Normally changes are made through the InfModel's add and * remove calls are will be handled appropriately. However, in some cases changes * are made "behind the InfModels's back" and this forces a full reconsult of * the changed data. */ @Override public void rebind() { getInfGraph().rebind(); }
/** * Returns a derivations model. The rule reasoners typically create a * graph containing those triples added to the base graph due to rule firings. * In some applications it can useful to be able to access those deductions * directly, without seeing the raw data which triggered them. In particular, * this allows the forward rules to be used as if they were rewrite transformation * rules. * @return the deductions model, if relevant for this class of inference * engine or null if not. */ @Override public Model getDeductionsModel() { Graph deductionsGraph = getInfGraph().getDeductionsGraph(); if (deductionsGraph != null) { if (deductionsModel == null || (deductionsModel.getGraph() != deductionsGraph) ) { deductionsModel = new ModelCom(deductionsGraph); } } return deductionsModel; }
public void testInfGraph() { InfGraph ig = getInfGraph(); assertSame( ig.getPrefixMapping(), ig.getRawGraph().getPrefixMapping() ); } }
data.add(new Triple(C1, q, C3)); InfGraph infgraph = makeInfGraph(ruleList, data, new Node[]{p, q}); infgraph.setDerivationLogging(true); TestUtil.assertIteratorValues(this, infgraph.find(a, null, null), new Triple[] { new Triple(a, p, C3) }); Iterator<Derivation> derivs = infgraph.getDerivation(new Triple(a, p, C3)); StringWriter outString = new StringWriter(250); PrintWriter out = new PrintWriter(outString);