root/Explorator/trunk/app/models/resource_set.rb @ 128

Revision 128, 4.0 KB (checked in by samuraraujo, 5 years ago)
Line 
1#This class represent a domain class where the resources are stored when
2#operated by the interface.
3#You can treat this class as a set where his elements are define by the
4#expression @expresion.
5#Author: Samur Araujo
6#Date: 25 jun 2008.
7
8class EXPLORATOR::Set < RDFS::Resource
9   include RenderHelper
10  self.class_uri = RDFS::Resource.new('http://www.tecweb.inf.puc-rio.br/ontologies/2008/explorator/01/core#Set')   
11  #:name - #an word character(in ascii code) that names the set.
12  #:rsid - resourceset id
13  #:expresion - a SemanticExpression instance.
14  #:resources - a array of RDFS::Resources
15  #:offset - used to paginate the set
16  #:pagination - the number of itens that a page should have.
17  attr_accessor  :resources 
18  #The constructor
19  #receive as parameter a SemanticExpression instance.
20  #'http://www.tecweb.inf.puc-rio.br/explorator/id/' + @rsid
21  def initialize(uri)
22    super(uri)         
23    @elements = Array.new   
24    if self.explorator::expression != nil           
25      #enable only the repositories necessary to perform the expression eval.
26      #setup_repositories()
27      self.expression=self.explorator::expression
28    end
29    self.save
30  end
31  def init(exp) 
32    $contextindex = 65 if $contextindex == nil
33    self.explorator::pagination=30
34    self.explorator::offset = 0
35   
36    #naming the set 
37    self.explorator::name=$contextindex.chr
38    #The set is named automacally, so a counter should be incremented here.
39    $contextindex +=1
40    #Reset the counter when the name reach the 'Z' letter (90 in ASCII).
41    $contextindex = 65 if $contextindex > 90
42    #add this set to the pool.
43    self.expression=exp
44    Thread.current[:application].add(self);
45  end 
46  #Returns an array of resources.
47  def resources   
48    return @elements.collect{|s,p,o| s}.compact.uniq 
49  end
50  def elements
51    return @elements
52  end
53  #setup repositories
54  def setup_repositories
55    r = self.explorator::repository   
56    r.each do |x|     
57      Repository.enable_by_title(x)
58    end
59  end
60  def set_repositories
61    #set the current repositories enable when the query was evaluated
62    r =  ConnectionPool.read_adapters.collect{|x| x.title}   
63    self.explorator::repository=r
64  end
65  #defines a new expression and reevaluates the expression.
66  def expression=(exp) 
67    puts '## Evaluating Expression ##'
68    puts exp
69   # begin           
70      #for avoid loop evaluation.
71      current_expression = self.explorator::expression
72      exp.gsub!("'" + self.to_s + "'",current_expression) if  current_expression != nil
73      #evaluates the expression     
74      x = eval(exp)     
75#      updates the expression
76      self.explorator::expression = exp
77      #  set_repositories()
78   # rescue     
79      #whether the expression is invalid.
80  #    raise ExploratorError.new('Expression could not be evaluated')
81  #  end   
82    #whether the expression is not a SemanticExpresion instance.
83    raise ExploratorError.new('The expression must be a SemanticExpression instance') if !x.instance_of? SemanticExpression
84    #return a sorted array of RDFS::Resources
85    @elements = x.result
86    #try to sort the array as a array of numbers
87#    begin
88#      #@resources.sort!{|a,b|a.to_f<=>b.to_f}
89#      @resources.sort!()
90#    rescue     
91#      puts 'ERRO SORTING'
92#      #whether the expression is invalid.     
93#    end   
94   
95     #@resources=sorting_resource(@resources)
96    @elements
97  end
98  def sorting_resource(resources)
99    sorted = Hash.new   
100    resources.each {|r|       
101      sorted[r] = render_resource(r)     
102    }     
103    y=sorted.sort{|a,b| a[1]<=>b[1]}     
104    y.collect{|x|  x[0]}
105  end
106  #defined the offset. this will be used for paginated the access to all resources.
107  def setWithOffset(offset)         
108    if offset != nil
109      self.explorator::offset = offset
110    end
111    self
112  end
113  def addFilter(filter)
114    self.expression=self.explorator::expression + '.'+ filter
115  end
116  def sum
117    self.expression=self.explorator::expression + '.'+  "filter('inject( 0 ) { |sum,x| sum+x.to_i } ')"
118   
119  end
120end
Note: See TracBrowser for help on using the browser.