use of gate.Resource 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);
}
}
use of gate.Resource in project gate-core by GateNLP.
the class ResourcePersistence method createObject.
@Override
public Object createObject() throws PersistenceException, ResourceInstantiationException {
if (initParams != null)
initParams = PersistenceManager.getTransientRepresentation(initParams, containingControllerName, initParamOverrides);
if (features != null)
features = PersistenceManager.getTransientRepresentation(features, containingControllerName, initParamOverrides);
if (initParamOverrides != null) {
// check if there is a map for this resource Id in the overrides
String containingControllerNameToUse = containingControllerName == null ? "" : containingControllerName;
String resourceId = containingControllerNameToUse + "\t" + resourceName;
if (initParamOverrides.containsKey(resourceId)) {
Map<String, Object> parmOverrides = initParamOverrides.get(resourceId);
if (initParams instanceof FeatureMap) {
FeatureMap fm = (FeatureMap) initParams;
// do this in a loop instead of using putAll so we can log the changes
for (String name : parmOverrides.keySet()) {
Object obj = parmOverrides.get(name);
log.info("Overriding init parameter " + name + " for " + containingControllerNameToUse + "//" + resourceName + " with " + obj);
fm.put(name, obj);
}
}
}
}
Resource res = Factory.createResource(resourceType, (FeatureMap) initParams, (FeatureMap) features, resourceName);
return res;
}
use of gate.Resource in project gate-core by GateNLP.
the class TestPersist method testMultipleLrs.
// testSimple()
/**
* Test multiple LRs
*/
public void testMultipleLrs() 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
SerialDataStore sds = new SerialDataStore(storageDir.toURI().toURL().toString());
sds.create();
sds.open();
// 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());
// create another document with some annotations / features on it
Document doc2 = Factory.newDocument(new URL(server + "tests/html/test1.htm"));
doc.getFeatures().put("hi there again", new Integer(23232));
doc.getAnnotations().add(new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap());
// create a corpus with the documents
Corpus corp = Factory.newCorpus("Hamish test corpus");
corp.add(doc);
corp.add(doc2);
LanguageResource persCorpus = sds.adopt(corp);
sds.sync(persCorpus);
// read the documents back
List<Resource> lrsFromDisk = new ArrayList<Resource>();
List<String> lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
Iterator<String> idsIter = lrIds.iterator();
while (idsIter.hasNext()) {
String lrId = idsIter.next();
FeatureMap features = Factory.newFeatureMap();
features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
features.put(DataStore.LR_ID_FEATURE_NAME, lrId);
Resource lr = Factory.createResource("gate.corpora.SerialCorpusImpl", features);
lrsFromDisk.add(lr);
}
if (DEBUG)
System.out.println("LRs on disk" + lrsFromDisk);
// check that the versions we read back match the originals
Corpus diskCorp = (Corpus) lrsFromDisk.get(0);
Document diskDoc = diskCorp.get(0);
if (DEBUG)
Out.prln("Documents in corpus: " + corp.getDocumentNames());
assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName()));
if (DEBUG)
Out.prln("Memory features " + corp.getFeatures());
if (DEBUG)
Out.prln("Disk features " + diskCorp.getFeatures());
assertTrue("corp feat != mem feat", corp.getFeatures().equals(diskCorp.getFeatures()));
if (DEBUG)
Out.prln("Annotations in doc: " + diskDoc.getAnnotations());
assertTrue("doc annotations from disk not equal to memory version", TestEqual.annotationSetsEqual(doc.getAnnotations(), diskDoc.getAnnotations()));
assertTrue("doc from disk not equal to memory version", TestEqual.documentsEqual(doc, diskDoc));
Iterator<Document> corpusIter = diskCorp.iterator();
while (corpusIter.hasNext()) {
if (DEBUG)
Out.prln(corpusIter.next().getName());
else
corpusIter.next();
}
// assertTrue("doc2 from disk not equal to memory version",
// doc2.equals(diskDoc2));
// delete the datastore
sds.delete();
}
use of gate.Resource in project gate-core by GateNLP.
the class TestCreole method testFactory.
// testIntrospection
/**
* Test the Factory resource creation provisions
*/
public void testFactory() throws Exception {
FeatureMap params = Factory.newFeatureMap();
params.put("features", Factory.newFeatureMap());
params.put(Document.DOCUMENT_URL_PARAMETER_NAME, new URL(TestDocument.getTestServerName() + "tests/doc0.html"));
Resource res = Factory.createResource("gate.corpora.DocumentImpl", params);
assertNotNull("unable to create document", res);
}
use of gate.Resource in project gate-core by GateNLP.
the class TestCreole method allAutoloaders.
// testViews()
/**
* Utility method to check that a list of resources are all
* auto-loading.
*/
protected boolean allAutoloaders(List<? extends Resource> l) {
if (l != null) {
Resource res = null;
ResourceData resData = null;
CreoleRegister cr = Gate.getCreoleRegister();
Iterator<? extends Resource> iter = l.iterator();
while (iter.hasNext()) {
res = iter.next();
if (DEBUG)
Out.prln(res);
resData = cr.get(res.getClass().getName());
if (DEBUG)
Out.prln(resData);
if (!resData.isAutoLoading())
return false;
}
}
return true;
}
Aggregations