ArgumentPositionMapping.java

// FILE. . . . . d:/hak/hlt/src/hlt/fot/fuz/ArgumentPositionMapping.java
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . Hak-Laptop
// STARTED ON. . Sun Jul 15 07:37:57 2018

package hlt.fot.fuz;



This class represents an argument-position mapping as a set of pairs between distinct but α-similar Functors for each admissible fuzzy degree α in the defined functor similarity.

See also:  /Signature, /Functor, SignatureSimilarity
Copyright:  © by the author
Author:  Hassan Aït-Kaci
Version:  Last modified on Sun Aug 12 15:10:04 2018 by hak



import hlt.fot.Functor;

import hlt.language.util.SetOf;
import hlt.language.util.ArrayList;
import hlt.language.util.IntArrayList;

public class ArgumentPositionMapping
{
  

This mapping's fuzzy degree. It is initialized in the constructor and never changes.


  private double degree;

  

Returns this mapping's fuzzy degree.


  public double degree ()
  {
    return degree;
  }

  

The position maps this mapping is part of.


  private ArgumentPositionMaps positionMaps;

  

Returns the position maps this mapping is part of.


  public ArgumentPositionMaps positionMaps ()
  {
    return positionMaps;
  }

  

The index in positionMaps.argumentMappings() of this mapping's fuzzy degree.


  private int degreeIndex;

  

Returns index in positionMaps.argumentMappings() of this mapping's fuzzy degree.


  public int degreeIndex ()
  {
    return degreeIndex;
  }

  /* ************************************************************************ */

  

Returns the similarity this refers to.


  public  SignatureSimilarity similarity ()
  {
    return positionMaps.similarity();
  }

  

Returns the similarity's signature.


  public  SimilarFunctorSignature signature ()
  {
    return similarity().signature();
  }

  /* ************************************************************************ */

  ///////////////////////
  // FROM functor data //
  ///////////////////////

  

This mapping's "from" Functor. It is initialized in the constructor and never changes.


  private Functor from;

  

Returns the "from" function symbol (a Functor) of this ArgumentPositionMapping.


  public Functor from ()
  {
    return from;
  }

  

The ArrayList domainPositions is a list of mutually distinct Integer objects. Its size is equal to from.arity(). Each Integer element at index i in this list, 0i < from.arity(), has an int value equal to i+1. It is initialized in the constructor and is never modified thereafter because it serves as the base structure for the set domain.


  private ArrayList domainPositions;

  

The full set of Integer elements of domainPositions of the from functor representing {1,..., from.arity()}. It is initialized in the constructor and never changes.


  private SetOf fullDomainPositions;

  

A set of Integer elements of domainPositions of the from functor. It is initialized by default in the constructor to the set {1,...,Math.min(from.arity(),to.arity())} but can be modified using the defineMapping method.


  private SetOf domain;

  

Returns the domain of this mapping as a set of positions if one is defined, or null if from.arity() = 0; otherwise sets domain to the set {1, ..., Math.min(from.arity(),to.arity())} and returns it.


  public SetOf domain ()
  {
    if (domain != null)
      return domain;
    
    if (from.arity() == 0)
      return null;

    domain = new SetOf(domainPositions);

    for (int pos = 0; pos < Math.min(from.arity(),to.arity()); pos++)
      domain.add(domainPositions.get(pos));
    
    return domain;
  }

  

Sets this mapping's domain to the specified verified domain and returns it.


  public SetOf setDomain (SetOf domain)
  {
    return this.domain = verifiedDomain(domain);
  }

  

Verifies that the specified set is a subset of from's full domain and that its number of elements does not exceed Math.min(from.arity(),to.arity()). If ok, it returns the domain. Otherwise, it throws an BadArgumentPositionMappingException with a detailed message.


  public SetOf verifiedDomain (SetOf domain)
  {
    if (!domain.isSubsetOf(fullDomainPositions) || domain.size() > Math.min(from.arity(),to.arity()))
      throw new BadArgumentPositionMappingException
	    ("fuzzy degree: "+degree+", "+
	     "from: "+from+", "+
	     "to: "+to+"; "+
	     "argument position map's domain "+domain+
	     " should be a subset of "+fullDomainPositions+
	     " and not exceed "+ Math.min(from.arity(),to.arity()));

    return domain;
  }

  /* ************************************************************************ */

  /////////////////////
  // TO functor data //
  /////////////////////

  

This mapping's "to" Functor.


  private Functor to;

  

Returns the "to" function symbol (a Functor) of this ArgumentPositionMapping. It is initialized in the constructor and never changes.


