View Javadoc

    1   /* RuleSequence
    2   *
    3   * $Id: DecideRuleSequence.java 4912 2007-02-18 21:11:08Z gojomo $
    4   *
    5   * Created on Mar 3, 2005
    6   *
    7   * Copyright (C) 2005 Internet Archive.
    8   *
    9   * This file is part of the Heritrix web crawler (crawler.archive.org).
    10  *
    11  * Heritrix is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU Lesser Public License as published by
    13  * the Free Software Foundation; either version 2.1 of the License, or
    14  * any later version.
    15  *
    16  * Heritrix is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU Lesser Public License for more details.
    20  *
    21  * You should have received a copy of the GNU Lesser Public License
    22  * along with Heritrix; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24  */
    25  package org.archive.crawler.deciderules;
    26
    27  import java.util.Iterator;
    28  import java.util.logging.Level;
    29  import java.util.logging.Logger;
    30
    31  import javax.management.AttributeNotFoundException;
    32
    33  import org.archive.crawler.settings.MapType;
    34
    35  /***
    36   * RuleSequence represents a series of Rules, which are applied in turn
    37   * to give the final result.  Rules return {@link DecideRule#ACCEPT},
    38   * {@link DecideRule#REJECT}, or {@link DecideRule#PASS}.  The final result
    39   * of a DecideRuleSequence is that of the last rule decision made, either
    40   * ACCEPT or REJECT (PASS is used by rules that do not have an opinion
    41   * on a particular processing pass).
    42   *
    43   * @author gojomo
    44   */
    45  public class DecideRuleSequence extends DecideRule {
    46
    47      private static final long serialVersionUID = 8918111430698683110L;
    48
    49      private static final Logger logger =
    50          Logger.getLogger(DecideRuleSequence.class.getName());
    51
    52      public static final String ATTR_RULES = "rules";
    53
    54      public DecideRuleSequence(String name) {
    55          this(name,"DecideRuleSequence. Multiple DecideRules applied in " +
    56              "order with last non-PASS the resulting 'decision'.");
    57      }
    58      public DecideRuleSequence(String name, String description) {
    59          super(name);
    60          setDescription(description);
    61
    62          addElementToDefinition(new MapType(ATTR_RULES,
    63                  "This is a list of DecideRules to be applied in sequence.",
    64                  DecideRule.class));
    65      }
    66
    67      public Object decisionFor(Object object) {
    68          Object runningAnswer = PASS;
    69          for(Iterator iter = getRules(object).iterator(object);
    70                  iter.hasNext();) {
    71              DecideRule r = (DecideRule)iter.next();
    72              if(runningAnswer==r.singlePossibleNonPassDecision(object)) {
    73                  // there's no chance this rule will change the decision;
    74                  continue;
    75              }
    76              Object answer = r.decisionFor(object);
    77              if (logger.isLoggable(Level.FINE)) {
    78                  logger.fine("Rule " + r.getName() + " of " + this.getName() +
    79                      " decided " + answer + " on " + object);
    80              }
    81              if (answer != PASS) {
    82                  runningAnswer = answer;
    83              }
    84          }
    85          if (logger.isLoggable(Level.FINE)) {
    86              logger.fine("Decision of " + this.getName() + " was " +
    87                  runningAnswer);
    88          }
    89          return runningAnswer;
    90      }
    91
    92      protected MapType getRules(Object o) {
    93          MapType rules = null;
    94          try {
    95              rules = (MapType)getAttribute(o, ATTR_RULES);
    96          } catch (AttributeNotFoundException e) {
    97              logger.severe(e.getLocalizedMessage());
    98          }
    99          return rules;
    100     }
    101
    102     /* kick-update all constituent rules
    103      * (non-Javadoc)
    104      * @see org.archive.crawler.deciderules.DecideRule#kickUpdate()
    105      */
    106     public void kickUpdate() {
    107         for(Iterator iter = getRules(null).iterator(null);
    108                 iter.hasNext();) {
    109             DecideRule r = (DecideRule)iter.next();
    110             r.kickUpdate();
    111         }
    112     }
    113 }