Skip to content

Commit

Permalink
Merge pull request #134 from aminya/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored May 14, 2020
2 parents 8591893 + a352e7b commit f8b8f22
Show file tree
Hide file tree
Showing 22 changed files with 1,028 additions and 1,144 deletions.
7 changes: 7 additions & 0 deletions benchmark/bench.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ P1, P2, U = creation();
#=
Benchmark Result
v 0.10
# small bump in extraction is because of two additional hasmethod check in nodeparse. Using traits this will be fixed
8.899 μs (100 allocations: 3.61 KiB)
3.938 μs (66 allocations: 3.17 KiB)
308.500 μs (403 allocations: 16.33 KiB)
V 0.9.2 - julia 1.4
10.000 μs (99 allocations: 3.39 KiB)
Expand Down
32 changes: 16 additions & 16 deletions deps/SnoopCompile/precompile/precompile_AcuteML.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function _precompile_()
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
isdefined(AcuteML, Symbol("#8#17")) && precompile(Tuple{getfield(AcuteML, Symbol("#8#17")),Nothing})
precompile(Tuple{Core.kwftype(typeof(AcuteML.render2file)),NamedTuple{(:id, :age, :field, :GPA, :courses),Tuple{Int64,Int64,String,Float64,Array{String,1}}},typeof(render2file),String,Bool})
precompile(Tuple{typeof(AcuteML.aml_create),Expr,Array{Union{Expr, Symbol},1},Array{Any,1},Array{Union{Expr, Symbol, Type},1},Array{Union{Expr, Symbol},1},Array{Union{Missing, String},1},Array{Union{Missing, Function, Symbol},1},Array{Union{Missing, Type},1},String,Type{T} where T,Array{Union{Missing, Function, Symbol},0},Bool,Array{Union{Nothing, Expr},1},Array{Union{Nothing, Expr},1},Array{Union{Nothing, Expr},1},Symbol})
precompile(Tuple{typeof(AcuteML.aml_parse),Expr})
Expand All @@ -11,17 +10,19 @@ function _precompile_()
precompile(Tuple{typeof(AcuteML.multiString),Float64})
precompile(Tuple{typeof(AcuteML.multiString),Int64})
precompile(Tuple{typeof(AcuteML.multiString),String})
precompile(Tuple{typeof(addelm!),Document,String,Nothing,Type{AbsAttribute}})
precompile(Tuple{typeof(addelm!),Node,String,Array{Any,1},Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Array{Float64,1},Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Array{Int64,1},Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Array{String,1},Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Float64,Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Int64,Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,Nothing,Type{AbsAttribute}})
precompile(Tuple{typeof(addelm!),Node,String,Nothing,Type{AbsNormal}})
precompile(Tuple{typeof(addelm!),Node,String,String,Type{AbsAttribute}})
precompile(Tuple{typeof(addelm!),Node,String,String,Type{AbsNormal}})
precompile(Tuple{typeof(AcuteML.nodeparse),Type{T} where T,Node})
precompile(Tuple{typeof(addnode!),Node,String,Array{Any,1},Type{AbsNormal}})
precompile(Tuple{typeof(addnode!),Node,String,Array{Float64,1},Type{AbsNormal}})
precompile(Tuple{typeof(addnode!),Node,String,Array{Int64,1},Type{AbsNormal}})
precompile(Tuple{typeof(addnode!),Node,String,Array{String,1},Type{AbsNormal}})
precompile(Tuple{typeof(addnode!),Node,String,Nothing,Type{AbsAttribute}})
precompile(Tuple{typeof(addnode!),Node,String,Nothing,Type{AbsNormal}})
precompile(Tuple{typeof(addnode!),Node,String,String,Type{AbsAttribute}})
precompile(Tuple{typeof(addnode!),Node,String,String,Type{AbsNormal}})
precompile(Tuple{typeof(createnode),Type{AbsEmpty},String})
precompile(Tuple{typeof(createnode),Type{AbsHtml},String})
precompile(Tuple{typeof(createnode),Type{AbsNormal},String})
precompile(Tuple{typeof(createnode),Type{AbsXml},String})
precompile(Tuple{typeof(findcontent),String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(findcontent),Type{Array{Any,1}},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(findcontent),Type{Array{Float64,1}},String,Node,Type{AbsNormal}})
Expand All @@ -31,14 +32,13 @@ function _precompile_()
precompile(Tuple{typeof(findcontent),Type{String},String,Node,Type{AbsAttribute}})
precompile(Tuple{typeof(findcontent),Type{String},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(findcontent),Type{Union{Nothing, String}},String,Node,Type{AbsAttribute}})
precompile(Tuple{typeof(initialize_node),Type{AbsEmpty},String})
precompile(Tuple{typeof(initialize_node),Type{AbsHtml},String})
precompile(Tuple{typeof(initialize_node),Type{AbsNormal},String})
precompile(Tuple{typeof(initialize_node),Type{AbsXml},String})
precompile(Tuple{typeof(newTemplate),String,Symbol})
precompile(Tuple{typeof(newTemplate),String})
precompile(Tuple{typeof(updatecontent!),Array{Any,1},Array{Node,1},Node,Type{T} where T})
precompile(Tuple{typeof(updatecontent!),Array{Any,1},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(updatecontent!),Array{Float64,1},Array{Node,1},Node,Type{T} where T})
precompile(Tuple{typeof(updatecontent!),Array{Float64,1},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(updatecontent!),Array{Int64,1},Array{Node,1},Node,Type{T} where T})
precompile(Tuple{typeof(updatecontent!),Array{Int64,1},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(updatecontent!),Array{String,1},String,Node,Type{AbsNormal}})
precompile(Tuple{typeof(updatecontent!),String,String,Node,Type{AbsAttribute}})
Expand Down
8 changes: 3 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ makedocs(;
),
pages=[
"Home" => "index.md",

"Value Checking" => "valueChecking.md",
"Supported Value Types" => "supportedValueTypes.md",
"Value Checking" => "valueChecking.md",
"Extra Contructors" => "extraConstructors.md",

"Custom Value Types" => "customValueTypes.md",
"Custom Contructors - AcuteML Backend" => "customConstructors.md",

"Custom Contructors" => "customConstructors.md",
"DOM/XPath API" => "domxpath.md",
"Templating" => "templating.md",
"Syntax Reference" => "SyntaxReference.md"
],
Expand Down
59 changes: 0 additions & 59 deletions docs/src/customConstructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,62 +110,3 @@ mathclass = MathClass(xml)

