Class: CqlRuby::CqlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/cql_ruby/cql_generator.rb

Overview

A generator that produces random CQL queries. <P> Why is that useful? Mainly to produce test-cases for CQL parsers (including the CQLParser class in this package): you can generate a random search tree, render it to XCQL and remember the result. Then decompile the tree to CQL, feed the generated CQL to the parser of your choice, and check that the XCQL it comes up with is the same what you got from your initial rendering. <P> This code is based on the same grammar as the CQLParser class in this distribution - there is a generate_x() method for each grammar element X.

See Also:

  • href="http://zing.z3950.org/cql/index.html" >http://zing.z3950.org/cql/index.html

Version:

  • $Id: CQLGenerator.java,v 1.9 2007/07/03 15:41:35 mike Exp $

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ CqlGenerator

Returns a new instance of CqlGenerator.



22
23
24
25
26
# File 'lib/cql_ruby/cql_generator.rb', line 22

def initialize( params )
  @debug_mode = params[ :debug ] || false
  @params = params
  srand( params[ :seed ] ) if params and params[ :seed ]
end

Instance Attribute Details

#debug_modeObject

Returns the value of attribute debug_mode.



20
21
22
# File 'lib/cql_ruby/cql_generator.rb', line 20

def debug_mode
  @debug_mode
end

#paramsObject

Returns the value of attribute params.



20
21
22
# File 'lib/cql_ruby/cql_generator.rb', line 20

def params
  @params
end

#rndObject

Returns the value of attribute rnd.



20
21
22
# File 'lib/cql_ruby/cql_generator.rb', line 20

def rnd
  @rnd
end

Instance Method Details

#debug(s) ⇒ Object



28
29
30
# File 'lib/cql_ruby/cql_generator.rb', line 28

def debug( s )
  puts( "DEBUG: #{s}" ) if @debug_mode
end

#generate_base_relationObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/cql_ruby/cql_generator.rb', line 84

def generate_base_relation
  return "=" if maybe( :equals_relation )
  return generate_numeric_relation if maybe( :numeric_relation )
  case rand(3)
    when 0 then index = "exact"
    when 1 then index = "all"
    when 2 then index = "any"
  end
  index
end

#generate_cql_queryObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cql_ruby/cql_generator.rb', line 32

def generate_cql_query
  return generate_search_clause  unless maybe( :complex_query )
  
  node1 = generate_cql_query
  node2 = generate_search_clause
  
  if maybe( :proxOp ) 
    # TODO: generate proximity nodes
  else
    case rand( 3 )
    when 0 then return CqlAndNode.new( node1, node2, ModifierSet.new( "and" ) )
    when 1 then return CqlOrNode.new( node1, node2, ModifierSet.new( "or" ) )
    when 2 then return CqlNotNode.new( node1, node2, ModifierSet.new( "or" ) )
    end      
  end
  
  generate_search_clause
end

#generate_indexObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cql_ruby/cql_generator.rb', line 61

def generate_index
  if rand(2) == 0 
    case rand(3)
      when 0 then index = "dc.author"
      when 1 then index = "dc.title"
      when 2 then index = "dc.subject"
    end
  else
    case rand(4)
      when 0 then index = "bath.author"
      when 1 then index = "bath.title"
      when 2 then index = "bath.subject"
      when 3 then index = "foo>bar"
    end
  end
  index
end

#generate_numeric_relationObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/cql_ruby/cql_generator.rb', line 110

def generate_numeric_relation
  case rand(6)
    when 0 then return "<"
    when 1 then return ">"
    when 2 then return "<="
    when 3 then return ">="
    when 4 then return "<>"
    when 5 then return "="
  end
end

#generate_relationObject



79
80
81
82
# File 'lib/cql_ruby/cql_generator.rb', line 79

def generate_relation
  base = generate_base_relation
  CqlRelation.new( base )
end

#generate_search_clauseObject



51
52
53
54
55
56
57
58
59
# File 'lib/cql_ruby/cql_generator.rb', line 51

def generate_search_clause
  return generate_cql_query if maybe( :complex_clause )
  
  index = generate_index
  relation = generate_relation
  term = generate_term
  
  CqlTermNode.new( index, relation, term )
end

#generate_termObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cql_ruby/cql_generator.rb', line 95

def generate_term
  case rand(10)
    when 0 then return "cat"
    when 1 then return "\"cat\""
    when 2 then return "comp.os.linux"
    when 3 then return "xml:element"
    when 4 then return "<xml.element>"
    when 5 then return "prox/word/>=/5"
    when 6 then return ""
    when 7 then return "frog fish"
    when 8 then return "the complete dinosaur"
    when 9 then return "foo*bar"
  end
end

#maybe(key) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/cql_ruby/cql_generator.rb', line 121

def maybe( key )
  probability = @params[ key ] || ".1"
  
  dice = rand
  threshold = probability.to_f
  res = dice < threshold
  
  debug( "dice=#{dice} vs #{threshold} = #{res.to_s}" )
  res
end