Package modTransf.engine

Source Code of modTransf.engine.SimpleMockTransformation$TopRuleFilter

//Source file: H:\\temp\\generated\\modTransf\\engine\\ExecutableTransformation.java

package modTransf.engine;

import modTransf.model.ModelHelper;
import modTransf.util.UnmodifiableFilteredCollection;
import modTransf.util.Filter;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import modTransf.model.MultiModelHelper;
import modTransf.model.SimpleMultiModelHelper;

/**
* A simple implementation of Transformation for tests.
* This class is used in tests requiring a Transformation object.
*/
public class SimpleMockTransformation implements Transformation
{
   /**
    * The manager of models.
    */
   protected MultiModelHelper modelHelpers;

   /**
    * List of target during an execution.
    */
   protected List targetNames;

   /**
    * The set of associated rules.
    */
   protected RuleSet ruleSet = new RuleSet();


   public SimpleMockTransformation()
   {
     modelHelpers = new SimpleMultiModelHelper();
   }

   /**
    * Create an executable transformation. Specifies the names and types of models
    * participating in the transformation.
    * Also specifies the default name of the target models. These later names can be
    * overiden at execution.
    * @param models Comma separated string representing the models names and types.
    * @param defaultTargets Comma separated string representing the names of the
    * default targets.@param modelDeclarations
    */
   public SimpleMockTransformation(String defaultTargets)
   {
     modelHelpers = new SimpleMultiModelHelper();
    setTargetNames( defaultTargets );
   }

   /**
    * Set the models participating to the transformation.
    *
    * @param modelDeclarations Comma separated list of model declarations.
    */
   public void setTargetNames( String defaultTargets )
    {
      String names[] = defaultTargets.split("\\s*,\\s*"); // "\s*,\s*" ??

      for(int i=0; i<names.length; i++ )
      {
        String name = names[i];
        targetNames.add( name );
      }
    }

   /**
    * Access method for the defaultTargetNames property.
    *
    * @return   the current value of the defaultTargetNames property
    */
   public List getTargetNames()
   {
      return targetNames;
   }

   /**
    * Sets the value of the defaultTargetNames property.
    *
    * @param aDefaultTargetNames the new value of the defaultTargetNames property
    */
   public void setTargetNames(List aDefaultTargetNames)
   {
      targetNames = aDefaultTargetNames;
   }

   /**
    * Is the specified modelName a target model ?
    * @param modelName String
    * @return boolean
    */
   public boolean isTargetModel(String modelName)
  {
    return targetNames.contains(modelName);
  }

  /**
   * Is the specified modelName a source model ?
   * @param modelName String
   * @return boolean
   */
  public boolean isSourceModel(String modelName)
{
   return !targetNames.contains(modelName);
}

    /**
    * Get the MultiModelHelper managing the models.
    * @throws TransformationException
    * @return MultiModelHelper
    */
   public MultiModelHelper getModels()
     {
       return modelHelpers;
     }

   /**
    * Get the list of TopRule.
    * TopRule are rules denoting a relation. They can be executed as is.
    * @return List
    */
   public Collection getTopRules()
   {
     return ruleSet.getTopRule();
   }

   /**
    * @param name
    * @return modTransf.engine.Rule
    */
   public Rule getRule(String name)
   {
    return ruleSet.getRule(name);
   }

   /**
    * @param rule
    */
   public void addRule(Rule rule)
   {
     ruleSet.addRule( rule );
   }

  /**
   * transform
   *
   * @param models ModelHelper[]
   */
  public void transform(ModelHelper[] models)
  {
    throw new UnsupportedOperationException();
  }

  /**
   * getRuleSet
   *
   * @return RuleSet
   */
  public Transformation.RuleSet getRuleSet()
  {
    return ruleSet;
  }

  /**
   * getReaderWriterFactory
   *
   * @return ReaderWriterFactory
   */
  public ReaderWriterFactory getReaderWriterFactory()
  {
    throw new UnsupportedOperationException("Not yet implemented.");
  }

  /**
    * Nested Class.
    * Set of rule implementation
    * <p>Titre : ModTransf V3</p>
    * <p>Description : </p>
    * <p>Copyright : Copyright (c) 2005</p>
    * <p>Soci�t� : </p>
    * @author Cedric Dumoulin
    * @version 3.0
    */
   private class RuleSet extends HashMap implements EngineLifeCycle, Transformation.RuleSet {
     //protected Map rules = new HashMap();

     private volatile Collection topRules;

     public void addRule( Rule rule )
     {
      put( rule.getRuleName(), rule);
     }

     public Rule getRule( String ruleName )
     {
       return (Rule)get(ruleName);
     }

     /**
      * Get a view List on TopRule.
      * @return List
      */
     public Collection getTopRule()
     {
       if( topRules == null )
         topRules = new UnmodifiableFilteredCollection( values(), new TopRuleFilter() );

       return topRules;
     }
     /**
      * engineFinish
      *
      * @param context RuleContext
      */
     public void engineFinish(RuleContext context)
       throws EngineException
     {
       Iterator iter = values().iterator();
       while(iter.hasNext())
       {
         Object obj = iter.next();
         if( obj instanceof EngineLifeCycle)
         {
          EngineLifeCycle rule = (EngineLifeCycle)obj;
          rule.engineFinish(context);
        }
       }
     }

     /**
      * engineStart
      *
      * @param context RuleContext
      */
     public void engineStart(RuleContext context)
       throws EngineException
     {
       Iterator iter = values().iterator();
       while(iter.hasNext())
       {
         Object obj = iter.next();
         if( obj instanceof EngineLifeCycle)
         {
           EngineLifeCycle rule = (EngineLifeCycle)obj;
           rule.engineStart(context);
         }
       }
     }
   }

   private class TopRuleFilter implements Filter {
     public boolean isAllowed( Object object )
     {
       return (object instanceof TopRule);
     }
   }
}
TOP

Related Classes of modTransf.engine.SimpleMockTransformation$TopRuleFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.