Montag, 11. Juni 2012

Testing UIMA with JUnit - I

Some basic functionc can help to test UIMA functions with JUnit tests. Here is my used UIMATestUtils.class:

public class UIMATestUtils {
   
    /**
     * Read type system
     * @return
     * @throws InvalidXMLException
     * @throws IOException
     */
    static public TypeSystemDescription readTypeSystem() throws InvalidXMLException, IOException {
        URL myURL = UIMAFramework.class.getResource("/TypeSystem.xml");
        TypeSystemDescription typeSysDes = UIMAFramework.getXMLParser()
                .parseTypeSystemDescription(new XMLInputSource(myURL));
        return typeSysDes;
    }

    /**
     * Create CAS
     * @param inputFolder
     * @return
     * @throws IOException
     * @throws InvalidXMLException
     * @throws ResourceInitializationException
     * @throws CollectionException
     */
    static public CAS createCas(String inputFolder) throws ResourceInitializationException, InvalidXMLException, IOException, CollectionException {
        CollectionReaderDescription crDesc = CollectionReaderFactory
                .createDescription(XCasReader.class, readTypeSystem(),
                        AbstractDeployer.PARAM_INPUTDIR, inputFolder);
        XCasReader reader = (XCasReader) UIMAFramework.produceCollectionReader(crDesc);
        CAS cas = CasCreationUtils.createCas(reader.getProcessingResourceMetaData());
        reader.run(cas);
        return cas;
    }

    /**
     * Create JCas
     * @param inputFolder
     * @return
     * @throws IOException
     * @throws InvalidXMLException
     * @throws ResourceInitializationException
     * @throws CASException
     * @throws CollectionException
     */
    static public JCas createJCas(String inputFolder) throws ResourceInitializationException, InvalidXMLException, IOException, CASException, CollectionException {
        CAS cas = createCas(inputFolder);
        return cas.getJCas();
    }
The XCasReader thereby is an XMIDeserializer, which mainly calls the UIMA function XmiCasDeserializer.deserialize(in, aCAS);.

To test UIMA functions, you often need the typesystem descriptor, JCas, or CAS objects as parameters. These functions handle this for you, whereby the JCas and CAS creation is based on the xmi deserializer function of UIMA. This requires a folder containing the xmi file you want to use as base for you JCas or CAS object. 

Keine Kommentare:

Kommentar veröffentlichen