-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (95 loc) · 3.42 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const {
EnapsoGraphDBClient
} = require("@innotrade/enapso-graphdb-client"); // connection data to the run GraphDB instance
const GRAPHDB_BASE_URL = "http://localhost:7200",
GRAPHDB_REPOSITORY = "RecipeBook",
GRAPHDB_USERNAME = "sharedUser",
GRAPHDB_PASSWORD = "user123",
GRAPHDB_CONTEXT_TEST = "http://www.semanticweb.org/simeon/ontologies/2023/0/untitled-ontology-2#";
const DEFAULT_PREFIXES = [
EnapsoGraphDBClient.PREFIX_OWL,
EnapsoGraphDBClient.PREFIX_RDF,
EnapsoGraphDBClient.PREFIX_RDFS,
EnapsoGraphDBClient.PREFIX_XSD,
EnapsoGraphDBClient.PREFIX_PROTONS,
{
prefix: "chef",
iri: "http://www.semanticweb.org/simeon/ontologies/2023/0/untitled-ontology-2#",
}
];
let graphDBEndpoint = new EnapsoGraphDBClient.Endpoint({
baseURL: GRAPHDB_BASE_URL,
repository: GRAPHDB_REPOSITORY,
prefixes: DEFAULT_PREFIXES
});
graphDBEndpoint.login(GRAPHDB_USERNAME, GRAPHDB_PASSWORD)
.then((result) => {
console.log("\x1b[36m", result);
}).catch((err) => {
console.log("\x1b[31m", err);
});
const express = require('express')
const app = express()
const cors = require('cors')
app.use(express.json())
app.use(cors())
app.use('/public', express.static('public'))
app.post('/available-recipes', function (req, res) {
graphDBEndpoint
.query(
fillAvailableRecipesQuery(req.body.basketName)
)
.then((result) => {
res.send(result.results.bindings);
})
.catch((err) => {
console.log(err);
});
});
app.post('/create/basket', function (req, res) {
graphDBEndpoint
.update(
req.body.query
)
.then((result) => {
res.send(result);
})
.catch((err) => {
console.log(err);
});
})
function fillAvailableRecipesQuery(basket) {
return `
SELECT ?recipe ?basket ?maxCookCount ?directions WHERE {
{
SELECT ?recipe ?basket ?directions (MIN(?cookCount) AS ?maxCookCount) (COUNT(*) as ?availableProductCount) WHERE {
?recipe chef:requires ?recipeContainer .
?recipeContainer chef:holds ?recipeProduct .
?recipe chef:directions ?directions .
?basket chef:contains ?basketContainer .
FILTER (?basket=chef:${basket}) .
?basketContainer chef:holds ?basketProduct .
FILTER (?recipeProduct=?basketProduct) .
?recipeContainer chef:measuringUnits ?recipeProductUnit .
?basketContainer chef:measuringUnits ?basketProductUnit .
FILTER (?recipeProductUnit=?basketProductUnit) .
?recipeContainer chef:quantity ?recipeQuantity .
?basketContainer chef:quantity ?basketQuantity .
FILTER (?basketQuantity >= ?recipeQuantity) .
BIND((?basketQuantity / ?recipeQuantity) AS ?cookCount)
}
GROUP BY ?recipe ?basket ?directions
} .
{
SELECT (?recipe AS ?requiredRecipe) (COUNT(*) as ?requiredProductCount) WHERE {
?recipe chef:requires ?recipeContainer .
?recipeContainer chef:holds ?product
}
GROUP BY ?recipe
} .
FILTER (?recipe = ?requiredRecipe) .
FILTER (?requiredProductCount <= ?availableProductCount)
}
`
}
app.listen(3000, () => console.log("\x1b[33m", "Server Started"))