  public Functor to ()
  {
    return to;
  }

  

The ArrayList rangePositions is a list of mutually distinct Integer objects. Its size is equal to to.arity(). Each Integer element at index i in this list, 0i < to.arity(), has an int value equal to i+1. It is initialized in the constructor and is never modified thereafter because it serves as the base structure for the set range.


  private ArrayList rangePositions;

  

The full set of Integer elements of rangePositions of the to functor representing {1,...,to.arity()}. It is initialized in the constructor and never changes. It is initialized in the constructor and never changes.


  private SetOf fullRangePositions;

  

A set of Integer elements of rangePositions of the to functor. It is initialized by default in the constructor to the set {1,...,Math.min(from.arity(),to.arity())} but can be modified using the defineMapping method.


  private SetOf range;

  

Returns the range of this mapping as a set of positions if one is defined, or null if to.arity() = 0; otherwise sets range to the set {1, ..., Math.min(from.arity(),to.arity())} and returns it.


  public SetOf range ()
  {
    if (range != null)
      return range;
    
    if (from.arity() == 0)
      return null;

    range = new SetOf(rangePositions);

    for (int pos = 0; pos < Math.min(from.arity(),to.arity()); pos++)
      range.add(rangePositions.get(pos));
    
    return range;
  }

  

Sets this mapping's range to the specified verified range and returns it.


  public SetOf setRange (SetOf range)
  {
    return this.range = verifiedRange(range);
  }

  

Verifies that the specified set is a subset of to's full range and that its number of elements does not exceed Math.min(from.arity(),to.arity()). If ok, it returns the domain. Otherwise, it throws an BadArgumentPositionMappingException with a detailed message.


  public SetOf verifiedRange (SetOf range)
  {
    if (!range.isSubsetOf(fullRangePositions) || range.size() > Math.min(from.arity(),to.arity()))
      throw new BadArgumentPositionMappingException
	("fuzzy degree: "+degree+", "+
	 "from: "+from+", "+
	 "to: "+to+"; "+
	 "argument position map's range "+range+
	 " should be a subset of "+fullRangePositions+
	 " and not exceed "+ Math.min(from.arity(),to.arity()));

    return range;
  }

  /* ************************************************************************ */

  //////////////////
  // MAPPING data //
  //////////////////
  
  

The two arrays mapping and inverse represent an ArgumentPositionMapping object. They are arrays of argument positions that correspond when comparing the distinct and degree-similar functors from and to. This correspondence must be injective. Therefore, these arrays are non-null only for a non-empty correspondence (i.e., when neither of the functors is a constant or when the similarity does not involve any arguments in either of the functors). Otherwise, they must be both defined and have equal length size ∈ {1, ..., Math.min(from.arity(),to.arity())}. If from's argument position ithis.domain() and to's argument position jthis.range correspond, then there must be a unique k{0, ..., size-1} such that mapping[k] = j and inverse[k] = i.

The arrays mapping and inverse are set by default to values that represent the identity on the largest set of argument positions common to from and to. So, size = Math.min(from.arity(),to.arity()) and domain = range = {1, ..., size}, such that for all k ∈ {0, ..., size-1}, mapping[k] = inverse[k] = k+1, since positions start at 1 while array indices start at 0.


  private int[] mapping;
  private int[] inverse;

  

Returns true iff this argument position mapping is empty.


  public boolean isEmpty ()
  {
    return mapping == null;
  }

  

Returns the size of this ArgumentPositionMapping.


  public int size ()
  {
    return isEmpty() ? 0 : mapping.length; // == inverse.length
  }

  /* ************************************************************************ */

  /////////////////
  // Constructor //
  /////////////////

  

This constructs a new ArgumentPositionMapping object to be part of the specified ArgumentPositionMaps at approximation degree of index i in similarity().degrees() between Functor from and Functor to. It is set by default to the identity on the set {1,...,Math.min(from.arity(),to.arity())}.


