de.citec.tcs.alignment.sequence.Node Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sequences Show documentation
Show all versions of sequences Show documentation
This module contains the sequence datastructure of the
TCS Alignment Toolbox. It defines the possible value sets in the
ValueType enum as well as the different KeywordSpecification classes, namely:
1.) StringKeywordSpecification for string type values.
2.) SymbolicKeywordSpecification for values from a discrete alphabet (also refer to the Alphabet class)
3.) VectorialKeywordSpecification for vectors of some length (or for scalars)
A NodeSpecification is a vector of such KeywordSpecifications and defines
the order of value sets. A node, then, is defined as a vector of values
from these value sets (also refer to the Value interface as well as the
StringValue, SymbolicValue and VectorialValue classes). Finally a
sequence is defined as a list of such nodes.
/*
* TCS Alignment Toolbox Version 3
*
* Copyright (C) 2016
* Benjamin Paaßen
* AG Theoretical Computer Science
* Centre of Excellence Cognitive Interaction Technology (CITEC)
* University of Bielefeld
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.citec.tcs.alignment.sequence;
import java.util.Arrays;
import lombok.NonNull;
/**
* This is a node in a Sequence storing values for the keywords defined in the
* sequence.
*
* More formally: This Node is defined according to a NodeSpecification that
* defines a set of keywords K as well as for each keyword k in K the allowed
* space of values V_k. Then this node is a mapping from keywords to concrete
* values:
*
* n(k) = v_k where v_k in V_k
*
* @author Benjamin Paassen - [email protected]
*/
public class Node {
private final Sequence sequence;
private final Value[] valueMap;
public Node(@NonNull Sequence sequence) {
this.sequence = sequence;
valueMap = new Value[sequence.getNodeSpecification().size()];
}
/**
* Returns the sequence this node belongs to.
*
* @return the sequence this node belongs to.
*/
public Sequence getSequence() {
return sequence;
}
/**
* Returns the value stored in this Node for the given keyword or null of no
* value is stored for the given keyword in this node.
*
* @param keyword a keyword
*
* @return the value stored in this Node for the given keyword or null of no
* value is stored for the given keyword in this node.
*
* @throws UnsupportedOperationException is thrown if the given keyword is
* not supported by the NodeSpecification.
*/
public Value getValue(@NonNull final String keyword) throws UnsupportedOperationException {
if (!sequence.getNodeSpecification().hasKeyword(keyword)) {
throw new UnsupportedOperationException("The given keyword is not defined in the sequence!");
}
return valueMap[sequence.getNodeSpecification().getKeywordIndex(keyword)];
}
/**
* Returns the value stored in this Node for the given keyword or null of no
* value is stored for the given keyword in this node.
*
* @param index a keyword index.
*
* @return the value stored in this Node for the given keyword or null of no
* value is stored for the given keyword in this node.
*
* @throws ArrayIndexOutOfBoundsException is thrown if the given index is
* too large or too small.
*/
public Value getValue(final int index) throws ArrayIndexOutOfBoundsException {
return valueMap[index];
}
/**
* Sets the value for the given keyword.
*
* @param keyword a keyword.
* @param value a value with the correct type.
*
* @throws UnsupportedOperationException is thrown if the given keyword is
* not supported by the NodeSpecification or if the given value has the
* wrong type.
*/
public void setValue(@NonNull final String keyword, final Value value)
throws UnsupportedOperationException {
if (!sequence.getNodeSpecification().hasKeyword(keyword)) {
throw new UnsupportedOperationException("The given keyword is not defined in the sequence!");
}
setValue(sequence.getNodeSpecification().getKeywordIndex(keyword), value);
}
/**
* Sets the value for the given keyword.
*
* @param index a keyword index.
* @param value a value with the correct type.
*
* @throws UnsupportedOperationException is thrown if the given value has
* the wrong type.
* @throws ArrayIndexOutOfBoundsException is thrown if the given index is
* too large or too small.
*/
public void setValue(final int index, final Value value)
throws UnsupportedOperationException, ArrayIndexOutOfBoundsException {
if (!sequence.getNodeSpecification().getKeywordSpecification(index).validate(value)) {
throw new UnsupportedOperationException("The given value " + value
+ " does not meet the given specification for the keyword!");
}
valueMap[index] = value;
}
@Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + Arrays.deepHashCode(this.valueMap);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Node other = (Node) obj;
if (!Arrays.deepEquals(this.valueMap, other.valueMap)) {
return false;
}
return true;
}
}