C # Implementation of three-valued logic

Source: Internet
Author: User
Tags bool constructor implement tostring
Three-valued logical classes in C #
The practical value of the three-valued logic has not been neglected, and in most books introducing relational database knowledge, it involves the discussion of NULL value and the three-valued logic. MSDN, however, gives a three-valued logic structure (struct) implemented in C #, which provides three-valued logic operations in the application layer. Related articles posted as follows:







C # Language Specification



11.4.2 Database Boolean type






The DBBool struct below implements a three-valued logical type. The possible values of this type is are Dbbool.true, Dbbool.false, and Dbbool.null, where the Null member indicates a unknow N value. Such three-valued logical types are commonly used in databases.

Using System;
public struct DBBool
{
The three possible dbbool values.
public static readonly DBBool Null = new DBBool (0);
public static readonly DBBool False = new DBBool (-1);
public static readonly DBBool True = new DBBool (1);
Private field that is stores–1, 0, 1 for False, Null, True.
SByte value;
Private instance constructor. The value parameter must be–1, 0, or 1.
DBBool (int value) {
This.value = (sbyte) value;
}
Properties to examine the value of a dbbool. return TRUE if this
DBBool has the given value, false otherwise.
public bool IsNull {get {return value = = 0;}}
public bool IsFalse {get {return value < 0;}}
public bool IsTrue {get {return value > 0;}}
Implicit conversion from bool to DBBool. Maps true to Dbbool.true and
False to Dbbool.false.
public static implicit operator DBBool (bool x) {
return x? True:false;
}
Explicit conversion from DBBool to bool. Throws an exception if the
Given DBBool is Null, otherwise returns TRUE or FALSE.
public static explicit operator bool (DBBool x) {
if (X.value = = 0) throw new InvalidOperationException ();
return x.value > 0;
}
Equality operator. Returns null if either operand is null, otherwise
Returns True or False.
public static DBBool operator = = (DBBool x, DBBool y) {
if (X.value = = 0 | | y.value = 0) return Null;
return x.value = = Y.value? True:false;
}
Inequality operator. Returns null if either operand is null, otherwise
Returns True or False.
public static DBBool operator!= (DBBool x, DBBool y) {
if (X.value = = 0 | | y.value = 0) return Null;
Return x.value!= y.value? True:false;
}
Logical negation operator. Returns True If the operand is False, Null
If the operand is Null, or False if the operand is True.
public static DBBool operator! (DBBool x) {
return new DBBool (-x.value);
}
Logical and operator. Returns False if either operand is false,
Otherwise null if either operand is null, otherwise True.
public static DBBool operator & (DBBool x, DBBool y) {
Return to New DBBool (X.value < y.value? x.value:y.value);
}
Logical OR operator. Returns true if either operand is True, otherwise
Null if either operand is null, otherwise False.
public static DBBool operator | (DBBool x, DBBool y) {
Return to New DBBool (X.value > Y.value? x.value:y.value);
}
Definitely true operator. Returns true if the operand is true, false
otherwise.
public static bool operator true (DBBool x) {
return x.value > 0;
}
Definitely false operator. Returns true if the operand is false, false
otherwise.
public static bool operator false (DBBool x) {
return X.value < 0;
}
public override bool Equals (object obj) {
if (!) ( obj is DBBool), return false;
return value = = ((dbbool) obj). value;
}
public override int GetHashCode () {
return value;
}
public override string ToString () {
if (value > 0) return "dbbool.true";
if (value < 0) return "Dbbool.false";
return "Dbbool.null";
}
}




--------------------------------------------------------------------------------

Send feedback on this topic to Microsoft

©microsoft Corporation. All rights reserved.

As we can see from the article, it uses the third algorithm we described earlier. This example builds a good framework that includes the necessary type conversions, comparisons, and essential GetHashCode and ToString methods in the. NET CLR, in addition to, or operations.

When we face this simple code with a beginner's mentality, there are several places worth learning:

Within the structure, the 1, 0, and the first represent three different logical states. And by defining false, NULL, true three constants to represent all possible values for all possible objects of that type. This kind of numerical and logical correspondence accords with people's conventional thinking habits and the aesthetic sense of mathematics. Also facilitates the realization GetHashCode method.

Using the internal numerical value, simple and beautiful to achieve the and/or/not operation. If you follow the other two of the three logical algorithms we mentioned earlier, it's not as beautiful as it looks. Perhaps this is the reason that many relational databases choose this algorithm to implement. Beauty is a very important thing in the system of mathematics.

