Skip to content

Commit 1cc513a

Browse files
authored
Add files via upload
1 parent 95f625f commit 1cc513a

19 files changed

+91560
-0
lines changed

EntityInstance.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// Created by gmook on 4/2/2021.
3+
//
4+
5+
#include "EntityInstance.hpp"
6+
#include <iostream>
7+
#include <iomanip>
8+
9+
10+
void EntityInstance::print(int numSpaces, bool addComma) {
11+
// Printing a instance within a set
12+
// Instance is represented by { }
13+
std::cout << std::setw(numSpaces-1) << "{" << std::endl;
14+
for (int i = 0; i < numAttributes(); i++) {
15+
// Create a variable that represents the last element
16+
int last = numAttributes() - 1;
17+
// Check if we are at the last attribute with a IF
18+
if (i == last) {
19+
// If we are at the last attribute then do not add a comma
20+
// we do this by setting addComma to false by passing it
21+
// with the print function call
22+
entityPairs.at(i).print(7, false);
23+
} else {
24+
// However, if we are not at the last attribute
25+
// add a comma by passing true with the print function call
26+
entityPairs.at(i).print(7, true);
27+
}
28+
}
29+
if(addComma) {
30+
// If the addComma boolean flag is set to true
31+
// then we simply add a comma to the end of }
32+
std::cout << std::setw(numSpaces) << "}," << std::endl;
33+
}
34+
else {
35+
// If the boolean addComma flag is set to false then remember
36+
// the comma takes up a extra space when printing. Since not adding comma
37+
// be sure to setw to setw(numSpaces - 1) in order to have
38+
// the correct alignment
39+
std::cout << std::setw(numSpaces - 1) << "}" << std::endl;
40+
}
41+
42+
}
43+
44+
// Function of STRING datatype because it is used to
45+
// return a ID which is off STRING datatype
46+
std::string EntityInstance::locateId() {
47+
//********
48+
// This functions will be similar to the locateGpa() function
49+
// because they both same the same goal, however, instead of a double(GPA)
50+
// this function will retrieve and return a string(ID).
51+
//********
52+
// Start by creating a variable of string datatype that can hold
53+
// the retrieved IDs
54+
std::string locateId;
55+
// Create a loop that goes over each element in the entityPairs vector
56+
for(int i = 0; i < entityPairs.size(); i++) {
57+
// Set the string we just created equal to a function from
58+
// the Pair class called locateId(). locateId() will
59+
// return _attributeStringValue when a previous token "ID" is found
60+
locateId = entityPairs.at(i).locateId();
61+
// We create a boolean in the Pair class called foundTheId() this acts
62+
// as a flag so we know when we locate a valid ID. If we find a valid ID using
63+
// the Pair class function locateID() then the flag foundTheId() becomes true.
64+
// Next, we simply set a test to say if(we found a ID from Pair) then break out of the loop
65+
if(entityPairs.at(i).foundTheId())
66+
break;
67+
}
68+
// Once broken out of the loop simply return the _attributeStringValue that was found and
69+
// set equal to locateId
70+
return locateId;
71+
}
72+
73+
74+
void EntityInstance::addPair(Pair &pair) {
75+
entityPairs.push_back(pair);
76+
}
77+
78+
int EntityInstance::numAttributes() {
79+
return entityPairs.size();
80+
}
81+
82+
// Function of DOUBLE datatype because we want to return a GPA
83+
// which is of DOUBLE datatype
84+
double EntityInstance::locateGpa() {
85+
// create a variable of double datatype to hold GPAs
86+
double locateGpa;
87+
// Next create a loop that can through through all pairs in the
88+
// entityPairs vector
89+
for (int i = 0; i < entityPairs.size(); i++) {
90+
// locateGpa() is a function inside of the Pair class
91+
// when it is called on it will return the _attributeNumberValue
92+
// at the selected pair
93+
locateGpa = entityPairs.at(i).locateGpa();
94+
// If the GPA is retrieved successfully, meaning its not 0
95+
// then we simply break out of the loop and return
96+
// the retrieved GPA
97+
if (locateGpa != 0)
98+
break;
99+
}
100+
// return the GPA > 0 that we just pulled from the Pair class
101+
return locateGpa;
102+
}
103+

EntityInstance.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by gmook on 4/7/2021.
3+
//
4+
5+
#ifndef P3P2_ENTITYINSTANCE_HPP
6+
#define P3P2_ENTITYINSTANCE_HPP
7+
8+
#include <string>
9+
#include <vector>
10+
#include "Pair.hpp"
11+
12+
class EntityInstance {
13+
public:
14+
void addPair(Pair &pair);
15+
int numAttributes();
16+
void print(int numSpaces, bool addComma);
17+
double locateGpa();
18+
std::string locateId();
19+
private:
20+
std::vector<Pair> entityPairs;
21+
};
22+
23+
#endif //P3P2_ENTITYINSTANCE_HPP

