From: <lh...@us...> - 2009-07-15 11:12:55
|
Revision: 314 http://tinytim.svn.sourceforge.net/tinytim/?rev=314&view=rev Author: lheuer Date: 2009-07-15 11:12:44 +0000 (Wed, 15 Jul 2009) Log Message: ----------- Applied patch provided by Hannes Niederhausen (CTMWriter & co). Modified Paths: -------------- tinytim-mio/trunk/build.xml tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMTopicMapWriter.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTMCLPreprocessor.java tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTemplate.java Added Paths: ----------- tinytim-mio/trunk/lib/tinytim-2.0.0a5-snapshot-200907151305.jar Removed Paths: ------------- tinytim-mio/trunk/lib/tinytim-2.0.0a5-snapshot-20090702.jar Modified: tinytim-mio/trunk/build.xml =================================================================== --- tinytim-mio/trunk/build.xml 2009-07-14 15:54:26 UTC (rev 313) +++ tinytim-mio/trunk/build.xml 2009-07-15 11:12:44 UTC (rev 314) @@ -10,7 +10,7 @@ <property name="lib.junit" value="${dir.lib}/junit-4.5.jar"/> <property name="lib.tmapi" value="${dir.lib}/tmapi-2.0a2.jar"/> - <property name="lib.tinytim" value="${dir.lib}/tinytim-2.0.0a5-snapshot-20090702.jar"/> + <property name="lib.tinytim" value="${dir.lib}/tinytim-2.0.0a5-snapshot-200907151305.jar"/> <property name="lib.tinytim.tests" value="${dir.lib}/tinytim-2.0.0a4-tests.jar"/> <property name="lib.logging" value="${dir.lib}/slf4j-api-1.5.8.jar"/> <property name="lib.mio" value="${dir.lib}/semagia-mio-0.9.4.jar"/> @@ -131,7 +131,6 @@ <javac destdir="${dir.build.classes}" debug="${debug}" target="1.5" - excludes="**/CTMTopicMapWriter.java, **/internal/**" > <classpath> <pathelement location="${lib.tmapi}"/> Deleted: tinytim-mio/trunk/lib/tinytim-2.0.0a5-snapshot-20090702.jar =================================================================== (Binary files differ) Added: tinytim-mio/trunk/lib/tinytim-2.0.0a5-snapshot-200907151305.jar =================================================================== (Binary files differ) Property changes on: tinytim-mio/trunk/lib/tinytim-2.0.0a5-snapshot-200907151305.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMTopicMapWriter.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMTopicMapWriter.java 2009-07-14 15:54:26 UTC (rev 313) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/CTMTopicMapWriter.java 2009-07-15 11:12:44 UTC (rev 314) @@ -68,6 +68,7 @@ * representation. * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @author Hannes Niederhausen * @version $Rev$ - $Date$ */ public class CTMTopicMapWriter implements TopicMapWriter { @@ -541,33 +542,32 @@ case TOPIC: { if (_tmcl) { toFilter = new Locator[] { + // Topic types TMCL.TOPIC_TYPE, - TMCL.OCCURRENCE_TYPE, TMCL.ASSOCIATION_TYPE, TMCL.ROLE_TYPE, + TMCL.OCCURRENCE_TYPE, TMCL.NAME_TYPE, TMCL.SCOPE_TYPE, + + // Model topics + TMCL.SCHEMA, TMCL.CONSTRAINT, + // Constraint types - TMCL.TOPIC_TYPE_CONSTRAINT, - TMCL.ASSOCIATION_TYPE_CONSTRAINT, - TMCL.ASSOCIATION_ROLE_TYPE, - TMCL.OCCURRENCE_TYPE_CONSTRAINT, - TMCL.NAME_TYPE_CONSTRAINT, TMCL.ABSTRACT_TOPIC_TYPE_CONSTRAINT, - TMCL.EXCLUSIVE_INSTANCE, + TMCL.OVERLAP_DECLARATION, TMCL.SUBJECT_IDENTIFIER_CONSTRAINT, TMCL.SUBJECT_LOCATOR_CONSTRAINT, - TMCL.NAME_TYPE, - TMCL.NAME_TYPE_SCOPE_CONSTRAINT, - TMCL.TOPIC_OCCURRENCE_TYPE, - TMCL.ASSOCIATION_TYPE_SCOPE_CONSTRAINT, - TMCL.OCCURRENCE_DATATYPE_CONSTRAINT, + TMCL.TOPIC_NAME_CONSTRAINT, TMCL.TOPIC_OCCURRENCE_CONSTRAINT, + TMCL.ROLE_PLAYER_CONSTRAINT, TMCL.SCOPE_CONSTRAINT, + TMCL.REIFIER_CONSTRAINT, TMCL.ASSOCIATION_ROLE_CONSTRAINT, - TMCL.ASSOCIATION_ROLE_PLAYER, - TMCL.OTHERROLE_CONSTRAINT, - TMCL.UNIQUE_OCCURRENCE_CONSTRAINT, + TMCL.OTHER_ROLE_CONSTRAINT, + TMCL.OCCURRENCE_DATATYPE_CONSTRAINT, + TMCL.UNIQUE_VALUE_CONSTRAINT, + TMCL.REGULAR_EXPRESSION_CONSTRAINT }; } break; @@ -578,7 +578,16 @@ toFilter = new Locator[] { toFilter[0], toFilter[1], - TMCL.APPLIES_TO + // Association types - applies-to is no more + TMCL.CONSTRAINED_TOPIC_TYPE, + TMCL.CONSTRAINED_STATEMENT, + TMCL.CONSTRAINED_ROLE, + TMCL.OVERLAPS, + TMCL.ALLOWED_SCOPE, + TMCL.ALLOWED_REIFIER, + TMCL.OTHER_CONSTRAINED_TOPIC_TYPE, + TMCL.OTHER_CONSTRAINED_ROLE, + TMCL.BELONGS_TO_SCHEMA, }; } break; @@ -587,14 +596,13 @@ toFilter = new Locator[] {TMDM.TYPE, TMDM.INSTANCE, TMDM.SUPERTYPE, TMDM.SUBTYPE}; if (_tmcl) { toFilter = new Locator[] {toFilter[0], toFilter[1], toFilter[2], toFilter[3], - TMCL.TOPIC_TYPE_ROLE, - TMCL.OCCURRENCE_TYPE_ROLE, - TMCL.ASSOCIATION_TYPE_ROLE, - TMCL.ROLE_TYPE_ROLE, - TMCL.OTHERROLE_TYPE_ROLE, - TMCL.NAME_TYPE_ROLE, - TMCL.SCOPE_TYPE_ROLE, - TMCL.CONSTRAINT_ROLE + // Role types + TMCL.ALLOWS, + TMCL.ALLOWED, + TMCL.CONSTRAINS, + TMCL.CONSTRAINED, + TMCL.CONTAINER, + TMCL.CONTAINEE }; } break; @@ -606,7 +614,7 @@ TMCL.CARD_MAX, TMCL.DATATYPE, TMCL.REGEXP, - TMCL.VALIDATION_EXPRESSION, + TMCL.VALIDATION_EXPRESSION }; } break; @@ -793,10 +801,7 @@ _writeSupertypeSubtype(supertype, wantSemicolon); wantSemicolon = true; } - for (ITemplate tpl: _getTemplates(topic)) { - _writeTemplate(tpl, wantSemicolon); - wantSemicolon = true; - } + for (Name name: _getNames(topic)) { _writeName((IName) name, wantSemicolon); wantSemicolon = true; @@ -813,6 +818,10 @@ _writeTopicRef(slo, wantSemicolon); wantSemicolon = true; } + for (ITemplate tpl: _getTemplates(topic)) { + _writeTemplate(tpl, wantSemicolon); + wantSemicolon = true; + } if (_exportIIDs) { for (Reference iid: _getItemIdentifiers(topic)) { _writeTopicRef(iid, wantSemicolon); Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTMCLPreprocessor.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTMCLPreprocessor.java 2009-07-14 15:54:26 UTC (rev 313) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTMCLPreprocessor.java 2009-07-15 11:12:44 UTC (rev 314) @@ -19,17 +19,18 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Logger; -import org.tinytim.mio.internal.ctm.ITMCLPreprocessor; -import org.tinytim.mio.internal.ctm.ITemplate; +import org.tinytim.core.value.Literal; import org.tinytim.internal.api.IIndexManagerAware; import org.tinytim.internal.api.ILiteral; import org.tinytim.internal.api.IOccurrence; +import org.tinytim.mio.internal.ctm.ITMCLPreprocessor; +import org.tinytim.mio.internal.ctm.ITemplate; import org.tinytim.voc.TMCL; - import org.tmapi.core.Association; import org.tmapi.core.Locator; import org.tmapi.core.Occurrence; @@ -42,20 +43,46 @@ * * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev:$ - $Date:$ + * @author Hannes Niederhausen + * @version $Rev$ - $Date$ */ public class DefaultTMCLPreprocessor implements ITMCLPreprocessor { - private static final Logger LOG = Logger.getLogger(DefaultTMCLPreprocessor.class.getName()); + private static final Logger LOG = Logger + .getLogger(DefaultTMCLPreprocessor.class.getName()); private final Map<Topic, Collection<ITemplate>> _topic2Templates; - private Topic _topicType; + private Topic _constrains; - private Topic _appliesTo; + private Topic _constrained; - private Topic _constraintRole; + private Topic _container; + private Topic _containee; + + private Topic _constrained_topic_type; + + private Topic _other_constrained_topic_type; + + private Topic _constrained_statement; + + private Topic _constrained_role; + + private Topic _belongs_to_schema; + + private Topic _other_constrained_role; + + private Topic _overlaps; + + private Topic _allowed_scope; + + private Topic _allowed_reifier; + + private Topic _allowed; + + private Topic _allows; + private Topic _cardMin; private Topic _cardMax; @@ -64,19 +91,21 @@ private Topic _regEx; - private Topic _topicTypeRole; + private ILiteral _cardMinDefault; - private Topic _scopeTypeRole; + private ILiteral _cardMaxDefault; - private Topic _assocTypeRole; + private ILiteral _regExDefault; - public DefaultTMCLPreprocessor() { _topic2Templates = new HashMap<Topic, Collection<ITemplate>>(); } - /* (non-Javadoc) - * @see org.tinytim.mio.internal.ctm.ITMCLPreprocessor#getSuppressableSubjectIdentifiers() + /* + * (non-Javadoc) + * + * @seeorg.tinytim.mio.internal.ctm.ITMCLPreprocessor# + * getSuppressableSubjectIdentifiers() */ @Override public Set<Locator> getSuppressableSubjectIdentifiers() { @@ -84,68 +113,176 @@ return null; } - /* (non-Javadoc) - * @see org.tinytim.mio.internal.ctm.ITMCLPreprocessor#getTopicToTemplatesMapping() + /* + * (non-Javadoc) + * + * @see + * org.tinytim.mio.internal.ctm.ITMCLPreprocessor#getTopicToTemplatesMapping + * () */ @Override public Map<Topic, Collection<ITemplate>> getTopicToTemplatesMapping() { - return _topic2Templates; + return _topic2Templates; } - /* (non-Javadoc) - * @see org.tinytim.mio.internal.ctm.ITMCLPreprocessor#process(org.tmapi.core.TopicMap, java.util.Collection, java.util.Collection) + /* + * (non-Javadoc) + * + * @see + * org.tinytim.mio.internal.ctm.ITMCLPreprocessor#process(org.tmapi.core + * .TopicMap, java.util.Collection, java.util.Collection) */ @Override public void process(TopicMap topicMap, Collection<Topic> topics, Collection<Association> assocs) { _init(topicMap); - TypeInstanceIndex tiIdx = ((IIndexManagerAware) topicMap).getIndexManager().getTypeInstanceIndex(); + TypeInstanceIndex tiIdx = ((IIndexManagerAware) topicMap) + .getIndexManager().getTypeInstanceIndex(); if (!tiIdx.isAutoUpdated()) { tiIdx.reindex(); } _processAbstractTopicConstraints(topicMap, tiIdx, topics, assocs); + _processUniqueConstraints(topicMap, tiIdx, topics, assocs); + _processDatatypeConstraints(topicMap, tiIdx, topics, assocs); + _processRegExConstraints(topicMap, tiIdx, topics, assocs); _processSubjectIdentifierConstraints(topicMap, tiIdx, topics, assocs); _processSubjectLocatorConstraints(topicMap, tiIdx, topics, assocs); - _processAssociationTypeScopeConstraints(topicMap, tiIdx, topics, assocs); - _processRoleConstraints(topicMap, tiIdx, topics, assocs); - _processPlayerConstraints(topicMap, tiIdx, topics, assocs); + _processOverlapConstraints(topicMap, tiIdx, topics, assocs); + _processAssociationRoleConstraints(topicMap, tiIdx, topics, assocs); + _processRolePlayerConstraints(topicMap, tiIdx, topics, assocs); + _processOtherRoleConstraints(topicMap, tiIdx, topics, assocs); _processOccurrenceConstraints(topicMap, tiIdx, topics, assocs); - _processOccurrenceTypeScopeConstraints(topicMap, tiIdx, topics, assocs); _processNameConstraints(topicMap, tiIdx, topics, assocs); - _processNameTypeScopeConstraints(topicMap, tiIdx, topics, assocs); + _processScopeConstraints(topicMap, tiIdx, topics, assocs); + _processReifierConstraints(topicMap, tiIdx, topics, assocs); + _processBelongsToSchema(topicMap, tiIdx, topics, assocs); } - private Collection<Topic> _getConstraintInstances(TopicMap topicMap, TypeInstanceIndex tiIdx, Locator subjectIdentifier) { - final Topic constraintType = topicMap.getTopicBySubjectIdentifier(subjectIdentifier); + private Collection<Topic> _getConstraintInstances(TopicMap topicMap, + TypeInstanceIndex tiIdx, Locator subjectIdentifier) { + final Topic constraintType = topicMap + .getTopicBySubjectIdentifier(subjectIdentifier); if (constraintType == null) { return Collections.emptySet(); } return tiIdx.getTopics(constraintType); } - private Collection<Topic> _getTopicTypeRolePlayers(Topic constraint, Collection<Association> assocs) { - return _getPlayers(constraint, _constraintRole, _topicTypeRole, assocs); + private Topic _getConstrainedTopicTypePlayer(Topic constraint, + Collection<Association> assocs) { + for (Role role : constraint.getRolesPlayed(_constrains, + _constrained_topic_type)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + for (Role otherRole : assoc.getRoles(_constrained)) { + assocs.remove(assoc); + return otherRole.getPlayer(); + } + } + return null; } - private Collection<Topic> _getPlayers(Topic constraint, Topic constraintRoleType, Topic otherRoleType, Collection<Association> assocs) { - Collection<Topic> result = new ArrayList<Topic>(); - for (Role role: constraint.getRolesPlayed(constraintRoleType, _appliesTo)) { + private Topic _getConstrainedRolePlayer(Topic constraint, + Collection<Association> assocs) { + for (Role role : constraint.getRolesPlayed(_constrains, + _constrained_role)) { Association assoc = role.getParent(); if (!_isBinary(assoc)) { continue; } - for (Role otherRole: assoc.getRoles(otherRoleType)) { + for (Role otherRole : assoc.getRoles(_constrained)) { assocs.remove(assoc); - result.add(otherRole.getPlayer()); + return otherRole.getPlayer(); } } - return result; + return null; } + private Topic _getOtherConstrainedTopicTypePlayer(Topic constraint, + Collection<Association> assocs) { + for (Role role : constraint.getRolesPlayed(_constrains, + _other_constrained_topic_type)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + for (Role otherRole : assoc.getRoles(_constrained)) { + assocs.remove(assoc); + return otherRole.getPlayer(); + } + } + return null; + } + + private Topic _getOtherConstrainedRolePlayer(Topic constraint, + Collection<Association> assocs) { + for (Role role : constraint.getRolesPlayed(_constrains, + _other_constrained_role)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + for (Role otherRole : assoc.getRoles(_constrained)) { + assocs.remove(assoc); + return otherRole.getPlayer(); + } + } + return null; + } + + private Topic _getConstrainedStatementPlayer(Topic constraint, + Collection<Association> assocs) { + Topic player = null; + for (Role role : constraint.getRolesPlayed(_constrains, + _constrained_statement)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + + player = assoc.getRoles(_constrained).iterator().next().getPlayer(); + assocs.remove(assoc); + } + return player; + } + + private Topic _getAllowedScopePlayer(Topic constraint, + Collection<Association> assocs) { + Topic player = null; + for (Role role : constraint.getRolesPlayed(_allows, _allowed_scope)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + + player = assoc.getRoles(_allowed).iterator().next().getPlayer(); + assocs.remove(assoc); + } + return player; + } + + private Topic _getAllowedReifierPlayer(Topic constraint, + Collection<Association> assocs) { + Topic player = null; + for (Role role : constraint.getRolesPlayed(_allows, _allowed_reifier)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + + player = assoc.getRoles(_allowed).iterator().next().getPlayer(); + assocs.remove(assoc); + } + return player; + } + private void _processAbstractTopicConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.ABSTRACT_TOPIC_TYPE_CONSTRAINT)) { + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.ABSTRACT_TOPIC_TYPE_CONSTRAINT)) { _processAbstractTopicConstraint(constraint, topics, assocs); } } @@ -153,213 +290,397 @@ private void _processAbstractTopicConstraint(Topic constraint, Collection<Topic> topics, Collection<Association> assocs) { ITemplate tpl = new DefaultTemplate("isAbstract"); - Collection<Topic> players = _getTopicTypeRolePlayers(constraint, assocs); - for (Topic player: players) { - _registerTemplate(player, tpl); - } - if (players.size() == constraint.getRolesPlayed().size() - && constraint.getOccurrences().isEmpty() - && constraint.getNames().isEmpty() - && constraint.getTypes().size() == 1 - && constraint.getSubjectLocators().isEmpty() - && constraint.getSubjectIdentifiers().isEmpty()) { - topics.remove(constraint); - } + Topic player = _getConstrainedTopicTypePlayer(constraint, assocs); + _registerTemplate(player, tpl); + removeConstraint(constraint, topics, 0); } - private void _processSubjectIdentifierConstraints(TopicMap topicMap, + private void _processAssociationRoleConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - _processLocatorConstraints(topicMap, tiIdx, "has-subjectidentifier", TMCL.SUBJECT_IDENTIFIER_CONSTRAINT, topics, assocs); + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.ASSOCIATION_ROLE_CONSTRAINT)) { + _processAssociationRoleConstraint(constraint, topics, assocs); + } + } - private void _processSubjectLocatorConstraints(TopicMap topicMap, + private void _processAssociationRoleConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + Topic player = _getConstrainedStatementPlayer(constraint, assocs); + Topic roleType = _getConstrainedRolePlayer(constraint, assocs); + + DefaultTemplate tpl = new DefaultTemplate("has-role"); + tpl.addParameter(roleType); + + int occCounter = _assignCardinality(constraint, tpl, 2); + + _registerTemplate(player, tpl); + removeConstraint(constraint, topics, occCounter); + } + + private void _processBelongsToSchema(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - _processLocatorConstraints(topicMap, tiIdx, "has-subjectlocator", TMCL.SUBJECT_LOCATOR_CONSTRAINT, topics, assocs); + for (Topic schema : _getConstraintInstances(topicMap, tiIdx, + TMCL.SCHEMA)) { + _processBelongsToSchema(schema, topics, assocs); + } } - private void _processLocatorConstraints(TopicMap topicMap, - TypeInstanceIndex tiIdx, String templateName, Locator constraintSID, Collection<Topic> topics, + private void _processBelongsToSchema(Topic schema, + Collection<Topic> topics, Collection<Association> assocs) { + + DefaultTemplate tpl = new DefaultTemplate("belongs-to"); + tpl.addParameter(schema); + + for (Role role : schema.getRolesPlayed(_container, _belongs_to_schema)) { + Association assoc = role.getParent(); + if (!_isBinary(assoc)) { + continue; + } + Topic construct = assoc.getRoles(_containee).iterator().next() + .getPlayer(); + + _registerTemplate(construct, tpl); + } + } + + private void _processReifierConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, constraintSID)) { - _processLocatorConstraint(constraint, templateName, constraintSID, topics, assocs); + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.BELONGS_TO_SCHEMA)) { + _processReifierConstraint(constraint, topics, assocs); } + } - private void _processLocatorConstraint(Topic constraint, - String templateName, Locator constraintSID, Collection<Topic> topics, - Collection<Association> assocs) { + private void _processReifierConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + Topic reifierType = _getConstrainedStatementPlayer(constraint, assocs); + Topic reifiableType = _getAllowedReifierPlayer(constraint, assocs); + ILiteral cardMin = _getCardMin(constraint); ILiteral cardMax = _getCardMax(constraint); - ILiteral regEx = _getRegEx(constraint); - //TODO: Default values? - if (cardMin == null || cardMax == null || regEx == null) { - return; + DefaultTemplate tpl = null; + if (cardMin.getValue().equals(cardMax.getValue())) { + if (cardMin.getValue().equals("0")) { + tpl = new DefaultTemplate("cannot-have-reifier"); + } + else { + tpl = new DefaultTemplate("must-have-reifier"); + tpl.addParameter(reifierType); + } } - DefaultTemplate tpl = new DefaultTemplate(templateName); - tpl.addParameter(cardMin); - tpl.addParameter(cardMax); - tpl.addParameter(regEx); - Collection<Topic> players = _getTopicTypeRolePlayers(constraint, assocs); - for (Topic player: players) { - _registerTemplate(player, tpl); + else { + tpl = new DefaultTemplate("may-have-reifier"); + tpl.addParameter(reifierType); + } - if (players.size() == constraint.getRolesPlayed().size() - && constraint.getOccurrences().size() == 3 - && constraint.getNames().isEmpty() - && constraint.getTypes().size() == 1 - && constraint.getSubjectLocators().isEmpty() - && constraint.getSubjectIdentifiers().isEmpty()) { - topics.remove(constraint); + + _registerTemplate(reifiableType, tpl); + removeConstraint(constraint, topics, 2); + } + + private void _processRolePlayerConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, + Collection<Association> assocs) { + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.ROLE_PLAYER_CONSTRAINT)) { + _processRolePlayerConstraint(constraint, topics, assocs); } + } - private void _processAssociationTypeScopeConstraints(TopicMap topicMap, + private void _processRolePlayerConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + Topic type = _getConstrainedTopicTypePlayer(constraint, assocs); + Topic assocType = _getConstrainedStatementPlayer(constraint, assocs); + Topic roleType = _getConstrainedRolePlayer(constraint, assocs); + + DefaultTemplate tpl = new DefaultTemplate("plays-role"); + tpl.addParameter(roleType); + tpl.addParameter(assocType); + int occCounter = _assignCardinality(constraint, tpl, 2); + + _registerTemplate(type, tpl); + removeConstraint(constraint, topics, occCounter); + } + + private void _processOtherRoleConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.ASSOCIATION_TYPE_ROLE); - if (type == null) { - return; + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.OTHER_ROLE_CONSTRAINT)) { + _processOtherRoleConstraint(constraint, topics, assocs); } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.ASSOCIATION_TYPE_SCOPE_CONSTRAINT)) { - _processTypeScopeConstraint(constraint, "has-association-scope", type, topics, assocs); + + } + + private void _processOtherRoleConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + Topic type = _getConstrainedTopicTypePlayer(constraint, assocs); + Topic assocType = _getConstrainedStatementPlayer(constraint, assocs); + Topic roleType = _getConstrainedRolePlayer(constraint, assocs); + Topic otherRole = _getOtherConstrainedRolePlayer(constraint, assocs); + Topic otherPlayer = _getOtherConstrainedTopicTypePlayer(constraint, + assocs); + + DefaultTemplate tpl = new DefaultTemplate("other-role"); + tpl.addParameter(roleType); + tpl.addParameter(type); + tpl.addParameter(otherRole); + tpl.addParameter(otherPlayer); + + _registerTemplate(assocType, tpl); + removeConstraint(constraint, topics, 0); + } + + private void _processUniqueConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, + Collection<Association> assocs) { + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.UNIQUE_VALUE_CONSTRAINT)) { + _processUniqueConstraint(constraint, topics, assocs); } } - private void _processRoleConstraints(TopicMap topicMap, + private void _processUniqueConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + ITemplate tpl = new DefaultTemplate("is-unique"); + Topic player = _getConstrainedStatementPlayer(constraint, assocs); + + _registerTemplate(player, tpl); + topics.remove(constraint); + + } + + private void _processDatatypeConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.ROLE_TYPE_ROLE); - if (type == null) { - return; + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.OCCURRENCE_DATATYPE_CONSTRAINT)) { + _processDatatypeConstraint(constraint, topics, assocs); } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.ASSOCIATION_ROLE_CONSTRAINT)) { - _processRoleConstraint(constraint, type, topics, assocs); - } } - //TODO: Merge with {@link #_processTypeScopeConstraint} - private void _processRoleConstraint(Topic constraint, Topic type, Collection<Topic> topics, + private void _processDatatypeConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + + ILiteral datatype = _getDatatype(constraint); + DefaultTemplate tpl = new DefaultTemplate("has-datatype"); + tpl.addParameter(datatype); + + Topic player = _getConstrainedStatementPlayer(constraint, assocs); + + _registerTemplate(player, tpl); + + removeConstraint(constraint, topics, 1); + } + + private void _processRegExConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - Topic topic = _getRolePlayer(constraint, type, assocs); - ILiteral cardMin = _getCardMin(constraint); - ILiteral cardMax = _getCardMax(constraint); - if (topic == null || cardMin == null || cardMax == null) { - return; + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.REGULAR_EXPRESSION_CONSTRAINT)) { + _processRegExConstraint(constraint, topics, assocs); } - DefaultTemplate tpl = new DefaultTemplate("has-role"); - tpl.addParameter(topic); - tpl.addParameter(cardMin); - tpl.addParameter(cardMax); - Collection<Topic> players = _getPlayers(constraint, _constraintRole, _assocTypeRole, assocs); - for (Topic player: players) { - _registerTemplate(player, tpl); - } - if (players.size() * 2 == constraint.getRolesPlayed().size() - && constraint.getOccurrences().size() == 2 - && constraint.getNames().isEmpty() - && constraint.getTypes().size() == 1 - && constraint.getSubjectLocators().isEmpty() - && constraint.getSubjectIdentifiers().isEmpty()) { - topics.remove(constraint); - } } - private void _processPlayerConstraints(TopicMap topicMap, + private void _processRegExConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + + ILiteral regExp = _getRegEx(constraint); + DefaultTemplate tpl = new DefaultTemplate("matches-regexp"); + tpl.addParameter(regExp); + + Topic player = _getConstrainedStatementPlayer(constraint, assocs); + + _registerTemplate(player, tpl); + // removing instance from topics if it has no further informations + removeConstraint(constraint, topics, 1); + } + + private void _processOverlapConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.ROLE_TYPE_ROLE); - if (type == null) { - return; + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.OVERLAP_DECLARATION)) { + _processOverlapConstraint(constraint, topics, assocs); } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.ASSOCIATION_ROLE_CONSTRAINT)) { - _processRoleConstraint(constraint, type, topics, assocs); + + } + + private void _processOverlapConstraint(Topic constraint, + Collection<Topic> topics, Collection<Association> assocs) { + + List<Topic> players = new ArrayList<Topic>(2); + for (Role role : constraint.getRolesPlayed(_allows, _overlaps)) { + Association assoc = role.getParent(); + players.add(assoc.getRoles(_allowed).iterator().next().getPlayer()); + + assocs.remove(assoc); } + DefaultTemplate tpl = new DefaultTemplate("overlaps"); + tpl.addParameter(players.get(1)); + + _registerTemplate(players.get(0), tpl); + removeConstraint(constraint, topics, 0); + } - private void _processOccurrenceConstraints(TopicMap topicMap, + private void _processSubjectIdentifierConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.OCCURRENCE_TYPE_ROLE); - if (type == null) { - return; + _processLocatorConstraints(topicMap, tiIdx, "has-subject-identifier", + TMCL.SUBJECT_IDENTIFIER_CONSTRAINT, topics, assocs); + } + + private void _processSubjectLocatorConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, + Collection<Association> assocs) { + _processLocatorConstraints(topicMap, tiIdx, "has-subject-locator", + TMCL.SUBJECT_LOCATOR_CONSTRAINT, topics, assocs); + } + + private void _processLocatorConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, String templateName, + Locator constraintSID, Collection<Topic> topics, + Collection<Association> assocs) { + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + constraintSID)) { + _processLocatorConstraint(constraint, templateName, constraintSID, + topics, assocs); } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.TOPIC_OCCURRENCE_CONSTRAINT)) { - _processHasTopicChildConstraint(constraint, "has-occurrence", type, topics, assocs); + } + + private void _processLocatorConstraint(Topic constraint, + String templateName, Locator constraintSID, + Collection<Topic> topics, Collection<Association> assocs) { + + int occCounter = 3; + + ILiteral regEx = _getRegEx(constraint); + if (regEx == null) { + regEx = _regExDefault; + occCounter--; } + + DefaultTemplate tpl = new DefaultTemplate(templateName); + tpl.addParameter(regEx); + + occCounter = _assignCardinality(constraint, tpl, occCounter); + + Topic player = _getConstrainedTopicTypePlayer(constraint, assocs); + _registerTemplate(player, tpl); + removeConstraint(constraint, topics, occCounter); } - private void _processOccurrenceTypeScopeConstraints(TopicMap topicMap, + private void _processScopeConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.OCCURRENCE_TYPE_ROLE); + final Topic type = topicMap + .getTopicBySubjectIdentifier(TMCL.SCOPE_TYPE); if (type == null) { return; } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.TOPIC_OCCURRENCE_TYPE)) { - _processTypeScopeConstraint(constraint, "has-occurrence-scope", type, topics, assocs); + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.SCOPE_CONSTRAINT)) { + _processScopeConstraint(constraint, type, topics, assocs); } } - private void _processTypeScopeConstraint(Topic constraint, String templateName, Topic type, + private void _processScopeConstraint(Topic constraint, Topic type, Collection<Topic> topics, Collection<Association> assocs) { - Topic topic = _getRolePlayer(constraint, type, assocs); - ILiteral cardMin = _getCardMin(constraint); - ILiteral cardMax = _getCardMax(constraint); - if (topic == null || cardMin == null || cardMax == null) { + Topic scope = _getAllowedScopePlayer(constraint, assocs); + + DefaultTemplate tpl = new DefaultTemplate("has-scope"); + tpl.addParameter(scope); + + int occCounter = _assignCardinality(constraint, tpl, 2); + + Topic player = _getConstrainedStatementPlayer(constraint, assocs); + _registerTemplate(player, tpl); + removeConstraint(constraint, topics, occCounter); + } + + private void _processOccurrenceConstraints(TopicMap topicMap, + TypeInstanceIndex tiIdx, Collection<Topic> topics, + Collection<Association> assocs) { + final Topic type = topicMap + .getTopicBySubjectIdentifier(TMCL.OCCURRENCE_TYPE); + if (type == null) { return; } - DefaultTemplate tpl = new DefaultTemplate(templateName); - tpl.addParameter(topic); - tpl.addParameter(cardMin); - tpl.addParameter(cardMax); - Collection<Topic> players = _getPlayers(constraint, _constraintRole, _scopeTypeRole, assocs); - for (Topic player: players) { - _registerTemplate(player, tpl); + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.TOPIC_OCCURRENCE_CONSTRAINT)) { + _processHasTopicChildConstraint(constraint, "has-occurrence", type, + topics, assocs); } - if (players.size() * 2 == constraint.getRolesPlayed().size() - && constraint.getOccurrences().size() == 2 - && constraint.getNames().isEmpty() - && constraint.getTypes().size() == 1 - && constraint.getSubjectLocators().isEmpty() - && constraint.getSubjectIdentifiers().isEmpty()) { - topics.remove(constraint); - } } private void _processNameConstraints(TopicMap topicMap, TypeInstanceIndex tiIdx, Collection<Topic> topics, Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.NAME_TYPE_ROLE); + final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.NAME_TYPE); if (type == null) { return; } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.NAME_TYPE)) { - _processHasTopicChildConstraint(constraint, "has-name", type, topics, assocs); + for (Topic constraint : _getConstraintInstances(topicMap, tiIdx, + TMCL.TOPIC_NAME_CONSTRAINT)) { + _processHasTopicChildConstraint(constraint, "has-name", type, + topics, assocs); } } - private void _processHasTopicChildConstraint(Topic constraint, String templateName, - Topic type, Collection<Topic> topics, Collection<Association> assocs) { - Topic topic = _getRolePlayer(constraint, type, assocs); - ILiteral cardMin = _getCardMin(constraint); - ILiteral cardMax = _getCardMax(constraint); - ILiteral regEx = _getRegEx(constraint); - if (topic == null || cardMin == null || cardMax == null || regEx == null) { + private void _processHasTopicChildConstraint(Topic constraint, + String templateName, Topic type, Collection<Topic> topics, + Collection<Association> assocs) { + Topic topic = _getConstrainedStatementPlayer(constraint, assocs); + + if (topic == null) { return; } + DefaultTemplate tpl = new DefaultTemplate(templateName); tpl.addParameter(topic); + + int occCounter = _assignCardinality(constraint, tpl, 2); + + Topic player = _getConstrainedTopicTypePlayer(constraint, assocs); + + _registerTemplate(player, tpl); + + removeConstraint(constraint, topics, occCounter); + } + + private int _assignCardinality(Topic constraint, DefaultTemplate tpl, + int counter) { + ILiteral cardMin = _getCardMin(constraint); + ILiteral cardMax = _getCardMax(constraint); + + int occCounter = counter; + + if (cardMin == null) { + occCounter--; + cardMin = _cardMinDefault; + } + if (cardMax == null) { + occCounter--; + cardMax = _cardMaxDefault; + } tpl.addParameter(cardMin); tpl.addParameter(cardMax); - tpl.addParameter(regEx); - Collection<Topic> players = _getTopicTypeRolePlayers(constraint, assocs); - for (Topic player: players) { - _registerTemplate(player, tpl); - } - if (players.size() * 2 == constraint.getRolesPlayed().size() - && constraint.getOccurrences().size() == 3 + return occCounter; + } + + /* + * removing instance from topics if it has no further informations + */ + private void removeConstraint(Topic constraint, Collection<Topic> topics, + int expectedOccurrences) { + if (constraint.getOccurrences().size() == expectedOccurrences && constraint.getNames().isEmpty() && constraint.getTypes().size() == 1 && constraint.getSubjectLocators().isEmpty() @@ -368,23 +689,6 @@ } } - private void _processNameTypeScopeConstraints(TopicMap topicMap, - TypeInstanceIndex tiIdx, Collection<Topic> topics, - Collection<Association> assocs) { - final Topic type = topicMap.getTopicBySubjectIdentifier(TMCL.NAME_TYPE_ROLE); - if (type == null) { - return; - } - for (Topic constraint: _getConstraintInstances(topicMap, tiIdx, TMCL.NAME_TYPE_SCOPE_CONSTRAINT)) { - _processTypeScopeConstraint(constraint, "has-name-scope", type, topics, assocs); - } - } - - private Topic _getRolePlayer(Topic constraint, Topic roleType, Collection<Association> assocs) { - Collection<Topic> players = _getPlayers(constraint, _constraintRole, roleType, assocs); - return players.isEmpty() ? null : players.iterator().next(); - } - private ILiteral _getValue(Topic topic, Topic type) { if (type == null) { return null; @@ -400,6 +704,10 @@ return _getValue(constraint, _cardMin); } + private ILiteral _getDatatype(Topic constraint) { + return _getValue(constraint, _datatype); + } + private ILiteral _getCardMax(Topic constraint) { return _getValue(constraint, _cardMax); } @@ -413,15 +721,39 @@ } private void _init(TopicMap topicMap) { - _appliesTo = topicMap.getTopicBySubjectIdentifier(TMCL.APPLIES_TO); - _constraintRole = topicMap.getTopicBySubjectIdentifier(TMCL.CONSTRAINT_ROLE); + _overlaps = topicMap.getTopicBySubjectIdentifier(TMCL.OVERLAPS); + _constrained_statement = topicMap + .getTopicBySubjectIdentifier(TMCL.CONSTRAINED_STATEMENT); + _constrained_topic_type = topicMap + .getTopicBySubjectIdentifier(TMCL.CONSTRAINED_TOPIC_TYPE); + _constrained_role = topicMap + .getTopicBySubjectIdentifier(TMCL.CONSTRAINED_ROLE); + _other_constrained_topic_type = topicMap + .getTopicBySubjectIdentifier(TMCL.OTHER_CONSTRAINED_TOPIC_TYPE); + _other_constrained_role = topicMap + .getTopicBySubjectIdentifier(TMCL.OTHER_CONSTRAINED_ROLE); + _belongs_to_schema = topicMap + .getTopicBySubjectIdentifier(TMCL.BELONGS_TO_SCHEMA); + + _allowed_scope = topicMap + .getTopicBySubjectIdentifier(TMCL.ALLOWED_SCOPE); + _allowed_reifier = topicMap + .getTopicBySubjectIdentifier(TMCL.ALLOWED_REIFIER); + _allowed = topicMap.getTopicBySubjectIdentifier(TMCL.ALLOWED); + _allows = topicMap.getTopicBySubjectIdentifier(TMCL.ALLOWS); + _constrained = topicMap.getTopicBySubjectIdentifier(TMCL.CONSTRAINED); + _constrains = topicMap.getTopicBySubjectIdentifier(TMCL.CONSTRAINS); + _containee = topicMap.getTopicBySubjectIdentifier(TMCL.CONTAINEE); + _container = topicMap.getTopicBySubjectIdentifier(TMCL.CONTAINER); + _cardMin = topicMap.getTopicBySubjectIdentifier(TMCL.CARD_MIN); _cardMax = topicMap.getTopicBySubjectIdentifier(TMCL.CARD_MAX); _datatype = topicMap.getTopicBySubjectIdentifier(TMCL.DATATYPE); _regEx = topicMap.getTopicBySubjectIdentifier(TMCL.REGEXP); - _topicTypeRole = topicMap.getTopicBySubjectIdentifier(TMCL.TOPIC_TYPE_ROLE); - _scopeTypeRole = topicMap.getTopicBySubjectIdentifier(TMCL.SCOPE_TYPE_ROLE); - _assocTypeRole = topicMap.getTopicBySubjectIdentifier(TMCL.ASSOCIATION_TYPE_ROLE); + + _cardMinDefault = Literal.create(0); + _cardMaxDefault = Literal.create("*", "ctm:integer"); + _regExDefault = Literal.create(".*"); } private void _registerTemplate(Topic topic, ITemplate tpl) { Modified: tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTemplate.java =================================================================== --- tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTemplate.java 2009-07-14 15:54:26 UTC (rev 313) +++ tinytim-mio/trunk/src/main/java/org/tinytim/mio/internal/ctm/impl/DefaultTemplate.java 2009-07-15 11:12:44 UTC (rev 314) @@ -24,7 +24,7 @@ * * * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> - * @version $Rev:$ - $Date:$ + * @version $Rev$ - $Date$ */ public class DefaultTemplate implements ITemplate { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |