Esempio n. 1
0
 private void PrintNotDistinguishedElement(
     CstNode e, BigInteger vector, EncodingResult result, LearningExperiment oracle)
 {
     if (--_printCount >= 0)
     {
         Console.WriteLine("==========================================");
         Console.WriteLine(e.Parent.Name + ", " + e.Name + ", " + e.Code);
         Console.WriteLine(Experiment.GetGoodAncestorNode(e).Code);
         Console.WriteLine(
             result.Vector2Node[vector].Parent.Name + ", "
             + result.Vector2Node[vector].Name + ", "
             + result.Vector2Node[vector].Code);
         Console.WriteLine(Experiment.GetGoodAncestorNode(result.Vector2Node[vector]).Code);
         Console.WriteLine("------------------------------------------");
         if (Experiment.GetGoodAncestorNode(e).Code.Contains("Contract.Requires") &&
             Experiment.GetGoodAncestorNode(result.Vector2Node[vector])
             .Code.Contains("Contract.Requires"))
         {
             oracle.IsAcceptedUsingOracle(e);
             oracle.IsAcceptedUsingOracle(result.Vector2Node[vector]);
         }
         foreach (var featureString in GetFeatureStringsByVector(vector))
         {
             Console.WriteLine(Experiment.Beautify(featureString));
         }
     }
 }
Esempio n. 2
0
 private void VerifySeedAcceptedNodes(
         IEnumerable<CstNode> seedCsts, ICollection<CstNode> uppermostSeedAcceptedNodes,
         LearningExperiment oracle) {
     var anotherUppermostSeedAcceptedNodes = seedCsts
             .SelectMany(cst => LearningExperimentUtil
                     .GetUppermostNodesByNames(cst, SelectedNodeNames))
             .Where(oracle.ProtectedIsAcceptedUsingOracle)
             .ToList();
     var b1 = !uppermostSeedAcceptedNodes.All(oracle.IsAcceptedUsingOracle);
     var b2 = anotherUppermostSeedAcceptedNodes
             .Select(node => node.AncestorWithSingleChild())
             .Any(e => !uppermostSeedAcceptedNodes.Contains(e));
     var b3 = uppermostSeedAcceptedNodes.Count != anotherUppermostSeedAcceptedNodes.Count;
     Console.WriteLine("Initial: " + string.Join(", ", oracle.OracleNames));
     Console.WriteLine("Learned: " + string.Join(", ", SelectedNodeNames));
     if (b1 || b2 || b3) {
         Console.WriteLine("--------------------------------------------------");
         foreach (var e in uppermostSeedAcceptedNodes) {
             Console.WriteLine(e);
         }
         Console.WriteLine("--------------------------------------------------");
         foreach (var e in anotherUppermostSeedAcceptedNodes) {
             Console.WriteLine(e);
         }
         throw new Exception("Wrong Oracle.");
     }
 }
Esempio n. 3
0
 private void EncodeTargetNodes(
     IEnumerable <CstNode> allUppermostNodes, EncodingResult result,
     LearningExperiment oracle)
 {
     foreach (var uppermostNode in allUppermostNodes)
     {
         var vector = uppermostNode.GetFeatureVector(_featureString2Bit, _extractor);
         if (oracle.IsAcceptedUsingOracle(uppermostNode))
         {
             // TODO: for debug
             if (result.IdealRejectedVector2GroupPath.ContainsKey(vector))
             {
                 PrintNotDistinguishedElement(uppermostNode, vector, result, oracle);
             }
             UpdateVector2GroupPath(result.IdealAcceptedVector2GroupPath, vector,
                                    uppermostNode);
         }
         else
         {
             // TODO: for debug
             if (result.IdealAcceptedVector2GroupPath.ContainsKey(vector))
             {
                 PrintNotDistinguishedElement(uppermostNode, vector, result, oracle);
             }
             UpdateVector2GroupPath(result.IdealRejectedVector2GroupPath, vector,
                                    uppermostNode);
         }
         UpdateVectorDict(result, vector, uppermostNode);
     }
 }
Esempio n. 4
0
 private void EncodeSeedNodes(
     IEnumerable <CstNode> seedNodes, EncodingResult result,
     IDictionary <BigInteger, string> idealVector2Path,
     IDictionary <BigInteger, string> seedVector2Path, LearningExperiment oracle)
 {
     foreach (var node in seedNodes)
     {
         var vector = node.GetFeatureVector(_featureString2Bit, _extractor);
         if (oracle.IsAcceptedUsingOracle(node))
         {
             // TODO: for debug
             if (result.IdealRejectedVector2GroupPath.ContainsKey(vector))
             {
                 PrintNotDistinguishedElement(node, vector, result, oracle);
             }
         }
         else
         {
             // TODO: for debug
             if (result.IdealAcceptedVector2GroupPath.ContainsKey(vector))
             {
                 PrintNotDistinguishedElement(node, vector, result, oracle);
             }
         }
         UpdateVector2GroupPath(idealVector2Path, vector, node);
         seedVector2Path[vector] = idealVector2Path[vector];
         UpdateVectorDict(result, vector, node);
     }
 }
