Search in sources :

Example 1 with ParameterList

use of gate.creole.ParameterList in project gate-core by GateNLP.

the class ResourcePersistence method extractDataFromSource.

@Override
public void extractDataFromSource(Object source) throws PersistenceException {
    if (!(source instanceof Resource)) {
        throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + Resource.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + Resource.class.getName());
    }
    Resource res = (Resource) source;
    resourceType = res.getClass().getName();
    resourceName = ((NameBearer) res).getName();
    ResourceData rData = Gate.getCreoleRegister().get(resourceType);
    if (rData == null)
        throw new PersistenceException("Could not find CREOLE data for " + resourceType);
    ParameterList params = rData.getParameterList();
    try {
        // get the values for the init time parameters
        initParams = Factory.newFeatureMap();
        // this is a list of lists
        Iterator<List<Parameter>> parDisjIter = params.getInitimeParameters().iterator();
        while (parDisjIter.hasNext()) {
            Iterator<Parameter> parIter = parDisjIter.next().iterator();
            while (parIter.hasNext()) {
                Parameter parameter = parIter.next();
                String parName = parameter.getName();
                Object parValue = res.getParameterValue(parName);
                if (storeParameterValue(parValue, parameter.getDefaultValue())) {
                    ((FeatureMap) initParams).put(parName, parValue);
                }
            }
        }
        initParams = PersistenceManager.getPersistentRepresentation(initParams);
        // get the features
        if (res.getFeatures() != null) {
            features = Factory.newFeatureMap();
            ((FeatureMap) features).putAll(res.getFeatures());
            features = PersistenceManager.getPersistentRepresentation(features);
        }
    } catch (ResourceInstantiationException | ParameterException rie) {
        throw new PersistenceException(rie);
    }
}
Also used : ResourceData(gate.creole.ResourceData) Resource(gate.Resource) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) ParameterList(gate.creole.ParameterList) Parameter(gate.creole.Parameter) ParameterList(gate.creole.ParameterList) List(java.util.List) ParameterException(gate.creole.ParameterException)

Example 2 with ParameterList

use of gate.creole.ParameterList in project gate-core by GateNLP.

the class CreoleProxy method createResource.

/**
 * Create an instance of a resource using default parameter values.
 *
 * @see #createResource(String,FeatureMap)
 */
public static Resource createResource(String resourceClassName) throws ResourceInstantiationException {
    // get the resource metadata
    ResourceData resData = Gate.getCreoleRegister().get(resourceClassName);
    if (resData == null) {
        Set<Plugin> plugins = Gate.getPlugins(resourceClassName);
        StringBuilder msg = new StringBuilder();
        msg.append("Couldn't get resource data for ").append(resourceClassName).append(".\n\n");
        if (plugins.isEmpty()) {
            msg.append("You may need first to load the plugin that contains your resource.\n");
            msg.append("For example, to create a gate.creole.tokeniser.DefaultTokeniser\n");
            msg.append("you need first to load the ANNIE plugin.\n\n");
        } else if (plugins.size() == 1) {
            msg.append(resourceClassName).append(" can be found in the ").append(plugins.iterator().next().getName()).append(" plugin\n\n");
        } else {
            msg.append(resourceClassName).append(" can be found in the following plugins\n   ");
            for (Plugin dInfo : plugins) {
                msg.append(dInfo.getName()).append(", ");
            }
            msg.setLength(msg.length() - 2);
            msg.append("\n\n");
        }
        msg.append("Go to the menu File->Manage CREOLE plugins or use the method\n");
        msg.append("Gate.getCreoleRegister().registerDirectories(pluginDirectoryURL).");
        throw new ResourceInstantiationException(msg.toString());
    }
    // get the parameter list and default values
    ParameterList paramList = resData.getParameterList();
    FeatureMap parameterValues = null;
    try {
        parameterValues = paramList.getInitimeDefaults();
    } catch (ParameterException e) {
        throw new ResourceInstantiationException("Couldn't get default parameters for " + resourceClassName + ": " + e);
    }
    return createResource(resourceClassName, parameterValues);
}
Also used : ResourceData(gate.creole.ResourceData) ParameterList(gate.creole.ParameterList) ParameterException(gate.creole.ParameterException) Plugin(gate.creole.Plugin) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 3 with ParameterList

use of gate.creole.ParameterList in project gate-core by GateNLP.

the class PRPersistence method extractDataFromSource.

/**
 * Populates this Persistence with the data that needs to be stored from the
 * original source object.
 */
@Override
public void extractDataFromSource(Object source) throws PersistenceException {
    if (!(source instanceof ProcessingResource)) {
        throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + ProcessingResource.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + ProcessingResource.class.getName());
    }
    super.extractDataFromSource(source);
    Resource res = (Resource) source;
    ResourceData rData = Gate.getCreoleRegister().get(res.getClass().getName());
    if (rData == null)
        throw new PersistenceException("Could not find CREOLE data for " + res.getClass().getName());
    // now get the runtime params
    ParameterList params = rData.getParameterList();
    try {
        // get the values for the init time parameters
        runtimeParams = Factory.newFeatureMap();
        // this is a list of lists
        Iterator<List<Parameter>> parDisjIter = params.getRuntimeParameters().iterator();
        while (parDisjIter.hasNext()) {
            Iterator<Parameter> parIter = parDisjIter.next().iterator();
            while (parIter.hasNext()) {
                Parameter parameter = parIter.next();
                String parName = parameter.getName();
                Object parValue = res.getParameterValue(parName);
                if (storeParameterValue(parValue, parameter.getDefaultValue())) {
                    ((FeatureMap) runtimeParams).put(parName, parValue);
                }
            }
        }
        runtimeParams = PersistenceManager.getPersistentRepresentation(runtimeParams);
    } catch (ResourceInstantiationException | ParameterException rie) {
        throw new PersistenceException(rie);
    }
}
Also used : ResourceData(gate.creole.ResourceData) ProcessingResource(gate.ProcessingResource) Resource(gate.Resource) ProcessingResource(gate.ProcessingResource) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) ParameterList(gate.creole.ParameterList) Parameter(gate.creole.Parameter) ParameterList(gate.creole.ParameterList) List(java.util.List) ParameterException(gate.creole.ParameterException)

Aggregations

ParameterException (gate.creole.ParameterException)3 ParameterList (gate.creole.ParameterList)3 ResourceData (gate.creole.ResourceData)3 ResourceInstantiationException (gate.creole.ResourceInstantiationException)3 FeatureMap (gate.FeatureMap)2 Resource (gate.Resource)2 Parameter (gate.creole.Parameter)2 PersistenceException (gate.persist.PersistenceException)2 List (java.util.List)2 ProcessingResource (gate.ProcessingResource)1 Plugin (gate.creole.Plugin)1