Class Literal

java.lang.Object
it.univr.di.labeledvalue.Literal
All Implemented Interfaces:
Comparable<Literal>

public final class Literal extends Object implements Comparable<Literal>
An immutable literal.

A literal is a char that can be preceded by the symbol '\u00ac', negated literal, or by the symbol '\u00bf', 'unknown' literal.

While the semantics of a literal and its negation are the standard ones, the semantics for an unknown literal are particular to the CSTN/CSTNU application.

An unknown literal, as '¿p' for example, is true if the value of the proposition letter 'p' is not assigned yet. False otherwise.

Therefore, if a state is characterized by the proposition '¿p', it means that the state is valid till the value of the proposition letter 'p' is unknown. In the instant the value of 'p' is set, '¿p' becomes false, and the associated state is no longer valid.

A literal object is immutable and must have a propositional letter.

Lastly, for efficiency reasons, this class allows for representing literals using at most Label.NUMBER_OF_POSSIBLE_PROPOSITIONS propositions in the range PROPOSITION_ARRAY. Label.NUMBER_OF_POSSIBLE_PROPOSITIONS is given by the fact that Label represents propositional labels using an integer (32 bits), so labels with at most 32 different propositions.

Version:
$Rev: 993 $
Author:
Roberto Posenato
  • Field Details

    • ABSENT

      public static final char ABSENT
      On 2017-10-27, using VisualVM, it was shown that representing the state values of a literal using an enum consumes a lot of memory in this kind of application.

      Therefore, I decided to simplify the representation using four constant values: ABSENT, STRAIGHT, NEGATED, and UNKNOWN. The char corresponding to each such constant is exploited in the class (to make them more efficient). So, don't change them without revising the whole class.

      ABSENT is useful only for internal methods. It is not admitted for defining a literal.

      See Also:
    • NEGATED

      public static final char NEGATED
      Constant NEGATED=Constants.NOT
      See Also:
    • PROPOSITIONS

      public static final String PROPOSITIONS
      R.E. representation of allowed propositions.
      See Also:
    • PROPOSITION_RANGE

      public static final String PROPOSITION_RANGE
      R.E. representation of PROPOSITION_ARRAY
      See Also:
    • STRAIGHT

      public static final char STRAIGHT
      Constant STRAIGHT=''
      See Also:
    • UNKNOWN

      public static final char UNKNOWN
      Constant UNKNOWN=Constants.UNKNOWN
      See Also:
    • PROPOSITION_ARRAY

      static final char[] PROPOSITION_ARRAY
      List of possible propositions managed by this class.

      Such a list is made by concatenating two blocks: a-z, and A-F.

      If such blocks are changed, please revise check(char) and index(char) methods because it exploits the bounds of such blocks. The length of this array cannot be modified without revising all this class code and the Label class.

      See Also:
  • Method Details

    • areComplement

      static boolean areComplement(char state1, char state2)
      Parameters:
      state1 - a possible state of a literal. No integrity check is done
      state2 - a possible state of a literal. No integrity check is done
      Returns:
      true if state1 and state2 are complement. False otherwise
      See Also:
    • charValue

      public static char charValue(int i)
      Parameters:
      i - a positive value smaller than 32.
      Returns:
      char at position i in PROPOSITION_ARRAY.
    • check

      public static boolean check(char c)
      Parameters:
      c - the char to check
      Returns:
      true if the char represents a valid literal identifier
    • getStateOrdinal

      static byte getStateOrdinal(char state)
      Returns the ordinal associate to state.
      Parameters:
      state - One of the following values: '\u00ac', '\u0001', '\u00bf'
      Returns:
      the ordinal associated with a proper state, a negative integer if the state is not recognized
    • hashCode

      static int hashCode(char c, char state)
      Hash code for a literal given as char c and state state.
      Parameters:
      c - char for proposition
      state - one of possible states of a literal NEGATED or STRAIGHT o UNKNOWN. No integrity check is done.
      Returns:
      an integer that is surely unique when 'a' ≤ c ≤ 'z'.
    • index

      static byte index(char c)
      Parameters:
      c - char for proposition
      Returns:
      the index of the given proposition c in PROPOSITION_ARRAY if it is a proposition, a negative integer otherwise.
    • parse

      @Nullable public static Literal parse(String s)
      Parses the string s returning the literal represented.
      Parameters:
      s - It can be a single char (PROPOSITION_ARRAY) or one of characters ['\u00ac' '\u00bf'] followed by a char of PROPOSITION_ARRAY. No spaces are allowed
      Returns:
      the literal represented by s if s is a valid representation of a literal, null otherwise
    • stateAsString

      static String stateAsString(char state)
      Parameters:
      state - state of the literal
      Returns:
      the string representation of state
    • toChars

      static char[] toChars(int propositionIndex, char state)
      Parameters:
      propositionIndex - index of proposition
      state - one of possible states of a literal NEGATED or STRAIGHT o UNKNOWN
      Returns:
      the char-array representation of a literal identified by its index and state parameter. If the state is not correct, an empty array is returned.
    • valueOf

      public static Literal valueOf(char v)
      Returns the positive literal of v.
      Parameters:
      v - a char in the range PROPOSITION_RANGE
      Returns:
      the straight literal of proposition v
    • valueOf

      @Nullable public static Literal valueOf(char v, char state)
      Return the literal having the given state of v.
      Parameters:
      v - the proposition letter
      state - one of possible states of a literal NEGATED or STRAIGHT o UNKNOWN
      Returns:
      a literal with name v and state state, null if the char is not valid or state if ABSENT.
    • valueOf

      public static Literal valueOf(Literal v, char state)
      Returns a new literal having same proposition of v but with state given by state.
      Parameters:
      v - a non null literal v
      state - one of possible states of a literal: NEGATED or STRAIGHT o UNKNOWN
      Returns:
      a new literal having same proposition of v but with state given by state.
    • compareTo

      public int compareTo(Literal o)
      Specified by:
      compareTo in interface Comparable<Literal>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • getComplement

      @Nullable public Literal getComplement()
      Returns the complement of this.

      The complement of a straight literal is the negated one.

      Returns:
      a new literal that is the complement of an unknown literal, which is null (an empty literal is not possible).
    • getName

      public char getName()
      Returns:
      the propositional letter associated with this
    • getNegated

      public Literal getNegated()
      Returns a literal that is the negation of this.
      Returns:
      a new literal with the same name and state negated
    • getState

      public char getState()
      Returns:
      the state
    • getStraight

      public Literal getStraight()
      Returns a literal that is the straight literal of this.
      Returns:
      a new literal with the same name and state straight
    • getUnknown

      public Literal getUnknown()
      Returns a literal that is the unknown literal of this.
      Returns:
      a new literal with the same name and state unknown
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • isComplement

      public boolean isComplement(Literal l)
      Parameters:
      l - an object.
      Returns:
      true if it is a complement literal of the given one
    • isNegated

      public boolean isNegated()
      Returns:
      true if it is a negated literal
    • isStraight

      public boolean isStraight()
      Returns:
      true if it is a straight literal
    • isUnknown

      public boolean isUnknown()
      Returns:
      true if it is a literal in the unknown state
    • toString

      public String toString()
      Overrides:
      toString in class Object