So i am to create a mock state machine that works to take in an order file and a state machine file. This order file has information on the orders and the state in which its currently in and then transitioning to. So far as my program works now it can detect and process these transitions properly but then i am also to flag invalid lines or invalid transitions. So i would think that i would use an If statement in order to detect that if this given state or given transition compared to the current one is not detected then it should mark this line as invalid. The issue i'm having is exactly that though, my if statement which should do that for some reason prints out either nothing at all and acts like there aren't any invalid transitions which based on the given text file to test with there are. Or it will print out the same thing three times. It should be detecting that cancelled
and fulfilled
are terminal states or states that don't have any transitions and are final states and that any new lines with that same order number after should be flagged but these following lines don't even appear to be acknowledged. So i'm wondering why this else if
statement isnt working as intended. The trouble area is listed with the comments in all caps.
Code - ParseState.java
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import javax.sound.sampled.Line;
import com.fasterxml.jackson.databind.ObjectMapper;
import csi311.MachineSpec.StateTransitions;
public class ParseState {
public ParseState() {
}
public void run(String filename, String filename2) throws Exception {
System.out.println("Parse State");
HashMap<String, String> hmap = new HashMap<>();
HashMap<String, String> h2map = new HashMap<>();
ArrayList<String> flaggedids = new ArrayList<String>();
ArrayList<String> flaggedidz = new ArrayList<String>();
HashMap<String, String> h3map = new HashMap<>();
ArrayList<String> pendingids = new ArrayList<String>();
ArrayList<String> fulfilledids = new ArrayList<String>();
ArrayList<String> cancelledids = new ArrayList<String>();
ArrayList<String> submittedids = new ArrayList<String>();
ArrayList<String> backordered = new ArrayList<String>();
if (filename != null && filename2 != null) {
String json = processFile(filename);
MachineSpec machineSpec = parseJson(json);
dumpMachine(machineSpec);
String line = null;
String orderid = "\\d\\d\\d-[A-Za-z][A-Za-z][A-Za-z]-\\d\\d\\d\\d";
String timestamp = "\\S[0-9]{12}";
String customerid = "\\S[0-9]{8}";
String price = "\\S[0-9]{0,4}.[0-9]{2}";
String quantity ="\\d{1,4}";
String description = "\\w+";
BufferedReader brrr = new BufferedReader(new FileReader(filename2));
while((line = brrr.readLine()) != null) {
if(line.trim().isEmpty()) {
continue;
}
String [] lineSplit = line.split(",");
//System.out.println(lineSplit.length);
String pkgtimestmp = lineSplit[0].trim();
String pkgOrderId = lineSplit[1].trim();
String pkgCustId = lineSplit[2].trim();
String pkgState = lineSplit[3].trim();
String pkgOrderDesc = lineSplit[4].trim();
String pkgQuantity = lineSplit[5].trim();
String pkgPrice = lineSplit[6].trim();
// System.out.println(pkgtimestmp);
// System.out.println(pkgOrderId);
// System.out.println(pkgCustId);
// System.out.println(pkgState);
// System.out.println(pkgOrderDesc);
// System.out.println(pkgQuantity);
// System.out.println(pkgPrice);
if(lineSplit.length >= 2) {
//System.out.println(pkgOrderId);
if(pkgOrderId.matches(orderid)) {
//System.out.println(pkgOrderId);
// make it so that if same id placing order other order will be tracked too!
if(hmap.containsKey(pkgOrderId)) {
//System.out.println(pkgOrderId);
if(lineSplit.length == 7) {
//System.out.println(pkgOrderDesc);
if(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
&& pkgPrice.matches(price)) {
//System.out.println(pkgOrderId);
//System.out.println(pkgOrderId + ":" + pkgState);
String state1 = h3map.get(pkgOrderId);
//System.out.println(state1);
for(StateTransitions lt : machineSpec.getMachineSpec()) {
//System.out.println(state1);
//System.out.println(pkgState);
if(lt.getState().contains(state1.toLowerCase())) {
//System.out.println(state1);
//System.out.println(pkgState);
if(lt.getTransitions().contains(pkgState.toLowerCase())) {
//System.out.println(state1);
//System.out.println(pkgState);
h3map.put(pkgOrderId, pkgState);
}
// WONT PRINT ANYTHING FOR SOME REASON
else if(!lt.getTransitions().contains(pkgState.toLowerCase())) {
System.out.println(state1);
System.out.println(pkgState);
}
}
// PRINTS WAY TOO MUCH INFORMATION WAY TOO MANY TIMES
else if(!lt.getState().contains(state1.toLowerCase())) {
System.out.println(state1);
System.out.println(pkgState);
}
}
}
else if(!(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
&& pkgPrice.matches(price))){
flaggedids.add(pkgOrderId);
if(!flaggedidz.contains(pkgOrderId)) {
flaggedidz.add(pkgOrderId);
}
}
}
else if(!(lineSplit.length == 7)) {
flaggedids.add(pkgOrderId);
if(!flaggedidz.contains(pkgOrderId)) {
flaggedidz.add(pkgOrderId);
}
}
}
else if(!hmap.containsKey(pkgOrderId)) {
//System.out.println(pkgOrderId);
hmap.put(pkgOrderId, pkgCustId);
if(lineSplit.length == 7) {
//System.out.println(pkgOrderId);
if(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
&& pkgPrice.matches(price)) {
//System.out.println(pkgOrderId);
//System.out.println(pkgState);
for(StateTransitions gt : machineSpec.getMachineSpec()) {
//System.out.println(pkgState);
if(gt.getState().contains("start")) {
//System.out.println(pkgState);
if(gt.getTransitions().contains(pkgState.toLowerCase())) {
h3map.put(pkgOrderId, pkgState);
//System.out.println(h3map);
}
}
}
}
else if(!(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
&& pkgPrice.matches(price))){
flaggedids.add(pkgOrderId);
if(!flaggedidz.contains(pkgOrderId)) {
flaggedidz.add(pkgOrderId);
}
}
}
else if(!(lineSplit.length == 7)) {
//System.out.println(pkgOrderId);
flaggedids.add(pkgOrderId);
if(!flaggedidz.contains(pkgOrderId)) {
flaggedidz.add(pkgOrderId);
}
}
}
}
else if(!pkgOrderId.matches(orderid)){
if(!flaggedids.contains(pkgOrderId)) {
flaggedids.add(pkgOrderId);
}
if(!flaggedidz.contains(pkgOrderId)) {
flaggedidz.add(pkgOrderId);
}
}
}
}
for(int i = 0; i < flaggedids.size(); i++) {
System.out.println("Flagging order: " + flaggedids.get(i));
}
//System.out.println("this is" + hmap);
System.out.println("flagged " + flaggedidz.size());
System.out.println("final mapping" + h3map);
brrr.close();
}
}
private void dumpMachine(MachineSpec machineSpec) {
if (machineSpec == null) {
return;
}
for (StateTransitions st : machineSpec.getMachineSpec()) {
System.out.println(st.getState() + " : " + st.getTransitions());
}
}
private String processFile(String filename) throws Exception {
System.out.println("Processing file: " + filename);
BufferedReader br = new BufferedReader(new FileReader(filename));
String json = "";
String line;
while ((line = br.readLine()) != null) {
json += " " + line;
}
br.close();
// Get rid of special characters - newlines, tabs.
return json.replaceAll("\n", " ").replaceAll("\t", " ").replaceAll("\r", " ");
}
private MachineSpec parseJson(String json) {
ObjectMapper mapper = new ObjectMapper();
try {
MachineSpec machineSpec = mapper.readValue(json, MachineSpec.class);
return machineSpec;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ParseState theApp = new ParseState();
String filename = null;
String filename2 = null;
if (args.length > 0) {
filename = args[0];
filename2 = args[1];
}
try {
theApp.run(filename, filename2);
}
catch (Exception e) {
System.out.println("Something bad happened!");
e.printStackTrace();
}
}
}
CODE - MachineSpec.java
package csi311;
import java.io.Serializable;
import java.util.List;
@SuppressWarnings("serial")
public class MachineSpec implements Serializable {
// Since defined as an inner class, must be declared static or Jackson can't deal.
public static class StateTransitions implements Serializable {
private String state;
private List<String> transitions;
public StateTransitions() { }
public String getState() { return state; }
public void setState(String state) { this.state = state.toLowerCase(); }
public List<String> getTransitions() { return transitions; }
public void setTransitions(List<String> transitions) {
this.transitions = transitions;
if (this.transitions != null) {
for (int i = 0; i < transitions.size(); i++) {
transitions.set(i, transitions.get(i).toLowerCase());
}
}
}
}
private List<StateTransitions> machineSpec;
public MachineSpec() { }
public List<StateTransitions> getMachineSpec() { return machineSpec; }
public void setMachineSpec(List<StateTransitions> machineSpec) { this.machineSpec = machineSpec; }
}
ORDER FILE TO TEST
1543113695001, 001-ABC-4567, 123456789, PENDING , bagel toaster , 1, 19.95
1543113695002, 001-ABC-4567, 123456789, submitted, bagel toaster , 2, 39.90
1543113695003, 001-ABC-4567, 123456789, fulfilled, bagel toaster , 2, 39.90
1543113695004, 002-123-4567, 123456789, fulfilled, bagel , 1, 1.50
1543113695005, 003-AAA-4599, 123456799, PENDING , bagel toaster , 1, 19.95
1543113695006, 003-AAA-4599, 123456799, cancelled, bagel toaster , 1, 19.95
1543113695007, 003-AAA-4599, 123456799, fulfilled, bagel toaster , 1, 19.95
1543113695008, 004-AAA-4598, 123456799, pending, bagel toaster , 1, 19.95
1543113695009, 005-AAA-4597, 123456799, pending, bagel toaster , 1, 19.95
1543113695010, 005-AAA-4597, 123456799, cancelled, bagel toaster , 1, 19.95
1543113695011, 006-AAA-4597, abcdefghi, pending, bagel toaster , 1, 19.95
1543113695011, 006-AAA-4597, abcdefghi, pending, bagel toaster , 1, 19.95
1543113695012, 007-AAA-4597, 112233445, pending, bagel toaster , 1, 19.95
1543113695013, 007-AAA-4597, 112233445, submitted, bagel toaster , 1, 19.95
1543113695014, 007-AAA-4597, 112233445, backordered, bagel toaster , 1, 19.95
1543113695015, 007-AAA-4597, 112233445, fulfilled, bagel toaster , 1, 19.95
State Machine File
{ "machineSpec" : [
{ "state" : "START", "transitions" : ["pending", "submitted"] },
{ "state" : "pending", "transitions" : ["PENDING", "cancelled", "submitted"] },
{ "state" : "submitted", "transitions" : ["cancelled", "submitted", "backordered", "fulfilled"] },
{ "state" : "backordered", "transitions" : ["backordered" ,"cancelled", "fulfilled"] }
] }
Current Output
Parse State
Processing file: test.in
start : [pending, submitted]
pending : [pending, cancelled, submitted]
submitted : [cancelled, submitted, backordered, fulfilled]
backordered : [backordered, cancelled, fulfilled]
PENDING
submitted
PENDING
submitted
PENDING
submitted
submitted
fulfilled
submitted
fulfilled
submitted
fulfilled
PENDING
cancelled
PENDING
cancelled
PENDING
cancelled
cancelled
fulfilled
cancelled
fulfilled
cancelled
fulfilled
cancelled
fulfilled
pending
cancelled
pending
cancelled
pending
cancelled
pending
submitted
pending
submitted
pending
submitted
submitted
backordered
submitted
backordered
submitted
backordered
backordered
fulfilled
backordered
fulfilled
backordered
fulfilled
Flagging order: 002-123-4567
Flagging order: 006-AAA-4597
Flagging order: 006-AAA-4597
flagged 2
final mapping{004-AAA-4598=pending, 005-AAA-4597=cancelled, 001-ABC-4567=fulfilled, 003-AAA-4599=cancelled, 007-AAA-4597=fulfilled}
Aucun commentaire:
Enregistrer un commentaire