Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataEntity / System / Data / Common / Utils / Boolean / ConversionContext.cs / 1 / ConversionContext.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace System.Data.Common.Utils.Boolean
{
///
/// Manages state used to translate BoolExpr to decision diagram vertices and back again.
/// Specializations exist for generic and DomainConstraint expressions.
///
internal abstract class ConversionContext
{
///
/// Gets the solver instance associated with this conversion context. Used to reterieve
/// canonical Decision Diagram vertices for this context.
///
internal readonly Solver Solver = new Solver();
///
/// Given a term in BoolExpr, returns the corresponding decision diagram vertex.
///
internal abstract Vertex TranslateTermToVertex(TermExpr term);
///
/// Describes a vertex as a series of literal->vertex successors such that the literal
/// logically implies the given vertex successor.
///
internal abstract IEnumerable> GetSuccessors(Vertex vertex);
}
///
/// VertexLiteral pair, used for ConversionContext.GetSuccessors
///
internal sealed class LiteralVertexPair
{
internal readonly Vertex Vertex;
internal readonly Literal Literal;
internal LiteralVertexPair(Vertex vertex, Literal literal)
{
this.Vertex = vertex;
this.Literal = literal;
}
}
///
/// Generic implementation of a ConversionContext
///
internal sealed class GenericConversionContext : ConversionContext
{
readonly Dictionary, int> _variableMap = new Dictionary, int>();
Dictionary> _inverseVariableMap;
internal override Vertex TranslateTermToVertex(TermExpr term)
{
int variable;
if (!_variableMap.TryGetValue(term, out variable))
{
variable = Solver.CreateVariable();
_variableMap.Add(term, variable);
}
return Solver.CreateLeafVertex(variable, Solver.BooleanVariableChildren);
}
internal override IEnumerable> GetSuccessors(Vertex vertex)
{
LiteralVertexPair[] successors = new LiteralVertexPair[2];
Debug.Assert(2 == vertex.Children.Length);
Vertex then = vertex.Children[0];
Vertex @else = vertex.Children[1];
// get corresponding term expression
InitializeInverseVariableMap();
TermExpr term = _inverseVariableMap[vertex.Variable];
// add positive successor (then)
Literal literal = new Literal(term, true);
successors[0] = new LiteralVertexPair(then, literal);
// add negative successor (else)
literal = literal.MakeNegated();
successors[1] = new LiteralVertexPair(@else, literal);
return successors;
}
private void InitializeInverseVariableMap()
{
if (null == _inverseVariableMap)
{
_inverseVariableMap = _variableMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
}
}
///
/// Specialization of ConversionContext for DomainConstraint BoolExpr
///
internal sealed class DomainConstraintConversionContext : ConversionContext>
{
///
/// A map from domain variables to decision diagram variables.
///
readonly Dictionary, int> _domainVariableToRobddVariableMap =
new Dictionary, int>();
Dictionary> _inverseMap;
///
/// Translates a domain constraint term to an N-ary DD vertex.
///
internal override Vertex TranslateTermToVertex(TermExpr> term)
{
var range = term.Identifier.Range;
var domainVariable = term.Identifier.Variable;
var domain = domainVariable.Domain;
if (range.All(element => !domain.Contains(element)))
{
// trivially false
return Vertex.Zero;
}
if (domain.All(element => range.Contains(element)))
{
// trivially true
return Vertex.One;
}
// determine assignments for this constraints (if the range contains a value in the domain, '1', else '0')
Vertex[] children = domain.Select(element => range.Contains(element) ? Vertex.One : Vertex.Zero).ToArray();
// see if we know this variable
int robddVariable;
if (!_domainVariableToRobddVariableMap.TryGetValue(domainVariable, out robddVariable))
{
robddVariable = Solver.CreateVariable();
_domainVariableToRobddVariableMap[domainVariable] = robddVariable;
}
// create a new vertex with the given assignments
return Solver.CreateLeafVertex(robddVariable, children);
}
internal override IEnumerable>> GetSuccessors(Vertex vertex)
{
InitializeInverseMap();
var domainVariable = _inverseMap[vertex.Variable];
// since vertex children are ordinally aligned with domain, handle domain as array
var domain = domainVariable.Domain.ToArray();
// foreach unique successor vertex, build up range
Dictionary> vertexToRange = new Dictionary>();
for (int i = 0; i < vertex.Children.Length; i++)
{
Vertex successorVertex = vertex.Children[i];
Set range;
if (!vertexToRange.TryGetValue(successorVertex, out range))
{
range = new Set(domainVariable.Domain.Comparer);
vertexToRange.Add(successorVertex, range);
}
range.Add(domain[i]);
}
foreach (var vertexRange in vertexToRange)
{
var successorVertex = vertexRange.Key;
var range = vertexRange.Value;
// construct a DomainConstraint including the given range
var constraint = new DomainConstraint(domainVariable, range.MakeReadOnly());
var literal = new Literal>(
new TermExpr>(constraint), true);
yield return new LiteralVertexPair>(successorVertex, literal);
}
}
private void InitializeInverseMap()
{
if (null == _inverseMap)
{
_inverseMap = _domainVariableToRobddVariableMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
}
}
}
// 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.Linq;
using System.Text;
using System.Diagnostics;
namespace System.Data.Common.Utils.Boolean
{
///
/// Manages state used to translate BoolExpr to decision diagram vertices and back again.
/// Specializations exist for generic and DomainConstraint expressions.
///
internal abstract class ConversionContext
{
///
/// Gets the solver instance associated with this conversion context. Used to reterieve
/// canonical Decision Diagram vertices for this context.
///
internal readonly Solver Solver = new Solver();
///
/// Given a term in BoolExpr, returns the corresponding decision diagram vertex.
///
internal abstract Vertex TranslateTermToVertex(TermExpr term);
///
/// Describes a vertex as a series of literal->vertex successors such that the literal
/// logically implies the given vertex successor.
///
internal abstract IEnumerable> GetSuccessors(Vertex vertex);
}
///
/// VertexLiteral pair, used for ConversionContext.GetSuccessors
///
internal sealed class LiteralVertexPair
{
internal readonly Vertex Vertex;
internal readonly Literal Literal;
internal LiteralVertexPair(Vertex vertex, Literal literal)
{
this.Vertex = vertex;
this.Literal = literal;
}
}
///
/// Generic implementation of a ConversionContext
///
internal sealed class GenericConversionContext : ConversionContext
{
readonly Dictionary, int> _variableMap = new Dictionary, int>();
Dictionary> _inverseVariableMap;
internal override Vertex TranslateTermToVertex(TermExpr term)
{
int variable;
if (!_variableMap.TryGetValue(term, out variable))
{
variable = Solver.CreateVariable();
_variableMap.Add(term, variable);
}
return Solver.CreateLeafVertex(variable, Solver.BooleanVariableChildren);
}
internal override IEnumerable> GetSuccessors(Vertex vertex)
{
LiteralVertexPair[] successors = new LiteralVertexPair[2];
Debug.Assert(2 == vertex.Children.Length);
Vertex then = vertex.Children[0];
Vertex @else = vertex.Children[1];
// get corresponding term expression
InitializeInverseVariableMap();
TermExpr term = _inverseVariableMap[vertex.Variable];
// add positive successor (then)
Literal literal = new Literal(term, true);
successors[0] = new LiteralVertexPair(then, literal);
// add negative successor (else)
literal = literal.MakeNegated();
successors[1] = new LiteralVertexPair(@else, literal);
return successors;
}
private void InitializeInverseVariableMap()
{
if (null == _inverseVariableMap)
{
_inverseVariableMap = _variableMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
}
}
///
/// Specialization of ConversionContext for DomainConstraint BoolExpr
///
internal sealed class DomainConstraintConversionContext : ConversionContext>
{
///
/// A map from domain variables to decision diagram variables.
///
readonly Dictionary, int> _domainVariableToRobddVariableMap =
new Dictionary, int>();
Dictionary> _inverseMap;
///
/// Translates a domain constraint term to an N-ary DD vertex.
///
internal override Vertex TranslateTermToVertex(TermExpr> term)
{
var range = term.Identifier.Range;
var domainVariable = term.Identifier.Variable;
var domain = domainVariable.Domain;
if (range.All(element => !domain.Contains(element)))
{
// trivially false
return Vertex.Zero;
}
if (domain.All(element => range.Contains(element)))
{
// trivially true
return Vertex.One;
}
// determine assignments for this constraints (if the range contains a value in the domain, '1', else '0')
Vertex[] children = domain.Select(element => range.Contains(element) ? Vertex.One : Vertex.Zero).ToArray();
// see if we know this variable
int robddVariable;
if (!_domainVariableToRobddVariableMap.TryGetValue(domainVariable, out robddVariable))
{
robddVariable = Solver.CreateVariable();
_domainVariableToRobddVariableMap[domainVariable] = robddVariable;
}
// create a new vertex with the given assignments
return Solver.CreateLeafVertex(robddVariable, children);
}
internal override IEnumerable>> GetSuccessors(Vertex vertex)
{
InitializeInverseMap();
var domainVariable = _inverseMap[vertex.Variable];
// since vertex children are ordinally aligned with domain, handle domain as array
var domain = domainVariable.Domain.ToArray();
// foreach unique successor vertex, build up range
Dictionary> vertexToRange = new Dictionary>();
for (int i = 0; i < vertex.Children.Length; i++)
{
Vertex successorVertex = vertex.Children[i];
Set range;
if (!vertexToRange.TryGetValue(successorVertex, out range))
{
range = new Set(domainVariable.Domain.Comparer);
vertexToRange.Add(successorVertex, range);
}
range.Add(domain[i]);
}
foreach (var vertexRange in vertexToRange)
{
var successorVertex = vertexRange.Key;
var range = vertexRange.Value;
// construct a DomainConstraint including the given range
var constraint = new DomainConstraint(domainVariable, range.MakeReadOnly());
var literal = new Literal>(
new TermExpr>(constraint), true);
yield return new LiteralVertexPair>(successorVertex, literal);
}
}
private void InitializeInverseMap()
{
if (null == _inverseMap)
{
_inverseMap = _domainVariableToRobddVariableMap.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
}
}
}
// 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
- ImportedPolicyConversionContext.cs
- ManipulationLogic.cs
- Context.cs
- BuildProvider.cs
- Compress.cs
- XmlSchemaValidationException.cs
- FileUtil.cs
- TextEffectResolver.cs
- ToolStripScrollButton.cs
- SourceFileBuildProvider.cs
- AccessDataSourceWizardForm.cs
- NotFiniteNumberException.cs
- BasicExpressionVisitor.cs
- documentsequencetextcontainer.cs
- _NegoStream.cs
- ReadOnlyKeyedCollection.cs
- FillBehavior.cs
- StylusEditingBehavior.cs
- SqlDependencyUtils.cs
- WeakReference.cs
- TableItemStyle.cs
- FtpWebResponse.cs
- UnicodeEncoding.cs
- HierarchicalDataBoundControl.cs
- ExecutionEngineException.cs
- CompilerCollection.cs
- NavigationPropertyEmitter.cs
- TextServicesCompartment.cs
- WindowsIdentity.cs
- FilteredDataSetHelper.cs
- VectorCollectionConverter.cs
- DataServiceEntityAttribute.cs
- EndEvent.cs
- CodeActivityMetadata.cs
- SourceSwitch.cs
- TraceSection.cs
- PropertyTabAttribute.cs
- Latin1Encoding.cs
- CompleteWizardStep.cs
- LocalizationCodeDomSerializer.cs
- ControlCommandSet.cs
- EmbossBitmapEffect.cs
- ItemsControlAutomationPeer.cs
- StringCollection.cs
- EventlogProvider.cs
- XmlSchemaSimpleType.cs
- WebPartDescription.cs
- OletxTransactionManager.cs
- ListViewUpdateEventArgs.cs
- BamlLocalizableResourceKey.cs
- XmlWrappingReader.cs
- X509CertificateClaimSet.cs
- Connector.cs
- SendingRequestEventArgs.cs
- ServiceDesigner.cs
- CorruptingExceptionCommon.cs
- SqlException.cs
- RuntimeHandles.cs
- Int16Storage.cs
- WorkflowNamespace.cs
- ImageKeyConverter.cs
- wgx_sdk_version.cs
- RoutedEventHandlerInfo.cs
- NamespaceQuery.cs
- FlowDocumentPage.cs
- Types.cs
- GeneralTransform3DTo2DTo3D.cs
- Symbol.cs
- WinFormsComponentEditor.cs
- MarginsConverter.cs
- FilterableAttribute.cs
- ResourceDescriptionAttribute.cs
- ResourceDescriptionAttribute.cs
- CellParagraph.cs
- ObjectQueryProvider.cs
- DataGridViewLinkColumn.cs
- SqlDataSourceCommandParser.cs
- DataTableExtensions.cs
- ProviderMetadata.cs
- ProcessHost.cs
- ControlCommandSet.cs
- HierarchicalDataBoundControl.cs
- NetTcpSection.cs
- Command.cs
- HttpApplication.cs
- SoapSchemaMember.cs
- GridViewCommandEventArgs.cs
- AttributeXamlType.cs
- XmlSchemaAttributeGroupRef.cs
- DependencyPropertyAttribute.cs
- EdmSchemaError.cs
- ProfileProvider.cs
- Function.cs
- CryptoApi.cs
- BrowserCapabilitiesFactoryBase.cs
- ProcessModelInfo.cs
- GridViewRowCollection.cs
- DecoratedNameAttribute.cs
- CodeNamespaceCollection.cs
- X509Certificate2.cs