Provide IsTrue, IsFalse, isnull judgment function, easy to use.

When three-valued logic transforms to two-valued logic and dbnull, it must show transitions (explicit), whereas only implicit conversions (implicit) are required.

True and false operators are implemented.

Overloaded the GetHashCode and ToString methods required by the. NET CLR. When we work in a particular environment, we should follow the requirements and conventions of that environment, which is often overlooked in actual development.

In order to meet the needs of actual use, I have a number of extensions to this class. Mainly as follows:

Conversions to and from the DBNull type (consider the type conversion exception).

The parsing method parse from string to three-valued logic (which changes the ToString () method accordingly).

A new constructor is added.

Increases the and/or operations that support the other two logical computing systems.

Increased conversion function Todbboolean used to assign values to database logical fields.

The new code is as follows:

Using System;

Namespace March.vboolean
{
<summary>
The three-valued logic class (Bool with three) supports System.DBNull.
</summary>
public struct BOOLW3
{
The three possible Boolw3 values.
public static readonly Boolw3 Null = new BOOLW3 (0);
public static readonly Boolw3 False = new BOOLW3 (-1);
public static readonly Boolw3 True = new BOOLW3 (1);
Private field that is stores–1, 0, 1 for False, Null, True.
SByte value;
Private instance constructor. The value parameter must be–1, 0, or 1.
BOOLW3 (int value)
{
This.value = (sbyte) value;
}

Public BOOLW3 (bool value)
{
This.value = value? (sbyte) 1: (sbyte)-1;
}
Public Boolw3 (DBNull value)
{
This.value = (sbyte) 0;
}
<summary>
To construct an instance from the logical field value of a database component
</summary>
<param name= "?" > can only be System.Boolean or DBNull type. </param>
Public Boolw3 (object value)
{
if (null = = value)
throw new ArgumentException ("The value must in true, False or dbnull!");
if (value. GetType () = = typeof (bool))
{
This.value = (bool) value? (sbyte) 1: (sbyte)-1;
Return
}
if (value. GetType () = = typeof (DBNull))
{
This.value = (sbyte) 0;
Return
}
throw new ArgumentException ("The value must in true, False or dbnull!");
}
<summary>
Resolves a value from a string.
</summary>
<param name= "value" > Optional value is "True", "False", "Null", possibly with qualified name "BOOLW3" </param>
public static BOOLW3 Parse (string value)
{
BOOLW3 Re = Null;
Switch (value)
{
Case "Boolw3.true":
Case "True":
{
Re.value = (sbyte) 1;
Break
}
Case "Boolw3.false":
Case "False":
{
Re.value = (sbyte)-1;
Break
}
Case "Boolw3.null":
Case "Null":
{
Re.value = (sbyte) 0;
Break
}
Default
throw new ArgumentException ("the value must in \ boolw3.true\", \ "boolw3.false\", \ "boolw3.null\", \ "true\", \ "False\" or \ "Null\");
}
return Re;
}
Properties to examine the value of a Boolw3. return TRUE if this
BOOLW3 has the given value, false otherwise.
public bool IsNull {get {return value = = 0;}}
public bool IsFalse {get {return value < 0;}}
public bool IsTrue {get {return value > 0;}}
Implicit conversion from bool to BOOLW3. Maps true to Boolw3.true and
False to Boolw3.false.
public static implicit operator BOOLW3 (bool x)
{
return x? True:false;
}

public static implicit operator BOOLW3 (DBNull x)
{
return Null;
}

Explicit conversion from BOOLW3 to bool. Throws an exception if the
Given BOOLW3 is Null, otherwise returns TRUE or FALSE.
public static explicit operator bool (BOOLW3 x)
{
if (X.value = = 0) throw new InvalidOperationException ();
return x.value > 0;
}

public static explicit operator DBNull (BOOLW3 x)
{
if (x.value!= 0) throw new InvalidOperationException ();
return dbnull.value;
}

Equality operator. Returns null if either operand is null, otherwise
Returns True or False.
public static BOOLW3 operator = = (Boolw3 x, Boolw3 y)
{
if (X.value = = 0 | | y.value = 0) return Null;
return x.value = = Y.value? True:false;
}
Inequality operator. Returns null if either operand is null, otherwise
Returns True or False.
public static BOOLW3 operator!= (Boolw3 x, Boolw3 y)
{
if (X.value = = 0 | | y.value = 0) return Null;
Return x.value!= y.value? True:false;
}
Logical negation operator. Returns True If the operand is False, Null
If the operand is Null, or False if the operand is True.
public static BOOLW3 operator! (BOOLW3 x)
{
return new BOOLW3 (-x.value);
}
Logical and operator. Returns False if either operand is false,
Otherwise null if either operand is null, otherwise True.
public static BOOLW3 operator & (Boolw3 x, Boolw3 y)
{
Return to New BOOLW3 (X.value < y.value? x.value:y.value);
}
Logical OR operator. Returns true if either operand is True, otherwise
Null if either operand is null, otherwise False.
public static BOOLW3 operator | (Boolw3 x, Boolw3 y)
{

Return to New BOOLW3 (X.value > Y.value? x.value:y.value);
}

<summary>
Verifyand is actually a logic and operation with a null value as the lowest priority. Typically used to verify data validation.
Returns false when at least one of the two operands is false, otherwise, at least one is true when true, or no
NULL is returned.
</summary>
<param name= "x" > left operand </param>
<param name= "Y" > right-hand operand </param>
<returns> result is Boolw3 type </returns>
public static BOOLW3 Verifyand (Boolw3 x, Boolw3 y)
{
if (x.value = = 1 | | y.value = = 1) return False;
if (x.value = = 1 | | y.value = 1) return True;
return Null;
}

<summary>
Verifyor is actually a logic or operation with a null value as the lowest priority. Typically used to verify data validation.
Returns true when at least one of the two operands is true, or false when at least one is false, no
NULL is returned.
</summary>
<param name= "x" > left operand </param>
<param name= "Y" > right-hand operand </param>
<returns> result is Boolw3 type </returns>
public static BOOLW3 Verifyor (Boolw3 x, Boolw3 y)
{
if (x.value = = 1 | | y.value = 1) return True;
if (X.value = = 1 & y.value = = 1) return False;
return True;
}

<summary>
Dband is the logic and operation of the highest priority value with null values, common to some database platforms. One of the operands is
NULL, the return value is null, and the other is the same as the two value logic.
</summary>
<param name= "x" > left operand </param>
<param name= "Y" > right-hand operand </param>
<returns> result is Boolw3 type </returns>
public static BOOLW3 Dband (Boolw3 x, Boolw3 y)
{
if (X.value = = 0 | | | y.value ==0) return Null;
Return to New BOOLW3 (X.value < y.value? x.value:y.value);
}

<summary>
Dbor are logic or operations with a null value as the highest priority, and are common to some database platforms. One of the operands is
NULL, the return value is null, and the other is the same as the two value logic.
</summary>
<param name= "x" > left operand </param>
<param name= "Y" > right-hand operand </param>
<returns> result is Boolw3 type </returns>
public static BOOLW3 Dbor (Boolw3 x, Boolw3 y)
{
if (X.value = = 0 | | | y.value ==0) return Null;
Return to New BOOLW3 (X.value > Y.value? x.value:y.value);
}

Definitely true operator. Returns true if the operand is true, false
otherwise.
public static bool operator true (BOOLW3 x)
{
return x.value > 0;
}
Definitely false operator. Returns true if the operand is false, false
otherwise.
public static bool operator false (BOOLW3 x)
{
return X.value < 0;
}
public override bool Equals (object obj)
{
if (!) ( obj is BOOLW3), return false;
return value = = ((Boolw3) obj). value;
}
public override int GetHashCode ()
{
return value;
}
public override string ToString ()
{
if (value > 0) return "boolw3.true";
if (value < 0) return "Boolw3.false";
return "Boolw3.null";
}

<summary>
Boolean-type (such as sqldbtype.bit) field assignment to the database access component
</summary>
<returns> returns an object whose internal encapsulation may be true, false, or dbnull.value</returns>
public Object Todbboolean ()
{
return (value = = 0)? (object) DBNull.Value: (object) (value>0);
}
}
}


The above code can be applied to meet the needs, but the following areas are worth further improvement:

The implementation of the three logical systems in the same type should be very uncoordinated, affecting code consistency and our general usage habits. Common code should be unified into a base class, and the and/or operation implementations are specified in the form of abstract methods or interfaces. Implemented in three different subclasses. and implement display type conversions between different child classes in subclasses. For unnecessary strong coupling, you can implement an implicit type conversion of subclasses to base classes (due to the nature of OO language, this is valid by default, as well as the explicit type conversions of the base class to subclasses (this can be implemented directly by defining a fictitious constructor in the base class, which is inherited by subclasses).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.