Revision: 17175
          http://sourceforge.net/p/gate/code/17175
Author:   markagreenwood
Date:     2013-12-17 18:56:27 +0000 (Tue, 17 Dec 2013)
Log Message:
-----------
started on listening/generating events for the relations, and some work on the 
internal set representaiton to use the relation ID instead of the length of a 
list

Modified Paths:
--------------
    gate/trunk/src/main/gate/relations/RelationSet.java

Added Paths:
-----------
    gate/trunk/src/main/gate/event/RelationSetEvent.java
    gate/trunk/src/main/gate/event/RelationSetListener.java

Added: gate/trunk/src/main/gate/event/RelationSetEvent.java
===================================================================
--- gate/trunk/src/main/gate/event/RelationSetEvent.java                        
        (rev 0)
+++ gate/trunk/src/main/gate/event/RelationSetEvent.java        2013-12-17 
18:56:27 UTC (rev 17175)
@@ -0,0 +1,63 @@
+/*
+ *  Copyright (c) 1995-2012, The University of Sheffield. See the file
+ *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ *  This file is part of GATE (see http://gate.ac.uk/), and is free
+ *  software, licenced under the GNU Library General Public License,
+ *  Version 2, June 1991 (in the distribution as file licence.html,
+ *  and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ *  Valentin Tablan 12/12/2000
+ *
+ *  $Id: AnnotationSetEvent.java 17080 2013-11-12 19:29:34Z markagreenwood $
+ */
+
+package gate.event;
+
+import gate.relations.Relation;
+import gate.relations.RelationSet;
+
+/**
+ * This class models events fired by an {@link gate.relatiosn.RelationSet}.
+ */
+public class RelationSetEvent extends GateEvent{
+
+  private static final long serialVersionUID = 6115461542702259816L;
+
+  /**Event type used for situations when a new annotation has been added*/
+  public static final int RELATION_ADDED = 901;
+
+  /**Event type used for situations when an annotation has been removed*/
+  public static final int RELATION_REMOVED = 902;
+
+  /**
+   * Constructor.
+   * @param source the {@link gate.relations.RelationSet} that fired the event
+   * @param type the type of the event
+   * @param relation the Relation that was added or removed.
+   */
+  public RelationSetEvent(RelationSet source,
+                            int type,
+                            Relation relation) {
+    super(source, type);
+    this.relation = relation;
+  }
+
+  /**
+   * Gets the document that has had an annotation added or removed.
+   * @return a {@link gate.Document}
+   */
+  public gate.Document getSourceDocument() {
+    return ((RelationSet)source).getAnnotationSet().getDocument();
+  }
+
+  /**
+   * Gets the relation that has been added or removed
+   * @return a {@link gate.relations.Relation}
+   */
+  public Relation getRelation() {
+    return relation;
+  }
+
+  private Relation relation;
+}
\ No newline at end of file

Added: gate/trunk/src/main/gate/event/RelationSetListener.java
===================================================================
--- gate/trunk/src/main/gate/event/RelationSetListener.java                     
        (rev 0)
+++ gate/trunk/src/main/gate/event/RelationSetListener.java     2013-12-17 
18:56:27 UTC (rev 17175)
@@ -0,0 +1,18 @@
+package gate.event;
+
+import java.util.EventListener;
+
+/**
+ * A listener for events fired by an {@link gate.relations.RelationSet}
+ * ({@link gate.event.RelationSetEvent})
+ */
+public interface RelationSetListener extends EventListener {
+
+  /**Called when a new {@link gate.relations.Relation} has been added*/
+  public void relationAdded(RelationSetEvent e);
+
+  /**Called when an {@link gate.relations.Relation} has been removed*/
+  public void relationRemoved(RelationSetEvent e);
+
+
+}
\ No newline at end of file

Modified: gate/trunk/src/main/gate/relations/RelationSet.java
===================================================================
--- gate/trunk/src/main/gate/relations/RelationSet.java 2013-12-16 11:21:06 UTC 
(rev 17174)
+++ gate/trunk/src/main/gate/relations/RelationSet.java 2013-12-17 18:56:27 UTC 
(rev 17175)
@@ -18,6 +18,10 @@
 import gate.Annotation;
 import gate.AnnotationSet;
 import gate.corpora.DocumentImpl;
+import gate.event.AnnotationSetEvent;
+import gate.event.AnnotationSetListener;
+import gate.event.RelationSetEvent;
+import gate.event.RelationSetListener;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -26,6 +30,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Vector;
 import java.util.regex.Matcher;
 
 import org.apache.log4j.Logger;
@@ -35,7 +40,7 @@
  * annotation set of a document will have one set of associated
  * relations).
  */
