From: <jbo...@li...> - 2006-01-23 01:06:14
|
Author: mic...@jb... Date: 2006-01-22 20:06:07 -0500 (Sun, 22 Jan 2006) New Revision: 2172 Modified: trunk/labs/jbossrules/drools-natural-dsl/src/test/java/org/drools/natural/template/TemplateTest.java Log: crazy experiment that actually worked. Modified: trunk/labs/jbossrules/drools-natural-dsl/src/test/java/org/drools/natural/template/TemplateTest.java =================================================================== --- trunk/labs/jbossrules/drools-natural-dsl/src/test/java/org/drools/natural/template/TemplateTest.java 2006-01-22 13:00:46 UTC (rev 2171) +++ trunk/labs/jbossrules/drools-natural-dsl/src/test/java/org/drools/natural/template/TemplateTest.java 2006-01-23 01:06:07 UTC (rev 2172) @@ -18,52 +18,44 @@ /** * Lets try it all together. - * Print out the target string. - * Print out substitution string. */ public void testBuildStrings() { - Chunk chunk1 = new Chunk("baby on board"); - Chunk chunk2 = new Chunk("{0}"); - Chunk chunk3 = new Chunk("and"); - Chunk chunk4 = new Chunk("{1}"); - Chunk chunk5 = new Chunk("burt ward"); - chunk1.next = chunk2; - chunk2.next = chunk3; - chunk3.next = chunk4; - chunk4.next = chunk5; + Context ctx = new Context(); - String nl = "yeah this is an expression baby on board exp1 and exp2 burt ward end."; - chunk1.process(nl); + //chunks represent a lexed grammar "left hand side" + ctx.addChunk("baby on board") + .addChunk("{0}") + .addChunk("and") + .addChunk("{1}") + .addChunk("burt ward"); - HashMap map = new HashMap(); - chunk1.buildValueMap(map); + //and this is the right hand side grammar mapping (no lexing required, simple hole filling !). + String grammar_r = "something({0}, {1})"; - StringBuffer buffer = new StringBuffer(); - chunk1.buildSubtitutionKey(buffer); - assertEquals("baby on board exp1 and exp2 burt ward", buffer.toString()); + //and this is the full expression + String nl = "yeah this is an expression baby on board exp1 and exp2 burt ward end."; - String grammar_r = "something({0}, {1})"; + //match the pattern, put the values in the map + HashMap map = new HashMap(); + ctx.processNL(nl, map); - String target = buildTarget( map, - grammar_r ); + //now get the chunk of nl that will be replaced with the target later. + String subKey = ctx.getSubstitutionKey(); + assertEquals("baby on board exp1 and exp2 burt ward", subKey); + + String target = ctx.populateTargetString( map, grammar_r ); + assertEquals("something(exp1, exp2)", target); + + String result = ctx.replaceNlWithTarget(nl, subKey, target); - String result = StringUtils.replace(nl, buffer.toString(), target); assertEquals("yeah this is an expression something(exp1, exp2) end.", result); - - + } - private String buildTarget(HashMap map, - String grammar_r) { - for ( Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { - String key = (String) iter.next(); - grammar_r = StringUtils.replace(grammar_r, key, (String) map.get(key)); - } - return grammar_r; - } + public void testBasicExpression() { @@ -165,6 +157,69 @@ static class Context { + Chunk start; + + /** + * Ad a chunk from the dictionary expression. + * A chunk is a piece of nl, or a hole. + * nl & holes should not be mixed. + */ + Context addChunk(String chunkText) { + Chunk chunk = new Chunk(chunkText); + if (start == null) { + start = chunk; + } else { + start.addToEnd(chunk); + } + return this; + } + + /** + * This will parse the input nl expression, and build a map of values for the "holes" + * in the grammar expression. + * It does this by getting the Chunks of the grammar to parse themselves. + */ + void processNL(String nl, Map map) { + start.process(nl); + start.buildValueMap(map); + } + + /** + * This builds a fragment of the nl expression which can be used + * to swap out a piece of the original with the target expression. + * + * The target expression is the "right hand side" of the grammar map. + */ + String getSubstitutionKey() { + StringBuffer buffer = new StringBuffer(); + start.buildSubtitutionKey(buffer); + return buffer.toString(); + } + + /** + * This will build the target string that you can use to substitute the original with. + * @param map The map of values to hole keys. + * @param grammar_r The grammar item which will have the values plugged into the "holes". + * @return The final expression ready for substitution. + */ + String populateTargetString(HashMap map, + String grammar_r) { + for ( Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { + String key = (String) iter.next(); + grammar_r = StringUtils.replace(grammar_r, key, (String) map.get(key)); + } + return grammar_r; + } + + /** + * @param nl The natural language expression. + * @param subKey The part of the nl expression to be swapped out. + * @param target The chunk to be swapped in to the nl + * @return The nl with the chunk replaced with the target. + */ + String replaceNlWithTarget(String nl, String subKey, String target) { + return StringUtils.replace(nl, subKey, target); + } } @@ -228,6 +283,14 @@ } } + void addToEnd(Chunk chunk) { + if (next == null) { + next = chunk; + } else { + next.addToEnd(chunk); + } + } + } |