Esempio n. 5
0
        public EncodingResult Encode(
            ICollection <string> codePaths, IEnumerable <CstNode> allCsts,
            LearningExperiment oracle, SeedNodeSet seedNodeSet = null)
        {
            var fileName = codePaths.Count > 0
                    ? string.Join(",", codePaths).GetHashCode() + "_" +
                           (codePaths.First() + "," + codePaths.Last() + ",").GetHashCode() + "_"
                           + codePaths.Count + ".encoded"
                    : null;
            var formatter = new BinaryFormatter();

            if (fileName != null && File.Exists(fileName))
            {
                using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
                    try {
                        var ret = ((EncodingResult)formatter.Deserialize(fs)).MakeImmutable();
                        Console.WriteLine("############### Warning ###############");
                        Console.WriteLine("Cache file of encoded result is used.");
                        Console.WriteLine("#######################################");
                        return(ret);
                    } catch (Exception e) {
                        Console.Error.WriteLine(e);
                    }
                }
            }

            var allUppermostNodes = allCsts.SelectMany(
                cst => LearningExperimentUtil.GetUppermostNodesByNames(cst, _selectedNodeNames));

            var result = new EncodingResult();

            if (seedNodeSet != null)
            {
                result.SeedAcceptedNodeCount = seedNodeSet.AcceptedNodes.Count;
                result.SeedNodeCount         = result.SeedAcceptedNodeCount
                                               + seedNodeSet.RejectedNodes.Count;
                EncodeSeedNodes(
                    seedNodeSet.AcceptedNodes, result, result.IdealAcceptedVector2GroupPath,
                    result.SeedAcceptedVector2GroupPath, oracle);
                EncodeSeedNodes(
                    seedNodeSet.RejectedNodes, result, result.IdealRejectedVector2GroupPath,
                    result.SeedRejectedVector2GroupPath, oracle);
            }
            EncodeTargetNodes(allUppermostNodes, result, oracle);

            if (fileName != null)
            {
                using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
                    formatter.Serialize(fs, result);
                }
            }
            return(result.MakeImmutable());
        }
Esempio n. 6
0
        public EncodingResult Encode(
                ICollection<string> codePaths, IEnumerable<CstNode> allCsts,
                LearningExperiment oracle, SeedNodeSet seedNodeSet = null) {
            var fileName = codePaths.Count > 0
                    ? string.Join(",", codePaths).GetHashCode() + "_" +
                      (codePaths.First() + "," + codePaths.Last() + ",").GetHashCode() + "_"
                      + codePaths.Count + ".encoded"
                    : null;
            var formatter = new BinaryFormatter();
            if (fileName != null && File.Exists(fileName)) {
                using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
                    try {
                        var ret = ((EncodingResult)formatter.Deserialize(fs)).MakeImmutable();
                        Console.WriteLine("############### Warning ###############");
                        Console.WriteLine("Cache file of encoded result is used.");
                        Console.WriteLine("#######################################");
                        return ret;
                    } catch (Exception e) {
                        Console.Error.WriteLine(e);
                    }
                }
            }

            var allUppermostNodes = allCsts.SelectMany(
                    cst => LearningExperimentUtil.GetUppermostNodesByNames(cst, _selectedNodeNames));

            var result = new EncodingResult();
            if (seedNodeSet != null) {
                result.SeedAcceptedNodeCount = seedNodeSet.AcceptedNodes.Count;
                result.SeedNodeCount = result.SeedAcceptedNodeCount
                                       + seedNodeSet.RejectedNodes.Count;
                EncodeSeedNodes(
                        seedNodeSet.AcceptedNodes, result, result.IdealAcceptedVector2GroupPath,
                        result.SeedAcceptedVector2GroupPath, oracle);
                EncodeSeedNodes(
                        seedNodeSet.RejectedNodes, result, result.IdealRejectedVector2GroupPath,
                        result.SeedRejectedVector2GroupPath, oracle);
            }
            EncodeTargetNodes(allUppermostNodes, result, oracle);

            if (fileName != null) {
                using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
                    formatter.Serialize(fs, result);
                }
            }
            return result.MakeImmutable();
        }
Esempio n. 7
0
        public SeedNodeSet(
                IEnumerable<CstNode> seedNodes, IList<CstNode> seedCsts, LearningExperiment oracle) {
            var uppermostSeedAcceptedNodes = seedNodes
                    .Select(node => node.AncestorWithSingleChild())
                    .ToImmutableHashSet();
            SelectedNodeNames = SelectNodeNames(uppermostSeedAcceptedNodes)
                    .ToImmutableHashSet();

            AcceptedNodes = CreateAcceptedNodes(uppermostSeedAcceptedNodes)
                    .ToImmutableHashSet();
            if (!AcceptedNodes.Any()) {
                throw new Exception("There are no accepted seed nodes!");
            }
            VerifySeedAcceptedNodes(seedCsts, uppermostSeedAcceptedNodes, oracle);

            RejectedNodes = CreateRejectedNodes(seedCsts, AcceptedNodes)
                    .ToImmutableList();
        }
