Esempio n. 1
0
        static private List <IntMapping> genMaps(int min_i, int min_j)
        {
            IntPair           parameters = new IntPair(min_i, min_j);
            List <IntMapping> resultMaps = new List <IntMapping>();

            if (computedIntMaps.ContainsKey(parameters))
            {
                return(computedIntMaps[parameters]);
            }

            // base case: end with final link pair
            if (min_i == a && min_j == b)
            {
                resultMaps.Add(new IntMapping());
                return(resultMaps);
            }

            if (min_i < a && a > b)
            {
                List <IntMapping> subMaps = genMaps(min_i + 1, min_j);
                foreach (IntMapping submap in subMaps)
                {
                    IntPair    x      = new IntPair(min_i, min_j - 1);
                    IntMapping newMap = (IntMapping)submap.Clone();
                    newMap.mapPairs.Insert(0, x);
                    resultMaps.Add(newMap);
                }
            }

            if (min_j < b && a < b)
            {
                List <IntMapping> subMaps = genMaps(min_i, min_j + 1);
                foreach (IntMapping submap in subMaps)
                {
                    IntPair    x      = new IntPair(min_i - 1, min_j);
                    IntMapping newMap = (IntMapping)submap.Clone();
                    newMap.mapPairs.Insert(0, x);
                    resultMaps.Add(newMap);
                }
            }

            if (min_i < a && min_j < b)
            {
                List <IntMapping> subMaps = genMaps(min_i + 1, min_j + 1);
                foreach (IntMapping submap in subMaps)
                {
                    IntMapping newMap = (IntMapping)submap.Clone();
                    IntPair    x      = new IntPair(min_i, min_j);
                    newMap.mapPairs.Insert(0, x);
                    resultMaps.Add(newMap);
                }
            }

            computedIntMaps[parameters] = resultMaps;
            return(resultMaps);
        }
Esempio n. 2
0
        public object Clone()
        {
            IntMapping map = new IntMapping();

            foreach (IntPair p in this.mapPairs)
            {
                map.mapPairs.Add(p);
            }
            return(map);
        }
Esempio n. 3
0
        /// <summary>
        /// Tries to add the given relationship to the analogy, only one relationships of a particular type is allowed.
        /// Requires relationships to conform to a valid left-to-right mapping!
        /// If the relationships conflicts with any previous relationship, the previous is removed and replaced with the new one.
        /// </summary>
        /// <param name="reason"></param>
        public bool TryToAddRelationship(Relationship relationship)
        {
            // Check for special case of LHS->RHS relationship, instead of subcomponent relationship.
            if (relationship.LHS == LHS && relationship.RHS == RHS)
            {
                // This is fine; add it. (Note: replaces any previous relationship of same type).
                AddRelationship(relationship);
                return(true);
            }

            List <IntMapping> maps = IntMapping.getAllMaps(LHS.Count, RHS.Count);
            // Make sure this relationship is valid in terms of the possible mappings that can occur left-to-right.
            // It has to show up in one of the consistent maps.
            List <IntMapping> consistentMaps = GetConsistentMaps(maps);
            bool found    = false;
            int  leftIdx  = relationship.LHS.IndexInParent;
            int  rightIdx = relationship.RHS.IndexInParent;

            foreach (IntMapping map in consistentMaps)
            {
                foreach (IntPair pair in map.mapPairs)
                {
                    if (pair.x == leftIdx && pair.y == rightIdx)
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }
            // If not found, don't add.
            if (!found)
            {
                return(false);
            }

            // No problems. Add it.
            AddRelationship(relationship);
            return(true);
        }