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

Revision 111, 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 exp
68   # begin           
69      #for avoid loop evaluation.
70     current_expression = self.explorator::expression
71      exp.gsub!("'" + self.to_s + "'",current_expression) if  current_expression != nil
72      #evaluates the expression
73     
74      x = eval(exp)
75     
76#      updates the expression
77      self.explorator::expression = exp
78      #  set_repositories()
79   # rescue     
80      #whether the expression is invalid.
81  #    raise ExploratorError.new('Expression could not be evaluated')
82  #  end   
83    #whether the expression is not a SemanticExpresion instance.
84    raise ExploratorError.new('The expression must be a SemanticExpression instance') if !x.instance_of? SemanticExpression
85    #return a sorted array of RDFS::Resources
86    @elements = x.result
87    #try to sort the array as a array of numbers
88#    begin
89#      #@resources.sort!{|a,b|a.to_f<=>b.to_f}
90#      @resources.sort!()
91#    rescue     
92#      puts 'ERRO SORTING'
93#      #whether the expression is invalid.     
94#    end   
95   
96     #@resources=sorting_resource(@resources)
97    @elements
98  end
99  def sorting_resource(resources)
100    sorted = Hash.new   
101    resources.each {|r|       
102      sorted[r] = render_resource(r)     
103    }     
104    y=sorted.sort{|a,b| a[1]<=>b[1]}     
105    y.collect{|x|  x[0]}
106  end
107  #defined the offset. this will be used for paginated the access to all resources.
108  def setWithOffset(offset)         
109    if offset != nil
110      self.explorator::offset = offset
111    end
112    self
113  end
114  def addFilter(filter)
115    self.expression=self.explorator::expression + '.'+ filter
116  end
117  def sum
118    self.expression=self.explorator::expression + '.'+  "filter('inject( 0 ) { |sum,x| sum+x.to_i } ')"
119   
120  end
121end
Note: See TracBrowser for help on using the browser.