|
From: Lars H. <lh...@us...> - 2005-03-06 17:34:49
|
Update of /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27141/src/org/tmapiutils/query/tolog/test/backends Added Files: BasicTests.java QueryTestBase.java Log Message: Initial tolog import --- NEW FILE: QueryTestBase.java --- /* Copyright (c) 2000-2004 The TM4J Project. All rights reserved. The license for this source code can be found in the file LICENSE.TXT with this distribution or at http://tm4j.org/LICENSE.txt */ /* *$Header: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends/QueryTestBase.java,v 1.1 2005/03/06 17:34:39 lheuer Exp $ */ package org.tmapiutils.query.tolog.test.backends; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.tm4j.net.Locator; import org.tm4j.test.ConfigurableBackendTest; import org.tmapiutils.query.tolog.TologResultsSet; import org.tmapiutils.query.tolog.QueryEvaluator; import org.tmapiutils.query.tolog.QueryEvaluatorFactory; import org.tmapi.core.Topic; import org.tmapi.core.TopicMap; import org.tmapi.core.TopicMapObject; /** * @author Kal * * Describe QueryTestBase here. */ public class QueryTestBase extends ConfigurableBackendTest { private HashMap m_maps; public QueryTestBase(String name) { super(name); m_maps = new HashMap(); } public TopicMap getTopicMap(String tmKey) throws Exception { TopicMap ret = (TopicMap) m_maps.get(tmKey); if (ret == null) { ret = super.getTopicMap(tmKey); m_maps.put(tmKey, ret); } return ret; } protected void doQueryWithModule(TopicMap tm, File moduleFile, String modulePrefix, String queryString, String[][] expectedResults) throws Exception { QueryEvaluator qe = QueryEvaluatorFactory.newQueryEvaluator(tm); qe.addRulesModule(new FileInputStream(moduleFile), modulePrefix); TologResultsSet rs = qe.execute(queryString); validateResultsSet(rs, expectedResults); } protected void doQuery(TopicMap tm, String queryString, String[][] expectedResults) throws Exception { QueryEvaluator qe = QueryEvaluatorFactory.newQueryEvaluator(tm); TologResultsSet rs = qe.execute(queryString); validateResultsSet(rs, expectedResults); } protected void doQuery(TopicMap tm, String [] rules, String queryString, String [][] expectedResults) throws Exception { QueryEvaluator qe = QueryEvaluatorFactory.newQueryEvaluator(tm); for (int i = 0; i < rules.length; i++) { qe.addRule(rules[i]); } TologResultsSet rs = qe.execute(queryString); validateResultsSet(rs, expectedResults); } protected void validateResultsSet(TologResultsSet rs, String[][] expectedResults) throws Exception { assertEquals("Results set size is wrong.", expectedResults.length, rs.getNumRows()); for (int i = 0; i < rs.getNumRows(); i++) { if (!validateRow(rs.getRow(i), expectedResults)) { fail("No match found for row " + i + " in results set. Got: " + dumpRow(rs.getRow(i))); } } } public String dumpResultsSet(TologResultsSet rs) { StringBuffer ret = new StringBuffer(); for (int i =0; i < rs.getNumRows(); i++) { ret.append(dumpRow(rs.getRow(i))); ret.append("\n"); } return ret.toString(); } /** * @param list * @return */ private String dumpRow(List row) { StringBuffer ret = new StringBuffer(); Iterator it = row.iterator(); while (it.hasNext()) { Object o = (Object)it.next(); if (o instanceof Topic) { ret.append(((Topic)o).getID()); } else { ret.append(o.getClass().getName() + ": " + o.toString()); } if (it.hasNext()) ret.append(", "); } return ret.toString(); } protected boolean validateRow(List row, String[][]expected) throws Exception { for (int i = 0; i < expected.length; i++) { if (validateRow(row, expected[i])) { return true; } } return false; } protected boolean validateRow(List row, String [] expected) throws Exception { if (row.size() != expected.length) return false; for (int i = 0; i < row.size(); i++) { Object o = row.get(i); String exp = expected[i]; boolean v = false; if (o instanceof TopicMapObject) { v = validate((TopicMapObject)o, exp); } else if (o instanceof Locator) { v = validate((Locator)o, exp); } else if (o instanceof String) { v = o.equals(exp); } if (!v) return false; } return true; } /** * @param locator * @param expected * @return */ private boolean validate(Locator locator, String expected) { return locator.getReference().equals(expected); } /** * @param topic * @param expected * @return */ private boolean validate(TopicMapObject tmo, String expected) throws Exception { if (tmo.getID().equals(expected)) { return true; } else { Locator loc = tmo.getTopicMap().getBaseLocator().resolveRelative("#" + expected); Iterator it = tmo.getSourceLocators().iterator(); while (it.hasNext()) { if (((Locator)it.next()).getReference().equals(expected)) return true; } } return false; } } /* * $Log: QueryTestBase.java,v $ * Revision 1.1 2005/03/06 17:34:39 lheuer * Initial tolog import * * Revision 1.8 2004/07/21 08:45:35 kal_ahmed * Fixed bugs in reifies predicate. * * Revision 1.7 2004/07/19 14:52:41 kal_ahmed * More fixes to processing of recursive rules. ClauseList now aborts when the input parameter list becomes empty. * * Revision 1.6 2004/07/16 13:51:52 kal_ahmed * Fixes for tolog engine handling of rules and rule-recursion. * * Revision 1.5 2004/06/21 18:42:28 kal_ahmed * Improved exception propagation. * * Revision 1.4 2004/06/20 14:12:31 kal_ahmed * Copied TologRsultsSet interface and TologFragmentBuilder implementation from org.tm4j.tolog to this package. * * Revision 1.3 2004/06/15 20:00:35 kal_ahmed * Added a few more tests over the block-world topic map. * * Revision 1.2 2004/06/15 19:44:48 kal_ahmed * Resynced expected results with actual topic map. * */ --- NEW FILE: BasicTests.java --- /* Copyright (c) 2000-2004 The TM4J Project. All rights reserved. The license for this source code can be found in the file LICENSE.TXT with this distribution or at http://tm4j.org/LICENSE.txt */ /* *$Header: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends/BasicTests.java,v 1.1 2005/03/06 17:34:39 lheuer Exp $ */ package org.tmapiutils.query.tolog.test.backends; import org.tmapiutils.query.tolog.QueryEvaluator; import org.tmapiutils.query.tolog.QueryEvaluatorFactory; import org.tmapiutils.query.tolog.TologResultsSet; import org.tmapi.core.TopicMap; /** * @author Kal * * Describe BasicTests here. */ public class BasicTests extends QueryTestBase { /** * @param name */ public BasicTests(String name) { super(name); } public void testOneDynamicAssociation() throws Exception { TopicMap tm = getTopicMap("tolog.blocks"); assertNotNull(tm); doQuery(tm, "select $A from has-shape($A:block, cylinder:shape)?", new String [][] { new String[] { "blueblock" } }); } public void testOpenDynamicAssociation() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $A, $B from restson($A:upper, $B:lower) ?", new String [][] { new String [] { "redblock", "blueblock" }, new String [] { "redblock", "greenblock"}, new String [] { "yellowblock", "redblock"}}); } public void testTwoDynamicAssociationsAND() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $A from restson($A:upper, $B:lower), has-shape($B:block, cylinder:shape) ?", new String [][] { new String[] { "redblock" } }); } public void testTwoDynamicAssociationsOR() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $A from { restson($A:upper, redblock:lower) | has-shape($A:block, cylinder:shape) }?", new String [][] { new String [] { "yellowblock" }, new String [] { "blueblock" } }); } public void testInstanceOf() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $A from instance-of($A, block) ?", new String [][] { new String [] {"blueblock"}, new String [] {"greenblock"}, new String [] {"redblock"}, new String [] {"yellowblock"}}); doQuery( getTopicMap("tolog.blocks"), "select $A from instance-of($A, block), has-shape($A:block, cylinder:shape) ?", new String [][] { new String [] {"blueblock"}}); } public void testNamesToValues() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $BLOCK, $NAMEVAL from instance-of($BLOCK, block), topic-name($BLOCK, $NAME), value($NAME, $NAMEVAL) ?", new String [][] { new String [] {"blueblock", "Blue Block"}, new String [] {"greenblock", "Green Block"}, new String [] {"redblock", "Red Block"}, new String [] {"yellowblock", "Yellow Block"}}); } public void testNamesAndOcurrencesToValues() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $BLOCK, $NAMEVAL, $WEIGHTVAL from " + "instance-of($BLOCK, block), topic-name($BLOCK, $NAME), occurrence($BLOCK, $OCC)," + "type($OCC, weight-g), " + "value($NAME, $NAMEVAL), value($OCC, $WEIGHTVAL) ?", new String [][] { new String [] {"blueblock", "Blue Block", "20"}, new String [] {"greenblock", "Green Block", "5"}, new String [] {"redblock", "Red Block", "10"}, new String [] {"yellowblock", "Yellow Block", "15"}}); } public void testDynamicAssociationWithVariableType() throws Exception { doQuery( getTopicMap("tolog.blocks"), "select $ASSOC, $OTHER from $ASSOC(redblock, $OTHER), $OTHER /= redblock ?", new String[][] { new String[] {"restson", "blueblock"}, new String[] {"restson", "greenblock"}, new String[] {"has-shape", "rectangle"}, new String[] {"restson", "yellowblock"} }); } private static final String [] auto_rules = new String [] { "matches-facet($FACET, $FV, $INST) :- " + " { classified-as($INST : instance, $FV:classification)" + " |" + " facet-has-hierarchy-type($FACET : facet, $ASSOC:facet-hierarchy-type)," + " direct-instance-of($SUPER, superordinate-role)," + " direct-instance-of($SUB, subordinate-role)," + " $ASSOC($FV:$SUPER, $X:$SUB)," + " matches-facet($FACET, $X, $INST)" + "}.", "subfacet-of($SUBFV, $SUPERFV, $FACET) :- " + " facet-has-hierarchy-type($FACET : facet, $ASSOC : facet-hierarchy-type)," + " direct-instance-of($SUPER, superordinate-role)," + " direct-instance-of($SUB, subordinate-role)," + " { $ASSOC($SUBFV : $SUB, $SUPERFV : $SUPER) " + " |" + " $ASSOC($SUBFV : $SUB, $X : $SUPER)," + " subfacet-of($X, $SUPERFV, $FACET)" + " }.", "direct-subfacet-of($SUBFV, $SUPERFV, $FACET) :- " + " facet-has-hierarchy-type($FACET:facet, $ASSOC:facet-hierarchy-type)," + " direct-instance-of($SUPER, superordinate-role)," + " direct-instance-of($SUB, subordinate-role)," + " $ASSOC($SUBFV:$SUB, $SUPERFV:$SUPER).", }; public void testRecursiveRules() throws Exception { doQuery( getTopicMap("tolog.facets"), auto_rules, "select $AUTO from matches-facet(body-type, hatchback, $AUTO) ?", new String[][] { new String [] {"clio1_2"}, new String [] {"astra1_2"}, new String [] {"peugot206"}, new String [] {"peugot307"}, }); } public void testRecursiveRulesList() throws Exception { doQuery( getTopicMap("tolog.facets"), auto_rules, "select $INST from matches-facet(body-type,hatchback,$INST), matches-facet(engine-type,petrol,$INST) ?", new String[][] { new String [] {"clio1_2"}, new String [] {"astra1_2"}, new String [] {"peugot206"}, }); } public void testRecursiveRulesList2() throws Exception { TopicMap tm = getTopicMap("tolog.facets"); QueryEvaluator qe = QueryEvaluatorFactory.newQueryEvaluator(tm); for (int i = 0; i < auto_rules.length; i++) { qe.addRule(auto_rules[i]); } TologResultsSet rs = null; rs = qe.execute("select $SUB from direct-subfacet-of($SUB, all-body-types, body-type) ?"); assertEquals(5, rs.getNumRows()); rs = qe.execute("select $S from subfacet-of($S, all-body-types, body-type) ?"); assertEquals(8, rs.getNumRows()); rs = qe.execute("select $AUTO from matches-facet(body-type, all-body-types, $AUTO) ?"); assertEquals(7, rs.getNumRows()); rs = qe.execute("select $AUTO from matches-facet(engine-type, all-engine-types, $AUTO) ?"); assertEquals(7, rs.getNumRows()); rs = qe.execute("select $AUTO from matches-facet(engine-size, all-engine-sizes, $AUTO) ?"); assertEquals(7, rs.getNumRows()); rs = qe.execute("select $AUTO from matches-facet(body-type, all-body-types, $AUTO), matches-facet(engine-type, all-engine-types, $AUTO), matches-facet(engine-size, all-engine-sizes, $AUTO) ?"); assertEquals(7, rs.getNumRows()); this.validateResultsSet(rs, new String[][] { new String [] {"clio1_2"}, new String [] {"astra1_2"}, new String [] {"peugot206"}, new String [] {"peugot307"}, new String [] {"almera1_8"}, new String [] {"zafira2_0"}, new String [] {"frontera2_2"}}); } public void testRuleWithNoMatch() throws Exception { TopicMap tm = getTopicMap("tolog.facets"); QueryEvaluator qe = QueryEvaluatorFactory.newQueryEvaluator(tm); qe.addRule( "direct-subfacet-of($SUBFV, $SUPERFV, $FACET) :- " + "facet-has-hierarchy-type($FACET:facet, $ASSOC:facet-hierarchy-type)," + "$ASSOC($SUBFV:$SUB, $SUPERFV:$SUPER)," + "direct-instance-of($SUPER, superordinate-role)," + "direct-instance-of($SUB, subordinate-role)."); qe.addRule( "subfacet-of($SUBFV, $SUPERFV, $FACET) :- " + "{ direct-subfacet-of($SUBFV, $X, $FACET), subfacet-of($X, $SUPERFV, $FACET) " + " | direct-subfacet-of($SUBFV, $SUPERFV, $FACET) }."); qe.addRule( "matches-facet($FACET, $FV, $INST) :- " + "{ subfacet-of($C,$FV,$FACET), classified-as($INST:instance, $C:classification)" + " | classified-as($INST:instance, $FV:classification) }."); TologResultsSet rs = null; rs = qe.execute("select $CHILD from direct-subfacet-of($CHILD, @petrol, @engine-type) ?"); dumpResultsSet(rs); assertEquals(0, rs.getNumRows()); } public void testReifiesPredicate() throws Exception { TopicMap tm = getTopicMap("tolog.reification"); /* doQuery( tm, "select $REIFIER from reifies($REIFIER, a1) ?", new String[][] { new String [] {"re-a1"}, }); */ doQuery( tm, "select $REIFIED from reifies(re-a1, $REIFIED) ?", new String[][] { new String [] {"a1"}, }); doQuery( tm, "select $REIFIER, $REIFIED from reifies($REIFIER, $REIFIED) ?", new String [][] { new String [] {"re-a1", "a1"} }); } } /* * $Log: BasicTests.java,v $ * Revision 1.1 2005/03/06 17:34:39 lheuer * Initial tolog import * * Revision 1.8 2004/07/21 08:45:35 kal_ahmed * Fixed bugs in reifies predicate. * * Revision 1.7 2004/07/19 16:02:43 kal_ahmed * Fixed tests to properly initialize input variable set. * * Revision 1.6 2004/07/19 14:52:40 kal_ahmed * More fixes to processing of recursive rules. ClauseList now aborts when the input parameter list becomes empty. * * Revision 1.5 2004/07/16 13:51:37 kal_ahmed * Fixes for tolog engine handling of rules and rule-recursion. * * Revision 1.4 2004/07/14 21:42:25 kal_ahmed * Parser now supports a variable for the association type in a dynamic association predicate. * * Revision 1.3 2004/06/15 20:00:35 kal_ahmed * Added a few more tests over the block-world topic map. * */ |