EntitySet.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by gmook on 4/2/2021.
3+
//
4+
#include <iostream>
5+
#include "EntitySet.hpp"
6+
7+
//Constructor
8+
EntitySet::EntitySet() {
9+
}
10+
11+
void EntitySet::setterForMatrix(std::vector<std::pair<std::string, double>> settingMatrix) {
12+
currentMatrix = settingMatrix;
13+
}
14+
15+
std::vector<std::pair<std::string, double>> EntitySet::getterForMatrix() {
16+
return currentMatrix;
17+
}
18+
19+
void EntitySet::addEntity(EntityInstance &entityInstance) {
20+
instances.push_back(entityInstance);
21+
}
22+
23+
void EntitySet::print() {
24+
std::cout << "[" << std::endl;
25+
for(int i = 0; i < instances.size(); i++) {
26+
int last = instances.size() - 1;
27+
if(i == last)
28+
instances.at(i).print(5,false);
29+
else
30+
instances.at(i).print(5, true);
31+
}
32+
std::cout << "]" << std::endl;
33+
}

EntitySet.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Created by gmook on 4/7/2021.
3+
//
4+
5+
#ifndef P3P2_ENTITYSET_HPP
6+
#define P3P2_ENTITYSET_HPP
7+
8+
9+
10+
#include <vector>
11+
#include "EntityInstance.hpp"
12+
13+
class EntitySet {
14+
public:
15+
EntitySet();
16+
void setterForMatrix(std::vector<std::pair<std::string, double>>);
17+
std::vector<std::pair<std::string, double>> getterForMatrix();
18+
void addEntity(EntityInstance &);
19+
void print();
20+
21+
private:
22+
std::vector<EntityInstance> instances;
23+
std::vector<std::pair<std::string, double>> currentMatrix;
24+
};
25+
26+
27+
#endif //P3P2_ENTITYSET_HPP

