Skip to content

Commit

Permalink
Added examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
tclune committed Mar 13, 2020
1 parent 71e3d3b commit 0c32dee
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 176 deletions.
11 changes: 9 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added
## Fixed

## [0.2.1] - 2020-03-13

## Added

- Added examples - apparently failed to commit thes previously. Build
with 'make Examples', and go to the build directory to run.


## [0.2.0] - 2020-03-11

## Added
### Added

- New interfaces for Configuration::get()
. allow for default values and testing if present
Expand All @@ -22,7 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved error handling throughout
. still needs more work.

## Fixed
### Fixed

- Some errors in lexing were exposed with pFlogger use cases.

Expand Down
4 changes: 4 additions & 0 deletions Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subdirectory (Trivial)
add_subdirectory (Simple)
add_subdirectory (Iterators)

6 changes: 6 additions & 0 deletions Examples/Iterators/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable (iterator.x iterator.F90)
target_link_libraries (iterator.x yafyaml gftl-shared)
add_dependencies (examples iterator.x)

FILE (COPY iterator.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

65 changes: 65 additions & 0 deletions Examples/Iterators/iterator.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
! This example does minimal error checking. The point is to
! demonstrate how to use iterators. In the absence of exceptions,
! error checking becomes verbose and obscures the main features.

program main
use yafyaml
use gFTL_IntegerVector
use gftl_StringIntegerMap
implicit none

type(Parser) :: p
type(Configuration) :: config
type(Configuration) :: subcfg
type(Configuration) :: shape_cfg

type(ConfigurationIterator) :: iter, iter2

integer :: status
character(:), pointer :: key
integer :: prime
character(:), pointer :: shape
integer :: n_edges


p = Parser('core')
config = p%load(FileStream('iterator.yaml'))

! Outer loop over mapping
iter = config%begin()
do while (iter /= config%end())
! Access mapping values with key()/value() methods on iterator.
key => iter%key()
subcfg = iter%value()

select case (key)
case ('primes')

! loop over primes (a yaml sequence)
iter2 = subcfg%begin()
do while (iter2 /= subcfg%end())
! Access sequence values with get() method on iterator
prime = iter2%get() ! cast as integer
print*,'another prime: ', prime
call iter2%next()
end do

case ('shapes')

! loop over shapes (a yaml mapping)
iter2 = subcfg%begin()
do while (iter2 /= subcfg%end())
shape => iter2%key()
shape_cfg = iter2%value()
n_edges = shape_cfg%at('num_edges')
print*,'shape: ',shape,' has ',n_edges, 'sides'
call iter2%next()
end do

end select


call iter%next()
end do

end program main
15 changes: 15 additions & 0 deletions Examples/Iterators/iterator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
primes:
- 2
- 3
- 5
- 7
shapes:
triangle:
num_edges: 3
edge_length: 3.12
square:
num_edges: 4
edge_length: 6.8



6 changes: 6 additions & 0 deletions Examples/Simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable (simple.x simple.F90)
target_link_libraries (simple.x yafyaml gftl-shared)
add_dependencies (examples simple.x)

FILE (COPY simple.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

107 changes: 107 additions & 0 deletions Examples/Simple/simple.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
program main
use yafyaml
use gFTL_IntegerVector
use gftl_StringIntegerMap
implicit none

type(Parser) :: p
type(Configuration) :: config
type(Configuration) :: subconfig

real :: x
character(:), allocatable :: name
logical :: flag

integer, allocatable :: sequence_a(:)
integer, allocatable :: sequence_b(:)

integer :: v1, v2, v3

logical :: is_present
integer :: status


p = Parser('core')
config = p%load(FileStream('simple.yaml'))

x = config%at('x')

if (x == 1.234) then
print*,'success'
else
print*,'failure; expected 1.234 but found ', x
end if


flag = .false.
flag = config%at('flag')

if (flag) then
print*,'success'
else
print*,'failure; expected .true.'
end if

sequence_a = config%at('sequence_a')

if (all (sequence_a == [1,2,3,4])) then
print*,'success'
else
print*,'failure in handling flow sequence; expected .true.'
end if

sequence_b = config%at('sequence_b')

if (all (sequence_b == [1,2,3,4])) then
print*,'success'
else
print*,'failure in handling block sequence; expected .true.'
end if

! Flow mapping
v1 = config%at('mapping_a', 'v1')
v2 = config%at('mapping_a', 'v2')

if (v1 == 7 .and. v2 == 8) then
print*,'success'
else
print*,'failure in handling flow mapping', v1, v2
end if

! Block mapping
v1 = config%at('mapping_b', 'v1')
v2 = config%at('mapping_b', 'v2')

if (v1 == 7 .and. v2 == 8) then
print*,'success'
else
print*,'failure in handling flow mapping', v1, v2
end if

call config%get(subconfig, 'mapping_b')

v1 = -1
call config%get(v1, 'mapping_b', 'v1', is_present=is_present, rc=status)
if (v1 == 7 .and. is_present .and. status == SUCCESS) then
print*,'success'
else
print*,'failure in handling flow mapping', v1, v2
end if

! Handle missing values
call config%get(v3, 'mapping_b', 'v3', default=17, is_present=is_present, rc=status)
if (v3 == 17 .and. (.not. is_present) .and. status == SUCCESS) then
print*,'success'
else
print*,'failure in handling flow mapping'
end if

! error if wrong type:
call config%get(flag, 'mapping_b', 'v2', is_present=is_present, rc=status)
if (is_present .and. status == INCONSISTENT_TYPE) then
print*,'expected failure'
else
print*,'should have failed, but did not'
end if

end program main
25 changes: 25 additions & 0 deletions Examples/Simple/simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# exercising the basics and non-nested sequences/mappings
x: 1.234
name: Fred
flag: True

# flow sequence
sequence_a: [ 1 , 2 , 3 , 4 ]

# block sequence
sequence_b:
- 1
- 2
- 3
- 4

# block mapping
mapping_b:
v1: 7
v2: 8

# flow mapping
mapping_a: { v1: 7 , v2: 8 }

...
6 changes: 6 additions & 0 deletions Examples/Trivial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_executable (trivial.x trivial.F90)
target_link_libraries (trivial.x yafyaml)
add_dependencies (examples trivial.x)

FILE (COPY trivial.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

19 changes: 19 additions & 0 deletions Examples/Trivial/trivial.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
program main
use yafyaml

type(Parser) :: p
type(Configuration) :: config
integer :: prime


p = Parser('core')
config = p%load(FileStream('trivial.yaml'))

prime = config%at('prime')

if (prime == 17) then
print*,'success'
else
print*,'failure; expected 17 but found ', prime
end if
end program main
3 changes: 3 additions & 0 deletions Examples/Trivial/trivial.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
prime: 17
...
Loading

0 comments on commit 0c32dee

Please sign in to comment.