Author: rwesten
Date: Thu Nov  7 09:41:21 2013
New Revision: 1539574

URL: http://svn.apache.org/r1539574
Log:
STANBOL-1132: applied the patch provided by Cristian Petroaca on the 28/Oct/13. 
I have made some minor modifications to account for the SpanTypeEnum that was 
moved to an own file. Also some formatting related changes where made

Added:
    
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java
   (with props)
    
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/
    
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java
   (with props)
Modified:
    
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/CorefTagSupport.java
    
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser
    
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer
    
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/NlpAnnotations.java
    
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/coref/CorefTag.java
    
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelation.java
    
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelation.java
    
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelationTag.java

Modified: 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/CorefTagSupport.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/CorefTagSupport.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/CorefTagSupport.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/CorefTagSupport.java
 Thu Nov  7 09:41:21 2013
@@ -15,6 +15,7 @@
  * limitations under the License.
  */package org.apache.stanbol.enhancer.nlp.json.valuetype.impl;
 
+import java.util.Collections;
 import java.util.Set;
 
 import org.apache.felix.scr.annotations.Component;
@@ -85,14 +86,13 @@ public class CorefTagSupport implements 
                        throw new IllegalStateException("Field 
'isRepresentative' must have a true/false format");
                }
                
-               CorefTag corefTag = new CorefTag(jIsRepresentative.asBoolean());
-               
                JsonNode node = jCoref.path(MENTIONS_TAG);
-        
+               Set<Span> mentions = Collections.<Span> emptySet();
+               
                if(node.isArray()) {
             ArrayNode jMentions = (ArrayNode)node;
             
-            for(int i=0;i<jMentions.size();i++) {
+            for(int i = 0;i < jMentions.size();i++) {
                 JsonNode member = jMentions.get(i);
                 
                 if(member.isObject()) {
@@ -116,11 +116,11 @@ public class CorefTagSupport implements 
                     
                     }
                     
-                    corefTag.addMention(mentionedSpan);
+                    mentions.add(mentionedSpan);
                 }
             }
                }    
                 
-               return corefTag;
+               return new CorefTag(jIsRepresentative.asBoolean(), 
Collections.unmodifiableSet(mentions));
        }       
 }
\ No newline at end of file

Added: 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java?rev=1539574&view=auto
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java
 (added)
