You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/12/22 15:56:19 UTC

svn commit: r893193 - in /lucene/mahout/trunk: collections-codegen-plugin/ collections-codegen-plugin/src/ collections-codegen-plugin/src/main/ collections-codegen-plugin/src/main/java/ collections-codegen-plugin/src/main/java/org/ collections-codegen-...

Author: srowen
Date: Tue Dec 22 14:56:19 2009
New Revision: 893193

URL: http://svn.apache.org/viewvc?rev=893193&view=rev
Log:
MAHOUT-226 (on behalf of Benson)

Added:
    lucene/mahout/trunk/collections-codegen-plugin/
    lucene/mahout/trunk/collections-codegen-plugin/pom.xml
    lucene/mahout/trunk/collections-codegen-plugin/src/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/
    lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/CodeGenerator.java
    lucene/mahout/trunk/math/src/main/java-templates/
    lucene/mahout/trunk/math/src/main/java-templates/org/
    lucene/mahout/trunk/math/src/main/java-templates/org/apache/
    lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/
    lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/
    lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/
    lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/KeyTypeValueTypeProcedure.java.t
Removed:
    lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleDoubleProcedure.java
    lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/DoubleIntProcedure.java
    lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/IntDoubleProcedure.java
    lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/IntIntProcedure.java

Added: lucene/mahout/trunk/collections-codegen-plugin/pom.xml
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/collections-codegen-plugin/pom.xml?rev=893193&view=auto
==============================================================================
--- lucene/mahout/trunk/collections-codegen-plugin/pom.xml (added)
+++ lucene/mahout/trunk/collections-codegen-plugin/pom.xml Tue Dec 22 14:56:19 2009
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+         xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <artifactId>mahout-parent</artifactId>
+    <groupId>org.apache.mahout</groupId>
+    <version>0.3-SNAPSHOT</version>
+    <relativePath>../maven</relativePath>
+  </parent>
+  <groupId>org.apache.mahout</groupId>
+  <artifactId>mahout-collection-codegen-plugin</artifactId>
+  <version>0.3-SNAPSHOT</version>
+  <packaging>maven-plugin</packaging>
+  <name>Maven Mojo to generate code for collections</name>
+  <url>http://lucene.apache.org/mahout/</url>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <scope>provided</scope>
+      <version>2.1.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <scope>provided</scope>
+      <version>2.1.0</version>
+    </dependency>
+    <dependency>
+    	<groupId>org.apache.maven.shared</groupId>
+	    <artifactId>file-management</artifactId>
+	    <version>1.2.1</version>
+	</dependency>
+   <dependency>
+    <groupId>velocity</groupId>
+    <artifactId>velocity</artifactId>
+    <version>1.5</version>
+	</dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>${junit.version}</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

