001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestRuleSetAssembler.java,v 1.7 2008/01/02 12:05:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.RuleSetAssembler;
013: import com.hp.hpl.jena.rdf.model.*;
014: import com.hp.hpl.jena.reasoner.rulesys.Rule;
015:
016: public class TestRuleSetAssembler extends AssemblerTestBase {
017: public TestRuleSetAssembler(String name) {
018: super (name);
019: }
020:
021: protected Class getAssemblerClass() {
022: return RuleSetAssembler.class;
023: }
024:
025: public void testRuleSetVocabulary() {
026: assertSubclassOf(JA.RuleSet, JA.HasRules);
027: assertDomain(JA.HasRules, JA.rule);
028: assertDomain(JA.HasRules, JA.rulesFrom);
029: assertDomain(JA.HasRules, JA.rules);
030: assertRange(JA.RuleSet, JA.rules);
031: }
032:
033: public void testRuleSetAssemblerType() {
034: testDemandsMinimalType(new RuleSetAssembler(), JA.RuleSet);
035: }
036:
037: public void testEmptyRuleSet() {
038: Assembler a = new RuleSetAssembler();
039: Resource root = resourceInModel("x rdf:type ja:RuleSet");
040: assertEquals(RuleSet.empty, a.open(root));
041: }
042:
043: public void testSingleRuleString() {
044: Assembler a = new RuleSetAssembler();
045: String ruleString = "[(?a P ?b) -> (?a Q ?b)]";
046: Resource root = resourceInModel("x rdf:type ja:RuleSet; x ja:rule '"
047: + ruleString.replaceAll(" ", "\\\\s") + "'");
048: RuleSet rules = (RuleSet) a.open(root);
049: Set expected = new HashSet(Rule.parseRules(ruleString));
050: assertEquals(expected, new HashSet(rules.getRules()));
051: }
052:
053: public void testMultipleRuleStrings() {
054: Assembler a = new RuleSetAssembler();
055: String ruleStringA = "[(?a P ?b) -> (?a Q ?b)]";
056: String ruleStringB = "[(?a R ?b) -> (?a S ?b)]";
057: Resource root = resourceInModel("x rdf:type ja:RuleSet"
058: + "; x ja:rule '"
059: + ruleStringA.replaceAll(" ", "\\\\s") + "'"
060: + "; x ja:rule '"
061: + ruleStringB.replaceAll(" ", "\\\\s") + "'");
062: RuleSet rules = (RuleSet) a.open(root);
063: Set expected = new HashSet(Rule.parseRules(ruleStringA));
064: expected.addAll(Rule.parseRules(ruleStringB));
065: assertEquals(expected, new HashSet(rules.getRules()));
066: }
067:
068: public void testRulesFrom() {
069: Assembler a = new RuleSetAssembler();
070: String rulesA = file("example.rules");
071: Resource root = resourceInModel("x rdf:type ja:RuleSet; x ja:rulesFrom "
072: + rulesA);
073: Set expected = new HashSet(Rule.rulesFromURL(rulesA));
074: RuleSet rules = (RuleSet) a.open(root);
075: assertEquals(expected, new HashSet(rules.getRules()));
076: }
077:
078: public void testSubRules() {
079: Assembler a = new RuleSetAssembler();
080: String ruleStringA = "[(?a P ?b) -> (?a Q ?b)]";
081: Resource root = resourceInModel("x rdf:type ja:RuleSet; x ja:rules y"
082: + "; y rdf:type ja:RuleSet; y ja:rule '"
083: + ruleStringA.replaceAll(" ", "\\\\s") + "'");
084: Set expected = new HashSet(Rule.parseRules(ruleStringA));
085: RuleSet rules = (RuleSet) a.open(root);
086: assertEquals(expected, new HashSet(rules.getRules()));
087: }
088:
089: public void testTrapsBadRulesObject() {
090: testTrapsBadRuleObject("ja:rules", "'y'");
091: testTrapsBadRuleObject("ja:rulesFrom", "17");
092: testTrapsBadRuleObject("ja:rule", "aResource");
093: testTrapsBadRuleObject("ja:rule", "17");
094: testTrapsBadRuleObject("ja:rule", "'something'xsd:else");
095: }
096:
097: private void testTrapsBadRuleObject(String property, String value) {
098: Assembler a = new RuleSetAssembler();
099: Resource root = resourceInModel("x rdf:type ja:RuleSet; x <property> <value>"
100: .replaceAll("<property>", property).replaceAll(
101: "<value>", value));
102: try {
103: a.open(root);
104: fail("should trap bad rules object " + value
105: + " for property " + property);
106: } catch (BadObjectException e) {
107: Model m = e.getRoot().getModel();
108: assertEquals(resource("x"), e.getRoot());
109: assertEquals(rdfNode(m, value), e.getObject());
110: }
111: }
112:
113: protected static String file(String name) {
114: return "file:testing/modelspecs/" + name;
115: }
116: }
117:
118: /*
119: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
120: * All rights reserved.
121: *
122: * Redistribution and use in source and binary forms, with or without
123: * modification, are permitted provided that the following conditions
124: * are met:
125: * 1. Redistributions of source code must retain the above copyright
126: * notice, this list of conditions and the following disclaimer.
127: * 2. Redistributions in binary form must reproduce the above copyright
128: * notice, this list of conditions and the following disclaimer in the
129: * documentation and/or other materials provided with the distribution.
130: * 3. The name of the author may not be used to endorse or promote products
131: * derived from this software without specific prior written permission.
132: *
133: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
134: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
135: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
136: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
137: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
138: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
139: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
140: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
141: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
142: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143: */
|