Search in sources :

Example 36 with FeatureMap

use of gate.FeatureMap in project gate-core by GateNLP.

the class TestRepositioningInfo method setUp.

/**
 * This method sets up the parameters for the files to be tested
 */
@Override
protected void setUp() throws Exception {
    if (!Gate.isInitialised()) {
        Gate.runInSandbox(true);
        Gate.init();
    }
    testFile = TestDocument.getTestServerName() + "tests/test-inline.xml";
    // creating documents
    try {
        FeatureMap params = Factory.newFeatureMap();
        params.put("sourceUrl", new URL(testFile));
        params.put("preserveOriginalContent", new Boolean("true"));
        params.put("collectRepositioningInfo", new Boolean("true"));
        doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params);
    } catch (MalformedURLException murle) {
        fail("Document cannot be created ");
    } catch (ResourceInstantiationException rie) {
        fail("Resources cannot be created for the test document");
    }
}
Also used : FeatureMap(gate.FeatureMap) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 37 with FeatureMap

use of gate.FeatureMap in project gate-core by GateNLP.

the class TestPersist method testSimple.

// testSaveRestore()
/**
 * Simple test
 */
public void testSimple() throws Exception {
    // create a temporary directory; because File.createTempFile actually
    // writes the bloody thing, we need to delete it from disk before calling
    // DataStore.create
    File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
    storageDir.delete();
    // create and open a serial data store
    DataStore sds = Factory.createDataStore("gate.persist.SerialDataStore", storageDir.toURI().toURL().toString());
    // check we can get empty lists from empty data stores
    @SuppressWarnings("unused") List<String> lrTypes = sds.getLrTypes();
    // create a document with some annotations / features on it
    String server = TestDocument.getTestServerName();
    Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
    doc.getFeatures().put("hi there", new Integer(23232));
    doc.getAnnotations().add(new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap());
    // save the document
    Document persDoc = (Document) sds.adopt(doc);
    sds.sync(persDoc);
    // remember the persistence ID for reading back
    // (in the normal case these ids are obtained by DataStore.getLrIds(type))
    Object lrPersistenceId = persDoc.getLRPersistenceId();
    // read the document back
    FeatureMap features = Factory.newFeatureMap();
    features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId);
    features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
    Document doc2 = (Document) Factory.createResource("gate.corpora.DocumentImpl", features);
    // parameters should be different
    // check that the version we read back matches the original
    assertTrue(TestEqual.documentsEqual(persDoc, doc2));
    // delete the datastore
    sds.delete();
}
Also used : FeatureMap(gate.FeatureMap) DataStore(gate.DataStore) TestDocument(gate.corpora.TestDocument) Document(gate.Document) File(java.io.File) URL(java.net.URL)

Example 38 with FeatureMap

use of gate.FeatureMap in project gate-core by GateNLP.

the class TestCreole method testIntrospection.

// testArbitraryMetadata()
/**
 * Test resource introspection
 */
@SuppressWarnings("unused")
public void testIntrospection() throws Exception {
    // get the gate.Document resource and its class
    ResourceData docRd = reg.get("gate.corpora.DocumentImpl");
    assertNotNull("couldn't find document res data (2)", docRd);
    Class<?> resClass = docRd.getResourceClass();
    // get the beaninfo and property descriptors for the resource
    BeanInfo docBeanInfo = Introspector.getBeanInfo(resClass, Object.class);
    PropertyDescriptor[] propDescrs = docBeanInfo.getPropertyDescriptors();
    // print all the properties in the reource's bean info;
    // remember the setFeatures method
    Method setFeaturesMethod = null;
    for (int i = 0; i < propDescrs.length; i++) {
        Method getMethodDescr = null;
        Method setMethodDescr = null;
        Class<?> propClass = null;
        PropertyDescriptor propDescr = propDescrs[i];
        propClass = propDescr.getPropertyType();
        getMethodDescr = propDescr.getReadMethod();
        setMethodDescr = propDescr.getWriteMethod();
        if (setMethodDescr != null && setMethodDescr.getName().equals("setFeatures"))
            setFeaturesMethod = setMethodDescr;
        if (DEBUG)
            printProperty(propDescrs[i]);
    }
    // try setting the features property
    // invoke(Object obj, Object[] args)
    LanguageResource res = (LanguageResource) resClass.newInstance();
    FeatureMap feats = Factory.newFeatureMap();
    feats.put("things are sunny in sunny countries", "aren't they?");
    Object[] args = new Object[1];
    args[0] = feats;
    setFeaturesMethod.invoke(res, args);
    assertTrue("features not added to resource properly", res.getFeatures().get("things are sunny in sunny countries").equals("aren't they?"));
}
Also used : FeatureMap(gate.FeatureMap) PropertyDescriptor(java.beans.PropertyDescriptor) LanguageResource(gate.LanguageResource) SimpleBeanInfo(java.beans.SimpleBeanInfo) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method)