Esempio n. 8
0
 public static void Create(
         CstNode root, List<SelectedFragment> acceptingFragments,
         LearningExperiment learningExperiment) {
     foreach (var fragment in acceptingFragments) {
         var rootNode = fragment.SurroundingRange.FindInnermostNode(root);
         var node = fragment.Node;
         var paths = new HashSet<string>();
         var path = node.Name;
         paths.Add(path);
         while ((node = node.Parent) != rootNode) {
             path = path + "<" + node.Name + node.RuleId;
             paths.Add(path);
         }
     }
 }
Esempio n. 9
0
 private void PrintNotDistinguishedElement(
         CstNode e, BigInteger vector, EncodingResult result, LearningExperiment oracle) {
     if (--_printCount >= 0) {
         Console.WriteLine("==========================================");
         Console.WriteLine(e.Parent.Name + ", " + e.Name + ", " + e.Code);
         Console.WriteLine(Experiment.GetGoodAncestorNode(e).Code);
         Console.WriteLine(
                 result.Vector2Node[vector].Parent.Name + ", "
                 + result.Vector2Node[vector].Name + ", "
                 + result.Vector2Node[vector].Code);
         Console.WriteLine(Experiment.GetGoodAncestorNode(result.Vector2Node[vector]).Code);
         Console.WriteLine("------------------------------------------");
         if (Experiment.GetGoodAncestorNode(e).Code.Contains("Contract.Requires") &&
             Experiment.GetGoodAncestorNode(result.Vector2Node[vector])
                     .Code.Contains("Contract.Requires")) {
             oracle.IsAcceptedUsingOracle(e);
             oracle.IsAcceptedUsingOracle(result.Vector2Node[vector]);
         }
         foreach (var featureString in GetFeatureStringsByVector(vector)) {
             Console.WriteLine(Experiment.Beautify(featureString));
         }
     }
 }
Esempio n. 10
0
 private void EncodeTargetNodes(
         IEnumerable<CstNode> allUppermostNodes, EncodingResult result,
         LearningExperiment oracle) {
     foreach (var uppermostNode in allUppermostNodes) {
         var vector = uppermostNode.GetFeatureVector(_featureString2Bit, _extractor);
         if (oracle.IsAcceptedUsingOracle(uppermostNode)) {
             // TODO: for debug
             if (result.IdealRejectedVector2GroupPath.ContainsKey(vector)) {
                 PrintNotDistinguishedElement(uppermostNode, vector, result, oracle);
             }
             UpdateVector2GroupPath(result.IdealAcceptedVector2GroupPath, vector,
                     uppermostNode);
         } else {
             // TODO: for debug
             if (result.IdealAcceptedVector2GroupPath.ContainsKey(vector)) {
                 PrintNotDistinguishedElement(uppermostNode, vector, result, oracle);
             }
             UpdateVector2GroupPath(result.IdealRejectedVector2GroupPath, vector,
                     uppermostNode);
         }
         UpdateVectorDict(result, vector, uppermostNode);
     }
 }
Esempio n. 11
0
 private void EncodeSeedNodes(
         IEnumerable<CstNode> seedNodes, EncodingResult result,
         IDictionary<BigInteger, string> idealVector2Path,
         IDictionary<BigInteger, string> seedVector2Path, LearningExperiment oracle) {
     foreach (var node in seedNodes) {
         var vector = node.GetFeatureVector(_featureString2Bit, _extractor);
         if (oracle.IsAcceptedUsingOracle(node)) {
             // TODO: for debug
             if (result.IdealRejectedVector2GroupPath.ContainsKey(vector)) {
                 PrintNotDistinguishedElement(node, vector, result, oracle);
             }
         } else {
             // TODO: for debug
             if (result.IdealAcceptedVector2GroupPath.ContainsKey(vector)) {
                 PrintNotDistinguishedElement(node, vector, result, oracle);
             }
         }
         UpdateVector2GroupPath(idealVector2Path, vector, node);
         seedVector2Path[vector] = idealVector2Path[vector];
         UpdateVectorDict(result, vector, node);
     }
 }
Esempio n. 12
0
 private void InitializeModeMenu() {
     var menuItems = new List<ToolStripMenuItem>();
     var exps = new LearningExperiment[] {
         new JavaComplexStatementExperiment(),
         new JavaSuperComplexBranchExperiment(),
     };
     foreach (var exp in exps) {
         var menuItem = menuItemModes.DropDownItems.Add(
                 exp.GetType().Name.Contains("Statement")
                         ? "Statement" : "Branch") as ToolStripMenuItem;
         menuItems.Add(menuItem);
         menuItem.Click += (o, args) => {
             _activeExperiment = exp;
             foreach (var item in menuItems) {
                 item.Checked = false;
             }
             menuItem.Checked = true;
         };
     }
     menuItems[1].PerformClick();
 }