Added: lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/CodeGenerator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/CodeGenerator.java?rev=893193&view=auto
==============================================================================
--- lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/CodeGenerator.java (added)
+++ lucene/mahout/trunk/collections-codegen-plugin/src/main/java/org/apache/mahout/collection_codegen/CodeGenerator.java Tue Dec 22 14:56:19 2009
@@ -0,0 +1,195 @@
+package org.apache.mahout.collection_codegen;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.model.fileset.FileSet;
+import org.apache.maven.shared.model.fileset.util.FileSetManager;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @description Generate java code with Velocity.
+ * @goal generate 
+ * @phase generate-sources
+ * @requiresProject true
+ */
+public class CodeGenerator extends AbstractMojo {
+  
+  private static final String[] NO_STRINGS = new String[] { null };
+  private static final Charset UTF8 = Charset.forName("utf-8");
+
+  /**
+   * @parameter default-value="${basedir}/src/test/java-templates"
+   */
+  private String testTemplateRoot;
+  
+  /**
+   * Path where the generated sources should be placed
+   * 
+   * @parameter expression="${cg.outputDirectory}"
+   *            default-value="${project.build.directory}/generated-test-sources"
+   */
+  private File testOutputDirectory;
+  
+  /**
+   * @parameter default-value="${basedir}/src/main/java-templates"
+   */
+  private String sourceTemplateRoot;
+  
+  /**
+   * Path where the generated sources should be placed
+   * 
+   * @parameter default-value="${project.build.directory}/generated-sources"
+   */
+   private File outputDirectory;
+  
+   /**
+    * Comma-separated list of value types.
+    * @parameter default-value="byte,char,int,long,float,double"
+    * @required
+    */
+   private String valueTypes;
+
+   /**
+    * Comma-separated list of key types.
+    * @parameter default-value="byte,char,int,long,float,double"
+    * @required
+    */
+   private String keyTypes;
+  
+  /**
+   * @parameter expression="${project}"
+   * @required
+   */
+  private MavenProject project;
+  private VelocityEngine velocityEngine;
+  
+  public void execute() throws MojoExecutionException {
+    File f = outputDirectory;
+    
+    if (!f.exists()) {
+      f.mkdirs();
+    }
+    
+    if (testOutputDirectory != null && !testOutputDirectory.exists()) {
+      testOutputDirectory.mkdirs();
+    }
+    
+    velocityEngine = new VelocityEngine();
+    // we want to use absolute paths.
+    velocityEngine.setProperty("file.resource.loader.path", "/");
+    try {
+      velocityEngine.init();
+    } catch (Exception e) {
+      throw new MojoExecutionException("Unable to initialize velocity", e);
+    }
+    
+    if (sourceTemplateRoot != null) {
+      runGeneration(sourceTemplateRoot, outputDirectory);
+    }
+    if (testTemplateRoot != null) {
+      runGeneration(testTemplateRoot, testOutputDirectory);
+    }
+    
+    if (project != null && outputDirectory != null && outputDirectory.exists()) {
+      project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
+  }
+  if (project != null && testOutputDirectory != null && testOutputDirectory.exists()) {
+      project.addTestCompileSourceRoot(testOutputDirectory.getAbsolutePath());
+  }
+
+  }
+
+  private void runGeneration(String thisSourceRoot, File thisOutputDirectory) {
+    FileSetManager fileSetManager = new FileSetManager();
+    FileSet fileSet = new FileSet();
+    fileSet.setDirectory(thisSourceRoot);
+    List<String> includes = new ArrayList<String>();
+    includes.add("**/*.java.t");
+    fileSet.setIncludes(includes);
+
+    String[] includedFiles = fileSetManager.getIncludedFiles(fileSet);
+    for (String template : includedFiles) {
+      File templateFile = new File(thisSourceRoot, template);
+      File thisTemplateOutputDirectory = new File(thisOutputDirectory, templateFile.getParentFile().getPath().substring(fileSet.getDirectory().length()));
+      thisTemplateOutputDirectory.mkdirs();
+      processOneTemplate(templateFile, thisTemplateOutputDirectory);
+    }
+  }
+
+  private void processOneTemplate(File templateFile, File thisOutputDirectory) {
+    boolean hasKey = templateFile.getName().contains("KeyType");
+    boolean hasValue = templateFile.getName().contains("ValueType");
+    String[] keys;
+    if (hasKey) {
+      keys = keyTypes.split(",");
+    } else {
+      keys = NO_STRINGS;
+    }
+    String[] values;
+    if (hasValue) {
+      values = valueTypes.split(",");
+    } else {
+      values = NO_STRINGS;
+    }
+    for (String key : keys) {
+      for (String value : values) {
+        String outputName = templateFile.getName().replaceFirst("\\.java\\.t$", ".java");
+        String keyCap = null;
+        VelocityContext vc = new VelocityContext();
+        if (key != null) {
+          keyCap = key.toUpperCase().charAt(0) + key.substring(1);
+          outputName = outputName.replaceAll("KeyType", keyCap);
+          vc.put("keyType", key);
+          vc.put("keyTypeCap", keyCap);
+        }
+        String valueCap = null;
+        if (value != null) {
+          valueCap = value.toUpperCase().charAt(0) + value.substring(1);
+          outputName = outputName.replaceAll("ValueType", valueCap);
+          vc.put("valueType", value);
+          vc.put("valueTypeCap", valueCap);
+        }
+        try {
+          Template template = velocityEngine.getTemplate(templateFile.getCanonicalPath());
+          File outputFile = new File(thisOutputDirectory, outputName);
+          getLog().info("Writing to " + outputFile.getAbsolutePath());
+          FileOutputStream fos = new FileOutputStream(outputFile);
+          OutputStreamWriter osw = new OutputStreamWriter(fos, UTF8);
+          template.merge(vc, osw);
+          osw.close();
+        } catch (Exception e) {
+          getLog().error(e);
+        }
+      }
+    }
+  }
+}

Added: lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/KeyTypeValueTypeProcedure.java.t
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/KeyTypeValueTypeProcedure.java.t?rev=893193&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/KeyTypeValueTypeProcedure.java.t (added)
+++ lucene/mahout/trunk/math/src/main/java-templates/org/apache/mahout/math/function/KeyTypeValueTypeProcedure.java.t Tue Dec 22 14:56:19 2009
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.mahout.math.function;
+
+/*
+Copyright 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+
+/**
+ * Interface that represents a procedure object: a procedure that takes two arguments and does not return a value.
+ *
+ */
+public interface ${keyTypeCap}${valueTypeCap}Procedure {
+
+  /**
+   * Applies a procedure to two arguments. Optionally can return a boolean flag to inform the object calling the
+   * procedure.
+   *
+   * <p>Example: forEach() methods often use procedure objects. To signal to a forEach() method whether iteration should
+   * continue normally or terminate (because for example a matching element has been found), a procedure can return
+   * <tt>false</tt> to indicate termination and <tt>true</tt> to indicate continuation.
+   *
+   * @param first  first argument passed to the procedure.
+   * @param second second argument passed to the procedure.
+   * @return a flag  to inform the object calling the procedure.
+   */
+  boolean apply(${keyType} first, ${valueType} second);
+}