Skip to content

Commit

Permalink
update the query
Browse files Browse the repository at this point in the history
  • Loading branch information
syang0624 committed Jun 24, 2022
1 parent 5d92046 commit b7afd42
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 136 deletions.
74 changes: 0 additions & 74 deletions Supply-Chain-Analysis/db_scripts/queries/checkStocking.gsql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create query impactAnalysis(set<vertex<site>> affectedSites, uint maxDepth) for graph demo_graph syntax v2
create query impact_analysis(set<vertex<site>> affectedSites, uint maxDepth) for graph demo_graph syntax v2
{
/*
Start from multiple input affected sites, return the impacted site-product pairs. The impacted site-product pairs will be indentified from two paths, one is site-(delivery)-site relationship. Another one is site-(produce)-product-(use)-product relationship. Since not nessesarily all product belongs to a site is getting impacted. To calculte the subset we need infomation from both paths. That is the impacted product needs to use another impacted product delivered from a impacted site. This will require both delivery relationship and dependency relationship between products. Because a delivered product may not be used, a impacted product being used may not delivered from the very impacted site.
Expand All @@ -10,19 +10,19 @@ create query impactAnalysis(set<vertex<site>> affectedSites, uint maxDepth) for

typedef tuple<string siteName, string prodName> spPair;

SetAccum<spPair> @pairSet;
SetAccum<spPair> @pair_Set;

// products a site that has been affected
SetAccum<string> @affectedProd;
SetAccum<string> @affected_Prod;

// number of iteration
SumAccum<int> @@iteration = 0;

// storing the result for unfinished goods
SetAccum<edge> @@resultSet;
SetAccum<edge> @@result_Set;

// for pass along the edge info for visualization
SetAccum<edge> @edgeMsgSet;
SetAccum<edge> @edge_Msg_Set;

effectedSite = {affectedSites};

Expand All @@ -31,48 +31,48 @@ create query impactAnalysis(set<vertex<site>> affectedSites, uint maxDepth) for
// get the impacted products from the impacted sites
effectedProduct = select p
from effectedSite:s -(produce>:e)- product:p
where @@iteration == 0 or s.@affectedProd.contains(p.name) == true
accum p.@pairSet += spPair(s.name, p.name)
where @@iteration == 0 or s.@affected_Prod.contains(p.name) == true
accum p.@pair_Set += spPair(s.name, p.name)
// we assume all of the products got impacted from the input sites
,case when @@iteration == 0 then
s.@affectedProd += p.name
,@@resultSet += e
s.@affected_Prod += p.name
,@@result_Set += e
end
;

// from the impacted products get its downsteam products
downStreamProd = select p
from effectedProduct:ep -(usedBy>:e) - product:p
accum p.@pairSet += ep.@pairSet
,p.@edgeMsgSet += e
accum p.@pair_Set += ep.@pair_Set
,p.@edge_Msg_Set += e
;

// find the impacted sites from the previous effectedSite set
effectedSite = select st
from effectedSite:s -(deliver>:e) - site:st
accum
case when s.@affectedProd.contains(e.itemId) then
st.@pairSet += spPair(s.name,e.itemId)
,st.@edgeMsgSet += e
case when s.@affected_Prod.contains(e.itemId) then
st.@pair_Set += spPair(s.name,e.itemId)
,st.@edge_Msg_Set += e
end
;

// keep the impacted sites that are has deliveted imapcted product that is delivered
// from a upstream site, and they also use this produt in producting their product
effectedSite = select st
from effectedSite:st -(produce>:e) - product:p
where COUNT(st.@pairSet INTERSECT p.@pairSet) > 0
where COUNT(st.@pair_Set INTERSECT p.@pair_Set) > 0
accum
st.@affectedProd += p.name
,@@resultSet += e
,@@resultSet += st.@edgeMsgSet
,@@resultSet += p.@edgeMsgSet
st.@affected_Prod += p.name
,@@result_Set += e
,@@result_Set += st.@edge_Msg_Set
,@@result_Set += p.@edge_Msg_Set
post-accum
p.@pairSet.clear()
p.@pair_Set.clear()
;

@@iteration += 1;
end;

print @@resultSet;
print @@result_Set;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create query pricePrediction(set<string> input, int maxIteration, bool doUpdate) for graph demo_graph syntax v2
create query price_prediction(set<string> input, int maxIteration, bool doUpdate) for graph demo_graph syntax v2
{
/*
With a set of string as input, in the format of PRODUCT_NAME,NEW_PRICE, recalculate the new price for the connected BOM subgraph.
Expand All @@ -14,42 +14,42 @@ create query pricePrediction(set<string> input, int maxIteration, bool doUpdate)
*/

SumAccum<int> @@depth = 0;
MaxAccum<int> @maxDepth = 0;
MaxAccum<int> @max_Depth = 0;
OrAccum @visited_1 = false;

OrAccum @isStart = false;
OrAccum @is_Start = false;

MapAccum<string,float> @@result;
MapAccum<string,float> @mapping;
MapAccum<string,int> @amountMap;
MapAccum<string,int> @amount_Map;
OrAccum @visited = false;
SumAccum<float> @new_price;

MapAccum<vertex,double> @@inputMap;
SetAccum<vertex> @@startSet;
MapAccum<vertex,double> @@input_Map;
SetAccum<vertex> @@start_Set;

SetAccum<edge> @@displaySet;
SetAccum<vertex> @@displayV;
SetAccum<edge> @@display_Set;
SetAccum<vertex> @@display_V;

String input_key = "";
String input_val = "";

// parse the input strings and put them in to a vertex set
foreach str in input do
GetKeyVal(str, input_key, input_val);
@@inputMap += (to_vertex(input_key, "product") -> str_to_double(input_val));
@@startSet += to_vertex(input_key, "product");
@@input_Map += (to_vertex(input_key, "product") -> str_to_double(input_val));
@@start_Set += to_vertex(input_key, "product");
end;

// traverse from the input vertexe set
Start = {@@startSet};
Start = {@@start_Set};

Start1 = select s from Start:s
accum s.@isStart = true,
accum s.@is_Start = true,
s.@visited_1 = true,
s.@visited = true,
s.@new_price = @@inputMap.get(s)
post-accum s.price = @@inputMap.get(s)
s.@new_price = @@input_Map.get(s)
post-accum s.price = @@input_Map.get(s)
;

@@depth = 1;
Expand All @@ -62,23 +62,23 @@ create query pricePrediction(set<string> input, int maxIteration, bool doUpdate)
from Start1:o-(usedBy>)- product:u
post-accum
// when start product is on the path of other start product
case when u.@isStart == true then
u.@maxDepth += @@depth
case when u.@is_Start == true then
u.@max_Depth += @@depth
end,
// if first time visit just pass the depth along
case when u.@visited_1 == false then
u.@visited_1 = true
// keep the largest depth
else
u.@maxDepth += @@depth
u.@max_Depth += @@depth
end
having u.@maxDepth == 0;
having u.@max_Depth == 0;
@@depth += 1;
end;

// start from the input vertex set again
Start2 = select s from Start:s
where s.@maxDepth == 0;
where s.@max_Depth == 0;

// reset the depth
@@depth = 1;
Expand All @@ -93,7 +93,7 @@ create query pricePrediction(set<string> input, int maxIteration, bool doUpdate)
// search first
Start2 = select u
from Start2:o -(usedBy>)- product:u
where (u.@maxDepth == 0 or u.@maxDepth <= @@depth) and u.@visited == false ;
where (u.@max_Depth == 0 or u.@max_Depth <= @@depth) and u.@visited == false ;

// then do the calculation
Start2 = select o
Expand All @@ -104,15 +104,15 @@ create query pricePrediction(set<string> input, int maxIteration, bool doUpdate)
else
o.@mapping += (e.formula_order->u.price)
end
,o.@amountMap += (e.formula_order->e.useAmount)
,@@displaySet += e
,@@displayV += u
,o.@amount_Map += (e.formula_order->e.useAmount)
,@@display_Set += e
,@@display_V += u
post-accum
// dynamically evaluate the formula string stored as an attribute
string formula = o.formula,
foreach (key,val) in o.@mapping do
// replace the $1 string with the runtime value
formula = replaceStr(formula,"$"+key,to_string(val*o.@amountMap.get(key)))
formula = replaceStr(formula,"$"+key,to_string(val*o.@amount_Map.get(key)))
end,
o.@new_price = evaluate(formula,"double"),

Expand All @@ -127,9 +127,9 @@ create query pricePrediction(set<string> input, int maxIteration, bool doUpdate)
@@depth += 1;
end;

print @@displaySet;
print @@display_Set;

final = {@@displayV};
final = {@@display_V};

print final;
}
15 changes: 0 additions & 15 deletions Supply-Chain-Analysis/db_scripts/queries/showWholeGraph.gsql

This file was deleted.

15 changes: 15 additions & 0 deletions Supply-Chain-Analysis/db_scripts/queries/show_whole_graph.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
create query show_whole_graph () for graph demo_graph syntax v2
{
// print all vertexes and edges
SetAccum<edge> @@display_Set;

Start = {ANY};

print Start;

Start = select s
from Start:s-((prodOrder|prodStocking|usedBy>|produce>|deliver>):e)-:t
accum @@display_Set += e;

print @@display_Set;
}

0 comments on commit b7afd42

Please sign in to comment.