• 2007-07-11

    Google Technology - [读书]

    Tag:读书 随笔
    和一个朋友讨论技术的学习,都有什么技术需要一个技术人员储备的,无意间朋友告诉我,Google关心的技术都有:
    • algorithms
    • artificial intelligence
    • compiler optimization
    • computer architecture
    • computer graphics
    • data compression
    • data mining
    • file system design
    • genetic algorithms
    • information retrieval
    • machine learning
    • natural language processing
    • operating systems
    • profiling
    • robotics
    • text processing
    • user interface design
    • web information retrieval
    • and more!
    参见http://labs.google.com/,细节http://labs.google.com/papers.html
    中文的http://labs.google.cn/并没有这个。
  • 2007-07-11

    温习Annotation - [Java]

    Tag:Java
    现在annotation正在改变着java developer的一些开发生活,各种框架都在充分应用annotation来简化开发,我们来看看annotation到底是怎么一回事?
    为什么引入annotation?
    由于在Java的一些实际开发中,已经有了一些annotation出现的雏形.比方说webservice使用一些tag自动生成一些接口和实现,EJB的deployment descriptor对象,transient修饰符代表不能被serialization,@deprecated等。那么他们的共用特性是什么呢?尽管这些数据不会直接对Java的语义产生影响,但是是通过工具、库或者运行时程序的语义来散发影响力的。以这种方式出现的新功能称之为annotation机制。所以Java是在增强Java语言支持易于开发的背景下提出annotation机制的,annotation对于已有的Java机制的影响力主要在于Java source files、Java class files和运行时的reflection;主要关联java classes,interfaces,methods和fields。(,或者能够能够被javac compiler或者其他的工具读取、或者作为配置项能够被存贮在class文件中、或者在运行时用Java reflection API检测的到。)
    虽然在第一天提出annotation这个特性,就一直在强调:annotion易于使用,但是应用开发者无须自己定义annotaion类型。但是不论是更好的使用现有的annotation类型,还是便于有朝一日自己确实需要扩展annotion类型,或者更好的理解开发模型,更好的理解程序的开发运行,都非常有必要了解一些annotation的。

    我们看一下Meta-annotations,看到底如何自己定义一个annnotation类型。Meta-annotations主要包括四个部分:
    Target
    Retention
    Documented
    Inherited
    Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.
     Target(ElementType.TYPE)-can be applied to any element of a class
     Target(ElementType.FIELD)-can be applied to a field or property
     Target(ElementType.METHOD)-can be applied to a method level annotation
     Target(ElementType.PARAMETER)-can be applied to the parameters of a method
     Target(ElementType.CONSTRUCTOR)-can be applied to constructors
     Target(ElementType.LOCAL_VARIABLE)-can be applied to local variables
     Target(ElementType.ANNOTATION_TYPE)-indicates that the declared type itself is an annotation typ
    Retention用来指定annotation类型保留在哪儿和多长时间。
     RetentionPolicy.SOURCE-Annotations with this type will be by retained only at the source level and will be ignored by the compiler
     RetentionPolicy.CLASS-Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM
     RetentionPolicy.RUNTIME-Annotations with this type will be retained by the VM so they can be read only at run-time
    Documented用来指定这个annotation类型应当被javadoc工具文档化使用。
    Inherited用来指定使用这个类型的类是个继承来的。
    举一个简单的例子:
    测试用例:
    public class AnnotationTestableTeseCase {

        @Test
        public void go() throws SecurityException, ClassNotFoundException {
            int passed = 0, failed = 0;
            for (Method m : Class.forName(Foo.class.getName()).getMethods()) {
                if (m.isAnnotationPresent(Testable.class)) {
                    try {
                        m.invoke(null);
                        passed++;
                    } catch (Throwable ex) {

                        failed++;
                    }
                }
            }
            Assert.assertEquals(2, passed);
            Assert.assertEquals(2, failed);
        }
    }
    Annotation类型定义类:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Testable {
    }
    使用Testable类型的客户端代码:
    public class Foo {
        @Testable
        public static void m1() {
        }

        public static void m2() {
        }

        @Testable
        public static void m3() {
            throw new RuntimeException("Boom");
        }

        public static void m4() {
        }

        @Testable
        public static void m5() {
        }

        public static void m6() {
        }

        @Testable
        public static void m7() {
            throw new RuntimeException("Crash");
        }

        public static void m8() {
        }
    }
    Okey!
    参考:
    http://java.sun.com/developer/technicalArticles/releases/j2se15/
    http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
    http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html
    http://www.developer.com/java/other/article.php/10936_3556176_3
  • 2007-07-11

    第一章节 - [读书]

    Tag:Ruby 读书
    先从一段Ruby程序开始,即欧几里德最大公约数的算法实现:
    测试用例当然要先,
    require 'test/unit'
    require './one/Apple'

    class TestApple < Test::Unit::TestCase

      def test_cal
        ys = Apple.new
        assert_equal(5,ys.gcd(35,25))
        assert_equal(50,ys.gcd(100,50))
        assert_equal(9,ys.gcd(27,18))
        assert_equal(5,ys.gcd(5,10))
        assert_equal(5,ys.gcd(5,0))
        assert_equal(-5,ys.gcd(-5,0))
        assert_raise(ArgumentError){ys.gcd(0,0)}
        assert_equal(23,ys.gcd(0,23))
      end
    end
    然后就是算法的实现代码了:
    class Apple

      def gcd(arg1=0, arg2=0)
        if((arg1==0)&&(arg2==0))
          raise ArgumentError, "arg1 and arg2 are 0.", caller
        else
          gcdImpl(arg1,arg2)
        end    
      end
     
      def gcdImpl(arg1=0, arg2=0)
        if(arg2 == 0)
          return arg1
        elsif
          temp=arg1%arg2
          if(temp == 0)
            return arg2
          else
            gcdImpl(arg2,temp)
          end
        end
      end

    end

    从一个算法的描述来实现一个算法,仅仅是编写一段程序而已。而对于算法这种问题的程序化解决方案而言,需要如何一回事情呢?比方说,最大公约数到底是怎么一回事,5和0的最大公约数怎么可能是5呢?所以要学习算法到底是怎么一回事,需要探讨更多的问题?
    对于问题的理解、性能、选择、数据结构、如何设计、算法的描述、正确性证明、分析、代码实现等等一系列单元。当程序实现仅仅是第一反应的时候,问题理解、分析、设计、验证就变得异常重要了。
    算法所拥有的通用问题类型:排序、查找、字符串处理、图问题、组合问题、几何问题、数值问题;都是一些已经相当成熟的问题类型。很多使我们常常听说或者涉及的,也是算法设计和分析频繁的地方,算是很多的“肩膀”吧,算法都是都是在这里站立起来的。
  • 2007-06-28

    学习算法 - [读书]

    Tag:读书
    大学修的计算机专业,毕业之时,《数据结构》《计算方法》《操作系统》《编译原理》等如同课本一样,永远的被打包起来。工作以来,一直在做Java的开发,Web开发和服务端应用,总是零星的涉及到些许数组、链表、树等数据结构,也主要以线性数据结构为主;算法基本上就是“经典”的数据结构书上所讨论的几种算法,JDK都有实现,在应用中早已无需我们思考太多。重要的是我们要把应用按着需求实现了,重要的是我们一步一步把应用带向我们认为简单务实美的方向前进。闲暇思索,对于虚拟机的看法不过是面试的话语;对于开发的认识同样如此,多是面试的谈资;对于一个更有经验的Java应用开发者,能够在开发提供一套解决方案,组合解决问题的模式积攒起来一段可预期时间内解决一系列问题,也许有人将这归之为架构师,谁又能说这不是一条Java应用架构师之路呢?有一天,恍然大悟,其实Java应用开发者比拼的是各自的开发经验、领域知识、开阔的思路、强悍的执著;事情总没有唯一的,以前和dreamhead讨论过业务,当然也有无数的把应用开发者的需求作为业务的Java开发“仁人志士”。而对于一个从事了几年Java应用开发的人来说,那么我们的激情下一个积攒点在哪儿呢?虽然刚开始接触计算机,就知道一句名言“程序=算法+数据结构”,现实中这几年算法对于我,如同鲁迅先生笔中的故乡,“愈远了”,“渐渐远离了我”,“模糊了”。
    人生有多少个十年,人生有几多青春,对于一个个体,又有几多沉淀?
    一如我刚毕业的青春年少无知,对于技术这个词汇激情澎湃,几乎认为是自己生命的全部。对于人来说,最重要的不是你能否学技术,学了多少技术,掌握了多少技术功底;而是一个人前进的方向是什么,你的人生想要的是什么,你的人生在计算机这个舞台你如何定位思考、如何的目标的?想好了再前行,前行了就不再犹豫。
    算法是否总给人一种严肃的科学的概念,我相信,算法并不严肃,可以嬉笑怒骂,可以轻轻松松的和我们很多一线开发人员在一起。
    非常感谢作者Anay Levitin著书《Introduction to the Design and Analysis of Algorithms》,感谢译者潘彦翻译这么好的书籍《算法设计与分析基础》,特别感谢dreamhead五一来沈阳一聚并送这本书给我。
    虽然序言前言部分往往都有精髓的思想语句,然后对于算法无知的我决定直切正题,从绪论开始阅读。
    为什么要学习算法?
    由于“微积分”“算法”被并成为科学殿堂两大宝石的论断,很为大家熟知,至于算法是如何的英明神武,在现实中也许需要细细体会,我并未有多少深深体会悍然汗然。
    作者直接扔出了为什么学习算法这个话题,一个是计算机专业人士必备,另外一个是能开发人们的分析能力。如果能有选择,我宁愿选择第二个,我想必备意味着用这个东西现在或者未来要解决切实的问题,我现在并不是先有棘手的问题--没有棘手的问题而去学习本身未必不是一个棘手的问题;如果籍此能够在具备计算机人士必须具备的基础之外,成就一下自己分析能力的增强,真是不错。
    什么是算法?
    该书中,对于算法的定义是“一系列解决问题的清晰指令,即对于符合一定规范的输入,能够在有限时间内获得所要求的输出。”可以看到算法,有输入,有输出,有指令,当然有执行这些指令的环境,也许第一个想到是计算机,对,计算机,但是也许是从事数学计算的人。定义等同于算法是解决问题的办法。
    所以既然是解决问题,当然有问题域/值域;算法本身如何表达描述,算法自己的优劣之分;等等。
    第一个例子是关于“计算两个整数的最大公约数”问题,记得在SICP一书中也阐述了这个例子。解决这个问题首先寻找的是“最大公约数的定义”,并仔细描述了欧几里得算法、连续整数检测算法、“艾拉托色尼筛”算法;并采用了结构化描述和伪码两种方式描述算法细节。
    该书以如此简洁的言语和例子为我们打开了算法的道路。在接下来的一章节,会首先看到“算法是问题的程序化解决方案”,是的,我们就是要去寻找问题的程序化解决方案的。
  • 简单开始
    下载并且安装Ruby 1.8.4;
    RubyGems;
    通过命令行安装所有的Rails和其依赖:
    gem install rails --include-dependencies
    在用户工作目录运行命令行,即创建第一个rails的模板:
    rails depot
    cd depot
    ruby script/server
    在浏览器打开http://localhost:3000即可发现第一个rails的web应用已经开始run起来了。

    然后我参考书籍《Agile Web Development with Rails》2nd Edition的第一个例子一步一步的动手写了第一个ROR程序。(具体的过程不详细记录了.)
    从数据库准备、数据库的配置、创建表、migrate、创建controller和maintenance、add missing column、添加validation功能、使用scaffold、定制html、装填unit test数据。整个例子的思路比较清晰,从数据库、数据访问、业务、controller、展示;给我留下深刻的影响的是:自动生成功能、内嵌web server、支持数据库配置,支持应用的模板、支持mvc模板等,所以让人似乎感觉到一些DSL的色彩。

    做一个复杂一些的例子
    数据库准备
    支持数据库Mysql,从http://dev.mysql.com/downloads/mysql/5.0.html#downloads下载数据库Mysql安装,并且下载Mysql Administrator作为数据库管理工具。
    创建Schema为:depot_dev
    在文件./config/database.yml中对数据库进行配置:
    development:
      adapter: mysql
      database: depot_dev
      username: depot
      password: depot
      host: localhost
    测试配置:rake db:migrate
    如果配置不正确在控制台会出现错误的日志。
    创建Products的Model和Table如下:
    D:\IDE\rails-exampe\depot>ruby script/generate model product
          exists  app/models/
          exists  test/unit/
          exists  test/fixtures/
          create  app/models/product.rb
          create  test/unit/product_test.rb
          create  test/fixtures/products.yml
          create  db/migrate
          create  db/migrate/001_create_products.rb
    在./db/migrate/001_create_products.rb用类似DDL的形式表达表的结构:
    class CreateProducts < ActiveRecord::Migration
      def self.up
        create_table :products do |t|
          t.column :title,       :string
          t.column :description, :text
          t.column :image_url,   :string
        end
      end

      def self.down
        drop_table :products
      end
    end
    Okey,那就Migrate吧:
    D:\IDE\rails-exampe\depot>rake db:migrate
    D:0:Warning: require_gem is obsolete.  Use gem instead.
    (in D:/IDE/rails-exampe/depot)
    == CreateProducts: migrating ==================================================
    -- create_table(:products)
       -> 0.0780s
    == CreateProducts: migrated (0.0780s) =========================================
    数据库一切搞定,下一步开始应用层了,首先创建Controller:
    D:\IDE\rails-exampe\depot>ruby script/generate controller admin
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/admin
          exists  test/functional/
          create  app/controllers/admin_controller.rb
          create  test/functional/admin_controller_test.rb
          create  app/helpers/admin_helper.rb
    创建Maintenance应用:
    编辑app/controller/admin_controller.rb
    class AdminController < ApplicationController
      scaffold :product
    end
    启动ruby script/server,通过浏览器就可以浏览这个Rail scaffolds的应用了。
    迭代2:Add a missing column
    D:\IDE\rails-exampe\depot>ruby script/generate migration add_price
          exists  db/migrate
          create  db/migrate/002_add_price.rb
    编辑db/migrate/002_add_price.rb
    class AddPrice < ActiveRecord::Migration
      def self.up
        add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0
      end

      def self.down
        remove_column :products, :price
      end
    end
    D:\IDE\rails-exampe\depot>rake db:migrate
    D:0:Warning: require_gem is obsolete.  Use gem instead.
    (in D:/IDE/rails-exampe/depot)
    == AddPrice: migrating ========================================================
    -- add_column(:products, :price, :decimal, {:default=>0, :precision=>8, :scale=>
    2})
       -> 0.2650s
    == AddPrice: migrated (0.2650s) ===============================================
    一切Okey,第二个迭代也已经结束:)
    第三个迭代:Validate!
    编辑app/model/product.rb文件:
    class Product < ActiveRecord::Base
      validates_presence_of :title, :description, :image_url
      validates_numericality_of :price
      validates_uniqueness_of :title
      validates_format_of :image_url,
                          :with    => %r{\.(gif|jpg|png)$}i,
                          :message => "must be a URL for a GIF, JPG, or PNG image"

      protected
      def validate
        errors.add(:price, "should be at least 0.01") if price.nil? ||  price < 0.01
      end
    end
    第四个迭代:Prettier Listings
    创建静态的scaffold如下:
    D:\IDE\rails-exampe\depot>ruby script/generate scaffold product admin
          exists  app/controllers/
          exists  app/helpers/
          exists  app/views/admin
          exists  app/views/layouts/
          exists  test/functional/
      dependency  model
          exists    app/models/
          exists    test/unit/
          exists    test/fixtures/
            skip    app/models/product.rb
       identical    test/unit/product_test.rb
       identical    test/fixtures/products.yml
          create  app/views/admin/_form.rhtml
          create  app/views/admin/list.rhtml
          create  app/views/admin/show.rhtml
          create  app/views/admin/new.rhtml
          create  app/views/admin/edit.rhtml
    overwrite app/controllers/admin_controller.rb? [Ynaqd] y
           force  app/controllers/admin_controller.rb
    overwrite test/functional/admin_controller_test.rb? [Ynaqd] y
           force  test/functional/admin_controller_test.rb
       identical  app/helpers/admin_helper.rb
    overwrite app/views/layouts/admin.rhtml? [Ynaqd] y
           force  app/views/layouts/admin.rhtml
          create  public/stylesheets/scaffold.css
    定制list.rhtml:
    <div id="product-list">
      <h1>Product Listing</h1>

      <table cellpadding="5" cellspacing="0">
      <% for product in @products %>
        <tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">

          <td>
            <img class="list-image" src="<%= product.image_url %>"/>
          </td>

          <td width="60%">
            <span class="list-title"><%= h(product.title) %></span><br />
            <%= h(truncate(product.description, 80)) %>
          </td>

          <td class="list-actions">
            <%= link_to 'Show', :action => 'show', :id => product %><br/>
            <%= link_to 'Edit', :action => 'edit', :id => product %><br/>
            <%= link_to 'Destroy', { :action  => 'destroy', :id => product },
                                     :confirm => "Are you sure?",
                                     :method  => :post %>
          </td>
        </tr>
      <% end %>
      </table>
    </div>

    <%=  if @product_pages.current.previous
           link_to("Previous page", { :page => @product_pages.current.previous })
         end
    %>
    <%= if @product_pages.current.next
          link_to("Next page", { :page => @product_pages.current.next })
        end
    %>

    <br />

    <%= link_to 'New product', :action => 'new' %>
    增加测试数据:
    D:\IDE\rails-exampe\depot>ruby script/generate migration add_test_data
          exists  db/migrate
          create  db/migrate/003_add_test_data.rb
    编辑db/migrate/003_add_test_data.rb:
    class AddTestData < ActiveRecord::Migration
      def self.up
        Product.delete_all
        Product.create(:title => 'Pragmatic Version Control',
          :description =>
          %{<p>
            This book is a recipe-based approach to using Subversion that will
            get you up and running quickly--and correctly. All projects need
        version control: it's a foundational piece of any project's
        infrastructure. Yet half of all project teams in the U.S. don't use
        any version control at all. Many others don't use it well, and end
        up experiencing time-consuming problems.
          </p>},
        :image_url => '/images/svn.jpg',
        :price => 28.50)
      end

      def self.down
        Product.delete_all
      end
    end
    安装测试数据:
    D:\IDE\rails-exampe\depot>rake db:migrate
    D:0:Warning: require_gem is obsolete.  Use gem instead.
    (in D:/IDE/rails-exampe/depot)
    == AddTestData: migrating =====================================================
    == AddTestData: migrated (0.2030s) ============================================
    编辑Layouts:app/views/layouts/admin.rhtml
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
      <title>Admin: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold', 'depot' %>
    </head>
    <body>
    <p style="color: green"><%= flash[:notice] %></p>
    <%= yield :layout %>
    </body>
    </html>
    编辑:app/views/admin/list.rhtml
    <div id="product-list">
      <h1>Product Listing</h1>

      <table cellpadding="5" cellspacing="0">
      <% for product in @products %>
        <tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">

          <td>
            <img class="list-image" src="<%= product.image_url %>"/>
          </td>

          <td width="60%">
            <span class="list-title"><%= h(product.title) %></span><br />
            <%= h(truncate(product.description, 80)) %>
          </td>

          <td class="list-actions">
            <%= link_to 'Show', :action => 'show', :id => product %><br/>
            <%= link_to 'Edit', :action => 'edit', :id => product %><br/>
            <%= link_to 'Destroy', { :action  => 'destroy', :id => product },
                                     :confirm => "Are you sure?",
                                     :method  => :post %>
          </td>
        </tr>
      <% end %>
      </table>
    </div>

    <%=  if @product_pages.current.previous
           link_to("Previous page", { :page => @product_pages.current.previous })
         end
    %>
    <%= if @product_pages.current.next
          link_to("Next page", { :page => @product_pages.current.next })
        end
    %>

    <br />

    <%= link_to 'New product', :action => 'new' %>
    http://www.rubyonrails.org/down
    书籍《Agile Web Development with Rails》2nd Edition

    另,JSR 315: Java Servlet 3.0 Specification under review at JCP:http://jcp.org/en/jsr/detail?id=315可以看到Servlet 3.0的规范中要涉及到的一些东西。