You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Bojan Janisch <bo...@scai.fraunhofer.de> on 2013/07/04 15:35:36 UTC

Re: Clone a JCas

Hey Richard,

I've tried your way, but getting NullPointerException over NullPointerException at the merging.

Exception in thread "ECJ Evaluation Thread 0" java.lang.NullPointerException
	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:85)
	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:50)
	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl.createTypeOrder(LinearTypeOrderBuilderImpl.java:291)
	at org.apache.uima.cas.impl.CASMgrSerializer.getIndexRepository(CASMgrSerializer.java:462)
	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1079)
	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1071)
	at org.apache.uima.cas.impl.Serialization.deserializeCASComplete(Serialization.java:57)
	at rulevolution.utils.JCasUtils.cloneJCas(JCasUtils.java:257)
	at rulevolution.utils.JCasUtils.copyCasPool(JCasUtils.java:212)
	at rulevolution.RulEvolution.evaluate(RulEvolution.java:156)
	at ec.simple.SimpleEvaluator.evalPopChunk(SimpleEvaluator.java:259)
	at ec.simple.SimpleEvaluator$SimpleEvaluatorThreadCG.run(SimpleEvaluator.java:341)
	at ec.util.ThreadPool$PoolThread.run(ThreadPool.java:57)


Do you or anybody else know where my mistake is? Here's the Code I've programmed for cloning:

public static JCas cloneJCas(JCas aJCas){
	JCas jcas = null;
	try {
		jcas = UIMATypeSystemUtils.getEmptyCasFromTypeSystem(AbstractDeployer.TYPESYSTEM).getJCas();
		
		CASCompleteSerializer complete = new CASCompleteSerializer();
		complete.setCasSerializer(new CASSerializer());
		complete.setCasMgrSerializer(new CASMgrSerializer());
		complete.getCASMgrSerializer().addTypeSystem(aJCas.getCasImpl().getTypeSystemImpl());
		complete.getCASSerializer().addCAS(aJCas.getCasImpl());
		byte[] aBytes = SerializationUtils.serialize(complete);
		
		deserializeCASComplete((CASCompleteSerializer)SerializationUtils.deserialize(aBytes), jcas.getCasImpl());
	} 
	....
	
	return jcas;
}

The method which throws the exception:

private TotalTypeOrder(int[] typeList, TypeSystem ts) {
      super();
      TypeSystemImpl tsi = (TypeSystemImpl) ts;
      this.order = typeList;
      this.typeCodeToOrder = new int[this.order.length + tsi.getSmallestType()]; //Thats the line I get the exception
      for (int i = 0; i < this.order.length; i++) {
	this.typeCodeToOrder[this.order[i]] = i;
      }
}


I appreciate any help.

Greetings

Bojan



----- Ursprüngliche Mail -----
Von: "Richard Eckart de Castilho" <ri...@gmail.com>
An: user@uima.apache.org
Gesendet: Dienstag, 25. Juni 2013 11:46:59
Betreff: Re: Clone a JCas

Hi,

CASes can work outside UIMA, no idea why you get this exception.

If you want to fully clone a CAS, an alternative approach would be to
serialize the CAS to a binary representation, create a new CAS, and
deserialize the original CAS into the new one. Probably much faster even.

See [1] and [2] for some code driving the UIMA binary serialization. In your
case, serializing to a ByteArrayOutputStream and deserializing from an ByteArrayInputStream
seems a good approach.

-- Richard

[1] https://dkpro-core-asl.googlecode.com/svn/de.tudarmstadt.ukp.dkpro.core-asl/trunk/de.tudarmstadt.ukp.dkpro.core.io.bincas-asl/src/main/java/de/tudarmstadt/ukp/dkpro/core/io/bincas/SerializedCasWriter.java

[2] https://dkpro-core-asl.googlecode.com/svn/de.tudarmstadt.ukp.dkpro.core-asl/trunk/de.tudarmstadt.ukp.dkpro.core.io.bincas-asl/src/main/java/de/tudarmstadt/ukp/dkpro/core/io/bincas/SerializedCasReader.java

Am 03.06.2013 um 23:49 schrieb Bojan Janisch <bo...@scai.fraunhofer.de>:

> Hi everyone,
> 
> I'm a german student and currently working at my bachelor 
> thesis. I'm new to this mailing list so I hope my question 
> is suitable for you.
> 
> I'm using Genetic Algorithms to create rules which processes / 
> annotates the JCases. After that, the results are evaluated and 
> a F-Score is returned which flows into the fitness function of 
> the genetic algorithm. So I'm only using the JCases for 
> evaluation and don't need to write them back. 
> 
> I already tried the CasCopier.copyCas() method:
> 
> public static JCas copyJCas(JCas aJCas) {
>  JCas jcas = null;
>  jcas = JCasFactory.createJCas(UIMATestUtils.readTypeSystem());
>  CasCopier.copyCas(aJCas.getCas(), jcas.getCas(), true);
>  return jcas;
> }
> 
> but I'm getting these two exceptions with the following 
> stacktraces:
> 
> Exception in thread "ECJ Evaluation Thread 0" java.lang.ArrayIndexOutOfBoundsException: 1015
> 	at org.apache.uima.jcas.impl.JCasHashMap.put(JCasHashMap.java:139)
> 	at org.apache.uima.jcas.impl.JCasImpl.putJfsFromCaddr(JCasImpl.java:1035)
> 	at org.apache.uima.jcas.impl.JCasImpl$JCasFsGenerator.createFS(JCasImpl.java:847)
> 	at org.apache.uima.cas.impl.CASImpl.ll_getFSForRef(CASImpl.java:3117)
> 	at org.apache.uima.cas.impl.CASImpl.createFS(CASImpl.java:1773)
> 	at org.apache.uima.cas.impl.FeatureStructureImpl.getFeatureValue(FeatureStructureImpl.java:234)
> 	at org.apache.uima.util.CasCopier.copyFeatures(CasCopier.java:312)
> 	at org.apache.uima.util.CasCopier.copyFs(CasCopier.java:190)
> 	at org.apache.uima.util.CasCopier.copyCasView(CasCopier.java:149)
> 	at org.apache.uima.util.CasCopier.copyCas(CasCopier.java:108)
> 	at rulevolution.utils.JCasUtils.copyJCas(JCasUtils.java:80)
> 	at rulevolution.utils.JCasUtils.copyCasPool(JCasUtils.java:94)
> 	at rulevolution.RulEvolution.evaluate(RulEvolution.java:105)
> 	at ec.simple.SimpleEvaluator.evalPopChunk(SimpleEvaluator.java:259)
> 	at ec.simple.SimpleEvaluator$SimpleEvaluatorThreadCG.run(SimpleEvaluator.java:341)
> 	at ec.util.ThreadPool$PoolThread.run(ThreadPool.java:57)
> 
> Exception in thread "ECJ Evaluation Thread 2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 1
> 	at java.util.ArrayList.rangeCheck(Unknown Source)
> 	at java.util.ArrayList.get(Unknown Source)
> 	at org.apache.uima.cas.impl.FSIndexRepositoryImpl$LeafPointerIterator.initPointerIterator(FSIndexRepositoryImpl.java:628)
> 	at org.apache.uima.cas.impl.FSIndexRepositoryImpl$LeafPointerIterator.<init>(FSIndexRepositoryImpl.java:636)
> 	at org.apache.uima.cas.impl.FSIndexRepositoryImpl$LeafPointerIterator.<init>(FSIndexRepositoryImpl.java:612)
> 	at org.apache.uima.cas.impl.FSIndexRepositoryImpl.createPointerIterator(FSIndexRepositoryImpl.java:158)
> 	at org.apache.uima.cas.impl.FSIndexRepositoryImpl$IndexImpl.iterator(FSIndexRepositoryImpl.java:792)
> 	at org.apache.uima.util.CasCopier.copyCasView(CasCopier.java:145)
> 	at org.apache.uima.util.CasCopier.copyCas(CasCopier.java:108)
> 	at rulevolution.utils.JCasUtils.copyJCas(JCasUtils.java:80)
> 	at rulevolution.utils.JCasUtils.copyCasPool(JCasUtils.java:94)
> 	at rulevolution.RulEvolution.evaluate(RulEvolution.java:105)
> 	at ec.simple.SimpleEvaluator.evalPopChunk(SimpleEvaluator.java:259)
> 	at ec.simple.SimpleEvaluator$SimpleEvaluatorThreadCG.run(SimpleEvaluator.java:341)
> 	at ec.util.ThreadPool$PoolThread.run(ThreadPool.java:57)
> 
> My goal would be generating a JCas pool at beginning of the algorithm. Then cloning the entire 
> jcas pool for each rule evaluation. After the rule is evaluated the cloned cas pool needs to be
> deleted.
> 
> Does someone know why I'm getting these exceptions and are there some ways to 
> work with JCas objects outside of UIMA Concext? 
> 
> Greetings 
> 
> Bojan
> 


Re: Clone a JCas

Posted by Bojan Janisch <bo...@scai.fraunhofer.de>.
Hey Richard,

thanks for your help. A friend helped me also alot to fix this and we've come to a two-line solution:

//aJCas is the source JCas; jcas is an empty JCas which will be overridden. 

byte[] aBytes = SerializationUtils.serialize(Serialization.serializeCASComplete(aJCas.getCasImpl()));
Serialization.deserializeCASComplete((CASCompleteSerializer) SerializationUtils.deserialize(aBytes), jcas.getCasImpl());

This may be even faster than using an OutputStream, but I've not compared the performance. 

Greetings

Bojan

----- Ursprüngliche Mail -----
Von: "Richard Eckart de Castilho" <ri...@gmail.com>
An: user@uima.apache.org
Gesendet: Donnerstag, 4. Juli 2013 20:48:51
Betreff: Re: Clone a JCas

Something like this should work:

JCas jcas1 = …
	    
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream docOS = new ObjectOutputStream(buffer);
CASCompleteSerializer serializerOut = Serialization.serializeCASComplete(jcas1.getCasImpl());
docOS.writeObject(serializerOut);
docOS.close();

JCas jcas2 = …

ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
CASCompleteSerializer serializerIn = (CASCompleteSerializer) is.readObject();
Serialization.deserializeCASComplete(serializerIn, (CASImpl) jcas2.getCasImpl());

You could try without the buffer and just passing the seralizerOut as serializerIn, but I don't
know if that works because I don't know if the internal data structures are copied to the target
CAS or if they are directly injected.

-- Richard


Am 04.07.2013 um 15:35 schrieb Bojan Janisch <bo...@scai.fraunhofer.de>:

> Hey Richard,
> 
> I've tried your way, but getting NullPointerException over NullPointerException at the merging.
> 
> Exception in thread "ECJ Evaluation Thread 0" java.lang.NullPointerException
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:85)
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:50)
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl.createTypeOrder(LinearTypeOrderBuilderImpl.java:291)
> 	at org.apache.uima.cas.impl.CASMgrSerializer.getIndexRepository(CASMgrSerializer.java:462)
> 	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1079)
> 	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1071)
> 	at org.apache.uima.cas.impl.Serialization.deserializeCASComplete(Serialization.java:57)
> 	at rulevolution.utils.JCasUtils.cloneJCas(JCasUtils.java:257)
> 	at rulevolution.utils.JCasUtils.copyCasPool(JCasUtils.java:212)
> 	at rulevolution.RulEvolution.evaluate(RulEvolution.java:156)
> 	at ec.simple.SimpleEvaluator.evalPopChunk(SimpleEvaluator.java:259)
> 	at ec.simple.SimpleEvaluator$SimpleEvaluatorThreadCG.run(SimpleEvaluator.java:341)
> 	at ec.util.ThreadPool$PoolThread.run(ThreadPool.java:57)
> 
> 
> Do you or anybody else know where my mistake is? Here's the Code I've programmed for cloning:
> 
> public static JCas cloneJCas(JCas aJCas){
> 	JCas jcas = null;
> 	try {
> 		jcas = UIMATypeSystemUtils.getEmptyCasFromTypeSystem(AbstractDeployer.TYPESYSTEM).getJCas();
> 		
> 		CASCompleteSerializer complete = new CASCompleteSerializer();
> 		complete.setCasSerializer(new CASSerializer());
> 		complete.setCasMgrSerializer(new CASMgrSerializer());
> 		complete.getCASMgrSerializer().addTypeSystem(aJCas.getCasImpl().getTypeSystemImpl());
> 		complete.getCASSerializer().addCAS(aJCas.getCasImpl());
> 		byte[] aBytes = SerializationUtils.serialize(complete);
> 		
> 		deserializeCASComplete((CASCompleteSerializer)SerializationUtils.deserialize(aBytes), jcas.getCasImpl());
> 	} 
> 	....
> 	
> 	return jcas;
> }
> 
> The method which throws the exception:
> 
> private TotalTypeOrder(int[] typeList, TypeSystem ts) {
>      super();
>      TypeSystemImpl tsi = (TypeSystemImpl) ts;
>      this.order = typeList;
>      this.typeCodeToOrder = new int[this.order.length + tsi.getSmallestType()]; //Thats the line I get the exception
>      for (int i = 0; i < this.order.length; i++) {
> 	this.typeCodeToOrder[this.order[i]] = i;
>      }
> }
> 
> 
> I appreciate any help.
> 
> Greetings
> 
> Bojan


