[isidorus-cvs] r618 - branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base

lgiessmann at common-lisp.net lgiessmann at common-lisp.net
Thu Jul 14 12:05:37 UTC 2011


Author: lgiessmann
Date: Thu Jul 14 05:05:36 2011
New Revision: 618

Log:
gdl-frontend: Widgets: implemented the class GdlPosition that wraps a topic of the type gdl:Position

Added:
   branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/GdlPosition.java
Modified:
   branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/IGdlContainer.java
   branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/TmHelper.java

Added: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/GdlPosition.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/GdlPosition.java	Thu Jul 14 05:05:36 2011	(r618)
@@ -0,0 +1,110 @@
+package us.isidor.gdl.anaToMia.Widgets.base;
+
+import java.util.ArrayList;
+
+import com.google.gwt.core.client.JsArray;
+
+import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Occurrence;
+import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Topic;
+import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.TopicMap;
+import us.isidor.gdl.anaToMia.Widgets.environment.InvalidGdlSchemaException;
+import us.isidor.gdl.anaToMia.Widgets.value.AutoNumUnitValue;
+import us.isidor.gdl.anaToMia.Widgets.value.PositionStyleValue;
+
+
+public class GdlPosition implements GdlDescriptor {
+	private Topic tmRepresentative = null;
+	
+	
+	@SuppressWarnings("unused")
+	private GdlPosition(){}
+	
+	
+	public GdlPosition(Topic tmRepresentative){
+		this.tmRepresentative = tmRepresentative;
+	}
+	
+	
+	// a helper method that returns all occurrences of the type bound to the passed PSI
+	private JsArray<Occurrence> getOccurrences(String occurrenceType){
+		TopicMap tm = this.tmRepresentative.getTopicMap();
+		Topic occType = tm.getTopicBySubjectIdentifier(tm.createLocator(occurrenceType));
+		if(occType == null) return null;
+		else return tmRepresentative.getOccurrences(occType);
+	}
+
+
+	// a helper method that returns one occurrence of the type bound to the passed PSI.
+	// If more than one occurrence is available an InvalidGdlSchemaException is thrown.
+	// If nor occurrence is available the return value is null
+	private Occurrence getNoneOrOneUnscopedOccurrence(String occurrenceType) throws InvalidGdlSchemaException{
+		JsArray<Occurrence> occs = getOccurrences(occurrenceType);
+		ArrayList<Occurrence> unscopedOccs = new ArrayList<Occurrence>();
+		for(int i = 0; i != occs.length(); ++i){
+			if(occs.get(i).getScope().length() == 0) unscopedOccs.add(occs.get(i));
+		}
+
+		if(unscopedOccs.size() > 1){
+			throw new InvalidGdlSchemaException("The topic " + TmHelper.getAnyIdOfTopic(this.tmRepresentative) + " must be bound to none or one unscoped occurrence of the type " + occurrenceType + ", but is bound " + unscopedOccs.size() + " times to it");
+		} else if(unscopedOccs.size() == 1){
+			return unscopedOccs.get(0);
+		} else {
+			return null;
+		}
+	}
+	
+	
+	@Override
+	public Topic getTmRepresentative() {
+		return this.tmRepresentative;
+	}
+
+	
+	public AutoNumUnitValue getTop() throws InvalidGdlSchemaException {
+		Occurrence topOcc = null;
+		topOcc = getNoneOrOneUnscopedOccurrence(GdlPsis.OccurrenceType.gdlTop);
+
+		if(topOcc == null) return null;
+		else return new AutoNumUnitValue(topOcc.getValue());
+	}
+	
+	
+	public AutoNumUnitValue getRight() throws InvalidGdlSchemaException {
+		Occurrence rightOcc = null;
+		rightOcc = getNoneOrOneUnscopedOccurrence(GdlPsis.OccurrenceType.gdlRight);
+
+		if(rightOcc == null) return null;
+		else return new AutoNumUnitValue(rightOcc.getValue());
+	}
+	
+	
+	public AutoNumUnitValue getBottom() throws InvalidGdlSchemaException {
+		Occurrence bottomOcc = null;
+		bottomOcc = getNoneOrOneUnscopedOccurrence(GdlPsis.OccurrenceType.gdlBottom);
+
+		if(bottomOcc == null) return null;
+		else return new AutoNumUnitValue(bottomOcc.getValue());
+	}
+	
+	
+	public AutoNumUnitValue getLeft() throws InvalidGdlSchemaException {
+		Occurrence leftOcc = null;
+		leftOcc = getNoneOrOneUnscopedOccurrence(GdlPsis.OccurrenceType.gdlLeft);
+
+		if(leftOcc == null) return null;
+		else return new AutoNumUnitValue(leftOcc.getValue());
+	}
+	
+	
+	public PositionStyleValue getPositionStyle()throws InvalidGdlSchemaException {
+		Occurrence positionStyleOcc = null;
+		positionStyleOcc = getNoneOrOneUnscopedOccurrence(GdlPsis.OccurrenceType.gdlPositionStyle);
+
+		if(positionStyleOcc == null) return null;
+		else try{
+			return PositionStyleValue.valueOf(positionStyleOcc.getValue().toUpperCase());
+		}catch(IllegalArgumentException e){
+			throw new InvalidGdlSchemaException("a position style value must be one of static, relative or absolute, but is " + positionStyleOcc.getValue());
+		}
+	}
+}

Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/IGdlContainer.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/IGdlContainer.java	Thu Jul 14 02:42:52 2011	(r617)
+++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/IGdlContainer.java	Thu Jul 14 05:05:36 2011	(r618)
@@ -1,11 +1,13 @@
 package us.isidor.gdl.anaToMia.Widgets.base;
 
 import java.util.ArrayList;
