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