/*
 * LensKit, a reference implementation of recommender algorithms.
 * Copyright 2010-2011 Regents of the University of Minnesota
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.dtree;


/**
 * Helper methods for data trees.
 * 
 * @author Michael Ekstrand <ekstrand@cs.umn.edu>
 *
 */
public class Trees {
    private Trees() {}
    
    public static String childValue(DataNode node, String name) {
        return childValue(node, name, null);
    }
    
    public static String childValue(DataNode node, String name, String dft) {
        for (DataNode n: node.getChildren()) {
            if (n.getName().equals(name)) {
                return n.getValue();
            }
        }
        return dft;
    }
}

@@ -895,16 +895,49 @@
 dtree;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 %0A/**%0A * 
@@ -1073,24 +1073,244 @@
 s() %7B%7D%0A    %0A
+    public static DataNode child(DataNode node, String name) %7B%0A        for (DataNode n: node.getChildren()) %7B%0A            if (n.getName().equals(name))%0A                return n;%0A        %7D%0A        return null;%0A    %7D%0A    %0A
     public s
@@ -1641,32 +1641,32 @@
     %7D%0A        %7D%0A
-
         return d
@@ -1675,10 +1675,1034 @@
 ;%0A    %7D%0A
+    %0A    private static final Pattern truePattern = %0A            Pattern.compile(%22%5Etrue%7Cyes$%22, Pattern.CASE_INSENSITIVE);%0A    private static final Pattern falsePattern =%0A            Pattern.compile(%22%5Efalse%7Cno$%22, Pattern.CASE_INSENSITIVE);%0A    %0A    static boolean reMatches(Pattern pat, String st) %7B%0A        return pat.matcher(st).find();%0A    %7D%0A    %0A    public static boolean childValueBool(DataNode node, String name, boolean dft) %7B%0A        String v = childValue(node, name);%0A        if (v == null) %7B%0A            return dft;%0A        %7D else if (reMatches(truePattern, v)) %7B%0A            return true;%0A        %7D else if (reMatches(falsePattern, v)) %7B%0A            return false;%0A        %7D else %7B%0A            throw new IllegalArgumentException(%22Invalid boolean: %22 + v);%0A        %7D%0A    %7D%0A    %0A    public static int childValueInt(DataNode node, String name, int dft) %7B%0A        String v = childValue(node, name);%0A        if (v == null) %7B%0A            return dft;%0A        %7D else %7B%0A            return Integer.parseInt(v);%0A        %7D%0A    %7D%0A
 %7D%0A

Add more child navigation features to data trees.