JSONParser.cpp

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//
2+
// Created by gmook on 4/2/2021.
3+
//
4+
5+
#include "JSONParser.hpp"
6+
7+
JSONParser::JSONParser(JSONTokenizer &tokenizer): tokenizer{tokenizer} {}
8+
9+
Pair JSONParser::parseAPair(){
10+
// Create a instance of JSONToken called token
11+
// and populate it with a token from tokenizer
12+
// using getToken()
13+
JSONToken token = tokenizer.getToken();
14+
// DO a check to see if the token received is of string datatype.
15+
// We know something is of string datatype because we are not reading the data
16+
// the tokenizer has already done that. Instead we are checking the tokens. Each time
17+
// a token has been made a boolean is changed alongside of its creation. For instance, when a "ID" token is found
18+
// it sets a boolean called retrieveString() to be true. Therefore, we can use these boolean flags to make checks
19+
// and further parse the data.
20+
if(!token.retrieveString()){
21+
// If not a string then exit
22+
// Everything below is part of a error message
23+
std::cout << "EXITING: parseAPair expected string, but instead received: " << std::endl;
24+
std::cout << "-----> ";
25+
token.print();
26+
std::cout << " <-----";
27+
exit(9);
28+
}
29+
// When the first IF statement does not hit that means a string is found. Therefore,
30+
// create a variable of string datatype that stores the string. A function has already
31+
// been built out inside of the token class to get this string and that function is fetchQuotedString()
32+
std::string firstPartOfPair = token.fetchQuotedString();
33+
// Get next token using getToken()
34+
token = tokenizer.getToken();
35+
if(!token.boolCreateColonJsonToken()){
36+
// If not a colon then exit
37+
// Everything below is part of a error message
38+
std::cout << "EXITING: parseAPair expected colon, but instead received: " << std::endl;
39+
std::cout << "-----> ";
40+
token.print();
41+
std::cout << " <-----";
42+
exit(9);
43+
}
44+
// Get next token using getToken()
45+
// Allows us to discard ":" since this is
46+
// not a token we have to save
47+
token = tokenizer.getToken();
48+
49+
// IF the token is a GPA
50+
if(token.gpaF()){
51+
// Create a variable of double datatype set it equal
52+
// to the GPA using a function from token that returns gpa
53+
double secondPartOfPair = token.grabGPA();
54+
// We already have the first value of the pair so we insert our
55+
// newly created double into the second value of the pair
56+
Pair pair(firstPartOfPair, secondPartOfPair);
57+
return pair;
58+
}
59+
//IF the token is a string
60+
else if(token.retrieveString()){
61+
// Create a new variable with a datatype string
62+
// use the function from token to retrieve the string and set
63+
// it equal to that string
64+
std::string secondPartOfPair = token.fetchQuotedString();
65+
// We already have the first value of the pair so we insert our
66+
// newly created string into the second value of the pair
67+
Pair pair(firstPartOfPair, secondPartOfPair);
68+
return pair;
69+
}
70+
else{
71+
std::cout << "EXITING: Neither a string or double datatype were found:";
72+
std::cout << "-----> ";
73+
token.print();
74+
std::cout << " <----- ";
75+
exit(9);
76+
}
77+
}
78+
79+
// This ENTIRE function was given by Professor
80+
// It is used as a model for parseAPair and parseJSONEntity
81+
// However, it is pretty much replicated in order
82+
// to make parseJSONEntity because it holds the same structure
83+
EntityInstance JSONParser::parseJSONObject() {
84+
// parseJSONObject is responsible for parsing a JSON object. As such,
85+
// the first token is expected to be an open brace.
86+
JSONToken token = tokenizer.getToken();
87+
if( ! token.boolCreateOpenCurlyJsonToken() ) {
88+
std::cout << "Error: JSONParser::parseJSONObject: Expected an open brace, but found" << std::endl;
89+
token.print();
90+
std::cout << "Terminating..." << std::endl;
91+
exit(1);
92+
}
93+
EntityInstance instance;
94+
do {
95+
Pair pair = parseAPair();
96+
instance.addPair(pair);
97+
token = tokenizer.getToken();
98+
} while( token.boolCreateCommaJsonToken() ); // after having read a pair, we expect a comma
99+
// So, the above loop terminates when we did find a comma.
100+
// that means we have parsed an entire object. In that case, token must contain
101+
// the close brace of that object.
102+
if( ! token.boolCreateClosedCurlyJsonToken() ) {
103+
std::cout << "Error: JSONParser::parseJSONObject: Expected a close brace, but found" << std::endl;
104+
token.print();
105+
std::cout << "Terminating..." << std::endl;
106+
exit(1);
107+
}
108+
109+
//**********MATRIX CREATION AID************
110+
//1.
111+
// Make a pair in the currentMatrix with ID and GPA
112+
// Create a variable of datatype string that stores the ID
113+
std::string existingID = instance.locateId();
114+
//2.
115+
// Create a variable of datatype double that stores GPA
116+
double existingGPA = instance.locateGpa();
117+
//1 & 2.
118+
// Push back a pair into the currentMatrix
119+
// std::make_pair MUST be used to create the pair of ID and GPA
120+
currentMatrix.push_back(std::make_pair(existingID, existingGPA));
121+
//******************************************
122+
return instance;
123+
}
124+
125+
// Similar to parseJSONObject()
126+
EntitySet JSONParser::parseJSONEntity(){
127+
JSONToken token = tokenizer.getToken();
128+
if(!token.boolCreateOpenSquareBracketJsonToken()){
129+
std::cout << "EXITING: parseJSONEntity expected open bracket, instead:" << std::endl;
130+
std::cout << "-----> ";
131+
token.print();
132+
std::cout << " <-----";
133+
exit(9);
134+
}
135+
EntitySet set;
136+
do{
137+
EntityInstance instance = parseJSONObject();
138+
set.addEntity(instance);
139+
token = tokenizer.getToken();
140+
} while(token.boolCreateCommaJsonToken());
141+
if(!token.boolCreateClosedSquareBracketJsonToken()){
142+
std::cout << "EXITING: parseJSONEntity expected close bracket, instead:" << std::endl;
143+
std::cout << "-----> ";
144+
token.print();
145+
std::cout << " <-----";
146+
exit(9);
147+
}
148+
set.setterForMatrix(currentMatrix);
149+
return set;
150+
}
151+
152+

JSONParser.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Created by gmook on 4/7/2021.
3+
//
4+
5+
#ifndef P3P2_JSONPARSER_HPP
6+
#define P3P2_JSONPARSER_HPP
7+
8+
#include <string>
9+
#include <iostream>
10+
#include "EntityInstance.hpp"
11+
#include "JSONTokenizer.hpp"
12+
#include "EntitySet.hpp"
13+
14+
class JSONParser {
15+
public:
16+
JSONParser(JSONTokenizer &tokenizer);
17+
Pair parseAPair();
18+
EntityInstance parseJSONObject();
19+
EntitySet parseJSONEntity();
20+
21+
private:
22+
JSONTokenizer &tokenizer;
23+
std::vector<std::pair<std::string,double>> currentMatrix;
24+
};
25+
26+
27+
#endif //P3P2_JSONPARSER_HPP

0 commit comments

Comments
 (0)