Comparing version 0.0.5-alpha to 0.1.0
@@ -36,2 +36,3 @@ module.exports = compile | ||
parts.push(generateActivateProperties(opts, constraints)) | ||
parts.push(generateDummyActivateProperties(opts, constraints)) | ||
@@ -54,3 +55,4 @@ if (!opts.withoutExport) { | ||
constraints[name] = { | ||
occurences: {} | ||
occurences: {}, | ||
tell: {} | ||
} | ||
@@ -123,2 +125,17 @@ | ||
function generateDummyActivateProperties (opts, constraints) { | ||
var parts = [] | ||
for (var constraintName in constraints) { | ||
for (var arity in constraints[constraintName].tell) { | ||
if (!constraints[constraintName].occurences.hasOwnProperty(arity)) { | ||
// dummy activator function needed | ||
parts.push('CHR.prototype._' + constraintName + '_' + arity + '_activate = function (constraint) {}') | ||
} | ||
} | ||
} | ||
return parts.join('\n') | ||
} | ||
function generateOccurenceProperties (opts, rule, constraints) { | ||
@@ -209,3 +226,3 @@ var parts = [] | ||
parts.push(rule.body.map(function (body) { | ||
return generateTell(opts, body) | ||
return generateTell(opts, body, constraints) | ||
}).map(indentBy(level)).join('\n')) | ||
@@ -280,3 +297,3 @@ } | ||
function generateTell (opts, body) { | ||
function generateTell (opts, body, constraints) { | ||
var expr = '' | ||
@@ -289,2 +306,5 @@ if (body.type === 'Constraint') { | ||
expr += ')' | ||
setTell(constraints, body) | ||
return expr | ||
@@ -302,2 +322,13 @@ } | ||
function setTell(constraints, c) { | ||
if (!constraints[c.name]) { | ||
constraints[c.name] = { | ||
occurences: {}, | ||
tell: {} | ||
} | ||
} | ||
constraints[c.name].tell[c.arity] = true | ||
} | ||
function generateExpression (opts, parameter) { | ||
@@ -310,2 +341,5 @@ if (parameter.type === 'Identifier') { | ||
} | ||
if (parameter.type === 'Literal') { | ||
return escape(parameter.value) | ||
} | ||
} | ||
@@ -343,5 +377,5 @@ | ||
if (parameter.type === 'Literal') { | ||
parts.push(indent(0)+'if ('+to+'['+i+'] !== '+escape(parameter.value)+') {') | ||
parts.push(indent(1)+ 'return') | ||
parts.push(indent(0)+'}') | ||
parts.push(indent(0) + 'if (' + to + '[' + i + '] !== ' + escape(parameter.value) + ') {') | ||
parts.push(indent(1) + 'return') | ||
parts.push(indent(0) + '}') | ||
return | ||
@@ -355,5 +389,5 @@ } | ||
function escape(val) { | ||
function escape (val) { | ||
if (typeof val === 'string') { | ||
return '"'+val+'"' | ||
return '"' + val + '"' | ||
} | ||
@@ -360,0 +394,0 @@ |
{ | ||
"name": "chr", | ||
"version": "0.0.5-alpha", | ||
"version": "0.1.0", | ||
"description": "Compile and run Constraint Handling Rules (CHR) in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# CHR.js | ||
Compile and run Constraint Handling Rules (CHR) in JavaScript | ||
Compile and run Constraint Handling Rules (CHR) in JavaScript. | ||
## Getting Started | ||
The [online version at chrjs.net](http://chrjs.net/) is the easiest way to generate a constraint solver. Just enter your Constraint Handling Rules, try adding some constraints, and download the generated solver code. | ||
## Install | ||
@@ -73,8 +77,10 @@ | ||
More example CHR scripts are provided in the project's `/examples` directory. | ||
More example CHR scripts are provided in the project's `/examples` directory or at [chrjs.net](http://chrjs.net/). | ||
## Background | ||
The implementation is based on the compilation schema presented in the paper [CHR for imperative host languages](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.149.8471) (2008; Peter Van Weert, Pieter Wuille, Tom Schrijvers, Bart Demoen). As of yet basically none of the mentioned optimizations have been implemented. | ||
CHR.js was realized as a project as part of the Masters programme in Computer Science at the University of Ulm. Its Project Report with additional information about its architecture can be found online: https://fnogatz.github.io/paper-now-chrjs/. | ||
A list of open points for improving can be found in the [wiki](https://github.com/fnogatz/CHR.js/wiki/Todo). | ||
The implementation is based on the compilation scheme presented in the paper [CHR for imperative host languages](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.149.8471) (2008; Peter Van Weert, Pieter Wuille, Tom Schrijvers, Bart Demoen). As of yet basically none of the mentioned optimizations have been implemented. | ||
A list of open points for improving can be found in the [wiki](https://github.com/fnogatz/CHR.js/wiki/Todo) and the [project report](https://fnogatz.github.io/paper-now-chrjs/#summary). |
@@ -18,3 +18,3 @@ module.exports = Constraint | ||
res += '(' | ||
res += this.args.join(',') | ||
res += this.args.map(escape).join(',') | ||
res += ')' | ||
@@ -24,1 +24,9 @@ } | ||
} | ||
function escape (val) { | ||
if (typeof val === 'string') { | ||
return '"' + val + '"' | ||
} | ||
return val | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
740378
23
14863
86