-public class RelationSet implements Serializable {
+public class RelationSet implements Serializable, AnnotationSetListener {
 
   private static final long serialVersionUID = 8552798130184595465L;
 
@@ -90,6 +95,10 @@
    */
   protected AnnotationSet annSet;
 
+  private Vector<RelationSetListener> listeners = null;
+
+  private int maxID = 0;
+
   public AnnotationSet getAnnotationSet() {
     return annSet;
   }
@@ -109,8 +118,10 @@
     indexesByMember = new ArrayList<Map<Integer, BitSet>>();
     indexById = new HashMap<Integer, Relation>();
     deleted = new BitSet();
+
+    annSet.addAnnotationSetListener(this);
   }
-  
+
   /**
    * Empties the relation set
    */
@@ -169,20 +180,27 @@
    * @param rel the {@link Relation} to be added.
    */
   public void addRelation(Relation rel) {
+    maxID = Math.max(maxID, rel.getId());
+
     int relIdx = relations.size();
     relations.add(rel);
+
+    /** index by ID **/
     indexById.put(rel.getId(), rel);
+
+    /** index by type **/
     BitSet sameType = indexByType.get(rel.getType());
     if(sameType == null) {
-      sameType = new BitSet(relations.size());
+      sameType = new BitSet(rel.getId());
       indexByType.put(rel.getType(), sameType);
     }
-    sameType.set(relIdx);
+    sameType.set(rel.getId());
 
     // widen the index by member list, if needed
     for(int i = indexesByMember.size(); i < rel.getMembers().length; i++) {
       indexesByMember.add(new HashMap<Integer, BitSet>());
     }
+
     for(int memeberPos = 0; memeberPos < rel.getMembers().length; 
memeberPos++) {
       int member = rel.getMembers()[memeberPos];
       Map<Integer, BitSet> indexByMember = indexesByMember.get(memeberPos);
@@ -193,6 +211,9 @@
       }
       sameMember.set(relIdx);
     }
+
+    fireRelationAdded(new RelationSetEvent(this,
+            RelationSetEvent.RELATION_ADDED, rel));
   }
 
   /**
@@ -216,9 +237,8 @@
     List<Relation> res = new ArrayList<Relation>();
     BitSet rels = indexByType.get(type);
     if(rels != null) {
-      rels.andNot(deleted);
-      for(int relPos = 0; relPos < relations.size(); relPos++) {
-        if(rels.get(relPos)) res.add(relations.get(relPos));
+      for(int relPos = 0; relPos < maxID; relPos++) {
+        if(rels.get(relPos)) res.add(indexById.get(relPos));
       }
     }
     return res;
@@ -274,9 +294,15 @@
   public boolean deleteRelation(Relation relation) {
     int relIdx = relations.indexOf(relation);
     if(relIdx >= 0) {
+
+      // delete this relation from the type index
+      indexByType.get(relation.getType()).clear(relation.getId());
+
       deleted.set(relIdx);
       relations.set(relIdx, null);
       indexById.remove(relation.getId());
+      fireRelationRemoved(new RelationSetEvent(this,
+              RelationSetEvent.RELATION_REMOVED, relation));
       return true;
     } else {
       return false;
@@ -337,4 +363,60 @@
     str.append("]");
     return str.toString();
   }
+
+  @Override
+  public void annotationAdded(AnnotationSetEvent e) {
+    // we don't care about annotations being added so we do nothing
+  }
+
+  @Override
+  public void annotationRemoved(AnnotationSetEvent e) {
+
+    Annotation a = e.getAnnotation();
+
+    // find all relations which include the annotation and remove them
+
+    // may need to be an iterative method as we may remove relations
+    // which are themselves in a relation
+  }
+
+  public synchronized void removeRelationSetListener(RelationSetListener l) {
+    if(listeners != null && listeners.contains(l)) {
+      @SuppressWarnings("unchecked")
+      Vector<RelationSetListener> v =
+              (Vector<RelationSetListener>)listeners.clone();
+      v.removeElement(l);
+      listeners = v;
+    }
+  }
+
+  public synchronized void addAnnotationSetListener(RelationSetListener l) {
+    @SuppressWarnings("unchecked")
+    Vector<RelationSetListener> v =
+            listeners == null
+                    ? new Vector<RelationSetListener>(2)
+                    : (Vector<RelationSetListener>)listeners.clone();
+    if(!v.contains(l)) {
+      v.addElement(l);
+      listeners = v;
+    }
+  }
+
+  protected void fireRelationAdded(RelationSetEvent e) {
+    if(listeners != null) {
+      int count = listeners.size();
+      for(int i = 0; i < count; i++) {
+        listeners.elementAt(i).relationAdded(e);
+      }
+    }
+  }
+
+  protected void fireRelationRemoved(RelationSetEvent e) {
+    if(listeners != null) {
+      int count = listeners.size();
+      for(int i = 0; i < count; i++) {
+        listeners.elementAt(i).relationRemoved(e);
+      }
+    }
+  }
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to