Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
## Introduction
`yaml_c_wrapper.cpp` wraps `YAML::Node` into C objects so they can be part of a shared object library to interface with other languages
<!--
First install `yaml-cpp` by running

macOS:
```console
brew install yaml-cpp
```
or
```console
port install yaml-cpp
```

Linux:
```console
sudo apt-get install libyaml-cpp-dev
```

Windows:
```console
choco install yaml-cpp
```

Manual Install:
```console
git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp/src
mkdir build && cd build
cmake ..
cmake --build .
cmake --install .
```
-->
Implemented components of lattice expansion:
include
inherits
repeat

## Usage
In pals-cpp, run
Expand Down Expand Up @@ -66,6 +39,5 @@ To run a specific test, run
ctest --test-dir build -R "Test Name"
```

## Issue
`yaml-cpp`'s cmake only requires cmake version 3.4, which is deprecated. Warnings must
be suppressed to run properly
## To Do
add test cases for lattice expansion
67 changes: 42 additions & 25 deletions examples/yaml_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
#include "../src/yaml_c_wrapper.h"
#include <iostream>
#include "../src/yaml_c_wrapper.h"

// If file name is provided as a command line argument, this will print out the
// expanded contents of the file to the terminal, as well as to expand.pals.yaml.
// Otherwise, it will use the example file ex.pals.yaml

// See ex.pals.yaml for the example lattice file and expand.pals.yaml for the output of this file.
int main(int argc, char* argv[]) {
if (argc != 1) {
char* filename = argv[1];
std::string path = "../lattice_files/";
path += filename;
YAMLNodeHandle handle = parse_file(path.c_str());

int main() {
lattice_expand(handle);
std::cout << "Printing out contents of file: " << filename << std::endl;
std::cout << yaml_to_string(handle) << std::endl;
write_file(handle, "../lattice_files/expand.pals.yaml");
return 0;
}
// reading a lattice from a yaml file
// allow files to be read in through command line, print back out
YAMLNodeHandle handle = yaml_parse_file("../lattice_files/ex.pals.yaml");
// print out file name that was read in
YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml");
std::cout << "Printing out contents of file: " << "ex.pals.yaml" << std::endl;
// printing to terminal
std::cout << yaml_to_string(handle) << std::endl << std::endl;

// type checking
// prints "handle is of type sequence: 1", 1 meaning true
std::cout << "handle is of type sequence: " << (yaml_is_sequence(handle)) << "\n";
std::cout << "handle is of type sequence: " << (is_sequence(handle))
<< "\n";

// accessing sequence
YAMLNodeHandle node = yaml_get_index(handle, 0);
YAMLNodeHandle node = get_index(handle, 0);
/* prints
the first element is:
the first element is:
thingB:
kind: Sextupole
*/
std::cout << "the first element is: \n" << yaml_to_string(node) << "\n";

// accessing map
// prints "the value at key 'thingB' is: kind: Sextupole"
std::cout << "\nthe value at key 'thingB' is: " << yaml_to_string(yaml_get_key(node, "thingB")) << "\n";
std::cout << "\nthe value at key 'thingB' is: "
<< yaml_to_string(get_key(node, "thingB")) << "\n";

// creating a new node that's a map
YAMLNodeHandle map = yaml_create_map();
yaml_set_int(map, "first", 1);
// creating a new node that's a map
YAMLNodeHandle map = create_map();
set_value_int(map, "apples", 5);

// creating a new node that's a sequence
YAMLNodeHandle sequence = yaml_create_sequence();
yaml_push_string(sequence, "magnet1");
yaml_push_string(sequence, "");
YAMLNodeHandle scalar = yaml_create_scalar();
yaml_set_scalar_string(scalar, "magnet2");
yaml_set_at_index(sequence, 1, scalar);
YAMLNodeHandle sequence = create_sequence();
push_string(sequence, "magnet1");
push_string(sequence, "");
YAMLNodeHandle scalar = create_scalar();
set_scalar_string(scalar, "magnet2");
set_at_index(sequence, 1, scalar);
// give sequence a name by putting it in a map:
YAMLNodeHandle magnets = yaml_create_map();
yaml_set_node(magnets, "magnets", sequence);
YAMLNodeHandle magnets = create_map();
set_value_node(magnets, "magnets", sequence);

// adding new nodes to lattice
yaml_push_node(handle, map);
yaml_push_node(handle, magnets);
push_node(handle, map);
push_node(handle, magnets);

yaml_expand(handle);
// performing lattice expansion
lattice_expand(handle);

// writing modified lattice file to expand.pals.yaml
yaml_write_file(handle, "../lattice_files/expand.pals.yaml");
write_file(handle, "../lattice_files/expand.pals.yaml");
return 0;
}
7 changes: 6 additions & 1 deletion lattice_files/ex.pals.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
- thingB:
kind: Sextupole

- inj_line:
kind: BeamLine
multipass: true
Expand All @@ -16,3 +15,9 @@
- a_subline: # Item a_subline is repeated three times
repeat: 3
- include: "include.pals.yaml"
- a_subline:
kind: BeamLine
line:
- a
- b
- c
6 changes: 3 additions & 3 deletions lattice_files/include.pals.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- line:
first: 1
second: 2
thrid: 3
- first: 1
- second: 2
- third: 3
Loading