From: Lars H. <lh...@us...> - 2005-07-08 16:03:32
|
Update of /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30131/src/org/tmapiutils/query/tolog/test/backends Modified Files: BasicTests.java QueryTestBase.java Log Message: Tabs -> Whitespaces Index: QueryTestBase.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends/QueryTestBase.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** QueryTestBase.java 8 Mar 2005 12:33:59 -0000 1.3 --- QueryTestBase.java 8 Jul 2005 16:03:21 -0000 1.4 *************** *** 1,173 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! 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.tmapi.core.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 Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! 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).getObjectId()); ! } 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.getObjectId().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; ! } ! } ! --- 1,173 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! 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.tmapi.core.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 Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! 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).getObjectId()); ! } 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.getObjectId().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; ! } ! } ! Index: BasicTests.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/test/backends/BasicTests.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** BasicTests.java 8 Mar 2005 12:33:59 -0000 1.2 --- BasicTests.java 8 Jul 2005 16:03:21 -0000 1.3 *************** *** 1,265 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! 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 Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! 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"} ! }); ! } ! } ! --- 1,265 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! 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 Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! 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"} ! }); ! } ! } ! |