📄 testbasics.java
字号:
}
/**
* Test axiom handling machinery
*/
public void testAxiomHandling() {
String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]" +
"[testRule2: (n1 q ?a) -> (n2, q, ?a)]" +
"[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]" +
"[axiom1: -> (n1 p n3)]";
List ruleList = Rule.parseRules(rules);
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem());
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
new Triple[] {
new Triple(n1, p, n3),
new Triple(n2, 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),
new Triple(n2, p, n3),
new Triple(n1, q, n4),
new Triple(n2, q, n4),
new Triple(n1, q, n3),
new Triple(n2, q, n3),
new Triple(res, p, n3)
});
}
/**
* Test schema partial binding machinery
*/
public void testSchemaBinding() {
String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]" +
"[testRule2: (n1 q ?a) -> (n2, q, ?a)]" +
"[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]";
List ruleList = Rule.parseRules(rules);
Graph schema = Factory.createGraphMem();
schema.add(new Triple(n1, p, n3));
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, q, n4));
data.add(new Triple(n1, q, n3));
Reasoner reasoner = new BasicForwardRuleReasoner(ruleList);
Reasoner boundReasoner = reasoner.bindSchema(schema);
InfGraph infgraph = boundReasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
new Triple[] {
new Triple(n1, p, n3),
new Triple(n2, p, n3),
new Triple(n1, q, n4),
new Triple(n2, q, n4),
new Triple(n1, q, n3),
new Triple(n2, q, n3),
new Triple(res, p, n3)
});
}
/**
* Test functor handling
*/
public void testEmbeddedFunctors() {
String rules = "(?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C rb:restriction all(?P, ?D))." +
"(?C rb:restriction all(eg:p, eg:D)) -> (?C rb:restriction 'allOK')." +
"[ -> (eg:foo eg:prop functor(eg:bar, 1)) ]" +
"[ (?x eg:prop functor(eg:bar, ?v)) -> (?x eg:propbar ?v) ]" +
"[ (?x eg:prop functor(?v, ?*)) -> (?x eg:propfunc ?v) ]" +
"";
List ruleList = Rule.parseRules(rules);
Model data = ModelFactory.createDefaultModel();
Resource R1 = data.createResource(PrintUtil.egNS + "R1");
Resource D = data.createResource(PrintUtil.egNS + "D");
Property p = data.createProperty(PrintUtil.egNS, "p");
Property prop = data.createProperty(PrintUtil.egNS, "prop");
Property propbar = data.createProperty(PrintUtil.egNS, "propbar");
Property propfunc = data.createProperty(PrintUtil.egNS, "propfunc");
Property rbr = data.createProperty(ReasonerVocabulary.RBNamespace, "restriction");
R1.addProperty(RDF.type, OWL.Restriction)
.addProperty(OWL.onProperty, p)
.addProperty(OWL.allValuesFrom, D);
Reasoner reasoner = new BasicForwardRuleReasoner(ruleList);
InfGraph infgraph = reasoner.bind(data.getGraph());
Model infModel = ModelFactory.createModelForGraph(infgraph);
Resource foo = infModel.createResource(PrintUtil.egNS + "foo");
Resource bar = infModel.createResource(PrintUtil.egNS + "bar");
RDFNode flit = infModel.getResource(R1.getURI()).getRequiredProperty(rbr).getObject();
assertNotNull(flit);
assertEquals(flit.toString(), "allOK");
// assertTrue(flit instanceof Literal);
// Functor func = (Functor)((Literal)flit).getValue();
// assertEquals("all", func.getName());
// assertEquals(p.getNode(), func.getArgs()[0]);
// assertEquals(D.getNode(), func.getArgs()[1]);
Literal one = (Literal)foo.getRequiredProperty(propbar).getObject();
assertEquals(new Integer(1), one.getValue());
}
/**
* The the minimal machinery for supporting builtins
*/
public void testBuiltins() {
String rules = //"[testRule1: (n1 ?p ?a) -> print('rule1test', ?p, ?a)]" +
"[r1: (n1 p ?x), addOne(?x, ?y) -> (n1 q ?y)]" +
"[r2: (n1 p ?x), lessThan(?x, 3) -> (n2 q ?x)]" +
"[axiom1: -> (n1 p 1)]" +
"[axiom2: -> (n1 p 4)]" +
"";
List ruleList = Rule.parseRules(rules);
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem());
TestUtil.assertIteratorValues(this, infgraph.find(n1, q, null),
new Triple[] {
new Triple(n1, q, Util.makeIntNode(2)),
new Triple(n1, q, Util.makeIntNode(5))
});
TestUtil.assertIteratorValues(this, infgraph.find(n2, q, null),
new Triple[] {
new Triple(n2, q, Util.makeIntNode(1))
});
}
/**
* The the "remove" builtin
*/
public void testRemoveBuiltin() {
String rules =
"[rule1: (?x p ?y), (?x q ?y) -> remove(0)]" +
"";
List ruleList = Rule.parseRules(rules);
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem());
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))
});
}
/**
* The the "drop" builtin
*/
public void testDropBuiltin() {
String rules =
"[rule1: (?x p ?y) -> drop(0)]" +
"";
List ruleList = Rule.parseRules(rules);
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem());
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, q, Util.makeIntNode(2))
});
}
/**
* Test the rebind operation.
*/
public void testRebind() {
String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
List ruleList = Rule.parseRules(rules);
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, n2));
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(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)
});
}
/**
* Test size bug, used to blow up if size was called before any queries.
*/
public void testSize() {
String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
List ruleList = Rule.parseRules(rules);
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, n2));
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(data);
assertEquals(infgraph.size(), 2);
}
/**
* Check validity report implementation, there had been a stupid bug here.
*/
public void testValidityReport() {
StandardValidityReport report = new StandardValidityReport();
report.add(false, "dummy", "dummy1");
report.add(false, "dummy", "dummy3");
assertTrue(report.isValid());
report.add(true, "dummy", "dummy2");
assertTrue( ! report.isValid());
report = new StandardValidityReport();
report.add(false, "dummy", "dummy1");
report.add(true, "dummy", "dummy2");
report.add(false, "dummy", "dummy3");
assertTrue( ! report.isValid());
report = new StandardValidityReport();
report.add(new ValidityReport.Report(false, "dummy", "dummy1"));
report.add(new ValidityReport.Report(true, "dummy", "dummy2"));
report.add(new ValidityReport.Report(false, "dummy", "dummy3"));
assertTrue( ! report.isValid());
}
/**
* Test the list conversion utility that is used in some of the builtins.
*/
public void testConvertList() {
Graph data = Factory.createGraphMem();
Node first = RDF.Nodes.first;
Node rest = RDF.Nodes.rest;
Node nil = RDF.Nodes.nil;
data.add(new Triple(n1, first, p));
data.add(new Triple(n1, rest, n2));
data.add(new Triple(n2, first, q));
data.add(new Triple(n2, rest, nil));
data.add(new Triple(n3, first, p));
data.add(new Triple(n3, rest, n4));
data.add(new Triple(n4, rest, n5));
data.add(new Triple(n5, first, q));
data.add(new Triple(n5, rest, nil));
String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
List ruleList = Rule.parseRules(rules);
InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(data);
RuleContext context = new BFRuleContext( (ForwardRuleInfGraphI) infgraph);
List result = Util.convertList(n1, context);
assertEquals(result.size(), 2);
assertEquals(result.get(0), p);
assertEquals(result.get(1), q);
List result2 = Util.convertList(n3, context);
assertEquals(result2.size(), 1);
assertEquals(result2.get(0), p);
}
}
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -