Error: EMF4SW Trasformation to EMF without new of RDF2Model

12 views
Skip to first unread message

dpndeveloper

unread,
Apr 18, 2011, 5:27:23 AM4/18/11
to emftriple-discuss
hi. i modified the code of example in
http://groups.google.com/group/emftriple-discuss/browse_thread/thread/f752d0785f7caff2

I used 2 static variables
private static RDF2Model r2m;
private static Model2RDF m2r;
and i used it to transform to EMF and to RDF n times.

Trasform to RDF always works, without new every time I use the
variable
Trasform to EMF work only if i do new of the variable anytime

Is it possible to use RDF2Model without re-new it, because it's
enought slow operation?

The code is the follow:

public class RDFTransformExample {

private static RDF2Model r2m;
private static Model2RDF m2r;
private static Map<String, Object> options;

public static void main(String[] args) throws IOException {
initialize();
trasformToRdf();
}

private static void initialize() {
// Register Resource Factories

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi",
new XMIResourceFactoryImpl());

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ttl",
new TTLResourceFactory());
// Register Metamodel
Models.register(NetworkPackage.eINSTANCE);

m2r = new Model2RDF();
r2m= new RDF2Model(NetworkPackage.eINSTANCE);
options = new HashMap<String, Object>();
options.put(RDFTransformation.OPTION_RDF_FORMAT,
RDFFormats.TURTLE_FORMAT);
}

private static void trasformToRdf() throws IOException {
// Create EObjects
SocialNetwork s =
NetworkFactory.eINSTANCE.createSocialNetwork();
s.setName("sc");
Person p1 = NetworkFactory.eINSTANCE.createPerson();
p1.setFirstName("John");
p1.setLastName("Smith");
p1.setId(1);
Person p2 = NetworkFactory.eINSTANCE.createPerson();
p2.setFirstName("Paul");
p2.setLastName("Robert");
p2.setId(2);
p2.getKnows().add(p1);
s.getMembers().add(p1);
s.getMembers().add(p2);
// Add root object to the resource
Resource resource = new XMIResourceImpl();
resource.getContents().add(s);

for (int i=1; i<=3; i++){
// Execute Model2RDF
System.out.println("Trasformation to RDF number: "+i);

//NOTE: The trasformation works always! i don't need to do new of
m2r
//m2r = new Model2RDF();

Resource rdf = m2r.transform(resource, options);
// Save output RDF
rdf.setURI(URI.createURI("out/model"+i+".ttl"));
rdf.save(null);
trasformToEmf(i, rdf);
}
}

private static void trasformToEmf(int index, Resource rdf) {
try{
System.out.println("Trasformation to EMF number: "+index);

// Execute RDF2Model

//NOTE: The trasformation works only if i de-comment next line but
this operation is enougth slow
//r2m = new RDF2Model(NetworkPackage.eINSTANCE);

Resource model = r2m.transform(rdf, options);
// Save output XMI
model.setURI(URI.createURI("out/model"+index+".xmi"));
model.save(null);
}catch (Exception e) {
e.printStackTrace();
}
}
}

If i execute this code, trasformation to EMF works only first time and
the output is:
Trasformation to RDF number: 1
Trasformation to EMF number: 1
Trasformation to RDF number: 2
Trasformation to EMF number: 2
java.lang.NullPointerException
at emf4sw.RDFTransformExample.trasformToEmf(RDFTransformExample.java:
102)
at emf4sw.RDFTransformExample.trasformToRdf(RDFTransformExample.java:
87)
at emf4sw.RDFTransformExample.main(RDFTransformExample.java:37)
Trasformation to RDF number: 3
Trasformation to EMF number: 3
java.lang.NullPointerException
at emf4sw.RDFTransformExample.trasformToEmf(RDFTransformExample.java:
102)
at emf4sw.RDFTransformExample.trasformToRdf(RDFTransformExample.java:
87)
at emf4sw.RDFTransformExample.main(RDFTransformExample.java:37)

If i de-comment line
//NOTE: The trasformation works only if i de-comment next line but
this operation is enougth slow
//r2m = new RDF2Model(NetworkPackage.eINSTANCE);
the code works and the output is:
Trasformation to RDF number: 1
Trasformation to EMF number: 1
Trasformation to RDF number: 2
Trasformation to EMF number: 2
Trasformation to RDF number: 3
Trasformation to EMF number: 3

Is it possible to use RDF2Model without re-new it?

Thanks for support
Domenico
Reply all
Reply to author
Forward
0 new messages