Esempio n. 1
0
 public QualitativeEdge(ConstraintNetwork network, Node startNode, Node endNode) : base(network, startNode, endNode)
 {
     if (startNode == null || endNode == null)
     {
         throw new ArgumentNullException("The start and end nodes must be specified!");
     }
 }
Esempio n. 2
0
        public Tcsp(ConstraintNetwork constraintNetwork)
        {
            Nodes = constraintNetwork.Nodes;
            Edges = new Dictionary <Tuple <Node, Node>, MetricEdge>();

            foreach (var edgeKey in constraintNetwork.Edges.Keys)
            {
                MetricEdge edge = (MetricEdge)constraintNetwork.Edges[edgeKey];

                edge.GenerateAllowedIntervals();
                Edges.Add(edgeKey, edge);
            }
        }
Esempio n. 3
0
        public Edge(ConstraintNetwork network, Node startNode, Node endNode)
        {
            if (network == null)
            {
                throw new ArgumentNullException("The constraint network of the edge cannot be null!");
            }

            this.Constraints        = new List <ConfigurationConstraint>();
            this.ImpliedConstraints = new List <ConfigurationConstraint>();
            this.Network            = network;
            this.StartNode          = startNode;
            this.EndNode            = endNode;
        }
        internal static ConstraintNetwork GenerateQualitativeConstraintNetwork(GKOStructuringContext strContext, RelationFamily calculus, Log log)
        {
            ConstraintNetwork resultNetwork = new ConstraintNetwork(calculus, strContext.Id + "_" + calculus.Name, log);
            List <ConfigurationConstraintTree> constraintTrees = strContext.StructuralConstraints.Where(x => x.RelationFamily == calculus).ToList();

            // Creating the constraints restricting that self edges should be labeled with the Equals relation from the calculus
            DomainConstraint            equalsDomainConstraint = DomainConstraint.SelfEqualsConstraint(calculus);
            ConfigurationConstraintTree equalsConstraint       = new ConfigurationConstraintTree(null, equalsDomainConstraint, strContext.Components.Where(x => x.Active).ToList(), log);

            // Adding the constraints restricting that self edges should be labeled with the Equals relation from the calculus
            constraintTrees.Add(equalsConstraint);

            resultNetwork.Context = strContext;
            // Adding all components as nodes in the constraint network
            strContext.Components.Where(x => x.Active).ToList().ForEach(x => resultNetwork.Nodes.Add(new Node(x)));

            // Creating all edges in the constraint network, so it is a complete graph
            foreach (var startNode in resultNetwork.Nodes)
            {
                foreach (var endNode in resultNetwork.Nodes)
                {
                    QualitativeEdge edge = new QualitativeEdge(resultNetwork, startNode, endNode);
                    resultNetwork.Edges.Add(new Tuple <Node, Node>(startNode, endNode), edge);
                }
            }

            // Each constraint tree has a number of configuration constraints
            foreach (var constraintTree in constraintTrees)
            {
                List <ConfigurationConstraint> constraints = constraintTree.GetAllConfigurationConstraints();

                // ToDo: Add check for equal constraints to avoid redundancy

                // Adding all constraints as edges in the networks
                constraints.ForEach(x =>
                {
                    // Hack: this uses the fact that each constraint has at least one allowed relation and all have the same signature and logic (i.e. start and end node)
                    Node startNode             = x.AllowedRelations[0].GetStartNode(x);
                    Node endNode               = x.AllowedRelations[0].GetEndNode(x);
                    Tuple <Node, Node> edgeKey = new Tuple <Node, Node>(startNode, endNode);
                    QualitativeEdge edge       = resultNetwork.Edges[edgeKey] as QualitativeEdge;

                    // Adding the constraint to the edge
                    edge.Constraints.Add(x);
                });
            }

            return(resultNetwork);
        }
