Skip to content

Commit

Permalink
Middle ground for correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
Rojods committed Dec 11, 2023
1 parent dd2641c commit 0316adc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,56 @@ default Optional<Javalin> standaloneModule(String[] args) {
.get("/info/is_caching", ctx -> ctx.result("true"))
.get("/decision/cache/exists",
ctx -> {
// cachedDecisionModels.stream()
// .filter(m -> m.globalMD5Hash()
// .map(hash -> Arrays.equals(hash, ctx.bodyAsBytes())).orElse(false))
// .findAny().ifPresentOrElse(m -> ctx.result("true"), () -> ctx.result("false"));
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
if (cachedDecisionModels.containsKey(bb)) {
// System.out.println("YES decision cache exists of "
// + Arrays.toString(ctx.bodyAsBytes()));
ctx.result("true");
if (ctx.isMultipartFormData()) {
if (cachedDecisionModels.values().stream().anyMatch(m -> m.category().equals(ctx.formParam("category")))) {
var parts = cachedDecisionModels.values().stream().map(DecisionModel::part).collect(Collectors.toSet());
for (var e : ctx.formParams("part")) {
// System.out.println("Checking if " + e + " is in parts for category " + ctx.formParam("category"));
if (parts.stream().noneMatch(s -> s.contains(e))) {
ctx.result("false");
return;
}
}
ctx.result("true");
} else {
ctx.result("false");
}
} else {
// System.out.println("NO decision cache exists of "
// + Arrays.toString(ctx.bodyAsBytes()));
ctx.result("false");
}
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
if (cachedDecisionModels.containsKey(bb)) {
// System.out.println("YES decision cache exists of "
// + Arrays.toString(ctx.bodyAsBytes()));
ctx.result("true");
} else {
// System.out.println("NO decision cache exists of "
// + Arrays.toString(ctx.bodyAsBytes()));
ctx.result("false");
}
}
})
.get("/design/cache/exists",
ctx -> {
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
if (cachedDesignModels.containsKey(bb)) {
ctx.result("true");
if (ctx.isMultipartFormData()) {
if (cachedDesignModels.values().stream().anyMatch(m -> m.category().equals(ctx.formParam("category")))) {
var elements = cachedDesignModels.values().stream().map(DesignModel::elements).collect(Collectors.toSet());
for (var e : ctx.formParams("elements")) {
System.out.println("Checking if " + e + " is in parts for category " + ctx.formParam("category"));
if (elements.stream().noneMatch(s -> s.contains(e))) {
ctx.result("false");
return;
}
}
ctx.result("true");
} else {
ctx.result("false");
}
} else {
ctx.result("false");
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
if (cachedDesignModels.containsKey(bb)) {
ctx.result("true");
} else {
ctx.result("false");
}
}
})
.get("/solved/cache/exists",
Expand Down Expand Up @@ -178,15 +206,15 @@ default Optional<Javalin> standaloneModule(String[] args) {
.put("/decision/cache/add",
ctx -> {
// System.out.println("Adding to decision cache: " + ctx.body());
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
OpaqueDecisionModel.fromJsonString(ctx.body()).ifPresentOrElse(opaque -> {
var bb = ByteBuffer.wrap(opaque.globalMD5Hash().get()); // TODO: fix possibl NPE later
fromOpaqueDecision(opaque).ifPresentOrElse(m -> {
System.out.println("Adding non-opaque to decision cache: "
+ m.globalMD5Hash().map(Arrays::toString).orElse("NO HASH"));
// System.out.println("Adding non-opaque to decision cache: "
// + m.globalMD5Hash().map(Arrays::toString).orElse("NO HASH"));
cachedDecisionModels.put(bb, m);
}, () -> {
System.out.println("Adding opaque to decision cache: "
+ opaque.globalMD5Hash().map(Arrays::toString).orElse("NO HASH"));
// System.out.println("Adding opaque to decision cache: "
// + opaque.globalMD5Hash().map(Arrays::toString).orElse("NO HASH"));
cachedDecisionModels.put(bb, opaque);
});
ctx.status(200);
Expand All @@ -204,7 +232,7 @@ default Optional<Javalin> standaloneModule(String[] args) {
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
OpaqueDesignModel.fromJsonString(ctx.body()).ifPresent(opaque -> {
fromOpaqueDesign(opaque).ifPresentOrElse(m -> {
System.out.println("Adding non opaque design model to cache: " + m.category());
// System.out.println("Adding non opaque design model to cache: " + m.category());
cachedDesignModels.put(bb, m);
}, () -> cachedDesignModels.put(bb, opaque));
});
Expand Down Expand Up @@ -307,6 +335,7 @@ default Optional<Javalin> standaloneModule(String[] args) {
var results = identification(designModels, decisionModels);
for (var result : results.identified()) {
result.globalMD5Hash().ifPresent(hash -> {
// System.out.println("Adding a %s decision model with hash %s to cache".formatted(result.category(), Arrays.toString(hash)));
cachedDecisionModels.put(ByteBuffer.wrap(hash), result);
});
}
Expand Down
2 changes: 1 addition & 1 deletion rust-blueprints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Module for StandaloneModule {
let mut identified_models = vec![];
for m in decision_models {
if let Some(refined) = m.downcast_ref::<OpaqueDecisionModel>().and_then(self.opaque_to_model) {
debug!("Refining a {}", refined.category());
// debug!("Refining a {}", refined.category());
identified_models.push(refined);
} else {
identified_models.push(m.clone());
Expand Down
17 changes: 13 additions & 4 deletions rust-orchestration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,16 @@ impl Module for ExternalServerModule {
design_models
.par_iter()
.filter(|m| {
let hash = m.global_md5_hash();
let mut form = Form::new();
form = form.text("category", m.category());
for e in m.elements() {
form = form.text("elements", e);
}
if let Ok(cache_url) = self.url.join("/design/cache/exists") {
return self
.client
.get(cache_url)
.body(hash)
.multipart(form)
.send()
.ok()
.and_then(|r| r.text().ok())
Expand All @@ -307,12 +311,17 @@ impl Module for ExternalServerModule {
decision_models
.par_iter()
.filter(|m| {
let hash = m.global_md5_hash();
let mut form = Form::new();
form = form.text("category", m.category());
for e in m.part() {
form = form.text("part", e);
}
// let hash = m.global_md5_hash();
if let Ok(cache_url) = self.url.join("/decision/cache/exists") {
return self
.client
.get(cache_url)
.body(hash)
.multipart(form)
.send()
.ok()
.and_then(|r| r.text().ok())
Expand Down

0 comments on commit 0316adc

Please sign in to comment.