Skip to content

Commit

Permalink
change ako and isa serializer ( index-based to method-based )
Browse files Browse the repository at this point in the history
  • Loading branch information
krosse committed Aug 3, 2010
1 parent 6ca4aba commit f304d92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 80 deletions.
Expand Up @@ -14,12 +14,10 @@
import java.util.HashSet;
import java.util.Set;

import org.tmapi.core.Association;
import org.tmapi.core.ModelConstraintException;
import org.tmapi.core.Role;
import org.tmapi.core.Topic;
import org.tmapi.core.TopicMap;
import org.tmapi.index.TypeInstanceIndex;

import de.topicmapslab.ctm.writer.core.CTMTopicMapWriter;
import de.topicmapslab.ctm.writer.exception.SerializerException;
Expand Down Expand Up @@ -80,7 +78,7 @@ public static boolean serialize(CTMTopicMapWriter writer,

// storing type already written
Set<Topic> writtenTypes = new HashSet<Topic>(instance.getTypes());

/*
* check additional types by extracting the TMDM association type
*/
Expand Down Expand Up @@ -115,51 +113,28 @@ public static boolean serialize(CTMTopicMapWriter writer,
"Invalid association item of type 'is-instance-of' - unexprected role types."));
}

/*
* get type-instance-index
*/
TypeInstanceIndex index = topicMap
.getIndex(TypeInstanceIndex.class);
if ( !index.isOpen()){
index.open();
}
/*
* iterate over association items
*/
for (Association association : index.getAssociations(instanceOf)) {
/*
* extract instance-role player
*/
Set<Role> instancePlayers = association.getRoles(instanceRole);
if (instancePlayers.size() != 1) {
for (Role role : instance.getRolesPlayed(instanceRole)) {
Set<Role> typePlayers = role.getParent().getRoles(typeRole);
if (typePlayers.size() != 1) {
throw new SerializerException(
new ModelConstraintException(
association,
"Invalid association item of type 'is-instance-of' - expected number of players of role-type 'instance' is 1, but was"
+ instancePlayers.size()));
role.getParent(),
"Invalid association item of type 'is-instance-of' - expected number of players of role-type 'type' is 1, but was"
+ typePlayers.size()));
}
Topic instancePlayer = instancePlayers.iterator().next()
.getPlayer();
if (instancePlayer.equals(instance)) {
Set<Role> typePlayers = association.getRoles(typeRole);
if (typePlayers.size() != 1) {
throw new SerializerException(
new ModelConstraintException(
association,
"Invalid association item of type 'is-instance-of' - expected number of players of role-type 'type' is 1, but was"
+ typePlayers.size()));
}
/*
* add to buffer
*/
Topic newType = typePlayers.iterator().next().getPlayer();
if (!writtenTypes.contains(newType)) {
buffer.appendTailLine(true, TABULATOR, ISA, writer
.getCtmIdentity().getMainIdentifier(
writer.getProperties(),
newType)
.toString());
}
/*
* add to buffer
*/
Topic newType = typePlayers.iterator().next().getPlayer();
if (!writtenTypes.contains(newType)) {
buffer
.appendTailLine(true, TABULATOR, ISA, writer
.getCtmIdentity().getMainIdentifier(
writer.getProperties(), newType)
.toString());
}
}
returnValue = true;
Expand Down
Expand Up @@ -11,12 +11,10 @@
import java.util.HashSet;
import java.util.Set;

import org.tmapi.core.Association;
import org.tmapi.core.ModelConstraintException;
import org.tmapi.core.Role;
import org.tmapi.core.Topic;
import org.tmapi.core.TopicMap;
import org.tmapi.index.TypeInstanceIndex;

import de.topicmapslab.identifier.TmdmSubjectIdentifier;

Expand Down Expand Up @@ -82,48 +80,22 @@ public static Set<Topic> getSupertypes(final Topic subtype)
"Invalid association item of type 'supertype-subtype' - unexprected role types.");
}

/*
* get type-instance-index
*/
TypeInstanceIndex index = topicMap
.getIndex(TypeInstanceIndex.class);
if ( !index.isOpen()){
index.open();
}
/*
* iterate over all association items
*/
for (Association association : index.getAssociations(kindOf)) {
Set<Role> subtypePlayers = association.getRoles(subtypeRole);
if (subtypePlayers.size() != 1) {
for (Role role : kindOf.getRolesPlayed(subtypeRole)) {
Set<Role> supertypePlayers = role.getParent()
.getRoles(supertypeRole);
if (supertypePlayers.size() != 1) {
throw new ModelConstraintException(
association,
"Invalid association item of type 'supertype-subtype' - expected number of players of role-type 'subtype' is 1, but was"
+ subtypePlayers.size());
role.getParent(),
"Invalid association item of type 'supertype-subtype' - expected number of players of role-type 'supertype' is 1, but was"
+ supertypePlayers.size());
}
/*
* extract player of subtype-role
* add supertype-role player
*/
Topic subtypePlayer = subtypePlayers.iterator().next()
.getPlayer();
/*
* check if player equals given type
*/
if (subtypePlayer.equals(subtype)) {
Set<Role> supertypePlayers = association
.getRoles(supertypeRole);
if (supertypePlayers.size() != 1) {
throw new ModelConstraintException(
association,
"Invalid association item of type 'supertype-subtype' - expected number of players of role-type 'supertype' is 1, but was"
+ supertypePlayers.size());
}
/*
* add supertype-role player
*/
supertypes.add(supertypePlayers.iterator().next()
.getPlayer());
}
supertypes.add(supertypePlayers.iterator().next().getPlayer());
}
}
return supertypes;
Expand Down
2 changes: 1 addition & 1 deletion ctm-serializer/src/test/java/TestTMCL.java
Expand Up @@ -562,7 +562,7 @@ private void addHasName() {

t.add(t1);

// t.setScanner(new HasNameTemplateScanner());
t.setScanner(new HasNameTemplateScanner());

templates.add(t);

Expand Down

0 comments on commit f304d92

Please sign in to comment.