Esempio n. 5
0
 internal SolutionDataQualitative(ConstraintNetwork network)
     : base(network)
 {
 }
 public MetricEdge(ConstraintNetwork network, Node startNode, Node endNode)
     : base(network, startNode, endNode)
 {
     this.ConstraintIntervals = new Dictionary <ConfigurationConstraint, List <Interval> >();
 }
        /// <summary>
        /// Generates the metric constraint network for the structured components
        /// </summary>
        /// <param name="structuringContexts">The list of structured components to generate the networks for.
        /// They must include the metric relation family in the list of included ones</param>
        /// <returns></returns>
        internal static List <ConstraintNetwork> GenerateMetricConstraintNetworks(List <GKOStructuringContext> structuringContexts, TmsManager tms, StructuralReasonerOptions options, Log log)
        {
            List <ConstraintNetwork>           constraintNetworks = new List <ConstraintNetwork>();
            List <List <Node> >                clusters           = new List <List <Node> >();
            List <ConfigurationConstraintTree> constraintTrees    = new List <ConfigurationConstraintTree>();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            // Adding all constraint trees to the list
            foreach (var context in structuringContexts)
            {
                context.StructuralConstraints.ForEach(x =>
                {
                    // Additionally assign the special metric values based on the context of the constraints
                    x.AssignSpecialMetricValues(context);
                    constraintTrees.Add(x);
                });
            }
            stopwatch.Stop();
            log.AddItem(LogType.Info, String.Format("Assigning special metric values finished ({0} ms).", stopwatch.ElapsedMilliseconds));

            stopwatch.Restart();
            // Combine the nodes in the constraint trees in linked clusters,
            // this serve as base for generating the constraint networks
            foreach (var constraintTree in constraintTrees)
            {
                clusters.AddRange(constraintTree.GetNodeClusters());
                ConstraintNetwork.NormalizeClusters(clusters);
            }
            stopwatch.Stop();
            log.AddItem(LogType.Info, String.Format("Finding node clusters finished ({0} ms).", stopwatch.ElapsedMilliseconds));

            #region TCSP specific
            // These constraints limit the domain of the decision variables for TCSP
            if (options.MetricReasoningAlgorithm == MetricReasoningAlgorithm.Tcsp)
            {
                stopwatch.Restart();
                List <Node> nodes = clusters.SelectMany(x => x).Where(x => x != null).Distinct().ToList();

                // Add min and max constraint for the constrained attributes
                foreach (var constrAttribute in nodes)
                {
                    List <GKOComponent> compList = new List <GKOComponent>()
                    {
                        constrAttribute.Component
                    };
                    DomainConstraint            minConstraint = DomainConstraint.MinValueConstraint(constrAttribute.Attribute, tms.MetricDomain);
                    DomainConstraint            maxConstraint = DomainConstraint.MaxValueConstraint(constrAttribute.Attribute, tms.MetricDomain);
                    ConfigurationConstraintTree minTree       = new ConfigurationConstraintTree(null, minConstraint, compList, log);
                    ConfigurationConstraintTree maxTree       = new ConfigurationConstraintTree(null, maxConstraint, compList, log);

                    constraintTrees.Add(minTree);
                    constraintTrees.Add(maxTree);
                }

                stopwatch.Stop();
                log.AddItem(LogType.Info, String.Format("Creating Min/Max constraints for TCSP finished ({0} ms).", stopwatch.ElapsedMilliseconds));
            }
            #endregion

            stopwatch.Restart();

            // Generating a new constraint network for each found node cluster
            foreach (var nodeCluster in clusters)
            {
                ConstraintNetwork network = new ConstraintNetwork(StructuralRelationsManager.MetricRelationsFamily, log);
                network.Nodes = nodeCluster;

                constraintNetworks.Add(network);
            }

            // Adding all configuration constraints in the constraint networks
            foreach (var constraintTree in constraintTrees)
            {
                ConfigurationConstraint[] treeConstraints = constraintTree.GetAllConfigurationConstraints().ToArray();

                for (int i = 0; i < treeConstraints.Length; i++)
                {
                    ConstraintNetwork.ApplyMetricConstraint(treeConstraints[i], constraintNetworks, log);
                }
            }

            // UId has to be assigned to the networks
            for (int i = 0; i < constraintNetworks.Count; i++)
            {
                constraintNetworks[i].UId = "MetricConstraintNetwork_" + (i + 1).ToString();
            }

            stopwatch.Stop();
            log.AddItem(LogType.Info, String.Format("Generating the constraint networks finished ({0} ms).", stopwatch.ElapsedMilliseconds));

            return(constraintNetworks);
        }
        /// <summary>
        /// Adds a configuration constraint
        /// </summary>
        /// <param name="constraint">The constraint to add</param>
        /// <param name="constrNetworks">The list of possible constraint networks where the constraint can e added</param>
        /// <param name="log"></param>
        private static void ApplyMetricConstraint(ConfigurationConstraint constraint, List <ConstraintNetwork> constrNetworks, Log log)
        {
            IMetricRelation relation = constraint.AllowedRelations[0] as IMetricRelation;

            if (relation == null)
            {
                log.AddItem(LogType.Warning, String.Format("A constraint ({0}) is skipped during the generation of a metric constraint network, because it is {1} relation and not recognized as metric.", constraint.DomainConstraint.Name, constraint.AllowedRelations[1].RelationFamily.Name));
            }
            else
            {
                ConstraintNetwork constraintNetwork = constrNetworks.Single(x => constraint.GetIncludedNodes().All(y => x.Nodes.Contains(y)));
                Node startNode                    = (relation as BinaryRelation).GetStartNode(constraint);
                Node endNode                      = (relation as BinaryRelation).GetEndNode(constraint);
                Tuple <Node, Node> edgeKey        = new Tuple <Node, Node>(startNode, endNode);
                Tuple <Node, Node> reverseEdgeKey = new Tuple <Node, Node>(endNode, startNode);
                List <Interval>    intervals      = relation.GetDifferenceIntervals(constraint);

                // Check if the edge already exists
                if (constraintNetwork.Edges.ContainsKey(edgeKey))
                {
                    // Adding the current constraint to the edge
                    MetricEdge edge = constraintNetwork.Edges[edgeKey] as MetricEdge;
                    ConfigurationConstraint equalConstraint = edge.Constraints.SingleOrDefault(x => x.Equals(constraint));

                    // Add the constraint if an equal one doesn't already exist
                    if (equalConstraint == null)
                    {
                        edge.AddConstraintIntervals(constraint, intervals, log);
                    }
                    else
                    {
                        // Adding the constraints as implied by a previous one
                        edge.ImpliedConstraints.Add(constraint);
                        constraint.EqualActiveConstraint = equalConstraint;
                    }
                }
                // Check if the reverse of the edge exists
                else if (constraintNetwork.Edges.ContainsKey(reverseEdgeKey))
                {
                    // Adding the inverse constraint to the mirror edge
                    MetricEdge              edge = constraintNetwork.Edges[reverseEdgeKey] as MetricEdge;
                    IMetricRelation         asMetricConstraint        = (IMetricRelation)constraint.AllowedRelations[0];
                    ConfigurationConstraint inverseConstraint         = asMetricConstraint.GetInverseConstraint(constraint);
                    IMetricRelation         inverseAsMetricConstraint = (IMetricRelation)inverseConstraint.AllowedRelations[0];
                    ConfigurationConstraint equalConstraint           = edge.Constraints.SingleOrDefault(x => x.Equals(inverseConstraint));

                    // Since the inverse constraint is used, it should be included instead of the current one in the constraint tree
                    constraint.OwningTree.SwitchConstraint(constraint, inverseConstraint);

                    // Add the constraint if an equal one doesn't already exist
                    if (equalConstraint == null)
                    {
                        edge.AddConstraintIntervals(inverseConstraint, inverseAsMetricConstraint.GetDifferenceIntervals(inverseConstraint), log);
                    }
                    else
                    {
                        // Adding the constraints as implied by a previous one
                        edge.ImpliedConstraints.Add(inverseConstraint);
                        inverseConstraint.EqualActiveConstraint = equalConstraint;
                    }
                }
                // Adding the new edge with the found intervals
                else
                {
                    MetricEdge edge = new MetricEdge(constraintNetwork, edgeKey.Item1, edgeKey.Item2);

                    edge.AddConstraintIntervals(constraint, intervals, log);
                    constraintNetwork.Edges.Add(edgeKey, edge);
                }
            }
        }