DasherJava / src / dasherJava / core / alphabets / xml / Alphabet.java
Alphabet.java
Raw
package dasherJava.core.alphabets.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dasherJava.core.alphabets.actions.Action;
import dasherJava.core.alphabets.actions.Action.AccessibilityAction;
import dasherJava.core.alphabets.actions.Action.KeyboardAction;
import dasherJava.core.alphabets.actions.Action.SocketOutputAction;
import dasherJava.core.collections.NamedObject;
import dasherJava.core.languageModeling.CharacterGroup;
import dasherJava.core.languageModeling.LanguageAlphabet;
import dasherJava.core.languageModeling.LanguageCharacter;

public class Alphabet implements NamedObject {
	
	private final String name;
	private final String orientation;
	private final String trainingFilename;
	private final String colorsName;
	private final List<Group> groups = new ArrayList<>();
	private final List<Node> nodes = new ArrayList<>();
	
	Alphabet(String name, String orientation, String trainingFilename, String colorsName) {
		this.name=name;
		this.orientation=orientation;
		this.trainingFilename=trainingFilename;
		this.colorsName=colorsName;
	}
	
	@Override
	public String getName() {
		return name;
	}
	
	public String getOrientation() {
		return orientation;
	}
	
	public String getTrainingFilename() {
		return trainingFilename;
	}
	
	public String getColorsName() {
		return colorsName;
	}
	
	public LanguageAlphabet getLanguageAlphabet(Colors colors) {
		LanguageAlphabet languageAlphabet = new LanguageAlphabet();
		Map<Group, GroupColorInfo> groupColorInfos = new HashMap<>();
		for (Group group : groups) {
			GroupColorInfo groupColorInfo = colors.getGroupColorInfo(group.getColorInfoName());
			languageAlphabet.addCharacterGroup(new CharacterGroup(group.getStartIndex(), group.getEndIndex(),
					group.getLabel(), groupColorInfo.getColor(), groupColorInfo.getOutlineColor(),
					groupColorInfo.getLabelColor(), group.getSpeedFactor()));
			groupColorInfos.put(group, groupColorInfo);
		}
		for (Node node : nodes) {
			GroupColorInfo groupColorInfo = groupColorInfos.get(node.getGroup());
			languageAlphabet.addCharacter(new LanguageCharacter(node.getTrainingUnicode(), node.getFixedProbability(),
					node.getLabel(), groupColorInfo.getNextNodeColor(),
					groupColorInfo.getNextNodeOutlineColor(colors.getDefaultOutlineColor()),
					groupColorInfo.getNextNodeLabelColor(colors.getDefaultLabelColor()), node.getActions()));
		}
		return languageAlphabet;
	}
	
	public boolean usesKeyboardOutput() {
		for (Node node : nodes) {
			for (Action action : node.getActions()) {
				if (action instanceof KeyboardAction) return true;
			}
		}
		return false;
	}
	
	public boolean usesAccessibilityInterface() {
		for (Node node : nodes) {
			for (Action action : node.getActions()) {
				if (action instanceof AccessibilityAction) return true;
			}
		}
		return false;
	}
	
	public boolean usesSocketOutput() {
		for (Node node : nodes) {
			for (Action action : node.getActions()) {
				if (action instanceof SocketOutputAction) return true;
			}
		}
		return false;
	}
	
	void addGroup(Group group) {
		groups.add(group);
	}
	
	void addNode(Node node) {
		nodes.add(node);
	}
	
	Node getLastNode() {
		if (nodes.isEmpty()) return null;
		return nodes.get(nodes.size()-1);
	}
	
	int getNumberOfNodes() {
		return nodes.size();
	}
}