+
+import us.isidor.gdl.anaToMia.Widgets.environment.ExecutionException;
 import us.isidor.gdl.anaToMia.Widgets.environment.InvalidGdlSchemaException;
 import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Topic;
 
 
 public interface IGdlContainer {
-	public GdlVisibleObject append(Topic ancestor, Topic current) throws InvalidGdlSchemaException;
+	public GdlVisibleObject append(Topic ancestor, Topic current) throws InvalidGdlSchemaException, ExecutionException;
 	public ArrayList<Topic> contains() throws InvalidGdlSchemaException;
 }

Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/TmHelper.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/TmHelper.java	Thu Jul 14 02:42:52 2011	(r617)
+++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/base/TmHelper.java	Thu Jul 14 05:05:36 2011	(r618)
@@ -378,4 +378,56 @@
 			return null;
 		}
 	}
+	
+	
+	// returns the topic that represents the position between the ancestor
+	// and descendant topics
+	public static Topic getPositionOf(Topic ancestor, Topic descendant) throws InvalidGdlSchemaException {
+		if(ancestor == null || descendant == null) return null;
+		
+		// get all potential valid associations that models a position association
+		TopicMap tm = ancestor.getTopicMap();
+		Topic ancestorRoleType = getTopicByPsi(GdlPsis.RoleType.gdlAncestor, tm);
+		Topic position = getTopicByPsi(GdlPsis.AssociationType.gdlPosition, tm);
+		ArrayList<Pair<Topic, Topic>> rolePlayertypesAndTypes = new ArrayList<Pair<Topic,Topic>>();
+		Topic descriptor = getTopicByPsi(GdlPsis.RoleType.gdlDescendant, tm);
+		Topic descendantRoleType = getTopicByPsi(GdlPsis.RoleType.gdlDescendant, tm);
+		Topic visibleObject = getTopicByPsi(GdlPsis.TopicType.gdlVisibleObject, tm);
+		rolePlayertypesAndTypes.add(new Pair<Topic, Topic>(position, descriptor));
+		rolePlayertypesAndTypes.add(new Pair<Topic, Topic>(visibleObject, descendantRoleType));
+		ArrayList<Association> posAssocs = getAssociationsOfTopic(ancestor, ancestorRoleType, position, null, rolePlayertypesAndTypes);
+		
+		if(posAssocs.size() != 1){
+			String top1 = getAnyIdOfTopic(ancestor);
+			String top2 = getAnyIdOfTopic(descendant);
+			String bindings = "";
+			for (Association assoc : posAssocs)
+				if(assoc.getRoles(position).length() != 0)
+					for(int i = 0; i != assoc.getRoles(position).length(); ++i)
+						bindings += ", " + getAnyIdOfTopic(assoc.getRoles(position).get(i).getPlayer());
+			if(bindings.length() == 0) bindings = "[]";
+			else bindings = bindings.substring(2);
+			throw new InvalidGdlSchemaException("the topics " + top1 + " and " + top2 + " must be bound to exaclty one position topic, but is bound to " + bindings);
+		}else {
+			ArrayList<Topic> positions = new ArrayList<Topic>();
+			for (Association assoc : posAssocs) {
+				JsArray<Role> validRoles = assoc.getRoles(position);
+				for(int i = 0; i != validRoles.length(); ++i)
+					if(isInstanceOf(validRoles.get(i).getPlayer(), position) &&	!positions.contains(validRoles.get(i).getPlayer())) positions.add(validRoles.get(i).getPlayer());
+				
+			}
+			
+			if(positions.size() != 1){
+				String top1 = getAnyIdOfTopic(ancestor);
+				String top2 = getAnyIdOfTopic(descendant);
+				String bindings = "";
+				for (Topic pos : positions) bindings += ", " + getAnyIdOfTopic(pos);
+				if(bindings.length() == 0) bindings = "[]";
+				else bindings = bindings.substring(2);
+				throw new InvalidGdlSchemaException("the topics " + top1 + " and " + top2 + " must be bound to exaclty one position topic, but is bound to " + bindings);				
+			}else {
+				return positions.get(0);
+			}
+		}
+	}
 }




More information about the Isidorus-cvs mailing list