Re: Clone a JCas

Posted by Richard Eckart de Castilho <ri...@gmail.com>.
Something like this should work:

JCas jcas1 = …
	    
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream docOS = new ObjectOutputStream(buffer);
CASCompleteSerializer serializerOut = Serialization.serializeCASComplete(jcas1.getCasImpl());
docOS.writeObject(serializerOut);
docOS.close();

JCas jcas2 = …

ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
CASCompleteSerializer serializerIn = (CASCompleteSerializer) is.readObject();
Serialization.deserializeCASComplete(serializerIn, (CASImpl) jcas2.getCasImpl());

You could try without the buffer and just passing the seralizerOut as serializerIn, but I don't
know if that works because I don't know if the internal data structures are copied to the target
CAS or if they are directly injected.

-- Richard


Am 04.07.2013 um 15:35 schrieb Bojan Janisch <bo...@scai.fraunhofer.de>:

> Hey Richard,
> 
> I've tried your way, but getting NullPointerException over NullPointerException at the merging.
> 
> Exception in thread "ECJ Evaluation Thread 0" java.lang.NullPointerException
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:85)
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl$TotalTypeOrder.<init>(LinearTypeOrderBuilderImpl.java:50)
> 	at org.apache.uima.cas.impl.LinearTypeOrderBuilderImpl.createTypeOrder(LinearTypeOrderBuilderImpl.java:291)
> 	at org.apache.uima.cas.impl.CASMgrSerializer.getIndexRepository(CASMgrSerializer.java:462)
> 	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1079)
> 	at org.apache.uima.cas.impl.CASImpl.reinit(CASImpl.java:1071)
> 	at org.apache.uima.cas.impl.Serialization.deserializeCASComplete(Serialization.java:57)
> 	at rulevolution.utils.JCasUtils.cloneJCas(JCasUtils.java:257)
> 	at rulevolution.utils.JCasUtils.copyCasPool(JCasUtils.java:212)
> 	at rulevolution.RulEvolution.evaluate(RulEvolution.java:156)
> 	at ec.simple.SimpleEvaluator.evalPopChunk(SimpleEvaluator.java:259)
> 	at ec.simple.SimpleEvaluator$SimpleEvaluatorThreadCG.run(SimpleEvaluator.java:341)
> 	at ec.util.ThreadPool$PoolThread.run(ThreadPool.java:57)
> 
> 
> Do you or anybody else know where my mistake is? Here's the Code I've programmed for cloning:
> 
> public static JCas cloneJCas(JCas aJCas){
> 	JCas jcas = null;
> 	try {
> 		jcas = UIMATypeSystemUtils.getEmptyCasFromTypeSystem(AbstractDeployer.TYPESYSTEM).getJCas();
> 		
> 		CASCompleteSerializer complete = new CASCompleteSerializer();
> 		complete.setCasSerializer(new CASSerializer());
> 		complete.setCasMgrSerializer(new CASMgrSerializer());
> 		complete.getCASMgrSerializer().addTypeSystem(aJCas.getCasImpl().getTypeSystemImpl());
> 		complete.getCASSerializer().addCAS(aJCas.getCasImpl());
> 		byte[] aBytes = SerializationUtils.serialize(complete);
> 		
> 		deserializeCASComplete((CASCompleteSerializer)SerializationUtils.deserialize(aBytes), jcas.getCasImpl());
> 	} 
> 	....
> 	
> 	return jcas;
> }
> 
> The method which throws the exception:
> 
> private TotalTypeOrder(int[] typeList, TypeSystem ts) {
>      super();
>      TypeSystemImpl tsi = (TypeSystemImpl) ts;
>      this.order = typeList;
>      this.typeCodeToOrder = new int[this.order.length + tsi.getSmallestType()]; //Thats the line I get the exception
>      for (int i = 0; i < this.order.length; i++) {
> 	this.typeCodeToOrder[this.order[i]] = i;
>      }
> }
> 
> 
> I appreciate any help.
> 
> Greetings
> 
> Bojan