+++ 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java
 Thu Nov  7 09:41:21 2013
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */package org.apache.stanbol.enhancer.nlp.json.valuetype.impl;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation;
+import org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelation;
+import org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag;
+import org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser;
+import org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer;
+import org.apache.stanbol.enhancer.nlp.model.AnalysedText;
+import org.apache.stanbol.enhancer.nlp.model.Span;
+import org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.ObjectNode;
+
+@Component(immediate = true, policy = ConfigurationPolicy.IGNORE)
+@Service(value = {ValueTypeParser.class, ValueTypeSerializer.class})
+@Property(name = ValueTypeParser.PROPERTY_TYPE, value = 
DependencyRelationSupport.TYPE_VALUE)
+public class DependencyRelationSupport implements 
ValueTypeParser<DependencyRelation>,
+        ValueTypeSerializer<DependencyRelation> {
+
+    public static final String TYPE_VALUE = 
"org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation";
+
+    private static final String RELATION_TYPE_TAG = "tag";
+    private static final String RELATION_STANBOL_TYPE_TAG = "relationType";
+    private static final String RELATION_IS_DEPENDEE_TAG = "isDependent";
+    private static final String RELATION_PARTNER_TYPE_TAG = "partnerType";
+    private static final String RELATION_PARTNER_START_TAG = "partnerStart";
+    private static final String RELATION_PARTNER_END_TAG = "partnerEnd";
+    private static final String ROOT_TAG = "ROOT";
+
+    @Override
+    public ObjectNode serialize(ObjectMapper mapper, DependencyRelation 
relation) {
+        ObjectNode jDependencyRelation = mapper.createObjectNode();
+
+        GrammaticalRelationTag gramRelTag = 
relation.getGrammaticalRelationTag();
+        jDependencyRelation.put(RELATION_TYPE_TAG, gramRelTag.getTag());
+        jDependencyRelation.put(RELATION_STANBOL_TYPE_TAG, 
gramRelTag.getGrammaticalRelation().ordinal());
+        jDependencyRelation.put(RELATION_IS_DEPENDEE_TAG, 
(relation.isDependent() ? "true" : "false"));
+
+        Span partner = relation.getPartner();
+        if (partner != null) {
+            jDependencyRelation.put(RELATION_PARTNER_TYPE_TAG, 
partner.getType().toString());
+            jDependencyRelation.put(RELATION_PARTNER_START_TAG, 
partner.getStart());
+            jDependencyRelation.put(RELATION_PARTNER_END_TAG, 
partner.getEnd());
+        } else {
+            jDependencyRelation.put(RELATION_PARTNER_TYPE_TAG, ROOT_TAG);
+            jDependencyRelation.put(RELATION_PARTNER_START_TAG, 0);
+            jDependencyRelation.put(RELATION_PARTNER_END_TAG, 0);
+        }
+
+        return jDependencyRelation;
+    }
+
+    @Override
+    public Class<DependencyRelation> getType() {
+        return DependencyRelation.class;
+    }
+
+    @Override
+    public DependencyRelation parse(ObjectNode jDependencyRelation, 
AnalysedText at) {
+        JsonNode tag = jDependencyRelation.path(RELATION_TYPE_TAG);
+
+        if (!tag.isTextual()) {
+            throw new IllegalStateException("Unable to parse 
GrammaticalRelationTag. The value of the "
+                                            + "'tag' field MUST have a textual 
value (json: "
+                                            + jDependencyRelation + ")");
+        }
+
+        GrammaticalRelation grammaticalRelation = 
GrammaticalRelation.class.getEnumConstants()[jDependencyRelation
+                .path(RELATION_STANBOL_TYPE_TAG).asInt()];
+        GrammaticalRelationTag gramRelTag = new 
GrammaticalRelationTag(tag.getTextValue(),
+                grammaticalRelation);
+
+        JsonNode isDependent = 
jDependencyRelation.path(RELATION_IS_DEPENDEE_TAG);
+
+        if (!isDependent.isBoolean()) {
+            throw new IllegalStateException("Field 'isDependent' must have a 
true/false format");
+        }
+
+        Span partnerSpan = null;
+        String typeString = 
jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG).getTextValue();
+
+        if (!typeString.equals(ROOT_TAG)) {
+            SpanTypeEnum spanType = 
SpanTypeEnum.valueOf(jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG)
+                    .getTextValue());
+            int spanStart = 
jDependencyRelation.path(RELATION_PARTNER_START_TAG).asInt();
+            int spanEnd = 
jDependencyRelation.path(RELATION_PARTNER_END_TAG).asInt();
+
+            switch (spanType) {
+                case Chunk:
+                    partnerSpan = at.addChunk(spanStart, spanEnd);
+                    break;
+                // unused types
+                // case Sentence:
+                // case Text:
+                // case TextSection:
+                // break;
+                case Token:
+                    partnerSpan = at.addToken(spanStart, spanEnd);
+                    break;
+            }
+        }
+
+        return new DependencyRelation(gramRelTag, isDependent.asBoolean(), 
partnerSpan);
+    }
+}

Propchange: 
stanbol/trunk/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/impl/DependencyRelationSupport.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeParser
 Thu Nov  7 09:41:21 2013
@@ -2,4 +2,5 @@ org.apache.stanbol.enhancer.nlp.json.val
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.NerTagSupport
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.MorphoFeaturesSupport
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.PhraseTagSupport
-org.apache.stanbol.enhancer.nlp.json.valuetype.impl.DependencyFeaturesSupport
\ No newline at end of file
+org.apache.stanbol.enhancer.nlp.json.valuetype.impl.DependencyRelationSupport
+org.apache.stanbol.enhancer.nlp.json.valuetype.impl.CorefTagSupport
\ No newline at end of file

Modified: 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp-json/src/main/resources/META-INF/services/org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer
 Thu Nov  7 09:41:21 2013
@@ -2,4 +2,5 @@ org.apache.stanbol.enhancer.nlp.json.val
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.NerTagSupport
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.MorphoFeaturesSupport
 org.apache.stanbol.enhancer.nlp.json.valuetype.impl.PhraseTagSupport
-org.apache.stanbol.enhancer.nlp.json.valuetype.impl.DependencyFeaturesSupport
\ No newline at end of file
+org.apache.stanbol.enhancer.nlp.json.valuetype.impl.DependencyRelationSupport
+org.apache.stanbol.enhancer.nlp.json.valuetype.impl.CorefTagSupport
\ No newline at end of file

Modified: 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/NlpAnnotations.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/NlpAnnotations.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/NlpAnnotations.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/NlpAnnotations.java
 Thu Nov  7 09:41:21 2013
@@ -17,7 +17,7 @@
 package org.apache.stanbol.enhancer.nlp;
 
 import org.apache.stanbol.enhancer.nlp.coref.CorefTag;
