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

Revision 128, 4.6 KB (checked in by samuraraujo, 5 years ago)
Line 
1#This class is a pool of sets. Each set should has a set of RDFS::Resources
2#This class is a singleton pattern.
3#Author: Samur Araujo
4#Date: 25 jun 2008.
5class EXPLORATOR::Application < RDFS::Resource
6  self.class_uri = RDFS::Resource.new('http://www.tecweb.inf.puc-rio.br/ontologies/2008/explorator/01/core#Application')   
7  def initialize(uri)
8    super(uri)                                                   
9  end
10  def is_current?()
11    if self.to_s == Application.instance.to_s         
12      return true
13    end
14    false
15  end                                 
16end
17class Application 
18   attr_accessor :uri
19
20  def initialize(id)   
21    super()
22    @uri = 'http://www.tecweb.inf.puc-rio.br/application/id/'+  id + '/'
23  end
24  def instance()   
25    if @instance == nil     
26      @cache = Hash.new
27      @instance=EXPLORATOR::Application.new(@uri + 'default')
28      @instance.explorator::uuid=@uri
29      @instance.rdfs::label='Default'
30      @instance.save
31    end 
32    @instance
33  end
34  #checks whether it is the current application
35 
36  #create a new entry in the pool
37  def add(resourceset)   
38    raise ExploratorError.new('Must be a ResourceSet instance') if !resourceset.instance_of? EXPLORATOR::Set
39    puts '## Adding to cache ##'
40    puts resourceset.uri
41    @cache['<' + resourceset.uri + '>'] = resourceset
42   
43    if instance.explorator::set == nil
44      instance.explorator::set = [resourceset]
45    else
46      instance.explorator::set = instance.all_explorator::set | [resourceset]
47    end
48  end   
49  #get a resource set
50  def get(uri)       
51    #EXPLORATOR::Set.new(uri)
52    puts '## Retrieving from the cache ##'
53    puts uri
54    @cache[uri]
55  end   
56  ##verifies whether the set was added in the pool
57  def is_set?(uri)   
58    if instance.explorator::set == nil
59      return false
60    end           
61    instance.all_explorator::set.collect {|x| x.to_s }.include?(uri)     
62  end   
63  #gets the list of all sets from the current application
64  def sets 
65    all = instance.all_explorator::set       
66    if all == nil
67      Array.new       
68    end
69    all
70  end
71  #save the environment as an application. This will creates an EXPLORATOR::Application instance and add all
72  #set to it.
73  def create(name)     
74    xapp = EXPLORATOR::Application.new(@uri + name)   
75    xapp.save
76    resources = instance.all_explorator::set
77    xapp.explorator::set = resources
78    xapp.rdfs::label=name 
79    xapp.explorator::uuid=@uri
80    # $instance = xapp     
81    @instance=xapp
82  end
83  #remove all set from the pool
84  def clear     
85    #remove each set by itself
86    s= instance.all_explorator::set
87    if s != nil
88      s.each do |resource|
89        apps = Query.new.distinct(:s).where(:s,:p,resource).execute         
90        if apps.size == 1 
91          SemanticExpression.new.delete(resource,nil,nil)
92        end
93      end
94    end
95    @cache = Hash.new
96    #remove the resources from the application
97    instance.explorator::set = []
98    instance.save     
99  end     
100  #load a saved application
101  def load(uri)   
102    puts "Loading ...  " + uri
103    #$instance = EXPLORATOR::Application.new(uri) 
104    @instance=EXPLORATOR::Application.new(uri) 
105    #puts  instance.all_explorator::set.size
106  end
107  #deletes an application
108  def delete(uri)   
109    #can not delete the current application
110    if uri == instance.to_s       
111      return false
112    end
113    app = EXPLORATOR::Application.new(uri)     
114    app.all_explorator::set.each do |resource|
115      #delete the set only whether is only referenced by the current application
116      apps = Query.new.distinct(:s).where(:s,:p,resource).execute       
117      if apps.size == 1 
118        SemanticExpression.new.delete(resource,nil,nil)
119      end       
120    end
121    #remove the resources from the application
122    SemanticExpression.new.delete(app,nil,nil)     
123    true
124  end   
125  #remove a specific resource set from the pool
126  def remove(uri)     
127    @cache[uri]=nil
128    resource = EXPLORATOR::Set.new(uri)     
129    #before remove store the set expression.     
130    exp = resource.explorator::expression
131    #remove the set from the application
132    instance.explorator::set = instance.all_explorator::set - [resource] 
133    #replace the expresssion in all expressions that used the set as parameter.
134    #this is necessary for avoid missing reference.
135    instance.all_explorator::set.each do |expset| 
136      expset.explorator::expression=expset.explorator::expression.gsub("'" + uri + "'",exp) 
137    end
138    #remove the set by itself
139    apps = Query.new.distinct(:s).where(:s,:p,resource).execute       
140    if apps.size == 1 
141      SemanticExpression.new.delete(resource,nil,nil)
142    end     
143    instance.save
144  end 
145end
Note: See TracBrowser for help on using the browser.