Skip to content

Commit

Permalink
Compilable. Part of tests and benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsvgit committed Dec 12, 2023
1 parent 233d812 commit 7ad997e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 65 deletions.
11 changes: 6 additions & 5 deletions Benchmarks/BenchmarkData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System.IO
open System.Text.RegularExpressions
open CFPQ_GLL
open CFPQ_GLL.Common
open CFPQ_GLL.LinearInputGraph

let private seed = 100
let private rnd = System.Random(seed)
Expand Down Expand Up @@ -110,9 +111,9 @@ let generateBenchmarkData (sizes: int array) (errorCnts: int array) =
type BenchmarkData = {
Name: string
Text: string
RSM: unit -> RSM.RSM
StartVertex: LinearInputGraphVertexBase
FinishVertex: LinearInputGraphVertexBase
RSM: unit -> RSM.RSM<Char>
StartVertex: LinearInputGraphVertexBase<Char>
FinishVertex: LinearInputGraphVertexBase<Char>
Size: int
Weight: int
Errors: string list
Expand All @@ -135,14 +136,14 @@ let private getDataFromText (name: string, text: string) =
let startVertex,mapping = graph.ToCfpqCoreGraph startV
let finalVertex = mapping[finalV]
let rsm = Tests.GolangRSM.golangRSM ()
let result, descriptorsCount = GLL.errorRecoveringEval finalVertex startVertex rsm GLL.AllPaths
let result, descriptorsCount = GLL.errorRecoveringEval finalVertex startVertex rsm GLL.AllPaths Char.Epsilon

let weight =
match result with
| GLL.QueryResult.MatchedRanges _ ->

let sppf = rsm.OriginalStartState.NonTerminalNodes.ToArray()
let root = sppf |> Array.filter (fun n -> startVertex = n.LeftPosition && finalVertex = n.RightPosition) |> Array.minBy(fun n -> n.Weight)
let root = sppf |> Array.filter (fun n -> (startVertex :> IInputGraphVertex<_>) = n.LeftPosition && (finalVertex :> IInputGraphVertex<_>) = n.RightPosition) |> Array.minBy(fun n -> n.Weight)
root.Weight |> int
| _ -> failwith "Result should be MatchedRanges"

Expand Down
2 changes: 1 addition & 1 deletion CFPQ_GLL/GLL.fs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ let evalFromState
let errorRecoveringEval<'inputVertex, 'token when 'inputVertex: equality and 'token: equality> finishVertex startVertex (query:RSM<'token>) mode (epsilon: 'token) =
let gss = GSS()
let matchedRanges = MatchedRanges(epsilon)
evalFromState (ErrorRecoveringDescriptorsStack()) gss matchedRanges startVertex finishVertex (query:RSM<'token>) mode
evalFromState (ErrorRecoveringDescriptorsStack()) gss matchedRanges startVertex finishVertex (query:RSM<'token>) epsilon mode

let onInputGraphChanged (changedVertices:seq<IInputGraphVertex<'token>>) =

Expand Down
4 changes: 2 additions & 2 deletions CFPQ_GLL/RsmBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ let many x = Many x
let some x = Sequence (x, many x)
let ( ** ) x y = Sequence (x,y)
let opt x = Alternative(x, Epsilon)
let literal (x:string) = NoLayout (x.ToCharArray() |> Array.map (Terminal >> Symbol) |> Array.reduce (fun x y -> Sequence (x,y)))
//let literal (x:string) = NoLayout (x.ToCharArray() |> Array.map (Terminal >> Symbol) |> Array.reduce (fun x y -> Sequence (x,y)))
let protect (x: RegexpWithLayoutConfig<'token>) = NoLayout x
let (=>) lhs rhs =
match lhs with
Expand Down Expand Up @@ -157,7 +157,7 @@ let addLayout regexp layoutSymbols =
| Epsilon -> Regexp.Epsilon
addLayout regexp

let build layoutSymbols rules eof =
let build layoutSymbols eof rules =

let nonTerminalToStartState = Dictionary<_,_>()
let addEdges = ResizeArray()
Expand Down
57 changes: 29 additions & 28 deletions Tests/(A or B)Start_DynamicTests.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Tests.DynamicTests.A_or_B_Star

(*
open System.Collections.Generic
open CFPQ_GLL
open CFPQ_GLL.Common
Expand All @@ -13,12 +13,12 @@ open Tests.InputGraph
open CFPQ_GLL.RsmBuilder
open Tests
let checkResult (testName:string) startVertices (q:RSM) (expectedNodes, expectedEdges, expectedDistances) result =
let checkResult (testName:string) startVertices (q:RSM<_>) (expectedNodes, expectedEdges, expectedDistances) result =
let validDotFileName = testName.Replace(',', ' ').Replace(' ', '_') + ".dot"
match result with
| QueryResult.MatchedRanges ranges ->
let sppf = q.OriginalStartState.NonTerminalNodes.ToArray()
let distances = sppf |> Array.map (fun n -> n.Distance) |> Array.sort
let distances = sppf |> Array.map (fun n -> n.Weight) |> Array.sort
printfn $"D for %s{validDotFileName}: %A{distances}"
let actual = TriplesStoredSPPF(sppf, Dictionary())
GLLTests.dumpResultToConsole actual
Expand All @@ -29,23 +29,23 @@ let checkResult (testName:string) startVertices (q:RSM) (expectedNodes, expected
| _ -> failwith "Result should be MatchedRanges"
let runGLLAndCheckResultForManuallyCreatedGraph (reachableVertices, gss, matchedRanges) (testName:string) startVertices (q:RSM) (expectedNodes, expectedEdges, expectedDistances) =
let runGLLAndCheckResultForManuallyCreatedGraph (reachableVertices, gss, matchedRanges) (testName:string) startVertices (q:RSM<_>) (expectedNodes, expectedEdges, expectedDistances) =
let result = evalFromState reachableVertices gss matchedRanges startVertices q AllPaths
checkResult (testName:string) startVertices (q:RSM) (expectedNodes, expectedEdges, expectedDistances) result
checkResult (testName:string) startVertices (q:RSM<_>) (expectedNodes, expectedEdges, expectedDistances) result
let terminalA = 0<terminalSymbol>
let terminalB = 1<terminalSymbol>
let initialGraph () =
let graphEntryPoint = InputGraphVertexBase() :> IInputGraphVertex
let graphExit = InputGraphVertexBase() :> IInputGraphVertex
let graphEntryPoint = InputGraphVertexBase() :> IInputGraphVertex<_>
let graphExit = InputGraphVertexBase() :> IInputGraphVertex<_>
graphEntryPoint.OutgoingEdges.Add(terminalA, HashSet [|graphExit|])
graphEntryPoint, graphExit
let initialTwoEdgesGraph () =
let v0 = InputGraphVertexBase() :> IInputGraphVertex
let v1 = InputGraphVertexBase() :> IInputGraphVertex
let v2 = InputGraphVertexBase() :> IInputGraphVertex
let v0 = InputGraphVertexBase() :> IInputGraphVertex<_>
let v1 = InputGraphVertexBase() :> IInputGraphVertex<_>
let v2 = InputGraphVertexBase() :> IInputGraphVertex<_>
v0.OutgoingEdges.Add(terminalA, HashSet [|v1|])
v1.OutgoingEdges.Add(terminalA, HashSet [|v2|])
v0,v1,v2
Expand All @@ -70,7 +70,7 @@ let expectedForInitialOneEdgeGraph =
nodes.Add(5, TriplesStoredSPPFNode.TerminalNode (0<inputGraphVertex>,0<terminalSymbol>,1<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5)|])
let distances = [|0<distance>; 1<distance>|]
let distances = [|0<weight>; 1<weight>|]
(nodes,edges,distances)
let expectedForInitialTwoEdgesGraph =
Expand All @@ -88,7 +88,7 @@ let expectedForInitialTwoEdgesGraph =
nodes.Add(10, TriplesStoredSPPFNode.TerminalNode (1<inputGraphVertex>,0<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance> ; 2<distance>|]
let distances = [|0<weight>; 1<weight> ; 2<weight>|]
(nodes,edges,distances)
Expand All @@ -109,7 +109,7 @@ let ``(a|b)* add A to end`` =
let expected = expectedForInitialOneEdgeGraph
runGLLAndCheckResultForManuallyCreatedGraph (reachableVertices, gss, matchedRanges) testName startVertices q expected
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex<_>
graphExit.OutgoingEdges.Add(terminalA, HashSet [|newGraphExit|])
let verticesWithChanges = HashSet [|graphExit|]
Expand All @@ -129,11 +129,11 @@ let ``(a|b)* add A to end`` =
nodes.Add(10, TriplesStoredSPPFNode.TerminalNode (1<inputGraphVertex>,0<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance>; 2<distance>|]
let distances = [|0<weight>; 1<weight>; 2<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let ``(a|b)* add loop with A`` =
let testName = "(a|b)* add loop with A"
Expand Down Expand Up @@ -171,7 +171,7 @@ let ``(a|b)* add loop with A`` =
nodes.Add(10, TriplesStoredSPPFNode.TerminalNode (1<inputGraphVertex>,0<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance>; 2<distance>|]
let distances = [|0<weight>; 1<weight>; 2<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
Expand All @@ -196,7 +196,7 @@ let ``(a|b)* add B to end`` =
MatchedRanges()
runGLLAndCheckResultForManuallyCreatedGraph (reachableVertices, gss, matchedRanges) testName startVertices q expected
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex<_>
graphExit.OutgoingEdges.Add(terminalB, HashSet [|newGraphExit|])
let verticesWithChanges = HashSet [|graphExit|]
Expand All @@ -217,11 +217,11 @@ let ``(a|b)* add B to end`` =
nodes.Add(10, TriplesStoredSPPFNode.TerminalNode (1<inputGraphVertex>,1<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance>; 2<distance>|]
let distances = [|0<weight>; 1<weight>; 2<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let ``(a|b)* add branch with B to start`` =
let testName = "(a|b)* add branch with B to start"
Expand All @@ -241,7 +241,7 @@ let ``(a|b)* add branch with B to start`` =
MatchedRanges()
runGLLAndCheckResultForManuallyCreatedGraph (reachableVertices, gss, matchedRanges) testName startVertices q expected
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex
let newGraphExit = InputGraphVertexBase() :> IInputGraphVertex<_>
graphEntryPoint.OutgoingEdges.Add(terminalB, HashSet [|newGraphExit|])
let verticesWithChanges = HashSet [|graphEntryPoint|]
Expand All @@ -260,12 +260,12 @@ let ``(a|b)* add branch with B to start`` =
nodes.Add(8, TriplesStoredSPPFNode.TerminalNode (0<inputGraphVertex>,1<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8)|])
let distances = [|0<distance>; 1<distance>; 1<distance>|]
let distances = [|0<weight>; 1<weight>; 1<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let ``(a|b)* replace A with B`` =
let testName = "(a|b)* replace A with B"
Expand Down Expand Up @@ -302,11 +302,11 @@ let ``(a|b)* replace A with B`` =
nodes.Add(5, TriplesStoredSPPFNode.TerminalNode (0<inputGraphVertex>,1<terminalSymbol>,1<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5)|])
let distances = [|0<distance>; 1<distance>|]
let distances = [|0<weight>; 1<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let ``(a|b)* replace last A with B`` =
Expand Down Expand Up @@ -349,11 +349,11 @@ let ``(a|b)* replace last A with B`` =
nodes.Add(10, TriplesStoredSPPFNode.TerminalNode (1<inputGraphVertex>,1<terminalSymbol>,2<inputGraphVertex>))
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance>; 2<distance>|]
let distances = [|0<weight>; 1<weight>; 2<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let ``(a|b)* replace first A with B`` =
Expand Down Expand Up @@ -404,11 +404,11 @@ let ``(a|b)* replace first A with B`` =
let edges = ResizeArray<_>([|(0,1); (1,2); (3,4); (4,5); (6,7); (7,8); (8,4); (8,9); (9,10)|])
let distances = [|0<distance>; 1<distance>; 2<distance>|]
let distances = [|0<weight>; 1<weight>; 2<weight>|]
(nodes,edges,distances)
let result = onInputGraphChanged verticesWithChanges reachableVertices gss matchedRanges startVertices q Mode.AllPaths
checkResult (testName:string) startVertices (q:RSM) expected result
checkResult (testName:string) startVertices (q:RSM<_>) expected result
let tests =
Expand All @@ -421,3 +421,4 @@ let tests =
//``(a|b)* add branch with B to start``
``(a|b)* add loop with A``
]
*)
Loading

0 comments on commit 7ad997e

Please sign in to comment.