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

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