-import org.apache.stanbol.enhancer.nlp.dependency.DependencyFeatures;
+import org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation;
 import org.apache.stanbol.enhancer.nlp.model.AnalysedText;
 import org.apache.stanbol.enhancer.nlp.model.Chunk;
 import org.apache.stanbol.enhancer.nlp.model.Token;
@@ -77,8 +77,8 @@ public interface NlpAnnotations {
         * other words in the sentence. Typically used on {@link Token}s.
         * <p>
         */
-       Annotation<DependencyFeatures> DEPENDENCY_ANNOTATION = new 
Annotation<DependencyFeatures>(
-                       "stanbol.enhancer.nlp.dependency", 
DependencyFeatures.class);
+       Annotation<DependencyRelation> DEPENDENCY_ANNOTATION = new 
Annotation<DependencyRelation>(
+                       "stanbol.enhancer.nlp.dependency", 
DependencyRelation.class);
 
        /**
         * {@link Annotation} representing all the words which are a

Modified: 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/coref/CorefTag.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/coref/CorefTag.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/coref/CorefTag.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/coref/CorefTag.java
 Thu Nov  7 09:41:21 2013
@@ -44,19 +44,18 @@ public class CorefTag extends Tag<CorefT
         */
        private Set<Span> mentions;
 
-       
        public CorefTag() {
-           //TODO: if mentions can be modified you can not use 
Collections.emptySet
-           //      because this would cause exceptions in #addMention or if 
users
-           //      to #getMentions().remove(...)
-           //IMHO mentions should be made immutable by using a
-           //Collections.unmodifiableSet(..) for the field and removing the
-           //#addMentions(..) method.
-               this(null, false, Collections.<Span> emptySet());
+               this(null, false, Collections.unmodifiableSet(Collections
+                               .<Span> emptySet()));
        }
 
        public CorefTag(boolean isRepresentative) {
-               this(null, isRepresentative, Collections.<Span> emptySet());
+               this(null, isRepresentative, 
Collections.unmodifiableSet(Collections
+                               .<Span> emptySet()));
+       }
+       
+       public CorefTag(boolean isRepresentative, Set<Span> mentions) {
+               this(null, isRepresentative, mentions);
        }
 
        public CorefTag(String tag, boolean isRepresentative, Set<Span> 
mentions) {
@@ -65,24 +64,34 @@ public class CorefTag extends Tag<CorefT
                this.isRepresentative = isRepresentative;
                this.mentions = mentions;
        }
+
        /**
         * Getter whether the {@link Token} to which this tag is attached is the
-     * representative metion in the chain.
+        * representative mention in the chain.
+        * 
         * @return the representative state
         */
        public boolean isRepresentative() {
                return this.isRepresentative;
        }
+
        /**
-        * Getter for the set of {@link Token}s representing mentions
-        * of the {@link Token} to which this tag is attached.
+        * Getter for the set of {@link Token}s representing mentions of the
+        * {@link Token} to which this tag is attached.
+        * 
         * @return
         */
        public Set<Span> getMentions() {
                return this.mentions;
        }
 
-       public void addMention(Span mention) {
-               this.mentions.add(mention);
+       public int hashCode() {
+               return super.hashCode()
+                               + ((this.mentions != null) ? 
this.mentions.hashCode() : 0);
+       }
+
+       public boolean equals(Object obj) {
+               return super.equals(obj) && (obj instanceof CorefTag)
+                               && (this.mentions.equals(((CorefTag) 
obj).getMentions()));
        }
 }

Modified: 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelation.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelation.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelation.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelation.java
 Thu Nov  7 09:41:21 2013
@@ -43,17 +43,9 @@ public class DependencyRelation {
         */
        private Span partner;
 
-       public DependencyRelation() {
-       }
-
-       public DependencyRelation(GrammaticalRelationTag 
grammaticalRelationTag) {
+       public DependencyRelation(GrammaticalRelationTag 
grammaticalRelationTag, boolean isDependent,
+                       Span partner) {
                this.grammaticalRelationTag = grammaticalRelationTag;
-       }
-
-       public DependencyRelation(GrammaticalRelationTag grammaticalRelationTag,
-                       boolean isDependent, Span partner) {
-               this(grammaticalRelationTag);
-
                this.isDependent = isDependent;
                this.partner = partner;
        }
@@ -62,27 +54,14 @@ public class DependencyRelation {
                return grammaticalRelationTag;
        }
 
-       public void setGrammaticalRelationTag(
-                       GrammaticalRelationTag grammaticalRelationTag) {
-               this.grammaticalRelationTag = grammaticalRelationTag;
-       }
-
        public boolean isDependent() {
                return isDependent;
        }
 
-       public void setDependent(boolean isDependent) {
-               this.isDependent = isDependent;
-       }
-
        public Span getPartner() {
                return this.partner;
        }
 
-       public void setPartner(Span partner) {
-               this.partner = partner;
-       }
-
        public int hashCode() {
                return grammaticalRelationTag.hashCode()
                                + ((partner != null) ? partner.hashCode() : 0)
@@ -90,14 +69,14 @@ public class DependencyRelation {
        }
 
        public boolean equals(Object obj) {
-               return super.equals(obj)
-                               && (obj instanceof DependencyRelation)
+               return (obj instanceof DependencyRelation)
                                && (this.grammaticalRelationTag
                                                .equals(((DependencyRelation) 
obj)
                                                                
.getGrammaticalRelationTag()))
                                && (this.isDependent == ((DependencyRelation) 
obj)
                                                .isDependent())
-                               && (this.partner
-                                               .equals(((DependencyRelation) 
obj).getPartner()));
+                               && (this.partner == null
+                                               && ((DependencyRelation) 
obj).getPartner() == null || this.partner
+                                                       
.equals(((DependencyRelation) obj).getPartner()));
        }
 }

Modified: 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelation.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelation.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelation.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelation.java
 Thu Nov  7 09:41:21 2013
@@ -16,8 +16,9 @@
 */
 package org.apache.stanbol.enhancer.nlp.dependency;
 
-import java.util.Collection;
-import java.util.HashSet;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -499,9 +500,9 @@ public enum GrammaticalRelation {
        private GrammaticalRelationCategory category;
 
        /**
-        * The parents of this grammatical relation.
+        * The parent of this grammatical relation.
         */
-       private Set<GrammaticalRelation> parents;
+       private GrammaticalRelation parent;
 
        GrammaticalRelation(GrammaticalRelationCategory category) {
                this(category, null);
@@ -513,34 +514,49 @@ public enum GrammaticalRelation {
 
        GrammaticalRelation(GrammaticalRelationCategory category,
                        GrammaticalRelation parent) {
-               if (parent != null) {
-                       this.parents = new HashSet<GrammaticalRelation>();
-                       addParents(parent);
-               }
-
-               if (category != null) {
-                       this.category = category;
-               }
+               this.parent = parent;
+               this.category = category;
        }
 
        public GrammaticalRelationCategory getCategory() {
                return this.category;
        }
 
-       public Collection<GrammaticalRelation> getParents() {
-               return this.parents;
+       public GrammaticalRelation getParent() {
+               return this.parent;
        }
 
-       private void addParents(GrammaticalRelation parent) {
-               this.parents.add(parent);
+       public Set<GrammaticalRelation> hierarchy() {
+               return transitiveClosureMap.get(this);
+       }
+
+       /**
+        * This is needed because one can not create EnumSet instances before 
the
+        * initialization of an Enum has finished.
+        * <p>
+        * To keep using the much faster {@link EnumSet} a static member 
initialised
+        * in an static {} block is used as a workaround.
+        */
+       private static final Map<GrammaticalRelation, Set<GrammaticalRelation>> 
transitiveClosureMap;
+
+       static {
+               transitiveClosureMap = new EnumMap<GrammaticalRelation, 
Set<GrammaticalRelation>>(
+                               GrammaticalRelation.class);
+
+               for (GrammaticalRelation relation : 
GrammaticalRelation.values()) {
+                       Set<GrammaticalRelation> parents = EnumSet.of(relation);
+
+                       GrammaticalRelation relationParent = 
relation.getParent();
+                       Set<GrammaticalRelation> transParents = 
transitiveClosureMap
+                                       .get(relationParent);
+
+                       if (transParents != null) {
+                               parents.addAll(transParents);
+                       } else if (relationParent != null) {
+                               parents.add(relationParent);
+                       } // else no parent
 
-               Collection<GrammaticalRelation> grandParents = 
parent.getParents();
-               if (grandParents == null) {
-                       this.category = parent.getCategory();
-               } else {
-                       for (GrammaticalRelation grandParent : grandParents) {
-                               addParents(grandParent);
-                       }
+                       transitiveClosureMap.put(relation, parents);
                }
        }
 }

Modified: 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelationTag.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelationTag.java?rev=1539574&r1=1539573&r2=1539574&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelationTag.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/main/java/org/apache/stanbol/enhancer/nlp/dependency/GrammaticalRelationTag.java
 Thu Nov  7 09:41:21 2013
@@ -39,14 +39,23 @@ public class GrammaticalRelationTag exte
                        GrammaticalRelation grammaticalRelation) {
                this(tag);
 
-               this.setGrammaticalRelation(grammaticalRelation);
+               this.grammaticalRelation = grammaticalRelation;
        }
 
        public GrammaticalRelation getGrammaticalRelation() {
                return grammaticalRelation;
        }
 
-       public void setGrammaticalRelation(GrammaticalRelation 
grammaticalRelation) {
-               this.grammaticalRelation = grammaticalRelation;
+       @Override
+       public int hashCode() {
+               return super.hashCode() + grammaticalRelation.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               return super.equals(obj)
+                               && obj instanceof GrammaticalRelationTag
+                               && grammaticalRelation
+                                               
.equals(((GrammaticalRelationTag) obj).grammaticalRelation);
        }
 }

Added: 
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java?rev=1539574&view=auto
==============================================================================
--- 
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java
 (added)
+++ 
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java
 Thu Nov  7 09:41:21 2013
@@ -0,0 +1,74 @@
+package org.apache.stanbol.enhancer.nlp.dependency;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.apache.clerezza.rdf.core.UriRef;
+import 
org.apache.stanbol.enhancer.contentitem.inmemory.InMemoryContentItemFactory;
+import org.apache.stanbol.enhancer.nlp.model.AnalysedText;
+import org.apache.stanbol.enhancer.nlp.model.AnalysedTextFactory;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.junit.Assert;
+
+public class DependencyRelationTest {
+    /**
+     * Empty AnalysedText instance created before each test
+     */
+    private static AnalysedText at;
+
+    private static final ContentItemFactory ciFactory = 
InMemoryContentItemFactory.getInstance();
+    private static final AnalysedTextFactory atFactory = 
AnalysedTextFactory.getDefaultInstance();
+
+    private static ContentItem ci;
+    private static Entry<UriRef,Blob> textBlob;
+
+    @BeforeClass
+    public static void setup() throws IOException {
+        ci = ciFactory.createContentItem(new StringSource(""));
+        textBlob = ContentItemHelper.getBlob(ci, 
Collections.singleton("text/plain"));
+        at = atFactory.createAnalysedText(textBlob.getValue());
+    }
+
+    @Test
+    public void testSimpleGrammaticalRelationInit() {
+        GrammaticalRelationTag gramRelationTag = new GrammaticalRelationTag(
+            "agent", GrammaticalRelation.Agent);
+        DependencyRelation depRelation = new 
DependencyRelation(gramRelationTag, false, at.addToken(0, 0));
+
+        Assert.assertEquals("agent", 
depRelation.getGrammaticalRelationTag().getTag());
+
+        GrammaticalRelation relation = 
depRelation.getGrammaticalRelationTag().getGrammaticalRelation();
+        Assert.assertEquals(GrammaticalRelation.Agent, relation);
+        Assert.assertEquals(null, relation.getParent());
+        Assert.assertEquals(GrammaticalRelationCategory.Argument, 
relation.getCategory());
+    }
+
+    @Test
+    public void testGrammaticalRelationWithHierarchyInit() {
+        GrammaticalRelationTag gramRelationTag = new GrammaticalRelationTag(
+            "abbrev", GrammaticalRelation.AbbreviationModifier);
+        DependencyRelation depRelation = new 
DependencyRelation(gramRelationTag, false, at.addToken(0, 0));
+
+        Assert.assertEquals("abbrev", 
depRelation.getGrammaticalRelationTag().getTag());
+
+        GrammaticalRelation relation = 
depRelation.getGrammaticalRelationTag().getGrammaticalRelation();
+        Assert.assertEquals(GrammaticalRelation.AbbreviationModifier, 
relation);
+        Assert.assertEquals(GrammaticalRelation.Modifier, 
relation.getParent());
+
+        Set<GrammaticalRelation> expectedHierarcy = 
EnumSet.of(GrammaticalRelation.Dependent,
+            GrammaticalRelation.Modifier, 
GrammaticalRelation.AbbreviationModifier);
+
+        Set<GrammaticalRelation> hierarchy = relation.hierarchy();
+        Assert.assertEquals(expectedHierarcy, hierarchy);
+    }
+}

Propchange: 
stanbol/trunk/enhancer/generic/nlp/src/test/java/org/apache/stanbol/enhancer/nlp/dependency/DependencyRelationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to