mathclass.students[2].log # "A genius with a GPA of 5.0 is found"
```

## Making a Type and constructor from scratch using AcuteML Backend

You can use AcuteML utilities to define custom type constructors from scratch or to override `@aml` defined constructors.

Notice that if you don't use `@aml`, you should include `aml::Node` as one of your fields.

Functions to use for custom html/xml constructor:
- [initialize_node](@ref): function to initialize the aml
- [addelm!](@ref) : to add elements (single or a vector of elements)
Use these functions, to make a method that calculates the `aml` inside the function and returns all of the fields.

Functions to use for custom html/xml extractor:
- [findcontent](@ref) : to extract elements
Use these functions, to make a method that gets the `aml::Node` and calculates and returns all of the fields.

Functions to support mutability:
- [updatecontent!](@ref): Finds all the elements with the address of string in the node, and updates the content.

# Example:
In this example we define `Identity` with custom constructors:
```julia
using AcuteML

mutable struct Identity
pitch::UN{Pitch}
rest::UN{Rest}
unpitched::UN{Unpitched}
aml::Node
end

function Identity(;pitch = nothing, rest = nothing, unpitched = nothing)

# This constructor only allows one the fields to exist - similar to choice element in XS

aml = initialize_node(AbsNormal, "identity")

if pitch != nothing
addelm!(aml, "pitch", pitch, AbsNormal)
elseif rest != nothing
addelm!(aml, "rest", rest, AbsNormal)
elseif unpitched != nothing
addelm!(aml, "unpitched", unpitched, AbsNormal)
else
error("one of the pitch, rest or unpitched should be given")
end

return Identity(pitch, rest, unpitched, aml)
end

function Identity(;aml)

pitch = findcontent(Pitch, "pitch", aml, AbsNormal)
rest = findcontent(Rest, "rest", aml, AbsNormal)
unpitched = findcontent(Unpitched, "unpitched", aml, AbsNormal)

return Identity(pitch, rest, unpitched, aml)
end
```
64 changes: 64 additions & 0 deletions docs/src/domxpath.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# AcuteML DOM/Xpath API

AcuteML provides a DOM/Xpath API that you can use to do lower level XML/HTML manipulation if needed.

You can use AcuteML utilities to define custom type constructors from scratch or to override `@aml` defined constructors.

Notice that if you don't use `@aml`, you should include `aml::Node` as one of your fields.

Functions to use for custom html/xml constructor:
- [createnode](@ref): function to create a node/document
- [addnode!](@ref) : To add nodes (single or a vector of nodes) as a child of given a node/document.
Use these functions, to make a method that calculates the `aml` inside the function and returns all of the fields.

Functions to use for custom html/xml extractor:
- [findfirst](@ref): to find the first node based on the given node name
- [findall](@ref): to find all of the nodes based on the given node name
- [findcontent](@ref) : to get the content of a node based on the given name
Use these functions, to make a method that gets the `aml::Node` and calculates and returns all of the fields.

Functions to support mutability:
- [updatecontent!](@ref): Finds all the elements with the address of string in the node, and updates the content.

## Making a Type and constructor from scratch using AcuteML Backend

# Example:
In this example we define `Identity` with custom constructors:
```julia
using AcuteML

