ewrarf :3
This commit is contained in:
commit
bd0065c635
6 changed files with 173 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules/
|
||||
|
22
package-lock.json
generated
Normal file
22
package-lock.json
generated
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "graphecs",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "graphecs",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bitecs": "^0.3.40"
|
||||
}
|
||||
},
|
||||
"node_modules/bitecs": {
|
||||
"version": "0.3.40",
|
||||
"resolved": "https://registry.npmjs.org/bitecs/-/bitecs-0.3.40.tgz",
|
||||
"integrity": "sha512-wAylY4pNfX8IeIH5phtwt1lUNtHKrkoSNrArI7Ris2Y4nEQWFIVvXdgAuqprEg9bq8Wolmlj0gVfeG6MFmtI2Q==",
|
||||
"license": "MPL-2.0"
|
||||
}
|
||||
}
|
||||
}
|
17
package.json
Normal file
17
package.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "graphecs",
|
||||
"version": "1.0.0",
|
||||
"main": "src/index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node src/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"bitecs": "^0.3.40"
|
||||
}
|
||||
}
|
43
src/index.js
Normal file
43
src/index.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
import { query } from './query.js'
|
||||
import {
|
||||
createWorld,
|
||||
Types,
|
||||
defineComponent,
|
||||
defineQuery,
|
||||
addEntity,
|
||||
addComponent,
|
||||
pipe,
|
||||
} from 'bitecs'
|
||||
|
||||
const Edge = { from: Types.eid, to: Types.eid }
|
||||
const Fox = defineComponent()
|
||||
const Knows = defineComponent(Edge)
|
||||
const Bot = defineComponent()
|
||||
|
||||
const components = {
|
||||
Fox,
|
||||
Bot,
|
||||
Knows
|
||||
}
|
||||
|
||||
const world = createWorld()
|
||||
world.components = components
|
||||
|
||||
const rown = addEntity(world)
|
||||
addComponent(world, Fox, rown)
|
||||
|
||||
const cynn = addEntity(world)
|
||||
addComponent(world, Bot, cynn)
|
||||
|
||||
const edge = addEntity(world)
|
||||
addComponent(world, Knows, edge)
|
||||
Knows.from[edge] = rown
|
||||
Knows.to[edge] = cynn
|
||||
|
||||
//const q = query(world, '(:Fox)')
|
||||
//q(world).forEach(eid => console.log(eid, 'has Fox!'))
|
||||
|
||||
const q2 = query(world, '(rawran:Fox)-[:Knows]->(cynnnnnnnn:Bot)')
|
||||
q2(world).forEach(eid => console.log(eid))
|
||||
|
||||
|
49
src/query.js
Normal file
49
src/query.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { defineQuery, hasComponent } from 'bitecs'
|
||||
/// danm this rules !!!
|
||||
const re = /\((?<fromName>\w+)?:(?<fromLabel>\w+)\)(-\[(?<edgeName>\w+)?:(?<edgeLabel>\w+)\]->\((?<toName>\w+)?:(?<toLabel>\w+)\))*/
|
||||
|
||||
const node = (name, label) => ({ name, label })
|
||||
|
||||
const parse = s => {
|
||||
const result = s.match(re)
|
||||
const groups = result.groups
|
||||
|
||||
return {
|
||||
from: node(groups.fromName || 'a', groups.fromLabel),
|
||||
to: node(groups.toName || 'b', groups.toLabel),
|
||||
edge: node(groups.edgeName, groups.edgeLabel)
|
||||
}
|
||||
}
|
||||
|
||||
export const query = (world, s) => {
|
||||
const meta = parse(s)
|
||||
const from = world.components[meta.from.label]
|
||||
const to = world.components[meta.to.label]
|
||||
const edge = world.components[meta.edge.label]
|
||||
|
||||
const fromQuery = defineQuery([from])
|
||||
const toQuery = defineQuery([to])
|
||||
const edgeQuery = defineQuery([edge])
|
||||
|
||||
return (world) => {
|
||||
const result = []
|
||||
|
||||
const fq = fromQuery(world)
|
||||
const tq = toQuery(world)
|
||||
const eq = edgeQuery(world)
|
||||
|
||||
for(const feid of fq) {
|
||||
const targets = []
|
||||
|
||||
for(const eeid of eq) {
|
||||
if(edge.from[eeid] === feid && hasComponent(world, to, edge.to[eeid])) {
|
||||
targets.push(edge.to[eeid])
|
||||
}
|
||||
}
|
||||
|
||||
result.push({ [feid]: targets })
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
40
src/result.js
Normal file
40
src/result.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
export class Result {
|
||||
constructor(value, error) {
|
||||
this.value = value
|
||||
this.error = error
|
||||
}
|
||||
|
||||
static Ok(value) {
|
||||
return new Result(value, null)
|
||||
}
|
||||
|
||||
static Err(error) {
|
||||
return new Result(null, error)
|
||||
}
|
||||
|
||||
isOk() {
|
||||
return this.error === null
|
||||
}
|
||||
|
||||
isErr() {
|
||||
return !this.isOk()
|
||||
}
|
||||
|
||||
map(fn) {
|
||||
if (this.isOk()) {
|
||||
return Result.Ok(fn(this.value))
|
||||
} else {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
flatMap(fn) {
|
||||
if (this.isOk()) {
|
||||
return fn(this.value)
|
||||
} else {
|
||||
return this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in a new issue