001: /*
002: * Copyright (C) 2007 Bastian Schenke (bastian.schenke(at)gmail.com) and
003: * <a href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package test.nz.org.take.r2ml.a;
020:
021: import java.io.StringReader;
022: import java.util.Iterator;
023:
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBElement;
026: import javax.xml.bind.Unmarshaller;
027:
028: import test.nz.org.take.r2ml.Log4jConfigurator;
029:
030: import de.tu_cottbus.r2ml.DerivationRule;
031: import de.tu_cottbus.r2ml.DerivationRuleSet;
032: import de.tu_cottbus.r2ml.GenericAtom;
033: import de.tu_cottbus.r2ml.RuleBase;
034: import nz.org.take.Fact;
035: import nz.org.take.KnowledgeBase;
036: import nz.org.take.KnowledgeElement;
037: import nz.org.take.TakeException;
038: import nz.org.take.r2ml.MappingContext;
039: import nz.org.take.r2ml.R2MLDriver;
040: import nz.org.take.r2ml.R2MLException;
041: import nz.org.take.r2ml.R2MLKnowledgeSource;
042: import nz.org.take.r2ml.XmlTypeHandler;
043: import nz.org.take.r2ml.reference.DefaultNameMapper;
044: import junit.framework.TestCase;
045:
046: public class R2MLDriverTest extends TestCase {
047:
048: R2MLDriver driver = null;
049:
050: public R2MLDriverTest() {
051: super ("R2MLDriverTest");
052: }
053:
054: @Override
055: protected void setUp() throws Exception {
056: super .setUp();
057: Log4jConfigurator.configure();
058: driver = R2MLDriver.get();
059: }
060:
061: @Override
062: protected void tearDown() throws Exception {
063: super .tearDown();
064: Log4jConfigurator.shutdown();
065: driver = null;
066: }
067:
068: public void test1() {
069: driver.logger.info("testInitialize01 started");
070: assertNotNull(driver.getHandlerByXmlType(RuleBase.class));
071: assertNotNull(driver
072: .getHandlerByXmlType(DerivationRuleSet.class));
073: assertNotNull(driver.getHandlerByXmlType(DerivationRule.class));
074: driver.logger.info("testInitialize01 finished");
075: }
076:
077: public void test2() {
078: driver.logger.info("testImportKB01 started");
079: String testXml = "<r2ml:RuleBase "
080: + " xmlns:xs='http://www.w3.org/2001/XMLSchema' "
081: + " xmlns:r2ml='http://www.rewerse.net/I1/2006/R2ML'>"
082: + " <r2ml:DerivationRuleSet r2ml:ruleSetID='DRS0047'> "
083: + " <r2ml:DerivationRule r2ml:ruleID='DR047a'>"
084: + " <r2ml:conditions>"
085: + " <r2ml:qf.Disjunction>"
086: + " <r2ml:GenericAtom r2ml:predicateID='pred1'>"
087: + " <r2ml:arguments>"
088: + " <r2ml:TypedLiteral r2ml:datatypeID='xs:string' r2ml:lexicalValue='A'/>"
089: + " </r2ml:arguments>"
090: + " </r2ml:GenericAtom>"
091: + " <r2ml:GenericAtom r2ml:predicateID='pred2'>"
092: + " <r2ml:arguments>"
093: + " <r2ml:TypedLiteral r2ml:datatypeID='xs:string' r2ml:lexicalValue='B'/>"
094: + " </r2ml:arguments>"
095: + " </r2ml:GenericAtom>"
096: + " </r2ml:qf.Disjunction>"
097: + " </r2ml:conditions>"
098: + " <r2ml:conclusion>"
099: + " <r2ml:GenericAtom r2ml:predicateID='pred3'>"
100: + " <r2ml:arguments>"
101: + " <r2ml:TypedLiteral r2ml:datatypeID='xs:string' r2ml:lexicalValue='C'/>"
102: + " </r2ml:arguments>" + " </r2ml:GenericAtom>"
103: + " </r2ml:conclusion>" + " </r2ml:DerivationRule>"
104: + " </r2ml:DerivationRuleSet>" + "</r2ml:RuleBase>";
105:
106: KnowledgeBase kb = null;
107: try {
108: StringReader input = new StringReader(testXml);
109: R2MLKnowledgeSource kSrc = new R2MLKnowledgeSource(input);
110: kb = kSrc.getKnowledgeBase();
111: } catch (R2MLException r2mle) {
112: fail("Internal Error: " + r2mle.toString());
113: } catch (TakeException e) {
114: fail("Internal Error: " + e.toString());
115: }
116: assertNotNull("No Knowledgebase returned!", kb);
117: assertEquals("Wrong number of rules:", 2, kb.getElements()
118: .size());
119: driver.logger.info("testImportKB01 finished");
120: }
121:
122: public void test3() {
123: driver.logger.info("testGenericAtomHandler started");
124: String testXml = "<r2ml:GenericAtom "
125: + " r2ml:predicateID='fatherOf' "
126: + " xmlns:xs='http://www.w3.org/2001/XMLSchema' "
127: + " xmlns:r2ml='http://www.rewerse.net/I1/2006/R2ML'>"
128: + " <r2ml:arguments> " + " <r2ml:TypedLiteral "
129: + " r2ml:datatypeID='xs:string' "
130: + " r2ml:lexicalValue='Max' /> "
131: + " <r2ml:PlainLiteral "
132: + " r2ml:languageTag='de-de' "
133: + " r2ml:lexicalValue='Jens' /> "
134: + " </r2ml:arguments>" + "</r2ml:GenericAtom>";
135:
136: try {
137: JAXBContext jc = JAXBContext
138: .newInstance("de.tu_cottbus.r2ml:de.tu_cottbus.r2ml.r2mlv:de.tu_cottbus.dc");//
139: Unmarshaller um = jc.createUnmarshaller();
140: GenericAtom genAtom = (GenericAtom) ((JAXBElement) um
141: .unmarshal(new StringReader(testXml))).getValue();
142: XmlTypeHandler handler = driver.getHandlerByXmlType(genAtom
143: .getClass());
144: Fact fact = (Fact) handler.importObject(genAtom);
145: assertTrue("Handler call recursion not properly resolved.",
146: MappingContext.get().isClean());
147: assertEquals("Wrong Predicatename", genAtom
148: .getPredicateID().toString(), fact.getPredicate()
149: .getName());
150: assertEquals("Number of arguments not correct", genAtom
151: .getArguments().getTerm().size(), fact
152: .getPredicate().getSlotTypes().length);
153: } catch (Exception e) {
154: fail("Exception occured" + e.toString());
155: }
156:
157: driver.logger.info("testGenericAtomHandler finished");
158:
159: }
160:
161: }
|