Package modTransf.engine

Source Code of modTransf.engine.ExecutableTransformation$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;

/**
* The root class from which a transformation start.
*/
public class ExecutableTransformation implements Transformation
{
  /**
   * Log for Transformation.
   * This log is used during rules development.
   * It logs rules processing.
   */
  protected static Log log = LogFactory.getLog("modTransf.engine.Transformation");

   /**
    * List of target names.
    */
   protected List defaultTargetNames= new ArrayList();

   /**
    * Ordered list of model declarations. These declarations are used to name models
    * passed as transform arguments.
    */
   protected List modelDeclaration = new ArrayList();

   /**
    * 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();

  protected ReaderWriterFactory readerWriterFactory;

  public ExecutableTransformation()
   {

   }

   /**
    * Create an executable transformation. Specifies the names and types of models
    * participating in the transformation.
    * @param models Comma separated string representing the models names and types.
    * @param modelDeclarations
    */
   public ExecutableTransformation(String modelDeclarations)
   {
     setModelDeclarations( modelDeclarations);

   }

   /**
    * 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 ExecutableTransformation(String modelDeclarations, String defaultTargets)
   {
    setModelDeclarations( modelDeclarations);
    setDefaultTargetNames( defaultTargets );
   }

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

     for(int i=0; i<models.length; i++ )
     {
       String modelName = models[i];
       log.info("Add model '" + modelName + "'." );
       modelDeclaration.add( new ModelDeclaration(modelName) );
     }
   }

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

      for(int i=0; i<names.length; i++ )
      {
        String name = names[i];
        log.info("Add target '" + name + "'." );
        defaultTargetNames.add( name );
      }
    }

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

   /**
    * Sets the value of the defaultTargetNames property.
    *
    * @param aDefaultTargetNames the new value of the defaultTargetNames property
    */
   public void setDefaultTargetNamesList(List aDefaultTargetNames)
   {
      defaultTargetNames = 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);
}

   /**
    * Transform the specified models according to the Transformation.
    * @param models All the models participating in the transformation, in the order
    * declared by the Transformation parameters. This include src and target models.
    * Targets model can be empty or not, but can't be null.
    * @param targets The name of the models used as target. The name are those used
    * inside the Transformation.
    */
   public void transform(ModelHelper[] models, String[] targets)
     throws EngineException, TransformationException
   {
     // Check arguments
     if( models.length != modelDeclaration.size() )
       throw new TransformationException("Expected '" + modelDeclaration.size()
                                         + "' models as parameter. Found '"
                                         + models.length + "' models." );

     // Specify target names
     if( targets != null )
       targetNames = Arrays.asList(targets);
      else
        targetNames = defaultTargetNames;


     // Create models manager
     modelHelpers = new SimpleMultiModelHelper();
     Iterator names = modelDeclaration.iterator();
     int i=0;
     while( names.hasNext() && i<models.length )
     {
       modelHelpers.registerModel( ((ModelDeclaration)names.next()).getName(), models[i]);
       i++;
     }


     // Create context
     SimpleRuleContext context = new SimpleRuleContext();
     context.setTransformation(this);
     // notify engineStart
     ruleSet.engineStart(context);
     // Rule should be set when an enterLocal() is done (to be done: add args to
     // enterLocal, or add setter in the interface)
     // Engine should be set here (so we need to know it !

      Collection topRules = getTopRules();
      Iterator iter = topRules.iterator();
      while(iter.hasNext())
      {
        //System.out.println("compute topRule");
        TopRule rule = (TopRule)iter.next();
        rule.resolveAll(context);
      }

      // notify engineStop
      ruleSet.engineFinish(context);

   }

   /**
    * Start the transformation on provided models.
    * @param models All the models participating in the transformation, in the order
    * declared by the Transformation parameters. This include src and target models.
    * Targets model can be empty or not, but can't be null.
    * @param targets The models used as target. These models should also be present
    * in the parameter "models". The order is not significant.
    */
   public void transform(ModelHelper[] models, ModelHelper[] targets)
     throws EngineException, TransformationException
   {
    throw new UnsupportedOperationException("Not yet implemented.");
   }

   /**
    * Transform the specified models according to the Transformation.
    * @param models All the models participating in the transformation, in the order
    * declared by the Transformation parameters. This include src and target models.
    * Targets model can be empty or not, but can't be null.
    * Target model names are derived from the defaultTargetNames that should be
    * specified. An exception is thrown if this is not the case.
    */
   public void transform(ModelHelper[] models)
     throws EngineException, TransformationException
   {
    transform(models,   (String[])defaultTargetNames.toArray(new String[]{}) );
   }

   /**
    * 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 );
   }

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

  /**
   * getReaderWriterFactory
   *
   * @return ReaderWriterFactory
   */
  public ReaderWriterFactory getReaderWriterFactory()
  {
    if( readerWriterFactory == null )
    {
      readerWriterFactory = new SimpleReaderWriterFactory();
    }

   return readerWriterFactory;
  }

  /**
    * 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 Transformation.RuleSet, EngineLifeCycle {
     //protected Map rules = new HashMap();

     private volatile Collection topRules;

     public void addRule( Rule rule )
     {
      //System.out.println("ExecutableTransformation.addRule('"+ rule.getRuleName() +"')");
      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.ExecutableTransformation$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.