Skip to content

Commit

Permalink
Trying to get regex to work
Browse files Browse the repository at this point in the history
  • Loading branch information
licioromao committed Mar 27, 2024
1 parent bacda99 commit dffeac3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ authors = ["Mykel Kochenderfer <[email protected]> and contributors"]
version = "1.0.0-DEV"

[deps]
Debugger = "31a5f54b-26ea-5ae9-a837-f05ce5417438"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"

[compat]
julia = "1.6"
OrderedCollections = "1.6"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
44 changes: 35 additions & 9 deletions src/WildcardArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module WildcardArrays

using OrderedCollections

struct WildcardArray{T,N} <: AbstractArray{T,N} where T <: Real
data::OrderedDict{NTuple{N, Int}, T} # this would be the ordered map
struct WildcardArray{T<:Real,N} <: AbstractArray{T,N}
data::OrderedDict{NTuple{N,Int},T} # this would be the ordered map
dims::NTuple{N,Int}
WildcardArray(dims::NTuple{N, Int}) = new(OrderedDict{NTuple{N, Int}, T}(), dims)
WildcardArray(dims::Vararg{Int, N}) = new(OrderedDict{NTuple{N, Int}, T}(), Tuple(dims))
WildcardArray(dims::NTuple{N, Int}) where {N} = new{Float64, N}(OrderedDict{NTuple{N, Int}, Float64}(), dims)
# WildcardArray(dims::Vararg{Int, N}) = new(OrderedDict{NTuple{N, Int}, T}(), Tuple(dims))
end

Base.size(wa::WildcardArray) = wa.dims
dict(wa::WildcardArray) = wa.data
eltype(::WildcardArray{T,N}) = T
eltype(::WildcardArray{T,N}) where {T,N} = T

function Base.getindex(wa::WildcardArray{T,N}, i::Vararg{Int, N})::T
function Base.getindex(wa::WildcardArray{T,N}, i::Vararg{Int,N})::T where {T,N}
keys_obj = dict(wa) |> keys |> collect
# println(keys_obj)
# println(eltype(wa))
Expand All @@ -24,8 +24,8 @@ function Base.getindex(wa::WildcardArray{T,N}, i::Vararg{Int, N})::T

index = 0

for kk in Iterators.product([[0,index] for index in i]...)
temp_index = findfirst(x->isequal(x,kk), keys_obj)
for kk in Iterators.product([[0, index] for index in i]...)
temp_index = findfirst(x -> isequal(x, kk), keys_obj)

if !isnothing(temp_index)
if temp_index > index
Expand All @@ -35,7 +35,7 @@ function Base.getindex(wa::WildcardArray{T,N}, i::Vararg{Int, N})::T
end

if index == 0
return zero(T)
return zero(T)
end

return dict(wa)[keys_obj[index]] # put implementation here
Expand All @@ -44,12 +44,38 @@ end
Base.ndims(wa::WildcardArray) = length(wa.dims)

function parse!(wa::WildcardArray, s::String, values::Vector{Vector{String}})
~, d₁, d₂ = size(wa)
regex_first_colon = r"^([^:]*):"
name_of_transition = match(regex_first_colon, s).captures[1] |> strip

sp = _parse_string(s, regex_first_colon)

reg_ex = Regex("\\s*$(name_of_transition):(.*):(.*):(.*):\\s*(\\S*) (\\s*.*\\s*){1,$(d₁)}R:|\\s*$(name_of_transition):(.*):(.*):(.*):\\s*(\\S*) \\s*(.*)\\s*\$")
match_reg = match(reg_ex, s)
tempstr = deepcopy(s)

while !isnothing(match_reg)
println(match_reg.captures)
println(tempstr)
tempstr = tempstr[maximum(match_reg.offsets) + 1:end]
match_reg = match(reg_ex, tempstr)
end

return nothing
# parse string into WildcardArray
end

function parse!(wa::WildcardArray, s::String)
# parse string into WildcardArray
end

function _parse_string(s::String, prefix::Regex)
sp = string.(split(s, "\n"))
spp = map(strip, [replace(line, prefix => "") for line in sp])
filter!(!isempty, spp)

return join(spp, " \n ")
end


end
79 changes: 54 additions & 25 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
using WildcardArrays
using Test

@testset "WildcardArrays.jl" begin
# @testset "WildcardArrays.jl" begin

str = """
R: north : warm : cold :more-warning 6
R: * : cold : almost-cold: warning 7
R: east : * : almost-cold: warning 7
R: south : cold : *: warning 7
R: south : cold : almost-cold: * 7
R: * : *: almost-cold: more-warning 8
R: * : warm : *: more-warning 8
R: * : warm : almost-cold: * 8
R: east : *: *: more-warning 8
R: east : *: warm: * 8
R: east : cold: *: * 8
R: west : * : * : * 10
R: * : cold : * : * 10
R: * :*: cold : * 10
R: * :*: * : more-warning 10
R: north : warm : cold :more-warning 1
R: * : cold : almost-cold: warning 2
R: east : * : almost-cold: warning 3
R: south : cold : *: warning 4
R: south : cold : almost-cold: *
7 10 20
R: * : *: almost-cold: more-warning 5
R: * : warm : *: more-warning 6
R: * : warm : almost-cold: * 7
R: east : *: *: more-warning 8
R: east : *: warm: * 9
R: east : cold: *: * 10
R: west : * : * : * 11
R: * : cold : * : * 12
R: * :*: cold : * 13
R: * :*: * : more-warning 14
R: * :*: * : * 0
"""
"""

str_original = deepcopy(str)

reg_ex = r"\s*R:(.*):(.*):(.*):\s*(.*) (.*)\s*(R):|\s*R:(.*):(.*):(.*):\s*(\S*) (\s*.*){1,1}\s*(R):|\s*R:(.*):(.*):(.*):\s*(\S*) \s*(.*)\s*$"
# rp = r"\s*R:(.*):(.*):(.*):\s*(.*) (.*)\s*(R):"
# reg_ex = r"R:(.*?)R:|R:(.*):(.*):(.*):(.*) (\d+)\n"
# bb = match(Regex(string(reg_ex.pattern, "|", rp.pattern)),str)
bb = match(reg_ex, str)
# println(bb.captures)
# println(str[maximum(bb.offsets):end])

while bb != nothing
global str, bb
println(str)
# sleep(4)
println(bb.captures)
# sleep(1)
str = str[maximum(bb.offsets):end]
# println(str)
# sleep(1)
bb = match(reg_ex, str)
end

# println(bb.captures)

# aa = string.(split(str, "\n"))
# aa = filter(!isempty, aa)

states = ["warm", "cold", "not-to-cold", "very-very-cold", "almost-cold", "ow-this-is-very-cold"]
actions = ["north", "south", "east", "west"]
observations = ["warning", "more-warning"]
nothing
# wa = WildcardArray{Float64, 4}(4, 6, 6, 2)
# parse!(wa, str, [actions, states, states, observations])
# @test size(wa) == (4, 6, 6, 2)
# @test eltype(wa) == Float64
# @test wa[1, 1, 1, 1] == 6

wa = WildcardArray{Float64, 4}(4, 6, 6, 2)
parse!(wa, str, [actions, states, states, observations])
@test size(wa) == (4, 6, 6, 2)
@test eltype(wa) == Float64
@test wa[1, 1, 1, 1] == 6

end
# end

0 comments on commit dffeac3

Please sign in to comment.