Example 39 with FeatureMap

use of gate.FeatureMap in project gate-core by GateNLP.

the class TestResourceReference method checkPersistence.

public void checkPersistence(File xgappFile, ResourceReference rr1, String expected) throws Exception {
    Resource resource = null, restored = null;
    try {
        FeatureMap params = Factory.newFeatureMap();
        params.put("param", rr1);
        resource = Factory.createResource(TestResource.class.getName(), params);
        PersistenceManager.saveObjectToFile(resource, xgappFile);
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(xgappFile);
        Element entry = doc.getRootElement().getChild("application").getChild("initParams").getChild("localMap").getChild("entry");
        assertEquals("couldn't find the paramameter entry", "param", entry.getChildText("string"));
        Element value = entry.getChild("gate.util.persistence.PersistenceManager-RRPersistence");
        assertNotNull("We couldn't find the RRPersistence wrapper", value);
        assertEquals("The URI was not as expected", expected, value.getChildText("uriString"));
        restored = (Resource) PersistenceManager.loadObjectFromFile(xgappFile);
        ResourceReference rr2 = (ResourceReference) restored.getParameterValue("param");
        assertEquals(rr1, rr2);
    } finally {
        if (xgappFile != null)
            xgappFile.deleteOnExit();
        if (resource != null) {
            Factory.deleteResource(resource);
        }
        if (restored != null) {
            Factory.deleteResource(restored);
        }
    }
}
Also used : FeatureMap(gate.FeatureMap) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) CreoleResource(gate.creole.metadata.CreoleResource) Resource(gate.Resource) TestDocument(gate.corpora.TestDocument) Document(org.jdom.Document)

Example 40 with FeatureMap

use of gate.FeatureMap in project gate-core by GateNLP.

the class TestSgml method testSgmlLoading.

// setUp
public void testSgmlLoading() throws Exception {
    assertTrue(true);
    // create the markupElementsMap map
    Map<String, String> markupElementsMap = null;
    gate.Document doc = null;
    /*
    markupElementsMap = new HashMap();
    // populate it
    markupElementsMap.put ("S","Sentence");
    markupElementsMap.put ("s","Sentence");
    markupElementsMap.put ("W","Word");
    markupElementsMap.put ("w","Word");
    */
    FeatureMap params = Factory.newFeatureMap();
    params.put(Document.DOCUMENT_URL_PARAMETER_NAME, new URL(TestDocument.getTestServerName() + "tests/sgml/Hds.sgm"));
    params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
    doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params);
    // get the docFormat that deals with it.
    // the parameter MimeType doesn't affect right now the behaviour
    // *
    gate.DocumentFormat docFormat = gate.DocumentFormat.getDocumentFormat(doc, doc.getSourceUrl());
    assertTrue("Bad document Format was produced. SgmlDocumentFormat was expected", docFormat instanceof gate.corpora.SgmlDocumentFormat);
    // set's the map
    docFormat.setMarkupElementsMap(markupElementsMap);
    docFormat.unpackMarkup(doc, "DocumentContent");
    AnnotationSet annotSet = doc.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
    assertEquals("For " + doc.getSourceUrl() + " the number of annotations" + " should be:1022", 1022, annotSet.size());
    // Verfy if all annotations from the default annotation set are consistent
    gate.corpora.TestDocument.verifyNodeIdConsistency(doc);
}
Also used : FeatureMap(gate.FeatureMap) AnnotationSet(gate.AnnotationSet) Document(gate.Document) URL(java.net.URL)

Aggregations

FeatureMap (gate.FeatureMap)55 Document (gate.Document)15 URL (java.net.URL)14 ResourceInstantiationException (gate.creole.ResourceInstantiationException)11 File (java.io.File)10 Resource (gate.Resource)8 GateRuntimeException (gate.util.GateRuntimeException)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 PersistenceException (gate.persist.PersistenceException)6 Annotation (gate.Annotation)5 AnnotationSet (gate.AnnotationSet)5 DataStore (gate.DataStore)5 LanguageResource (gate.LanguageResource)5 TestDocument (gate.corpora.TestDocument)4 ResourceData (gate.creole.ResourceData)4 SerialDataStore (gate.persist.SerialDataStore)4 InvalidOffsetException (gate.util.InvalidOffsetException)4 Corpus (gate.Corpus)3 ProcessingResource (gate.ProcessingResource)3