1   /*
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 26/10/2001
10   *
11   *  $Id: PRPersistence.java,v 1.2 2001/10/29 17:11:26 valyt Exp $
12   *
13   */
14  package gate.util.persistence;
15  
16  import gate.*;
17  import gate.creole.*;
18  import gate.util.*;
19  import gate.persist.PersistenceException;
20  
21  import java.util.*;
22  
23  
24  public class PRPersistence extends ResourcePersistence {
25    /**
26     * Populates this Persistence with the data that needs to be stored from the
27     * original source object.
28     */
29    public void extractDataFromSource(Object source)throws PersistenceException{
30      if(! (source instanceof ProcessingResource)){
31        throw new UnsupportedOperationException(
32                  getClass().getName() + " can only be used for " +
33                  ProcessingResource.class.getName() +
34                  " objects!\n" + source.getClass().getName() +
35                  " is not a " + ProcessingResource.class.getName());
36      }
37  
38      super.extractDataFromSource(source);
39      Resource res = (Resource)source;
40  
41      ResourceData rData = (ResourceData)
42                           Gate.getCreoleRegister().get(res.getClass().getName());
43      if(rData == null) throw new PersistenceException(
44                                  "Could not find CREOLE data for " +
45                                  res.getClass().getName());
46  
47      //now get the runtime params
48      ParameterList params = rData.getParameterList();
49      try{
50        //get the values for the init time parameters
51        runtimeParams = Factory.newFeatureMap();
52        //this is a list of lists
53        Iterator parDisjIter = ((List)params.getRuntimeParameters()).iterator();
54        while(parDisjIter.hasNext()){
55          Iterator parIter = ((List)parDisjIter.next()).iterator();
56          while(parIter.hasNext()){
57            Parameter parameter = (Parameter)parIter.next();
58            String parName = parameter.getName();
59            Object parValue = res.getParameterValue(parName);
60            ((Map)runtimeParams).put(parName,parValue);
61          }
62        }
63        runtimeParams = PersistenceManager.
64                        getPersistentRepresentation(runtimeParams);
65      }catch(ResourceInstantiationException rie){
66        throw new PersistenceException(rie);
67      }
68    }
69  
70    /**
71     * Creates a new object from the data contained. This new object is supposed
72     * to be a copy for the original object used as source for data extraction.
73     */
74    public Object createObject()throws PersistenceException,
75                                       ResourceInstantiationException{
76      Object res = super.createObject();
77      //now add the runtime parameters
78      if(runtimeParams != null){
79        runtimeParams = PersistenceManager.
80                        getTransientRepresentation(runtimeParams);
81        ((Resource)res).setParameterValues((FeatureMap)runtimeParams);
82      }
83  
84      return res;
85    }
86  
87    private Object runtimeParams;
88    static final long serialVersionUID = 2031381604712340552L;
89  }