  public ArgumentPositionMapping (ArgumentPositionMaps maps, int index, Functor from, Functor to)
  {
    positionMaps = maps;

    degreeIndex = index;
    degree = maps.degrees().get(degreeIndex);

    this.from = from;
    this.to   = to;

    int fromArity = from.arity();
    int toArity   = to.arity();

    int minArity  = Math.min(fromArity,toArity);
    int maxArity  = Math.max(fromArity,toArity);

    // build the domain and range bases to act as reference lists to the corresponding sets:
    domainPositions = new ArrayList(fromArity);
    rangePositions  = new ArrayList(toArity);

    for (int i = 0; i < maxArity; i++)
      {
	Integer position = Integer.valueOf(i+1); // because argument positions start from 1

	if (i < minArity)
	  {
	    domainPositions.set(i,position);
	    rangePositions.set(i,position);
	    continue;
	  }

	if (i < fromArity)
	  domainPositions.set(i,position);

	if (i < toArity)
	  rangePositions.set(i,position);
      }

    // build the corresponding domain and range as full sets of positions by default
    // (definedMapping method can modify them as appropriate):
    domain = new SetOf(domainPositions);
    range  = new SetOf(rangePositions);

    for (int i = 0; i < maxArity; i++)
      {
	Integer position = Integer.valueOf(i+1); 

	if (i < minArity)
	  {
	    domain.add(position);
	    range.add(position);
	    continue;
	  }

	if (i < fromArity)
	  domain.add(position);

	if (i < toArity)
	  range.add(position);

      }

    // initialize this mapping and its inverse to the identity on
    // {1,...,Math.min(from.arity(),to.arity())} by default
    // (definedMapping method can modify them as appropriate):
    if (minArity > 0)
      {
	mapping = new int[minArity];
	inverse = new int[minArity];

	for (int k = 0; k < minArity; k++)
	  mapping[k] = inverse[k] = k+1;
      }
  }

  

Defines an argument-position mapping with the specified list of ints. This list is of the form [i1, j1, ..., in, jn]. If it contains valid argument positions of the from and to functors, invoking this.defineMapping(positionPairs) will define this ArgumentPositionMapping's set of domainPositions domain = {Integer(i1), ..., Integer(in)} and its set of rangePositions range = {Integer(j1), ..., Integer(jn)}, and also define the corresponding arrays of values of these positions mapping and inverse, and return this ArgumentPositionMapping; otherwise, throws a BadArgumentPositionMappingException with a detailed message.

For example, let us assume that from.arity() = 5 and to.arity() = 4, and that their argument mapping specifier is of size 3 and equal to: 2:4 5:2 3:1, which corresponds to the list of ints: [2, 4, 5, 2, 3, 1]. This will give: mapping = [2, 5, 3], inverse = [4, 2, 1], domain = {Integer(2), Integer(3), Integer(5)}, and range = {Integer(1), Integer(2), Integer(4)}. Note that while order and repeated elements are relevant for lists, order is irrelevant for sets (we shall display them by default in natural order of arguments) and there can be no repeated element.


  public ArgumentPositionMapping defineMapping (IntArrayList positionPairs)
  {
    SetOf domain = new SetOf(domainPositions);
    SetOf range  = new SetOf(domainPositions);

    if (positionPairs == null)
      { // empty domain and range (and so empty mapping/inverse)
	this.domain  = domain;
	this.range   = range;
	return this;
      }

    int cardinality = positionPairs.size() / 2; // since size is necessarily even

    int[] mapping = new int[cardinality];
    int[] inverse = new int[cardinality];

    for (int i = 0; i < cardinality; i++)
      {
	Integer leftPosition  = Integer.valueOf(positionPairs.get(2*i));
	domain.add(leftPosition);

	Integer rightPosition = Integer.valueOf(positionPairs.get(2*i+1));
	range.add(rightPosition);

	mapping[i] = leftPosition.intValue();
	inverse[i] = leftPosition.intValue();
      }

    // verify that all argument positions are ok and define a one-to-one mapping

    int minArity = Math.min(from.arity(),to.arity());
    
    if (domain.size() > minArity)
      throw new BadArgumentPositionMappingException
	("fuzzy degree: "+degree+", "+
	 "from: "+from+", "+
	 "to: "+to+"; "+
	 "mapping domain too large for functor "+from+": "+domain);

    if (range.size() > minArity)
      throw new BadArgumentPositionMappingException
	("fuzzy degree: "+degree+", "+
	 "from: "+from+", "+
	 "to: "+to+"; "+
	 "mapping range too large for functor "+to+": "+range);

    if (domain.size() != range.size())
      throw new BadArgumentPositionMappingException
	("fuzzy degree: "+degree+", "+
	 "from: "+from+", "+
	 "to: "+to+
	 "; non one-one argument-position mapping from :"+domain+" to "+range);

    // all is ok; can commit:

    this.domain  = domain;
    this.range   = range;
    this.mapping = mapping;
    this.inverse = inverse;

    return this;
  }
}


This file was generated on Sat Aug 25 08:03:09 CEST 2018 from file ArgumentPositionMapping.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci