svnno****@sourc*****
svnno****@sourc*****
2009年 2月 6日 (金) 00:29:18 JST
Revision: 2617 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2617 Author: ashigeru Date: 2009-02-06 00:29:18 +0900 (Fri, 06 Feb 2009) Log Message: ----------- generic-treeにRewriterの仕組みを追加。 Modified Paths: -------------- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/converter/PrimitiveConverter.java artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/converter/PrimitiveConverterTest.java Added Paths: ----------- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/RewriteRule.java artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/Rewriter.java artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ValueRewriter.java artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/EmptyEliminator.java artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/Main.java artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/RewriterTest.java artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ValueRewriterTest.java -------------- next part -------------- Modified: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/converter/PrimitiveConverter.java =================================================================== --- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/converter/PrimitiveConverter.java 2009-02-05 15:24:17 UTC (rev 2616) +++ artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/converter/PrimitiveConverter.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -71,10 +71,13 @@ * <li> {@link BigDecimal} </li> * <li> {@link String} </li> * </ul> + * <p> + * {@code null}が引数に渡された場合、空文字列として返す。 + * </p> */ public Value convert(Object object, ObjectConverter converter) { if (object == null) { - return Terminal.of("null"); //$NON-NLS-1$ + return Terminal.of(""); //$NON-NLS-1$ } Class<? extends Object> klass = object.getClass(); if (TARGETS.contains(klass)) { Added: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/RewriteRule.java =================================================================== --- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/RewriteRule.java (rev 0) +++ artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/RewriteRule.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,122 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.rewrite; + +import org.jiemamy.utils.gtree.model.Entry; +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; + +/** + * {@link Value}を変換するルール。 + * <p> + * このクラスにおけるすべての変換ルールの実装は、何も変換を行わない。 + * それぞれのメソッドをオーバーライドして別の値を返すことにより、 + * {@code Generic Tree}上の任意の値を書き換えることができる。 + * </p> + * <p> + * 変換結果として{@code null}が返された場合、変換対象の親要素の種類によって + * それぞれ次のようなことが行われる。 + * </p> + * <table border="1"> + * <tr> + * <th> + * 親要素の種類 + * </th> + * <th> + * {@code null}が返された時の動作 + * </th> + * </tr> + * <tr> + * <td> + * {@link Sequence} + * </td> + * <td> + * 対象の値が{@link Sequence}上より除去される + * </td> + * </tr> + * <tr> + * <td> + * {@link Entry} + * </td> + * <td> + * 対象のキーや値を含むエントリ全体が{@link Record}上より除去される + * (キー、値のいずれか一つでも{@code null}である場合) + * </td> + * </tr> + * <tr> + * <td> + * (なし) + * </td> + * <td> + * 全体の結果が{@code null}となる + * </td> + * </tr> + * </table> + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public abstract class RewriteRule { + + /** + * {@link Terminal}を書き換える。 + * @param value + * 変換前の値 + * @return + * 変換後の値、 + * この値をツリー上から除去する場合は{@code null} + */ + protected Value rewriteTerminal(Terminal value) { + return value; + } + + /** + * 順序を考慮しないリスト({@link Sequence})を書き換える。 + * @param value + * 変換前の値 + * @return + * 変換後の値、 + * この値をツリー上から除去する場合は{@code null} + */ + protected Value rewriteOrderedList(Sequence value) { + return value; + } + + /** + * 順序付きリスト({@link Sequence})を書き換える。 + * @param value + * 変換前の値 + * @return + * 変換後の値、 + * この値をツリー上から除去する場合は{@code null} + */ + protected Value rewriteUnorderedList(Sequence value) { + return value; + } + + /** + * {@link Record}を書き換える。 + * @param value + * 変換前の値 + * @return + * 変換後の値、 + * この値をツリー上から除去する場合は{@code null} + */ + protected Value rewriteRecord(Record value) { + return value; + } +} Property changes on: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/RewriteRule.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/Rewriter.java =================================================================== --- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/Rewriter.java (rev 0) +++ artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/Rewriter.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,191 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.rewrite; + +import java.util.ArrayList; +import java.util.List; + +import org.jiemamy.utils.gtree.model.ElementVisitor; +import org.jiemamy.utils.gtree.model.Entry; +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; + +/** + * {@link RewriteRule}の一覧を元に{@link Value}が表現するツリー全体を書き換える。 + * <p> + * 複数の変換ルールが一度に指定された場合、 + * 対象の値をまず最初に指定されたルールによって変換し、その結果をさらに次のルールによって変換する。 + * 指定された変換ルールを順に適用していくことによって、最終的な結果を作成する。 + * ただし、値が削除されるような変換ルールがこの過程に存在する場合、 + * 以降のルール適用を行わずに直ちにその値を削除する。 + * </p> + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public class Rewriter { + + private static final RewriteVisitor KEEP_KEY = new RewriteVisitor(true); + private static final RewriteVisitor ALL = new RewriteVisitor(false); + + private final ValueRewriter valueRewriter; + + /** + * インスタンスを生成する。 + * @param ruleList 適用するルールの一覧 + * @throws NullPointerException 引数に{@code null}が指定された場合 + */ + public Rewriter(List<? extends RewriteRule> ruleList) { + if (ruleList == null) { + throw new NullPointerException("ruleList"); //$NON-NLS-1$ + } + this.valueRewriter = new ValueRewriter(ruleList); + } + + /** + * 指定の値を変換する。 + * <p> + * これは値の種類ごとに、次のように変換規則が適用される。 + * </p> + * <dl> + * <dt> + * {@link Terminal} + * </dt> + * <dd> + * 対象の値に対して直ちに変換規則が適用され、変換結果の値を利用する。 + * </dd> + * <dt> + * {@link Sequence} + * </dt> + * <dd> + * まず、それぞれの子要素に対して変換規則が適用され、適用結果を子要素とする + * {@link Sequence}を再構築する。 + * このとき、{@link Sequence}が順序付きリストを表現する場合、 + * それぞれの子要素の順序は再構築後も保持される。 + * ただし、子要素の変換結果が{@code null}となるようなものは、 + * 再構築される{@link Sequence}からは除去される。 + * その後、再構築した{@link Sequence}に対してさらに変換規則が適用され、 + * その変換結果の値を対象の値の変換結果として利用する。 + * </dd> + * <dt> + * {@link Record} + * </dt> + * <dd> + * まず、それぞれの子要素である{@link Entry}の{@link Entry#getKey() キー}に対して + * 変換規則を適用する。この結果が{@code null}でない場合、次に + * 同{@link Entry#getValue() 値}に対して変換規則を適用する。 + * いずれの結果も{@code null}でない場合、変換規則のキーと値からなる{@link Entry}を再構築し、 + * さらにそれらを子要素に持つような{@link Record}を作成する。 + * いずれかの変換結果が{@code null}となるような{@link Entry}は、 + * 再構築する{@link Record}からは除去される。 + * その後、再構築した{@link Record}に対してさらに変換規則が適用され、 + * その変換結果の値を対象の値の変換結果として利用する。 + * </dd> + * </dl> + * @param value 変換する値 + * @param keepEntryKey + * {@code true}が指定された場合は{@link Entry#getKey()}の値に対して + * 変換規則を適用しない、 + * {@code false}が指定された場合は{@link Entry#getKey()}の値に対しても + * 変換規則を適用する + * @return + * 変換結果、 + * 引数に指定された値自体が削除される場合は{@code null} + * @throws NullPointerException 引数に{@code null}が指定された場合 + */ + public Value apply(Value value, boolean keepEntryKey) { + if (value == null) { + throw new NullPointerException("value"); //$NON-NLS-1$ + } + RewriteVisitor engine = keepEntryKey ? KEEP_KEY : ALL; + return value.accept(engine, valueRewriter); + } + + private static class RewriteVisitor + extends ElementVisitor<Value, ValueRewriter, RuntimeException> { + + private boolean keepEntryKey; + + /** + * インスタンスを生成する。 + * @param keepEntryKey {@link Entry#getKey()}を保持するかどうか + */ + RewriteVisitor(boolean keepEntryKey) { + super(); + this.keepEntryKey = keepEntryKey; + } + + @Override + protected Value visitTerminal(Terminal elem, ValueRewriter context) { + assert elem != null; + assert context != null; + return context.rewrite(elem); + } + + @Override + protected Value visitSequence(Sequence elem, ValueRewriter context) { + assert elem != null; + assert context != null; + List<Value> results = new ArrayList<Value>(); + for (Value target : elem.getValues()) { + Value applied = target.accept(this, context); + if (applied != null) { + results.add(applied); + } + } + if (elem.getKind() == Value.Kind.ORDERED_LIST) { + return context.rewrite(Sequence.ordered(results)); + } + else if (elem.getKind() == Value.Kind.UNORDERED_LIST) { + return context.rewrite(Sequence.unordered(results)); + } + else { + throw new AssertionError(elem); + } + } + + @Override + protected Value visitRecord(Record elem, ValueRewriter context) { + assert elem != null; + assert context != null; + List<Entry> results = new ArrayList<Entry>(); + for (Entry target : elem.getEntries()) { + Value appliedKey; + if (keepEntryKey) { + appliedKey = target.getKey(); + } + else { + appliedKey = target.getKey().accept(this, context); + if (appliedKey == null) { + continue; // skip + } + } + Value appliedValue = target.getValue().accept(this, context); + if (appliedValue == null) { + continue; // skip + } + results.add(Entry.of(appliedKey, appliedValue)); + } + return context.rewrite(Record.of(results)); + } + + @Override + protected Value visitEntry(Entry elem, ValueRewriter context) { + throw new AssertionError(elem); + } + } +} Property changes on: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/Rewriter.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ValueRewriter.java =================================================================== --- artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ValueRewriter.java (rev 0) +++ artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ValueRewriter.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,120 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.rewrite; + +import java.util.List; + +import org.jiemamy.utils.gtree.model.ElementVisitor; +import org.jiemamy.utils.gtree.model.Entry; +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; +import org.jiemamy.utils.gtree.model.Value.Kind; + +/** + * 単一の{@link Value}オブジェクトを変換する。 + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +class ValueRewriter { + + /** + * 単一の値を変換する戦略オブジェクト。 + */ + private static final SingleRewriter ENGINE = new SingleRewriter(); + + /** + * 適用するルールの一覧。 + */ + private final List<? extends RewriteRule> rules; + + /** + * インスタンスを生成する。 + * @param ruleList 適用するルールの一覧 + * @throws NullPointerException 引数に{@code null}が指定された場合 + */ + public ValueRewriter(List<? extends RewriteRule> ruleList) { + super(); + if (ruleList == null) { + throw new NullPointerException("ruleList"); //$NON-NLS-1$ + } + this.rules = ruleList; + } + + /** + * 指定の値を変換する。 + * @param value 変換する値 + * @return 変換結果、削除する場合は{@code null} + * @throws NullPointerException 引数に{@code null}が指定された場合 + */ + public Value rewrite(Value value) { + if (value == null) { + throw new NullPointerException("value"); //$NON-NLS-1$ + } + Value current = value; + for (RewriteRule rule : rules) { + current = current.accept(ENGINE, rule); + if (current == null) { + return null; + } + } + return current; + } + + private static class SingleRewriter + extends ElementVisitor<Value, RewriteRule, RuntimeException> { + + /** + * インスタンスを生成する。 + */ + SingleRewriter() { + super(); + } + + @Override + protected Value visitTerminal(Terminal elem, RewriteRule context) { + assert elem != null; + return context.rewriteTerminal(elem); + } + + @Override + protected Value visitSequence(Sequence elem, RewriteRule context) { + assert elem != null; + Kind kind = elem.getKind(); + if (kind == Value.Kind.ORDERED_LIST) { + return context.rewriteOrderedList(elem); + } + else if (kind == Value.Kind.UNORDERED_LIST) { + return context.rewriteUnorderedList(elem); + } + else { + throw new AssertionError(elem); + } + } + + @Override + protected Value visitRecord(Record elem, RewriteRule context) { + assert elem != null; + return context.rewriteRecord(elem); + } + + @Override + protected Value visitEntry(Entry elem, RewriteRule context) { + throw new AssertionError(elem); + } + } +} Property changes on: artemis/trunk/generic-tree/src/main/java/org/jiemamy/utils/gtree/rewrite/ValueRewriter.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Modified: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/converter/PrimitiveConverterTest.java =================================================================== --- artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/converter/PrimitiveConverterTest.java 2009-02-05 15:24:17 UTC (rev 2616) +++ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/converter/PrimitiveConverterTest.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -118,7 +118,7 @@ @Test public void testConvert_Null() { Value result = INSTANCE.convert(null, new MockObjectConverter()); - assertThat(result, is(t("null"))); + assertThat(result, is(t(""))); } /** Added: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/EmptyEliminator.java =================================================================== --- artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/EmptyEliminator.java (rev 0) +++ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/EmptyEliminator.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,72 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.example.rewrite; + +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; +import org.jiemamy.utils.gtree.rewrite.RewriteRule; + +/** + * 空の要素を削除する規則。 + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public class EmptyEliminator extends RewriteRule { + + @Override + protected Value rewriteTerminal(Terminal value) { + String content = value.getRepresentation(); + + // 空なら消去 + if (content.length() == 0) { + return null; + } + // ++ しておく + return Terminal.of(content + "++"); + } + + @Override + protected Value rewriteOrderedList(Sequence value) { + // 空なら消去 + if (value.getValues().isEmpty()) { + return null; + } + // そのまま返す + return value; + } + + @Override + protected Value rewriteUnorderedList(Sequence value) { + // 空なら消去 + if (value.getValues().isEmpty()) { + return null; + } + // そのまま返す + return value; + } + + @Override + protected Value rewriteRecord(Record value) { + // 空なら消去 + if (value.getEntries().isEmpty()) { + return null; + } + // そのまま返す + return value; + } +} Property changes on: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/EmptyEliminator.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/Main.java =================================================================== --- artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/Main.java (rev 0) +++ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/Main.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,78 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.example.rewrite; + +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +import org.jiemamy.utils.gtree.converter.ConverterDriver; +import org.jiemamy.utils.gtree.converter.DefaultObjectConverter; +import org.jiemamy.utils.gtree.converter.ObjectConverter; +import org.jiemamy.utils.gtree.model.Value; +import org.jiemamy.utils.gtree.rewrite.Rewriter; +import org.jiemamy.utils.gtree.text.Emitter; + +/** + * {@link Rewriter}のサンプルプログラム。 + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public class Main { + + /** + * プログラムエントリ。 + * @param args 無視される + */ + public static void main(String[] args) { + + // ツリーを構築 + Map<Object, Object> root = new HashMap<Object, Object>(); + root.put("string", "Hello"); + root.put("null", null); + root.put("list", Arrays.asList("a", "b")); + root.put("emptyList", Arrays.asList()); + root.put("set", new HashSet<Object>(Arrays.asList("c", "d"))); + root.put("emptySet", new HashSet<Object>(Arrays.asList())); + root.put("map", Collections.singletonMap("Hello", "World")); + root.put("emptyMap", Collections.emptyMap()); + + // 普通の変換器で変換する + ObjectConverter converter = + DefaultObjectConverter.newInstance( + Collections.<ConverterDriver>emptyList()); + Value tree = converter.convert(root); + + // EmptyEliminator を使ってツリーを書き換える (エントリのキーは書き換えない) + Rewriter optimizer = new Rewriter(Arrays.asList(new EmptyEliminator())); + Value opt = optimizer.apply(tree, true); + + PrintWriter writer = new PrintWriter(System.out); + + // 普通の変換 + writer.println("=== Original Tree"); + Emitter.emit(tree, writer); + + // 書き換えたツリー + writer.println("=== Optimized Tree"); + Emitter.emit(opt, writer); + + writer.flush(); + } +} Property changes on: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/example/rewrite/Main.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/RewriterTest.java =================================================================== --- artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/RewriterTest.java (rev 0) +++ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/RewriterTest.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,212 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.rewrite; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.jiemamy.utils.gtree.model.Entry; +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; +import org.junit.Test; + +/** + * Test for {@link Rewriter}. + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public class RewriterTest { + + private RewriteRule insert = new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of("*" + value.getRepresentation()); + } + }; + + private RewriteRule ol2ul = new RewriteRule() { + @Override + protected Value rewriteOrderedList(Sequence value) { + return Sequence.unordered(value.getValues()); + } + }; + + private RewriteRule removeUl = new RewriteRule() { + @Override + protected Value rewriteUnorderedList(Sequence value) { + return null; + } + }; + + private RewriteRule removeQ = new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + if (value.getRepresentation().equals("?")) { + return null; + } + else { + return value; + } + } + }; + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_Keep() { + Rewriter rewriter = new Rewriter(r(insert)); + Value result = rewriter.apply(rc( + "t", "v", + "o", ol("a", "b", ol("x")), + "u", ul("c", "d", ol("y")), + "r", rc("e", "f")), true); + assertThat(result, is(rc( + "t", "*v", + "o", ol("*a", "*b", ol("*x")), + "u", ul("*c", "*d", ol("*y")), + "r", rc("e", "*f")))); + } + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_All() { + Rewriter rewriter = new Rewriter(r(insert)); + Value result = rewriter.apply(rc( + "t", "v", + "o", ol("a", "b", ol("x")), + "u", ul("c", "d", ol("y")), + "r", rc("e", "f")), false); + assertThat(result, is(rc( + "*t", "*v", + "*o", ol("*a", "*b", ol("*x")), + "*u", ul("*c", "*d", ol("*y")), + "*r", rc("*e", "*f")))); + } + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_Many() { + Rewriter rewriter = new Rewriter(r(ol2ul, insert)); + Value result = rewriter.apply(rc( + "t", "v", + "o", ol("a", "b", ol("x")), + "u", ul("c", "d", ol("y")), + "r", rc("e", "f")), true); + assertThat(result, is(rc( + "t", "*v", + "o", ul("*a", "*b", ul("*x")), + "u", ul("*c", "*d", ul("*y")), + "r", rc("e", "*f")))); + } + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_RemoveAndConvert() { + Rewriter rewriter = new Rewriter(r(removeUl, ol2ul)); + Value result = rewriter.apply(rc( + "t", "v", + "o", ol("a", "b", ol("x")), + "u", ul("c", "d", ol("y")), + "r", rc("e", "f")), true); + assertThat(result, is(rc( + "t", "v", + "o", ul("a", "b", ul("x")), + "r", rc("e", "f")))); + } + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_RemoveKey() { + Rewriter rewriter = new Rewriter(r(removeQ)); + Value result = rewriter.apply(rc( + "a", "a", + "b", "?", + "?", "c"), false); + assertThat(result, is(rc( + "a", "a"))); + } + + /** + * Test method for {@link Rewriter#apply(Value, boolean)}. + */ + @Test + public void testApply_ConvertAndRemove() { + Rewriter rewriter = new Rewriter(r(ol2ul, removeUl)); + Value result = rewriter.apply(rc( + "t", "v", + "o", ol("a", "b", ol("x")), + "u", ul("c", "d", ol("y")), + "r", rc("e", "f")), true); + assertThat(result, is(rc( + "t", "v", + "r", rc("e", "f")))); + } + + private List<RewriteRule> r(RewriteRule...rules) { + return Arrays.asList(rules); + } + + private Value ol(Object...contents) { + List<Value> list = new ArrayList<Value>(); + for (Object o : contents) { + list.add(value(o)); + } + return Sequence.ordered(list); + } + + private Value ul(Object...contents) { + List<Value> list = new ArrayList<Value>(); + for (Object o : contents) { + list.add(value(o)); + } + return Sequence.unordered(list); + } + + private Value rc(Object...nameAndValues) { + assert nameAndValues.length % 2 == 0; + ArrayList<Entry> results = new ArrayList<Entry>(); + for (int i = 0; i < nameAndValues.length; i += 2) { + Value key = value(nameAndValues[i]); + Value value = value(nameAndValues[i + 1]); + results.add(Entry.of(key, value)); + } + return Record.of(results); + } + + private Value value(Object contentOrValue) { + if (contentOrValue instanceof Value) { + return (Value) contentOrValue; + } + else { + return Terminal.of(String.valueOf(contentOrValue)); + } + } +} Property changes on: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/RewriterTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ValueRewriterTest.java =================================================================== --- artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ValueRewriterTest.java (rev 0) +++ artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ValueRewriterTest.java 2009-02-05 15:29:18 UTC (rev 2617) @@ -0,0 +1,315 @@ +/* + * Copyright 2009 Jiemamy Project and the Others. + * + * 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.jiemamy.utils.gtree.rewrite; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.jiemamy.utils.gtree.model.Entry; +import org.jiemamy.utils.gtree.model.Record; +import org.jiemamy.utils.gtree.model.Sequence; +import org.jiemamy.utils.gtree.model.Terminal; +import org.jiemamy.utils.gtree.model.Value; +import org.junit.Test; + +/** + * Test for {@link ValueRewriter}. + * @version $Date$ + * @author Suguru ARAKAWA (Gluegent, Inc.) + */ +public class ValueRewriterTest { + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Terminal_Once() { + ValueRewriter rewriter = new ValueRewriter(r(new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of("*" + value.getRepresentation()); + } + })); + Value result = rewriter.rewrite(value("Hello")); + assertThat(result, is(value("*Hello"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_OList_Once() { + ValueRewriter rewriter = new ValueRewriter(r(new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of("*" + value.getRepresentation()); + } + @Override + protected Value rewriteOrderedList(Sequence value) { + List<Value> modified = new ArrayList<Value>(); + modified.addAll(value.getValues()); + modified.add(Terminal.of("*")); + return Sequence.ordered(modified); + } + })); + Value result = rewriter.rewrite(ol("Hello", "World")); + assertThat(result, is(ol("Hello", "World", "*"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_UList_Once() { + ValueRewriter rewriter = new ValueRewriter(r(new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of("*" + value.getRepresentation()); + } + @Override + protected Value rewriteUnorderedList(Sequence value) { + List<Value> modified = new ArrayList<Value>(); + modified.addAll(value.getValues()); + modified.add(Terminal.of("*")); + return Sequence.unordered(modified); + } + })); + Value result = rewriter.rewrite(ul("Hello", "World")); + assertThat(result, is(ul("Hello", "World", "*"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Record_Once() { + ValueRewriter rewriter = new ValueRewriter(r(new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of("*" + value.getRepresentation()); + } + @Override + protected Value rewriteRecord(Record value) { + List<Entry> modified = new ArrayList<Entry>(); + modified.addAll(value.getEntries()); + modified.add(Entry.of(Terminal.of("+"), Terminal.of("-"))); + return Record.of(modified); + } + })); + Value result = rewriter.rewrite(rc("a", "b", "c", "d")); + assertThat(result, is(rc("a", "b", "c", "d", "+", "-"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Terminal_Three() { + final AtomicInteger counter = new AtomicInteger(); + RewriteRule insert = new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Terminal.of(value.getRepresentation() + counter.incrementAndGet()); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + insert, insert, insert)); + Value result = rewriter.rewrite(value("Hello")); + assertThat(result, is(value("Hello123"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_OList_Three() { + final AtomicInteger counter = new AtomicInteger(); + RewriteRule insert = new RewriteRule() { + @Override + protected Value rewriteOrderedList(Sequence value) { + List<Value> modified = new ArrayList<Value>(); + modified.addAll(value.getValues()); + modified.add(Terminal.of(String.valueOf( + counter.incrementAndGet()))); + return Sequence.ordered(modified); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + insert, insert, insert)); + Value result = rewriter.rewrite(ol("Hello")); + assertThat(result, is(ol("Hello", "1", "2", "3"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_UList_Three() { + final AtomicInteger counter = new AtomicInteger(); + RewriteRule insert = new RewriteRule() { + @Override + protected Value rewriteUnorderedList(Sequence value) { + List<Value> modified = new ArrayList<Value>(); + modified.addAll(value.getValues()); + modified.add(Terminal.of(String.valueOf( + counter.incrementAndGet()))); + return Sequence.unordered(modified); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + insert, insert, insert)); + Value result = rewriter.rewrite(ul("Hello")); + assertThat(result, is(ul("Hello", "1", "2", "3"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Record_Three() { + final AtomicInteger counter = new AtomicInteger(); + RewriteRule insert = new RewriteRule() { + @Override + protected Value rewriteRecord(Record value) { + List<Entry> modified = new ArrayList<Entry>(); + modified.addAll(value.getEntries()); + String key = String.valueOf(counter.incrementAndGet()); + String val = String.valueOf(counter.incrementAndGet()); + modified.add(Entry.of(Terminal.of(key), Terminal.of(val))); + return Record.of(modified); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + insert, insert, insert)); + Value result = rewriter.rewrite(rc("a", "b")); + assertThat(result, is(rc("a", "b", "1", "2", "3", "4", "5", "6"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Around() { + RewriteRule around = new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Sequence.ordered(Collections.singletonList(value)); + } + @Override + protected Value rewriteOrderedList(Sequence value) { + return Sequence.unordered(value.getValues()); + } + @Override + protected Value rewriteUnorderedList(Sequence value) { + Value e = value.getValues().get(0); + return Record.of(Collections.singletonList(Entry.of(e, e))); + } + @Override + protected Value rewriteRecord(Record value) { + Entry e = value.getEntries().get(0); + return Terminal.of(e.getKey().toString() + e.getValue()); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + around, // 'a' -> ['a'] + around, // ['a'] -> {'a'} + around, // {'a'} -> <'a':'a'> + around // <'a':'a'> -> 'aa' + )); + Value result = rewriter.rewrite(value("Hello")); + assertThat(result, is(value("HelloHello"))); + } + + /** + * Test method for {@link ValueRewriter#rewrite(org.jiemamy.utils.gtree.model.Value)}. + */ + @Test + public void testRewrite_Abort() { + RewriteRule around = new RewriteRule() { + @Override + protected Value rewriteTerminal(Terminal value) { + return Sequence.ordered(Collections.singletonList(value)); + } + @Override + protected Value rewriteOrderedList(Sequence value) { + return Sequence.unordered(value.getValues()); + } + @Override + protected Value rewriteUnorderedList(Sequence value) { + return null; + } + @Override + protected Value rewriteRecord(Record value) { + Entry e = value.getEntries().get(0); + return Terminal.of(e.getKey().toString() + e.getValue()); + } + }; + ValueRewriter rewriter = new ValueRewriter(r( + around, // 'a' -> ['a'] + around, // ['a'] -> {'a'} + around, // {'a'} -> null + around // <'a':'a'> -> 'aa' + )); + Value result = rewriter.rewrite(value("Hello")); + assertThat(result, is(nullValue())); + } + + private List<RewriteRule> r(RewriteRule...rules) { + return Arrays.asList(rules); + } + + private Value ol(Object...contents) { + List<Value> list = new ArrayList<Value>(); + for (Object o : contents) { + list.add(value(o)); + } + return Sequence.ordered(list); + } + + private Value ul(Object...contents) { + List<Value> list = new ArrayList<Value>(); + for (Object o : contents) { + list.add(value(o)); + } + return Sequence.unordered(list); + } + + private Value rc(Object...nameAndValues) { + assert nameAndValues.length % 2 == 0; + ArrayList<Entry> results = new ArrayList<Entry>(); + for (int i = 0; i < nameAndValues.length; i += 2) { + Value key = value(nameAndValues[i]); + Value value = value(nameAndValues[i + 1]); + results.add(Entry.of(key, value)); + } + return Record.of(results); + } + + private Value value(Object contentOrValue) { + if (contentOrValue instanceof Value) { + return (Value) contentOrValue; + } + else { + return Terminal.of(String.valueOf(contentOrValue)); + } + } +} Property changes on: artemis/trunk/generic-tree/src/test/java/org/jiemamy/utils/gtree/rewrite/ValueRewriterTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native