Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / Utils / Boolean / Sentence.cs / 1305376 / Sentence.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
namespace System.Data.Common.Utils.Boolean
{
///
/// Abstract base class for nodes in normal form expressions, e.g. Conjunctive Normal Form
/// sentences.
///
/// Type of expression leaf term identifiers.
internal abstract class NormalFormNode
{
private readonly BoolExpr _expr;
///
/// Initialize a new normal form node representing the given expression. Caller must
/// ensure the expression is logically equivalent to the node.
///
/// Expression logically equivalent to this node.
protected NormalFormNode(BoolExpr expr) { _expr = expr.Simplify(); }
///
/// Gets an expression that is logically equivalent to this node.
///
internal BoolExpr Expr { get { return _expr; } }
///
/// Utility method for delegation that return the expression corresponding to a given
/// normal form node.
///
/// Type of node
/// Node to examine.
/// Equivalent Boolean expression for the given node.
protected static BoolExpr ExprSelector(T_NormalFormNode node)
where T_NormalFormNode : NormalFormNode
{
return node._expr;
}
}
///
/// Abstract base class for normal form sentences (CNF and DNF)
///
/// Type of expression leaf term identifiers.
/// Type of clauses in the sentence.
internal abstract class Sentence : NormalFormNode
where T_Clause : Clause, IEquatable
{
private readonly Set _clauses;
///
/// Initialize a sentence given the appropriate sentence clauses. Produces
/// an equivalent expression by composing the clause expressions using
/// the given tree type.
///
/// Sentence clauses
/// Tree type for sentence (and generated expression)
protected Sentence(Set clauses, ExprType treeType)
: base(ConvertClausesToExpr(clauses, treeType))
{
_clauses = clauses.AsReadOnly();
}
// Produces an expression equivalent to the given clauses by composing the clause
// expressions using the given tree type.
private static BoolExpr ConvertClausesToExpr(Set clauses, ExprType treeType)
{
bool isAnd = ExprType.And == treeType;
Debug.Assert(isAnd || ExprType.Or == treeType);
IEnumerable> clauseExpressions =
clauses.Select(new Func>(ExprSelector));
if (isAnd)
{
return new AndExpr(clauseExpressions);
}
else
{
return new OrExpr(clauseExpressions);
}
}
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Sentence{");
builder.Append(_clauses);
return builder.Append("}").ToString();
}
}
///
/// Represents a sentence in disjunctive normal form, e.g.:
///
/// Clause1 + Clause2 . ...
///
/// Where each DNF clause is of the form:
///
/// Literal1 . Literal2 . ...
///
/// Each literal is of the form:
///
/// Term
///
/// or
///
/// !Term
///
/// Type of expression leaf term identifiers.
internal sealed class DnfSentence : Sentence>
{
// Initializes a new DNF sentence given its clauses.
internal DnfSentence(Set> clauses)
: base(clauses, ExprType.Or)
{
}
}
///
/// Represents a sentence in conjunctive normal form, e.g.:
///
/// Clause1 . Clause2 . ...
///
/// Where each DNF clause is of the form:
///
/// Literal1 + Literal2 + ...
///
/// Each literal is of the form:
///
/// Term
///
/// or
///
/// !Term
///
/// Type of expression leaf term identifiers.
internal sealed class CnfSentence : Sentence>
{
// Initializes a new CNF sentence given its clauses.
internal CnfSentence(Set> clauses)
: base(clauses, ExprType.And)
{
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
namespace System.Data.Common.Utils.Boolean
{
///
/// Abstract base class for nodes in normal form expressions, e.g. Conjunctive Normal Form
/// sentences.
///
/// Type of expression leaf term identifiers.
internal abstract class NormalFormNode
{
private readonly BoolExpr _expr;
///
/// Initialize a new normal form node representing the given expression. Caller must
/// ensure the expression is logically equivalent to the node.
///
/// Expression logically equivalent to this node.
protected NormalFormNode(BoolExpr expr) { _expr = expr.Simplify(); }
///
/// Gets an expression that is logically equivalent to this node.
///
internal BoolExpr Expr { get { return _expr; } }
///
/// Utility method for delegation that return the expression corresponding to a given
/// normal form node.
///
/// Type of node
/// Node to examine.
/// Equivalent Boolean expression for the given node.
protected static BoolExpr ExprSelector(T_NormalFormNode node)
where T_NormalFormNode : NormalFormNode
{
return node._expr;
}
}
///
/// Abstract base class for normal form sentences (CNF and DNF)
///
/// Type of expression leaf term identifiers.
/// Type of clauses in the sentence.
internal abstract class Sentence : NormalFormNode
where T_Clause : Clause, IEquatable
{
private readonly Set _clauses;
///
/// Initialize a sentence given the appropriate sentence clauses. Produces
/// an equivalent expression by composing the clause expressions using
/// the given tree type.
///
/// Sentence clauses
/// Tree type for sentence (and generated expression)
protected Sentence(Set clauses, ExprType treeType)
: base(ConvertClausesToExpr(clauses, treeType))
{
_clauses = clauses.AsReadOnly();
}
// Produces an expression equivalent to the given clauses by composing the clause
// expressions using the given tree type.
private static BoolExpr ConvertClausesToExpr(Set clauses, ExprType treeType)
{
bool isAnd = ExprType.And == treeType;
Debug.Assert(isAnd || ExprType.Or == treeType);
IEnumerable> clauseExpressions =
clauses.Select(new Func>(ExprSelector));
if (isAnd)
{
return new AndExpr(clauseExpressions);
}
else
{
return new OrExpr(clauseExpressions);
}
}
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Sentence{");
builder.Append(_clauses);
return builder.Append("}").ToString();
}
}
///
/// Represents a sentence in disjunctive normal form, e.g.:
///
/// Clause1 + Clause2 . ...
///
/// Where each DNF clause is of the form:
///
/// Literal1 . Literal2 . ...
///
/// Each literal is of the form:
///
/// Term
///
/// or
///
/// !Term
///
/// Type of expression leaf term identifiers.
internal sealed class DnfSentence : Sentence>
{
// Initializes a new DNF sentence given its clauses.
internal DnfSentence(Set> clauses)
: base(clauses, ExprType.Or)
{
}
}
///
/// Represents a sentence in conjunctive normal form, e.g.:
///
/// Clause1 . Clause2 . ...
///
/// Where each DNF clause is of the form:
///
/// Literal1 + Literal2 + ...
///
/// Each literal is of the form:
///
/// Term
///
/// or
///
/// !Term
///
/// Type of expression leaf term identifiers.
internal sealed class CnfSentence : Sentence>
{
// Initializes a new CNF sentence given its clauses.
internal CnfSentence(Set> clauses)
: base(clauses, ExprType.And)
{
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FormsAuthenticationUser.cs
- UIElementPropertyUndoUnit.cs
- ValidatingPropertiesEventArgs.cs
- FileLogRecordEnumerator.cs
- UnsafeNativeMethods.cs
- QilFunction.cs
- EntityDataSourceDesignerHelper.cs
- Quaternion.cs
- Table.cs
- Queue.cs
- SqlGenerator.cs
- WebPartConnection.cs
- ChannelSettingsElement.cs
- CompilationSection.cs
- OuterProxyWrapper.cs
- PartialArray.cs
- DataGridViewColumnDesignTimeVisibleAttribute.cs
- TreeNodeCollection.cs
- TableLayoutStyle.cs
- Listbox.cs
- EventHandlersStore.cs
- SiteMapNodeCollection.cs
- RuleElement.cs
- UnsafeNativeMethods.cs
- XmlUrlResolver.cs
- GuidTagList.cs
- InvokeGenerator.cs
- TextParagraph.cs
- HttpClientCertificate.cs
- UInt64Storage.cs
- SimpleWebHandlerParser.cs
- MediaCommands.cs
- Trace.cs
- SqlUdtInfo.cs
- WebPartDisplayModeEventArgs.cs
- UserNameSecurityToken.cs
- MethodToken.cs
- ListView.cs
- Trace.cs
- CodeFieldReferenceExpression.cs
- InheritedPropertyChangedEventArgs.cs
- StrongNameKeyPair.cs
- PipeStream.cs
- ControlPager.cs
- _ConnectionGroup.cs
- ThousandthOfEmRealPoints.cs
- Debug.cs
- XslCompiledTransform.cs
- TickBar.cs
- DataRecordInternal.cs
- BitmapEffectInput.cs
- PropertyEmitter.cs
- BitmapEffectCollection.cs
- RegexWorker.cs
- TypeReference.cs
- ReadOnlyDataSource.cs
- UserControlParser.cs
- ClientSettings.cs
- ToolStripLocationCancelEventArgs.cs
- VectorAnimationBase.cs
- WebPartMenu.cs
- DataObject.cs
- StaticFileHandler.cs
- ControlCollection.cs
- Operators.cs
- ApplicationBuildProvider.cs
- StateDesigner.Layouts.cs
- ItemList.cs
- DateTimeOffset.cs
- XPathDocumentNavigator.cs
- EFAssociationProvider.cs
- DataSourceCache.cs
- SourceSwitch.cs
- CacheOutputQuery.cs
- CriticalHandle.cs
- MetadataArtifactLoaderComposite.cs
- TransformGroup.cs
- ContextProperty.cs
- MethodRental.cs
- SubclassTypeValidator.cs
- HttpListenerResponse.cs
- Literal.cs
- SqlDataSourceView.cs
- ActivityScheduledRecord.cs
- ColorIndependentAnimationStorage.cs
- HtmlListAdapter.cs
- URLAttribute.cs
- EntityParameterCollection.cs
- StringConverter.cs
- SpeechRecognizer.cs
- RuleDefinitions.cs
- ConfigXmlComment.cs
- WorkflowTransactionService.cs
- SqlCommandBuilder.cs
- NameValueCollection.cs
- Blend.cs
- mongolianshape.cs
- RecordConverter.cs
- BuilderPropertyEntry.cs
- ExpressionList.cs