Search in sources :

Example 11 with ResourceData

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

the class TeamwareUtils method populateInputSetNamesForController.

/**
 * Populate the set of input annotation set names for a controller
 * based on heuristics. In the strict case we assume that if a PR in
 * the controller has an inputASName parameter whose value is not the
 * default set, and there is no PR earlier in the pipeline which has
 * the same set as its outputASName or annotationSetName parameter,
 * then it is probably a set that needs to be provided externally. In
 * the non-strict case we simply take all inputASName and
 * annotationSetName parameter values from the controller's PRs,
 * without regard for whether they may have been satisfied by an
 * earlier PR in the pipeline.
 *
 * @param setNames
 * @param c
 * @param strict
 */
private static void populateInputSetNamesForController(Set<String> setNames, Controller c, boolean strict) {
    Set<String> outputSetNamesSoFar = new HashSet<String>();
    Collection<ProcessingResource> prs = c.getPRs();
    try {
        for (ProcessingResource pr : prs) {
            ResourceData rd = Gate.getCreoleRegister().get(pr.getClass().getName());
            List<List<Parameter>> runtimeParams = rd.getParameterList().getRuntimeParameters();
            // check for inputASName and annotationSetName params
            for (List<Parameter> disjunction : runtimeParams) {
                for (Parameter param : disjunction) {
                    if (param.getName().equals("inputASName")) {
                        String setName = (String) pr.getParameterValue(param.getName());
                        if (!strict || (setName != null && !outputSetNamesSoFar.contains(setName))) {
                            setNames.add(setName);
                        }
                    } else if (!strict && param.getName().equals("annotationSetName")) {
                        setNames.add((String) pr.getParameterValue(param.getName()));
                    }
                }
            }
            // check for output set names and update that set
            if (strict) {
                for (List<Parameter> disjunction : runtimeParams) {
                    for (Parameter param : disjunction) {
                        if (param.getName().equals("outputASName") || param.getName().equals("annotationSetName")) {
                            outputSetNamesSoFar.add(String.valueOf(pr.getParameterValue(param.getName())));
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
    // ignore - this is all heuristics, after all
    }
}
Also used : ResourceData(gate.creole.ResourceData) ProcessingResource(gate.ProcessingResource) Parameter(gate.creole.Parameter) List(java.util.List) HashSet(java.util.HashSet)

Example 12 with ResourceData

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

the class SerialDataStore method sync.

// close()
/**
 * Save: synchonise the in-memory image of the LR with the persistent
 * image.
 */
@Override
public void sync(LanguageResource lr) throws PersistenceException {
    // check that this LR is one of ours (i.e. has been adopted)
    if (lr.getDataStore() == null || !lr.getDataStore().equals(this))
        throw new PersistenceException("LR " + lr.getName() + " has not been adopted by this DataStore");
    // find the resource data for this LR
    ResourceData lrData = Gate.getCreoleRegister().get(lr.getClass().getName());
    // create a subdirectory for resources of this type if none exists
    File resourceTypeDirectory = new File(storageDir, lrData.getClassName());
    if ((!resourceTypeDirectory.exists()) || (!resourceTypeDirectory.isDirectory())) {
        // create the directory in the meantime
        if (!resourceTypeDirectory.mkdir() && !resourceTypeDirectory.exists())
            throw new PersistenceException("Can't write " + resourceTypeDirectory);
    }
    // create an indentifier for this resource
    String lrName = null;
    Object lrPersistenceId = null;
    lrName = lr.getName();
    lrPersistenceId = lr.getLRPersistenceId();
    if (lrName == null)
        lrName = lrData.getName();
    if (lrPersistenceId == null) {
        lrPersistenceId = constructPersistenceId(lrName);
        lr.setLRPersistenceId(lrPersistenceId);
    }
    // we're saving a corpus. I need to save its documents first
    if (lr instanceof Corpus) {
        // check if the corpus is the one we support. CorpusImpl cannot be saved!
        if (!(lr instanceof SerialCorpusImpl))
            throw new PersistenceException("Can't save a corpus which " + "is not of type SerialCorpusImpl!");
        SerialCorpusImpl corpus = (SerialCorpusImpl) lr;
        // corresponding document IDs
        for (int i = 0; i < corpus.size(); i++) {
            // if the document is not in memory, there's little point in saving it
            if ((!corpus.isDocumentLoaded(i)) && corpus.isPersistentDocument(i))
                continue;
            if (DEBUG)
                Out.prln("Saving document at position " + i);
            if (DEBUG)
                Out.prln("Document in memory " + corpus.isDocumentLoaded(i));
            if (DEBUG)
                Out.prln("is persistent? " + corpus.isPersistentDocument(i));
            if (DEBUG)
                Out.prln("Document name at position" + corpus.getDocumentName(i));
            Document doc = corpus.get(i);
            try {
                // if the document is not already adopted, we need to do that first
                if (doc.getLRPersistenceId() == null) {
                    if (DEBUG)
                        Out.prln("Document adopted" + doc.getName());
                    doc = (Document) this.adopt(doc);
                    this.sync(doc);
                    if (DEBUG)
                        Out.prln("Document sync-ed");
                    corpus.setDocumentPersistentID(i, doc.getLRPersistenceId());
                } else {
                    // if it is adopted, just sync it
                    this.sync(doc);
                    if (DEBUG)
                        Out.prln("Document sync-ed");
                }
                // store the persistent ID. Needs to be done even if the document was
                // already adopted, in case the doc was already persistent
                // when added to the corpus
                corpus.setDocumentPersistentID(i, doc.getLRPersistenceId());
                if (DEBUG)
                    Out.prln("new document ID " + doc.getLRPersistenceId());
            } catch (Exception ex) {
                throw new PersistenceException("Error while saving corpus: " + corpus + "because of an error storing document " + ex.getMessage(), ex);
            }
        }
    // for loop through documents
    }
    // create a File to store the resource in
    File resourceFile = new File(resourceTypeDirectory, (String) lrPersistenceId);
    // dump the LR into the new File
    try {
        OutputStream os = new FileOutputStream(resourceFile);
        // after 1.1 the serialised files are compressed
        if (!currentProtocolVersion.equals("1.0"))
            os = new GZIPOutputStream(os);
        os = new BufferedOutputStream(os);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(lr);
        oos.close();
    } catch (IOException e) {
        throw new PersistenceException("Couldn't write to storage file: " + e.getMessage(), e);
    }
    // let the world know about it
    fireResourceWritten(new DatastoreEvent(this, DatastoreEvent.RESOURCE_WRITTEN, lr, lrPersistenceId));
}
Also used : ResourceData(gate.creole.ResourceData) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) IOException(java.io.IOException) Document(gate.Document) ObjectOutputStream(java.io.ObjectOutputStream) Corpus(gate.Corpus) URISyntaxException(java.net.URISyntaxException) GateRuntimeException(gate.util.GateRuntimeException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) SerialCorpusImpl(gate.corpora.SerialCorpusImpl) FileOutputStream(java.io.FileOutputStream) DatastoreEvent(gate.event.DatastoreEvent) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with ResourceData

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

the class ListEditorDialog method initLocalData.

protected void initLocalData(Collection<?> data, Class<?> collectionType) {
    try {
        ResourceData rData = Gate.getCreoleRegister().get(itemType);
        itemTypeClass = rData == null ? Class.forName(itemType, true, Gate.getClassLoader()) : rData.getResourceClass();
    } catch (ClassNotFoundException cnfe) {
        throw new GateRuntimeException(cnfe.toString());
    }
    finiteType = Gate.isGateType(itemType);
    ResourceData rData = Gate.getCreoleRegister().get(itemType);
    String typeDescription = null;
    if (List.class.isAssignableFrom(collectionType)) {
        typeDescription = "List";
        allowDuplicates = true;
    } else {
        if (Set.class.isAssignableFrom(collectionType)) {
            typeDescription = "Set";
            allowDuplicates = false;
        } else {
            typeDescription = "Collection";
            allowDuplicates = true;
        }
        if (SortedSet.class.isAssignableFrom(collectionType) && data != null) {
            comparator = ((SortedSet<?>) data).comparator();
        }
        if (comparator == null) {
            comparator = new NaturalComparator();
        }
    }
    listModel = new DefaultListModel();
    if (data != null) {
        if (comparator == null) {
            for (Object elt : data) {
                listModel.addElement(elt);
            }
        } else {
            Object[] dataArray = data.toArray();
            Arrays.sort(dataArray, comparator);
            for (Object elt : dataArray) {
                listModel.addElement(elt);
            }
        }
    }
    setTitle(typeDescription + " of " + ((rData == null) ? itemType : rData.getName()));
    addAction = new AddAction();
    removeAction = new RemoveAction();
}
Also used : ResourceData(gate.creole.ResourceData)

Example 14 with ResourceData

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

the class ResourceRenderer method prepareRendererCommon.

private void prepareRendererCommon(JComponent ownerComponent, Object value, boolean isSelected, boolean hasFocus) {
    setFont(ownerComponent.getFont());
    String text;
    String toolTipText;
    Icon icon;
    ResourceData rData = null;
    if (value instanceof Resource) {
        text = ((Resource) value).getName();
        rData = Gate.getCreoleRegister().get(value.getClass().getName());
    } else {
        text = (value == null) ? "<null>" : value.toString();
        if (value == null)
            setForeground(Color.red);
    }
    if (rData != null) {
        toolTipText = "<HTML>Type: <b>" + rData.getName() + "</b></HTML>";
        /*String iconName = rData.getIcon();
      if(iconName == null) {
        if(value instanceof LanguageResource)
          iconName = "lr";
        else if(value instanceof ProcessingResource)
          iconName = "pr";
        else if(value instanceof Controller) iconName = "application";
      }
      icon = (iconName == null) ? null : MainFrame.getIcon(iconName);*/
        icon = MainFrame.getIcon(rData.getIcon(), rData.getResourceClassLoader());
    } else {
        icon = null;
        toolTipText = null;
    }
    setText(text);
    setIcon(icon);
    setToolTipText(toolTipText);
}
Also used : ResourceData(gate.creole.ResourceData)

Example 15 with ResourceData

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

the class SerialControllerEditor method selectPR.

/**
 * Called when a PR has been selected in the member PRs table;
 * @param index row index in {@link #memberPRsTable} (or -1 if no PR is
 * currently selected)
 */
protected void selectPR(int index) {
    // stop current editing of parameters
    if (parametersEditor.getResource() != null) {
        try {
            parametersEditor.setParameters();
        } catch (ResourceInstantiationException rie) {
            JOptionPane.showMessageDialog(SerialControllerEditor.this, "Failed to set parameters for \"" + parametersEditor.getResource().getName() + "\"!\n", "GATE", JOptionPane.ERROR_MESSAGE);
            rie.printStackTrace(Err.getPrintWriter());
        }
        if (conditionalMode) {
            if (selectedPRRunStrategy != null && selectedPRRunStrategy instanceof AnalyserRunningStrategy) {
                AnalyserRunningStrategy strategy = (AnalyserRunningStrategy) selectedPRRunStrategy;
                strategy.setFeatureName(featureNameTextField.getText());
                strategy.setFeatureValue(featureValueTextField.getText());
            }
            selectedPRRunStrategy = null;
        }
    }
    // find the new PR
    ProcessingResource pr = null;
    if (index >= 0 && index < controller.getPRs().size()) {
        pr = controller.getPRs().get(index);
    }
    if (pr != null) {
        // update the GUI for the new PR
        ResourceData rData = Gate.getCreoleRegister().get(pr.getClass().getName());
        // update the border name
        parametersBorder.setTitle(" Runtime Parameters for the \"" + pr.getName() + "\" " + rData.getName() + ": ");
        // update the params editor
        // this is a list of lists
        List<List<Parameter>> parameters = rData.getParameterList().getRuntimeParameters();
        if (corpusControllerMode) {
            // remove corpus and document
            // create a new list so we don't change the one from CreoleReg.
            List<List<Parameter>> oldParameters = parameters;
            parameters = new ArrayList<List<Parameter>>();
            for (List<Parameter> aDisjunction : oldParameters) {
                List<Parameter> newDisjunction = new ArrayList<Parameter>();
                for (Parameter parameter : aDisjunction) {
                    if (!parameter.getName().equals("corpus") && !parameter.getName().equals("document")) {
                        newDisjunction.add(parameter);
                    }
                }
                if (!newDisjunction.isEmpty())
                    parameters.add(newDisjunction);
            }
        }
        parametersEditor.init(pr, parameters);
        if (conditionalMode) {
            strategyBorder.setTitle(" Run \"" + pr.getName() + "\"? ");
            // update the state of the run strategy buttons
            yes_RunRBtn.setEnabled(true);
            no_RunRBtn.setEnabled(true);
            conditional_RunRBtn.setEnabled(true);
            // editing the strategy panel causes the edits to be sent to the current
            // strategy object, which can lead to a race condition.
            // So we used a cached value instead.
            selectedPRRunStrategy = null;
            RunningStrategy newStrategy = ((ConditionalController) controller).getRunningStrategies().get(index);
            if (newStrategy instanceof AnalyserRunningStrategy) {
                featureNameTextField.setEnabled(true);
                featureValueTextField.setEnabled(true);
                conditional_RunRBtn.setEnabled(true);
                featureNameTextField.setText(((AnalyserRunningStrategy) newStrategy).getFeatureName());
                featureValueTextField.setText(((AnalyserRunningStrategy) newStrategy).getFeatureValue());
            } else {
                featureNameTextField.setEnabled(false);
                featureValueTextField.setEnabled(false);
                conditional_RunRBtn.setEnabled(false);
            }
            switch(newStrategy.getRunMode()) {
                case RunningStrategy.RUN_ALWAYS:
                    {
                        yes_RunRBtn.setSelected(true);
                        break;
                    }
                case RunningStrategy.RUN_NEVER:
                    {
                        no_RunRBtn.setSelected(true);
                        break;
                    }
                case RunningStrategy.RUN_CONDITIONAL:
                    {
                        conditional_RunRBtn.setSelected(true);
                        break;
                    }
            }
            // switch
            // now that the UI is updated, we can safely link to the new strategy
            selectedPRRunStrategy = newStrategy;
        }
    } else {
        // selected PR == null -> clean all mentions of the old PR from the GUI
        parametersBorder.setTitle(" No processing resource selected... ");
        parametersEditor.init(null, null);
        if (conditionalMode) {
            strategyBorder.setTitle(" No processing resource selected... ");
            yes_RunRBtn.setEnabled(false);
            no_RunRBtn.setEnabled(false);
            conditional_RunRBtn.setEnabled(false);
            featureNameTextField.setText("");
            featureNameTextField.setEnabled(false);
            featureValueTextField.setText("");
            featureValueTextField.setEnabled(false);
        }
    }
    // this seems to be needed to show the changes
    SerialControllerEditor.this.repaint(100);
}
Also used : ResourceData(gate.creole.ResourceData) RunningStrategy(gate.creole.RunningStrategy) AnalyserRunningStrategy(gate.creole.AnalyserRunningStrategy) UnconditionalRunningStrategy(gate.creole.RunningStrategy.UnconditionalRunningStrategy) ProcessingResource(gate.ProcessingResource) ArrayList(java.util.ArrayList) Parameter(gate.creole.Parameter) AnalyserRunningStrategy(gate.creole.AnalyserRunningStrategy) List(java.util.List) ArrayList(java.util.ArrayList) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Aggregations

ResourceData (gate.creole.ResourceData)18 ResourceInstantiationException (gate.creole.ResourceInstantiationException)9 Parameter (gate.creole.Parameter)6 List (java.util.List)6 ParameterException (gate.creole.ParameterException)5 FeatureMap (gate.FeatureMap)4 ProcessingResource (gate.ProcessingResource)4 PersistenceException (gate.persist.PersistenceException)4 Resource (gate.Resource)3 ParameterList (gate.creole.ParameterList)3 GateRuntimeException (gate.util.GateRuntimeException)3 ArrayList (java.util.ArrayList)3 Corpus (gate.Corpus)2 Document (gate.Document)2 AbstractProcessingResource (gate.creole.AbstractProcessingResource)2 AbstractResource (gate.creole.AbstractResource)2 ConditionalController (gate.creole.ConditionalController)2 Plugin (gate.creole.Plugin)2 CreoleEvent (gate.event.CreoleEvent)2 ActionEvent (java.awt.event.ActionEvent)2