001: /*
002: (c) Copyright 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: TestGenericRuleReasonerConfig.java,v 1.5 2008/01/23 15:49:10 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.reasoner.rulesys.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.test.AssemblerTestBase;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.reasoner.rulesys.*;
014: import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
015:
016: /**
017: Your eyes will bleed with the number of backslashes required in the substitute
018: strings.
019:
020: @author kers
021: */
022: public class TestGenericRuleReasonerConfig extends AssemblerTestBase {
023: public TestGenericRuleReasonerConfig(String name) {
024: super (name);
025: }
026:
027: protected Model setRequiredPrefixes(Model x) {
028: x.setNsPrefix("jr", ReasonerVocabulary.JenaReasonerNS);
029: return super .setRequiredPrefixes(x);
030: }
031:
032: public void testLoadsSingleRuleSetViaURL() {
033: testLoadsSingleRuleViaURL("jms");
034: testLoadsSingleRuleViaURL("jr");
035: }
036:
037: private void testLoadsSingleRuleViaURL(String ns) {
038: String where = "file:testing/modelspecs/example.rules";
039: Resource r = resourceInModel("x <ns>:ruleSetURL <where>"
040: .replaceAll("<ns>", ns).replaceAll("<where>", where));
041: List rules = Rule.rulesFromURL(where);
042: GenericRuleReasoner grr = new GenericRuleReasoner(null, r);
043: assertEquals(rules, grr.getRules());
044: }
045:
046: public void testLoadsSingleRuleFromString() {
047: testLoadsSingleRuleFromString("jms");
048: testLoadsSingleRuleFromString("jr");
049: }
050:
051: private void testLoadsSingleRuleFromString(String ns) {
052: String rule = "[R: (?x rdf:type eg:Thing) -> (?x eg:thing true)]";
053: List rules = Rule.parseRules(rule);
054: Resource r = resourceInModel("x <ns>:hasRule '<it>'"
055: .replaceAll("<ns>", ns).replaceAll("<it>",
056: rule.replaceAll(" ", "\\\\\\\\s")));
057: GenericRuleReasoner grr = new GenericRuleReasoner(null, r);
058: assertEquals(rules, grr.getRules());
059: }
060:
061: public void testLoadsSingleRuleViaRuleSetStringString() {
062: testLoadsRulesViaRuleSetStrings("jms");
063: testLoadsRulesViaRuleSetStrings("jr");
064: }
065:
066: private void testLoadsRulesViaRuleSetStrings(String ns) {
067: String ruleA = "[R: (?x rdf:type eg:Thing) -> (?x eg:thing true)]";
068: String ruleB = "[S: (?x rdf:type eg:Thung) -> (?x eg:thing false)]";
069: Set rules = rulesFromTwoStrings(ruleA, ruleB);
070: String modelString = "x <ns>:ruleSet _x; _x <ns>:hasRule '<A>'; _x <ns>:hasRule '<B>'"
071: .replaceAll("<ns>", ns).replaceAll("<A>",
072: ruleA.replaceAll(" ", "\\\\\\\\s")).replaceAll(
073: "<B>", ruleB.replaceAll(" ", "\\\\\\\\s"));
074: Resource r = resourceInModel(modelString);
075: GenericRuleReasoner grr = new GenericRuleReasoner(null, r);
076: assertEquals(rules, new HashSet(grr.getRules()));
077: }
078:
079: public void testLoadsMultipleRuleSetsViaRuleSetNode() {
080: testLoadsMultipleRuleSetsViaRuleSetNode("jms");
081: testLoadsMultipleRuleSetsViaRuleSetNode("jr");
082: }
083:
084: private void testLoadsMultipleRuleSetsViaRuleSetNode(String ns) {
085: String whereA = "file:testing/modelspecs/example.rules";
086: String whereB = "file:testing/modelspecs/extra.rules";
087: Resource r = resourceInModel("x <ns>:ruleSet _a; _a <ns>:ruleSetURL <whereA>; _a <ns>:ruleSetURL <whereB>"
088: .replaceAll("<ns>", ns).replaceAll("<whereA>", whereA)
089: .replaceAll("<whereB>", whereB));
090: GenericRuleReasoner grr = new GenericRuleReasoner(null, r);
091: assertEquals(rulesFromTwoPlaces(whereA, whereB), new HashSet(
092: grr.getRules()));
093: }
094:
095: private Set rulesFromTwoStrings(String ruleA, String ruleB) {
096: Set rules = new HashSet(Rule.parseRules(ruleA));
097: rules.addAll(Rule.parseRules(ruleB));
098: return rules;
099: }
100:
101: private Set rulesFromTwoPlaces(String whereA, String whereB) {
102: Set rules = new HashSet();
103: rules.addAll(Rule.rulesFromURL(whereA));
104: rules.addAll(Rule.rulesFromURL(whereB));
105: return rules;
106: }
107: }
|