All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unitn.disi.smatch.data.mappings.HashMapping Maven / Gradle / Ivy

The newest version!
package it.unitn.disi.smatch.data.mappings;

import it.unitn.disi.common.components.Configurable;
import it.unitn.disi.common.components.ConfigurableException;
import it.unitn.disi.smatch.data.ling.IAtomicConceptOfLabel;
import it.unitn.disi.smatch.data.trees.IContext;
import it.unitn.disi.smatch.data.trees.INode;

import java.util.*;

/**
 * Default mapping implementation. Permits only one relation between source and target. Maps source and target pairs to
 * the index of the relation character stored in a string.
 *
 * @author 
 */
public class HashMapping extends BaseMapping implements IContextMapping, IMappingFactory {

    protected Properties properties;

    // source+target pairs mapped to index of relations
    private Map, Integer> entries;
    // relations for the above pairs
    private StringBuilder relations;

    private static class NodePair {
        final K key;
        final V value;

        NodePair(K k, V v) {
            value = v;
            key = k;
        }

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

        public final boolean equals(Object o) {
            if (!(o instanceof NodePair)) {
                return false;
            }
            NodePair e = (NodePair) o;
            Object k1 = getKey();
            Object k2 = e.getKey();
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                Object v1 = getValue();
                Object v2 = e.getValue();
                if (v1 == v2 || (v1 != null && v1.equals(v2))) {
                    return true;
                }
            }
            return false;
        }

        public final int hashCode() {
            return (key == null ? 0 : key.hashCode()) ^
                    (value == null ? 0 : value.hashCode());
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }
    }

    public HashMapping(IContext sourceContext, IContext targetContext) {
        this();
        this.sourceContext = sourceContext;
        this.targetContext = targetContext;
        properties = new Properties();
    }

    public HashMapping(Properties properties) {
        this();
        this.properties = properties;
    }

    public HashMapping() {
        entries = new HashMap, Integer>();
        relations = new StringBuilder();
    }

    public Properties getProperties() {
        return properties;
    }

    public boolean setProperties(Properties newProperties) throws ConfigurableException {
        boolean result = !newProperties.equals(properties);
        if (result) {
            properties.clear();
            properties.putAll(newProperties);
        }

        return result;
    }

    public boolean setProperties(String fileName) throws ConfigurableException {
        return setProperties(Configurable.loadProperties(fileName));
    }

    public IContextMapping getContextMappingInstance(IContext source, IContext target) {
        return new HashMapping(source, target);
    }

    public IContextMapping getACoLMappingInstance(IContext source, IContext target) {
        return new HashMapping(source, target);
    }

    public int size() {
        return entries.size();
    }

    public boolean isEmpty() {
        return entries.isEmpty();
    }

    public boolean contains(Object o) {
        if (o == null) {
            return false;
        }
        if (getClass() != o.getClass()) {
            return false;
        }

        @SuppressWarnings("unchecked")
        IMappingElement e = (IMappingElement) o;
        if (IMappingElement.IDK == e.getRelation()) {
            return false;
        }
        Integer idx = entries.get(new NodePair(e.getSource(), e.getTarget()));
        return null != idx && 0 <= idx && idx < relations.length() && (e.getRelation() == relations.charAt(idx));
    }

    private class Itr implements Iterator> {
        private Iterator> i;
        private NodePair lastPair;

        private Itr(Iterator> i) {
            this.i = i;
        }

        public boolean hasNext() {
            return i.hasNext();
        }

        public IMappingElement next() {
            NodePair np = i.next();
            lastPair = np;
            return new MappingElement(np.getKey(), np.getValue(), relations.charAt(entries.get(np)));
        }

        public void remove() {
            int idx = entries.get(lastPair);
            relations.delete(idx, idx + 1);
            i.remove();
        }
    }

    public Iterator> iterator() {
        return new Itr(entries.keySet().iterator());
    }

    public boolean add(IMappingElement e) {
        return setRelation(e.getSource(), e.getTarget(), e.getRelation());
    }

    public boolean remove(Object o) {
        if (o == null) {
            return false;
        }
        if (getClass() != o.getClass()) {
            return false;
        }

        @SuppressWarnings("unchecked")
        IMappingElement e = (IMappingElement) o;
        NodePair np = new NodePair(e.getSource(), e.getTarget());
        Integer idx = entries.get(np);
        if (null == idx) {
            return false;
        } else {
            if (e.getRelation() == relations.charAt(idx)) {
                relations.delete(idx, idx + 1);
                entries.remove(np);
                return true;
            } else {
                return false;
            }
        }
    }

    public void clear() {
        entries.clear();
        relations = new StringBuilder();
    }

    public char getRelation(T source, T target) {
        NodePair np = new NodePair(source, target);
        Integer idx = entries.get(np);
        if (null == idx) {
            return IMappingElement.IDK;
        } else {
            return relations.charAt(idx);
        }
    }

    public boolean setRelation(T source, T target, char relation) {
        NodePair np = new NodePair(source, target);
        Integer idx = entries.get(np);
        if (null == idx) {
            if (IMappingElement.IDK != relation) {
                entries.put(np, relations.length());
                relations.append(relation);
                return true;
            }
            return false;
        } else {
            if (IMappingElement.IDK != relation) {
                if (relation != relations.charAt(idx)) {
                    relations.setCharAt(idx, relation);
                    return true;
                }
                return false;
            } else {
                relations.delete(idx, idx + 1);
                entries.remove(np);
                return true;
            }
        }
    }

    public List> getSources(final T source) {
        ArrayList> result = new ArrayList>();
        for (IMappingElement me : this) {
            if (source == me.getSource()) {
                result.add(me);
            }
        }
        //TODO impose the order to keep results consistent, because hashset iterator does not guarantee the order
        return result;
    }

    public List> getTargets(T target) {
        ArrayList> result = new ArrayList>();
        for (IMappingElement me : this) {
            if (target == me.getTarget()) {
                result.add(me);
            }
        }
        //TODO impose the order to keep results consistent, because hashset iterator does not guarantee the order
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy