Search in sources :

Example 1 with GateRuntimeException

use of gate.util.GateRuntimeException in project gate-core by GateNLP.

the class CreoleRegisterImpl method getAnnotationVRs.

// getAnnotationVRs()
/**
 * Returns a list of strings representing class names for annotation VRs that
 * are able to display/edit a given annotation type The default VR will be the
 * first in the returned list.
 */
@Override
public List<String> getAnnotationVRs(String annotationType) {
    if (annotationType == null)
        return Collections.unmodifiableList(new ArrayList<String>());
    LinkedList<String> responseList = new LinkedList<String>();
    String defaultVR = null;
    Iterator<String> vrIterator = vrTypes.iterator();
    while (vrIterator.hasNext()) {
        String vrClassName = vrIterator.next();
        ResourceData vrResourceData = this.get(vrClassName);
        if (vrResourceData == null)
            throw new GateRuntimeException("Couldn't get resource data for VR called " + vrClassName);
        Class<?> vrResourceClass = null;
        try {
            vrResourceClass = vrResourceData.getResourceClass();
        } catch (ClassNotFoundException ex) {
            throw new GateRuntimeException("Couldn't create a class object for VR called " + vrClassName);
        }
        // Test if VR can display all types of annotations
        if (vrResourceData.getGuiType() == ResourceData.NULL_GUI && vrResourceData.getAnnotationTypeDisplayed() != null && gate.creole.AnnotationVisualResource.class.isAssignableFrom(vrResourceClass)) {
            String annotationTypeDisplayed = vrResourceData.getAnnotationTypeDisplayed();
            if (annotationTypeDisplayed.equals(annotationType)) {
                responseList.add(vrClassName);
                if (vrResourceData.isMainView())
                    defaultVR = vrClassName;
            }
        // End if
        }
    // End if
    }
    // End while
    if (defaultVR != null) {
        responseList.remove(defaultVR);
        responseList.addFirst(defaultVR);
    }
    // End if
    return Collections.unmodifiableList(responseList);
}
Also used : GateRuntimeException(gate.util.GateRuntimeException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LinkedList(java.util.LinkedList)

Example 2 with GateRuntimeException

use of gate.util.GateRuntimeException in project gate-core by GateNLP.

the class CreoleRegisterImpl method getVREnabledAnnotationTypes.

/**
 * Returns a list of strings representing annotations types for which there
 * are custom viewers/editor registered.
 */
@Override
public List<String> getVREnabledAnnotationTypes() {
    LinkedList<String> responseList = new LinkedList<String>();
    Iterator<String> vrIterator = vrTypes.iterator();
    while (vrIterator.hasNext()) {
        String vrClassName = vrIterator.next();
        ResourceData vrResourceData = this.get(vrClassName);
        if (vrResourceData == null)
            throw new GateRuntimeException("Couldn't get resource data for VR called " + vrClassName);
        // Test if VR can display all types of annotations
        if (vrResourceData.getGuiType() == ResourceData.NULL_GUI && vrResourceData.getAnnotationTypeDisplayed() != null) {
            String annotationTypeDisplayed = vrResourceData.getAnnotationTypeDisplayed();
            responseList.add(annotationTypeDisplayed);
        }
    // End if
    }
    // End while
    return Collections.unmodifiableList(responseList);
}
Also used : GateRuntimeException(gate.util.GateRuntimeException) LinkedList(java.util.LinkedList)

Example 3 with GateRuntimeException

use of gate.util.GateRuntimeException in project gate-core by GateNLP.

the class CreoleRegisterImpl method getAllInstances.

public List<Resource> getAllInstances(String type, boolean includeHidden) throws GateException {
    List<Resource> res = new ArrayList<Resource>();
    Class<? extends Resource> targetClass;
    try {
        targetClass = Gate.getClassLoader().loadClass(type).asSubclass(Resource.class);
    } catch (ClassNotFoundException cnfe) {
        throw new GateException("Invalid type " + type);
    }
    for (Map.Entry<String, ResourceData> entry : entrySet()) {
        String aType = entry.getKey();
        Class<?> aClass;
        try {
            aClass = entry.getValue().getResourceClass();
            if (targetClass.isAssignableFrom(aClass)) {
                // filter out hidden instances
                Iterator<? extends Resource> newInstancesIter = get(aType).getInstantiations().iterator();
                while (newInstancesIter.hasNext()) {
                    Resource instance = newInstancesIter.next();
                    if (includeHidden || !Gate.getHiddenAttribute(instance.getFeatures())) {
                        res.add(instance);
                    }
                }
            }
        } catch (ClassNotFoundException cnfe) {
            throw new GateRuntimeException("A type registered in the creole register does not exist in the VM!");
        }
    }
    return res;
}
Also used : GateException(gate.util.GateException) VisualResource(gate.VisualResource) Resource(gate.Resource) LanguageResource(gate.LanguageResource) ProcessingResource(gate.ProcessingResource) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GateRuntimeException(gate.util.GateRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with GateRuntimeException

use of gate.util.GateRuntimeException in project gate-core by GateNLP.

the class LuceneDocument method createDocuments.

/**
 * Given an instance of Gate Document, it converts it into the format that
 * lucene can understand and can store in its indexes. This method also stores
 * the tokenStream on the disk in order to retrieve it at the time of
 * searching
 */
public List<Document> createDocuments(String corpusPersistenceID, gate.Document gateDoc, String documentID, List<String> annotSetsToInclude, List<String> annotSetsToExclude, List<String> featuresToInclude, List<String> featuresToExclude, String indexLocation, String baseTokenAnnotationType, Boolean createTokensAutomatically, String indexUnitAnnotationType) {
    if (baseTokenAnnotationType != null)
        baseTokenAnnotationType = baseTokenAnnotationType.trim();
    List<Document> toReturnBack = new ArrayList<Document>();
    List<String> annotSetsToIndex = new ArrayList<String>();
    // about annotation sets to exclude
    if (annotSetsToInclude.size() > 0) {
        annotSetsToIndex = annotSetsToInclude;
    // if there's only one annotation to index, we don't need to
    // create a MergeSet
    // if(annotSetsToIndex.size() == 1) createMergeSet = false;
    } else if (annotSetsToExclude.size() > 0) {
        // if there were no annotation sets to include, check if user has
        // provided any annotation sets to exclude
        // if so, we need to index all annotation sets but provided in the
        // annotationsetstoexclude list
        Set<String> namedAnnotSets = new HashSet<String>();
        if (gateDoc.getNamedAnnotationSets() != null && gateDoc.getNamedAnnotationSets().keySet() != null) {
            namedAnnotSets = gateDoc.getNamedAnnotationSets().keySet();
        }
        for (String setName : namedAnnotSets) {
            if (annotSetsToExclude.contains(setName))
                continue;
            annotSetsToIndex.add(setName);
        }
        if (!annotSetsToExclude.contains(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
            annotSetsToIndex.add(Constants.DEFAULT_ANNOTATION_SET_NAME);
        }
    } else {
        // if both annotation sets to include and annotation sets to
        // exclude are empty
        // we need to index all annotation sets
        Set<String> namedAnnotSets = new HashSet<String>();
        if (gateDoc.getNamedAnnotationSets() != null && gateDoc.getNamedAnnotationSets().keySet() != null) {
            namedAnnotSets = gateDoc.getNamedAnnotationSets().keySet();
        }
        for (String setName : namedAnnotSets) {
            annotSetsToIndex.add(setName);
        }
        annotSetsToIndex.add(Constants.DEFAULT_ANNOTATION_SET_NAME);
    }
    // lets find out the annotation set that contains tokens in it
    AnnotationSet baseTokenAnnotationSet = null;
    // search in annotation sets to find out which of them has the
    // baseTokenAnnotationType annotations
    // initially this is set to false
    boolean searchBaseTokensInAllAnnotationSets = false;
    boolean searchIndexUnitInAllAnnotationSets = false;
    // this variable tells whether we want to create manual tokens or
    // not
    boolean createManualTokens = false;
    // lets check if user's input is setName.basetokenAnnotationType
    int index = -1;
    if (baseTokenAnnotationType != null && baseTokenAnnotationType.length() > 0)
        index = baseTokenAnnotationType.lastIndexOf('.');
    // basetokenAnnotationType
    if (index >= 0) {
        // set name
        String setName = baseTokenAnnotationType.substring(0, index);
        // token type
        baseTokenAnnotationType = baseTokenAnnotationType.substring(index + 1, baseTokenAnnotationType.length());
        // annotation set
        if (setName.equals(Constants.DEFAULT_ANNOTATION_SET_NAME))
            baseTokenAnnotationSet = gateDoc.getAnnotations().get(baseTokenAnnotationType);
        else
            baseTokenAnnotationSet = gateDoc.getAnnotations(setName).get(baseTokenAnnotationType);
        // base token annotation type
        if (baseTokenAnnotationSet == null || baseTokenAnnotationSet.size() == 0) {
            System.err.println("Base Tokens " + baseTokenAnnotationType + " counldn't be found under the specified annotation set " + setName + "\n searching them in other annotation sets");
            searchBaseTokensInAllAnnotationSets = true;
        }
    } else {
        // either baseTokenAnnotation type is null or user hasn't provided
        // any annotaiton set name
        // so we search in all annotation sets
        searchBaseTokensInAllAnnotationSets = true;
    }
    if (baseTokenAnnotationType != null && baseTokenAnnotationType.length() > 0 && searchBaseTokensInAllAnnotationSets) {
        // we set this to true and if we find basetokens in any of the
        // annotationsets to index
        // we will set this to false
        createManualTokens = true;
        for (String aSet : annotSetsToIndex) {
            if (aSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
                AnnotationSet tempSet = gateDoc.getAnnotations().get(baseTokenAnnotationType);
                if (tempSet.size() > 0) {
                    baseTokenAnnotationSet = tempSet;
                    // System.out.println("found in default annotation set");
                    createManualTokens = false;
                    break;
                }
            } else {
                AnnotationSet tempSet = gateDoc.getAnnotations(aSet).get(baseTokenAnnotationType);
                if (tempSet.size() > 0) {
                    baseTokenAnnotationSet = tempSet;
                    // System.out.println("found in "+aSet);
                    createManualTokens = false;
                    break;
                }
            }
        }
    }
    // we'll have to create tokens ourselves
    if (baseTokenAnnotationType == null || baseTokenAnnotationType.length() == 0)
        createManualTokens = true;
    // lets check if we have to create ManualTokens
    if (createManualTokens) {
        if (!createTokensAutomatically.booleanValue()) {
            System.out.println("Tokens couldn't be found in the document - Ignoring the document " + gateDoc.getName());
            return null;
        }
        baseTokenAnnotationType = Constants.ANNIC_TOKEN;
        if (baseTokenAnnotationSet == null) {
            baseTokenAnnotationSet = new AnnotationSetImpl(gateDoc);
        }
        if (!createTokens(gateDoc, baseTokenAnnotationSet)) {
            System.out.println("Tokens couldn't be created manually - Ignoring the document " + gateDoc.getName());
            return null;
        }
    }
    // by now, baseTokenAnnotationSet will not be null for sure and we
    // know what's the baseTokenAnnotationType
    // lets find out the annotation set that contains
    // indexUnitAnnotationType in it
    AnnotationSet indexUnitAnnotationSet = null;
    // lets check if user has provided setName.indexUnitAnnotationType
    index = -1;
    if (indexUnitAnnotationType != null && indexUnitAnnotationType.trim().length() > 0)
        index = indexUnitAnnotationType.lastIndexOf('.');
    // indexUnitAnnotationType
    if (index >= 0) {
        // setName
        String setName = indexUnitAnnotationType.substring(0, index);
        // indexUnitAnnotationType
        indexUnitAnnotationType = indexUnitAnnotationType.substring(index + 1, indexUnitAnnotationType.length());
        if (setName.equals(Constants.DEFAULT_ANNOTATION_SET_NAME))
            indexUnitAnnotationSet = gateDoc.getAnnotations().get(indexUnitAnnotationType);
        else
            indexUnitAnnotationSet = gateDoc.getAnnotations(setName).get(indexUnitAnnotationType);
        // if so, we'll have to search other annotation sets
        if (indexUnitAnnotationSet == null || indexUnitAnnotationSet.size() == 0) {
            System.err.println("Index Unit " + indexUnitAnnotationType + " counldn't be found under the specified annotation set " + setName + "\n searching them in other annotation sets");
            searchIndexUnitInAllAnnotationSets = true;
        }
    } else {
        // either indexUnitAnnotationType is null or user hasn't provided
        // the setname
        searchIndexUnitInAllAnnotationSets = true;
    }
    // searching in all annotation set names
    if (indexUnitAnnotationType != null && indexUnitAnnotationType.length() > 0 && searchIndexUnitInAllAnnotationSets) {
        for (String aSet : annotSetsToIndex) {
            if (aSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME)) {
                AnnotationSet tempSet = gateDoc.getAnnotations().get(indexUnitAnnotationType);
                if (tempSet.size() > 0) {
                    indexUnitAnnotationSet = tempSet;
                    break;
                }
            } else {
                AnnotationSet tempSet = gateDoc.getAnnotations(aSet).get(indexUnitAnnotationType);
                if (tempSet.size() > 0) {
                    indexUnitAnnotationSet = tempSet;
                    break;
                }
            }
        }
    }
    // to null as well
    if (indexUnitAnnotationSet == null) {
        indexUnitAnnotationType = null;
    }
    int j = 0;
    for (String annotSet : annotSetsToIndex) {
        // we need to generate the Token Stream here, and send it to the
        // GateLuceneReader
        AnnotationSet aSetToIndex = annotSet.equals(Constants.DEFAULT_ANNOTATION_SET_NAME) ? gateDoc.getAnnotations() : gateDoc.getAnnotations(annotSet);
        Set<String> indexedFeatures = new HashSet<String>();
        // tempBaseTokenAnnotationSet is not null
        List<Token>[] tokenStreams = getTokens(gateDoc, aSetToIndex, featuresToInclude, featuresToExclude, baseTokenAnnotationType, baseTokenAnnotationSet, indexUnitAnnotationType, indexUnitAnnotationSet, indexedFeatures);
        // tokenStream is set to null
        if (tokenStreams == null)
            return null;
        // this is enabled only if there are more than one annotation sets
        // available to search in
        // if(createMergeSet) {
        // if(mergedSet == null) mergedSet = new AnnotationSetImpl(gateDoc);
        // 
        // // we need to merge all annotations but the
        // // baseTokenAnnotationType
        // for(String aType : aSetToIndex.getAllTypes()) {
        // 
        // if(aType.equals(baseTokenAnnotationType)) {
        // continue;
        // }
        // 
        // if(indexUnitAnnotationType != null
        // && aType.equals(indexUnitAnnotationType)) {
        // continue;
        // }
        // 
        // for(Annotation a : aSetToIndex.get(aType)) {
        // try {
        // mergedSet.add(a.getStartNode().getOffset(), a.getEndNode()
        // .getOffset(), a.getType(), a.getFeatures());
        // }
        // catch(InvalidOffsetException ioe) {
        // throw new GateRuntimeException(ioe);
        // }
        // }
        // 
        // }
        // }
        StringBuffer indexedFeaturesString = new StringBuffer();
        for (String aFeat : indexedFeatures) {
            indexedFeaturesString.append(aFeat + ";");
        }
        Document[] toReturn = new Document[tokenStreams.length];
        for (int i = 0; i < tokenStreams.length; i++, j++) {
            // make a new, empty document
            Document doc = new Document();
            // and then create the document
            LuceneReader reader = new LuceneReader(gateDoc, tokenStreams[i]);
            doc.add(Field.Keyword(Constants.DOCUMENT_ID, documentID));
            doc.add(Field.Keyword(Constants.DOCUMENT_ID_FOR_SERIALIZED_FILE, documentID + "-" + j));
            doc.add(Field.Keyword(Constants.INDEXED_FEATURES, indexedFeaturesString.substring(0, indexedFeaturesString.length() - 1)));
            if (corpusPersistenceID != null)
                doc.add(Field.Keyword(Constants.CORPUS_ID, corpusPersistenceID));
            doc.add(Field.Keyword(Constants.ANNOTATION_SET_ID, annotSet));
            doc.add(Field.Text("contents", reader));
            // here we store token stream on the file system
            try {
                writeOnDisk(tokenStreams[i], documentID, documentID + "-" + j, indexLocation);
            } catch (Exception e) {
                Err.println("\nIgnoring the document : " + gateDoc.getName() + " since its token stream cannot be written on the disk");
                Err.println("Reason: " + e.getMessage());
                return null;
            }
            // return the document
            toReturn[i] = doc;
        }
        toReturnBack.addAll(Arrays.asList(toReturn));
    }
    return toReturnBack;
}
Also used : HashSet(java.util.HashSet) AnnotationSet(gate.AnnotationSet) Set(java.util.Set) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Document(gate.creole.annic.apache.lucene.document.Document) InvalidOffsetException(gate.util.InvalidOffsetException) GateRuntimeException(gate.util.GateRuntimeException) IOException(java.io.IOException) AnnotationSetImpl(gate.annotation.AnnotationSetImpl) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 5 with GateRuntimeException

use of gate.util.GateRuntimeException in project gate-core by GateNLP.

the class MainFrame method datastoreOpened.

/**
 * Called when a {@link gate.DataStore} has been opened
 */
@Override
public void datastoreOpened(CreoleEvent e) {
    final DataStore ds = e.getDatastore();
    if (ds.getName() == null || ds.getName().length() == 0) {
        String name = ds.getStorageUrl();
        StringBuilder nameBuilder = new StringBuilder();
        // quick and dirty FSA
        int state = 0;
        for (int i = name.length() - 1; i >= 0 && state != 2; i--) {
            char currentChar = name.charAt(i);
            switch(state) {
                case 0:
                    // consuming slashes at the end
                    if (currentChar == '/') {
                    // consume
                    } else {
                        // we got the first non-slash char
                        state = 1;
                        nameBuilder.insert(0, currentChar);
                    }
                    break;
                case 1:
                    // eating up name chars
                    if (currentChar == '/') {
                        // we're done!
                        state = 2;
                    } else {
                        // we got a non-slash char
                        nameBuilder.insert(0, currentChar);
                    }
                    break;
                default:
                    throw new GateRuntimeException("A phanthom state of things!");
            }
        }
        if (nameBuilder.length() > 0)
            name = nameBuilder.toString();
        ds.setName(name);
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            NameBearerHandle handle = new NameBearerHandle(ds, MainFrame.this);
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(handle, false);
            resourcesTreeModel.insertNodeInto(node, datastoresRoot, 0);
            handle.addProgressListener(MainFrame.this);
            handle.addStatusListener(MainFrame.this);
        }
    });
// JPopupMenu popup = handle.getPopup();
// popup.addSeparator();
// // Create a CloseViewAction and a menu item based on it
// CloseViewAction cva = new CloseViewAction(handle);
// XJMenuItem menuItem = new XJMenuItem(cva, this);
// // Add an accelerator ATL+F4 for this action
// menuItem.setAccelerator(KeyStroke.getKeyStroke(
// KeyEvent.VK_H, ActionEvent.CTRL_MASK));
// popup.add(menuItem);
// // Put the action command in the component's action map
// if (handle.getLargeView() != null)
// handle.getLargeView().getActionMap().put("Hide current
// view",cva);
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DataStore(gate.DataStore) GateRuntimeException(gate.util.GateRuntimeException) Point(java.awt.Point)

Aggregations

GateRuntimeException (gate.util.GateRuntimeException)42 ArrayList (java.util.ArrayList)12 URL (java.net.URL)6 FeatureMap (gate.FeatureMap)5 PersistenceException (gate.persist.PersistenceException)5 GateException (gate.util.GateException)5 Point (java.awt.Point)5 LinkedList (java.util.LinkedList)5 DataStore (gate.DataStore)4 ResourceInstantiationException (gate.creole.ResourceInstantiationException)4 InvalidOffsetException (gate.util.InvalidOffsetException)4 GridBagConstraints (java.awt.GridBagConstraints)4 GridBagLayout (java.awt.GridBagLayout)4 MalformedURLException (java.net.MalformedURLException)4 List (java.util.List)4 JButton (javax.swing.JButton)4 JPanel (javax.swing.JPanel)4 Annotation (gate.Annotation)3 AnnotationSet (gate.AnnotationSet)3 Corpus (gate.Corpus)3