本文整理汇总了C#中CharSetSolver.ToCS方法的典型用法代码示例。如果您正苦于以下问题:C# CharSetSolver.ToCS方法的具体用法?C# CharSetSolver.ToCS怎么用?C# CharSetSolver.ToCS使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CharSetSolver
的用法示例。
在下文中一共展示了CharSetSolver.ToCS方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: gen_chsarp_TestSampleRegexes2csharp
public void gen_chsarp_TestSampleRegexes2csharp()
{
var solver = new CharSetSolver(BitWidth.BV16);
List<string> regexesAll = new List<string>(File.ReadAllLines(regexesFile));
List<int> timedout = new List<int>();
List<int> excluded = new List<int>(new int[] { 36, 50, 64, 65, 162, 166, 210, 238, 334, 355, 392, 455,
471, 490, 594, 611, 612, 671, 725, 731, 741, 760, 775, 800, 852,
870, 873, 880, 882, 893, 923, 991, 997, 1053, 1062, 1164, 1220,
1228, 1273, 1318, 1339, 1352, 1386, 1404, 1413, 1414, 1423, 1424,
1429, 1431, 1434, 1482, 1487, 1516, 1517, 1518, 1519, 1520, 1537,
1565, 1566, 1635, 1744, 1749, 1829, 1868 });
List<string> regexes = new List<string>();
for (int i = 1; i < regexesAll.Count; i++)
if (!excluded.Contains(i))
regexes.Add(regexesAll[i]);
int K = 50; //number of pos/neg strings to be generated for each regex
for (int i = 1; i < 100; i++)
{
try
{
var regex = regexes[i];
var aut = solver.Convert(regex, RegexOptions.Singleline);
var autDet = aut.Determinize(2000);
var autMin = autDet.Minimize();
var autMinC = aut.Complement();
if (autMin.IsEmpty || autMinC.IsEmpty)
continue;
CheckIsClean(autMin);
//var autMinExpr = z3.ConvertAutomatonGuardsToExpr(autMin);
//var sfa = new SFA<FuncDecl, Expr, Sort>(z3, z3.CharacterSort, autMinExpr);
//var stbb = new STBuilder<FuncDecl, Expr, Sort>(z3);
//var st = ST<FuncDecl, Expr, Sort>.SFAtoST(sfa);
//var stb = st.ToSTb();
////var csAcceptor = stb.Compile("RegexTransfomer", "SampleAcceptor", false, true);
var csAcceptor = solver.ToCS(autMin);
HashSet<string> posSamples = new HashSet<string>();
HashSet<string> negSamples = new HashSet<string>();
int k = autMin.FindShortestFinalPath(autMin.InitialState).Item1.Length;
var maxLengthAut = solver.Convert("^.{0," + (3 * k) + "}$").Determinize().Minimize();
int tries = 0;
var aut1 = autMin.Intersect(maxLengthAut);
while (posSamples.Count < K && tries < 10 * K)
{
var s = solver.GenerateMemberUniformly(aut1);
if (!s.EndsWith("\n"))
if (!posSamples.Add(s))
tries++;
}
tries = 0;
int k2 = autMinC.FindShortestFinalPath(autMin.InitialState).Item1.Length;
var maxLengthAut2 = solver.Convert("^.{0," + (3 * k2) + "}$").Determinize().Minimize();
var autMinCprefix = autMinC.Intersect(maxLengthAut2);
while (negSamples.Count < K && tries < 10 * K)
{
var s = solver.GenerateMemberUniformly(autMinCprefix);
if (!s.EndsWith("\n"))
if (!negSamples.Add(s))
tries++;
}
foreach (string s in posSamples)
{
if (!RexEngine.IsMatch(s, regex, RegexOptions.Singleline))
{
Console.WriteLine("match expected regex:" + i);
break;
}
if (!csAcceptor.IsMatch(s))
{
Console.WriteLine("match expected regex:" + i);
break;
}
}
foreach (string s in negSamples)
{
if (RexEngine.IsMatch(s, regex, RegexOptions.Singleline))
{
Console.WriteLine("mismatch expected regex:" + i);
break;
}
if (csAcceptor.IsMatch(s))
{
Console.WriteLine("mismatch expected regex:" + i);
break;
}
}
}
catch (TimeoutException)
{
Console.WriteLine("timeout regex:" + i);
timedout.Add(i);
continue;
}
}
//.........这里部分代码省略.........
示例2: TestRegex
static void TestRegex(Regex regex)
{
var solver = new CharSetSolver();
string myregex = regex.ToString();
//Regex.CompileToAssembly(new RegexCompilationInfo[] { new RegexCompilationInfo(myregex, RegexOptions.None, "EvilRegex", "RegexTransfomer", true) },
// new System.Reflection.AssemblyName("EvilRegex"));
var sfa = solver.Convert(myregex, regex.Options).RemoveEpsilons();
var sfaDet = sfa.Determinize();
var sfaMin = sfaDet.Minimize();
//solver.ShowGraph(sfa, "sfa");
//solver.ShowGraph(sfaDet, "sfaDet");
//solver.ShowGraph(sfaMin, "sfaMin");
var cs = solver.ToCS(sfaMin, true, "MyRegex", "RegexTransfomer");
var regexMin = solver.ConvertToRegex(sfaMin);
Console.WriteLine("------- given regex --------");
Console.WriteLine(myregex);
Console.WriteLine("----------------------------");
Console.WriteLine("-------- regexMin ----------");
Console.WriteLine(regexMin);
Console.WriteLine("----------------------------");
Console.WriteLine("-------- cs ----------------");
Console.WriteLine(cs.SourceCode);
Console.WriteLine("----------------------------");
string sIn = solver.GenerateMember(sfaMin);
string sOut = solver.GenerateMember(sfaMin.Complement());
string s = sIn;
int t1;
int t2;
int t3;
for (int i = 0; i < 2; i++)
{
//original regex
t1 = System.Environment.TickCount;
bool res1 = false;
for (int j = 0; j < 100000; j++)
res1 = Regex.IsMatch(s, regex.ToString(), regex.Options);
//res1 = evilregex.IsMatch(s);
t1 = System.Environment.TickCount - t1;
////minimized regex
//t2 = System.Environment.TickCount;
//bool res2 = false;
//for (int j = 0; j < 100000; j++)
// res2 = Regex.IsMatch(s, regexMin, regex.Options);
//t2 = System.Environment.TickCount - t2;
//code from minimized regex
t3 = System.Environment.TickCount;
bool res3 = false;
for (int j = 0; j < 100000; j++)
res3 = cs.IsMatch(s);
t3 = System.Environment.TickCount - t3;
Console.WriteLine(String.Format("{0}ms({1}), {2}ms({3})", t1, res1, t3, res3));
s = sOut;
}
Console.WriteLine("done...(press any key)");
Console.ReadKey();
}
示例3: gen_csharp_TestRegex2csharp
public void gen_csharp_TestRegex2csharp()
{
var solver = new CharSetSolver();
string regex = @"^(\w\d)+$";
var sfa = solver.Convert(regex, RegexOptions.Singleline).RemoveEpsilons();
var sfaDet = sfa.Determinize();
var sfaMin = sfaDet.Minimize();
//solver.ShowGraph(sfa, "sfa");
//solver.ShowGraph(sfaDet, "sfaDet");
//solver.ShowGraph(sfaMin, "sfaMin");
var cs = solver.ToCS(sfaMin, true, "Regex1", "RegexTransfomer");
var yes = cs.IsMatch("a1b2b4");
var no = cs.IsMatch("r5t6uu");
//Console.WriteLine(cs.SourceCode);
//Console.ReadLine();
Assert.IsTrue(yes);
Assert.IsFalse(no);
}