mutable struct Identity
pitch::UN{Pitch}
rest::UN{Rest}
unpitched::UN{Unpitched}
aml::Node
end

function Identity(;pitch = nothing, rest = nothing, unpitched = nothing)

# This constructor only allows one the fields to exist - similar to choice element in XS

aml = createnode(AbsNormal, "identity")

if pitch != nothing
addnode!(aml, "pitch", pitch, AbsNormal)
elseif rest != nothing
addnode!(aml, "rest", rest, AbsNormal)
elseif unpitched != nothing
addnode!(aml, "unpitched", unpitched, AbsNormal)
else
error("one of the pitch, rest or unpitched should be given")
end

return Identity(pitch, rest, unpitched, aml)
end

function Identity(;aml)

pitch = findcontent(Pitch, "pitch", aml, AbsNormal)
rest = findcontent(Rest, "rest", aml, AbsNormal)
unpitched = findcontent(Unpitched, "unpitched", aml, AbsNormal)

return Identity(pitch, rest, unpitched, aml)
end
```
2 changes: 1 addition & 1 deletion src/@aml/@aml_create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function aml_create(expr::Expr, args_param, args_defaultvalue, args_type, args_v
error("Invalid usage of @aml")
end
################################################################
node_initializer = :( aml = initialize_node($struct_nodetype, $struct_name) )
node_initializer = :( aml = createnode($struct_nodetype, $struct_name) )

struct_definition =:($expr)

Expand Down
4 changes: 2 additions & 2 deletions src/@aml/@aml_create/get_arg_xml_.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ function get_arg_xmlcreator(argcustomcreator, has_arg_xmlchecker::Bool, argtype,
if !has_arg_xmlchecker
arg_creator = quote
$(esc(argcustomcreator))
addelm!(aml, $argname, $esc_argvar, $argliteraltype)
addnode!(aml, $argname, $esc_argvar, $argliteraltype)
end
else
arg_creator=quote
$(esc(argcustomcreator))
if isnothing($esc_argvar) || ($esc_argfunction)($esc_argvar)
addelm!(aml, $argname, $esc_argvar, $argliteraltype)
addnode!(aml, $argname, $esc_argvar, $argliteraltype)
else
error("$($argname) doesn't meet criteria function")
end
Expand Down
2 changes: 1 addition & 1 deletion src/@aml/@aml_parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function aml_parse(expr::Expr)
argsexpr = expr.args[3] # arguments of the type

# TODO: optimize and fuse these
areargs_inds = findall(x->!(isa(x, LineNumberNode)), argsexpr.args)
areargs_inds = Base.findall(x->!(isa(x, LineNumberNode)), argsexpr.args)
macronum = count(x-> isa(x, Tuple{Symbol, Expr}), argsexpr.args)

data = argsexpr.args[areargs_inds]
Expand Down
14 changes: 9 additions & 5 deletions src/xmlutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import EzXML: Document, Node
################################################################
import Tables
# DataTypesSupport
include("xmlutils/aml_type_support.jl")
include("xmlutils/TypesSupport/amlTables.jl")
################################################################
include("xmlutils/initializer.jl")
include("xmlutils/creators.jl")
include("xmlutils/extractor_utils.jl")
include("xmlutils/extractors.jl")
include("xmlutils/updaters.jl")
include("xmlutils/addnode.jl")
include("xmlutils/nodeparse.jl")
include("xmlutils/findnode.jl")
include("xmlutils/findcontent.jl")
include("xmlutils/updater.jl")
################################################################

@deprecate addelm!(args...) addnode!(args...)
@deprecate initialize_node(args...) createnode(args...)
2 changes: 1 addition & 1 deletion src/xmlutils/TypesSupport/amlTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function amlTable(x)
pretty_table(io, x, backend = :html, standalone = false)
str = String(resize!(io.data, io.size))

html = findfirst("html/body/table",parsehtml(str))
html = Base.findfirst("html/body/table",parsehtml(str))
unlink!(html)
return html
end
Loading

0 comments on commit f8b8f22

Please sign in to comment.