Some days ago, I wrote a post about how to export a Topicmap in Deepamehta. This functionality is part of the final goal of doing an Import/Export plugin.

In this post, I will explain how have I done the import Topicmap method, that I will probably change in the next days, but in any case, I’ll tell what have I done until now 🙂

Let’s see what do we need to actually import a Topicmap.

  1. Have a Topicmap file to import, obvious 😉
  2. Create a new Topicmap where we will place the topics and associations of our exported Topicmap
  3. Add the topics of our exported Topicmap to our new Topicmap
  4. Add the associations of our exported Topicmap to our new Topicmap

So let’s proceed

First, read the file where the exported Topicmap is. For the moment, and to do things step by step, I’ve hard coded the file that I want to import.

As I said in the previous post, I exported the Topicmap into a JSON file placed in runner directory, that is at the root of your DM instance. We are going to read the same file that was exported there.

File file = new File("topicmap-11216.json");
String json = JavaUtils.readTextFile(file);

Now we will define a JSON Object from the read file

JSONObject topicmap = new JSONObject(json);

So the exported file with the Topicmap we have it accessable in JSON format in the variable topicmap.

The next task was to have a new Topicmap, where we will add the topics and the associations from the exported Topicmap.

Topic importedTopicmap = topicmapsService.createTopicmap("Imported Topicmap","dm4.webclient.default_topicmap_renderer", null);

To create a new map we need topicmapService, so you need to import it, that means add it to the rest of the import at the top of the file.

import de.deepamehta.plugins.topicmaps.service.TopicmapsService;

Fine, let’s continue. Which one was our third task? To add the topics of our exported Topicmap to our new Topicmap.

We will first create an array with the topics by extracting the information from the JSON that we already have.

We will first take a look at our JSON file, to parse it and visualize it you can check


So you can see that we have 3 parent objects: assocs, info and topics.

We are focusing in the topics, so we can see that there are 4 topics that need to be imported, we will access to the topics by getting the array associated to the key “topics”.

JSONArray topicsArray = topicmap.getJSONArray("topics");

When importing topics, there is one important thing to have in mind.
The way to retrieve the associations between topics, is because we establish an association between 2 topics through the topicIc, something like, association1 relates topicId=12 with topicId 23. So we have to map the new topicIds with the exported topicIds

We know that we are adding new topics to a recently created Topicmap. The id of the new topic will be automatically generated and set, so we don’t have the option of setting the id of the new topic to be the same as the id of the topic exported. That means that we have to keep track of which new topicId corresponds to the exported topicId by creating a Hash.

We define the Hash to map topic Ids.

Map<Long, Long> mapTopicIds = new HashMap();

In order to get the topics we have to iterate through the topicsArray by doing (for example)

 for (int i = 0, size = topicsArray.length(); i < size; i++)

By iterating we are going to get individual topics. If we take another look at the jsonparser, we can see that a topic is determined by an id, type_uri, uri, value, composite and view_props.

In each iteration we are going to get the content of all of these attributes.

 for (int i = 0, size = topicsArray.length(); i < size; i++)
                    JSONObject topic =  topicsArray.getJSONObject(i);
                    TopicModel model = new TopicModel(topic);
                    CompositeValueModel viewProps =new CompositeValueModel(topic.getJSONObject("view_props"));

                    Topic newTopic =  dms.createTopic(model, null);

                    long topicId = newTopic.getId();
                    long origTopicId = topic.getLong("id");
                    mapTopicIds.put(origTopicId, topicId);
                    topicmapsService.addTopicToTopicmap(topicmapId, topicId, viewProps);


To understand the loop, we’ll go line by line. By invoking getJSONObject(i) we will extract topic i.
On the other side we know that the createTopic method is provided by Deepamehta Servide (dms), where you need to pass a model and clientState. The clientState will be null.
The model we will be created by

TopicModel model = new TopicModel(topic);

so with a model and clientState we create the new Topic

Topic newTopic =  dms.createTopic(model, null);

We said that we have to keep the relation between new Topics id(newTopic.getId()) and exported Topics id(topic.getLong(“id”)), so we do that

long topicId = newTopic.getId();
long origTopicId = topic.getLong("id");
mapTopicIds.put(origTopicId, topicId);

In order to be able to add the new Topic to the new Topicmap, the topicmapService provides us with a couple of methods, the one we will choose is

addTopicToTopicmap(long topicmapId, long topicId, CompositeValueModel viewProps);

so before being able to add a Topic to a Topicmap, we need to get the CompositeValueModel, viewProps from the exported Topic.

 CompositeValueModel viewProps =new CompositeValueModel(topic.getJSONObject("view_props"));

Finally we can add the Topic with the info coming from the exportation to the new Topicmap

topicmapsService.addTopicToTopicmap(topicmapId, topicId, viewProps);

Wowww…now if you go to the webClient and click on import Topicmap, you will see how the topics appear in a new Topicmap :-))

And our last step was a”Add the associations of our exported Topicmap to our new Topicmap”

As with the topics we are going to extract the associations of the JSON file into an Array

JSONArray assocsArray = topicmap.getJSONArray("assocs");

We will iterate through the Array

 for (int i=0, size = assocsArray.length(); i< size; i++)

Take a moment, to check in the jsonparser, that the elements that form an association are id, role1, role2, type_uri, uri, value and composite. Inside role1 and role2 is stored the reference to the topicId to the "two sides" of the association.


As with Topics, DeepaMehta Service provides us with a createAssociation method

Association newAssociation = dms.createAssociation(assocModel, null);

We can create a new Association Model by

AssociationModel assocModel = new AssociationModel(assocsArray.getJSONObject(i));

but the important thing is to actually associate the two topics "coming" from our export file. To track this association we have already built a Hash that maps the topicId from the exported file with the new topicId, now we are going to use that mapping. So we have to change the values created automatically when creating an AssociationModel and set them with the values of the exported Topicmap.

RoleModel role1 = assocModel.getRoleModel1();
RoleModel role2 = assocModel.getRoleModel2();

So first, we get the RoleModel of the new association and we set the topicId(playerId is a general term that can be refered to a topicId or an associationId) to the value that was stored on our hash called mapTopicIds.

We are almost done 🙂 .. just one step missing, to add an Association to a Topicmap, the need the id of the new association, with that we are able to use the method provided by topicmapsService, AddAssociationToTopicmap to add our associations to our imported Topicmap 🙂

long assocId = newAssociation.getId()              topicmapsService.addAssociationToTopicmap(topicmapId, assocId);

So, if you go to the webClient and chose to Import a Topicmap, you'll see it directly on the screen, as an example the one that I have 🙂