Skip to content

Instantly share code, notes, and snippets.

@scytacki
Created August 31, 2011 15:39
Show Gist options
  • Save scytacki/1183852 to your computer and use it in GitHub Desktop.
Save scytacki/1183852 to your computer and use it in GitHub Desktop.
Custom rules for photosynthesis
/*globals MySystem rules nodes suggestions*/
// sun is connected to chloroplast and/or sun is connected to plant then chloroplast
if(!(rules.objectAt(0).check(nodes) ||
(rules.objectAt(2).check(nodes) &&
rules.objectAt(3).check(nodes) &&
rules.objectAt(4).check(nodes)))){
suggestions.pushObject(rules.objectAt(0).get('suggestion'));
}
// sun is connected to chloroplast and/or sun is connected to plant then chloroplast with light energy
if(!(rules.objectAt(1).check(nodes) ||
(rules.objectAt(2).check(nodes) &&
rules.objectAt(5).check(nodes) &&
rules.objectAt(6).check(nodes)))){
suggestions.pushObject(rules.objectAt(1).get('suggestion'));
}
// run through all the rules from index 7 on up as normal
rules.slice(7).forEach( function (rule) {
if (!rule.check(nodes)) {
suggestions.pushObject(rule.get('suggestion'));
}
});
// Look for links that don't match one of the 'should' link rules
var links = MySystem.store.find(MySystem.Link),
notAllowedLink;
links = links.filter(function (link) {return link.isComplete();});
notAllowedLink = links.find(function (link) {
var positiveLinkRules = rules.filterProperty('hasLink').filter(function(rule){return !rule.get('not');});
var matchingRule = positiveLinkRules.find(function (rule) {
// need to handle the link check here, in the future this should be moved into the core code
var paletteItem = rule.paletteItem(rule.get('type')),
otherPaletteItem = rule.paletteItem(rule.get('otherNodeType'));
switch(rule.get('linkDirection')) {
case '-->':
return rule.checkLink(link, paletteItem, otherPaletteItem);
case '<--':
return rule.checkLink(link, otherPaletteItem, paletteItem);
case '---':
return rule.checkLink(link, paletteItem, otherPaletteItem) || rule.checkLink(link, otherPaletteItem, paletteItem);
default:
throw "Invalid linkDirection value for diagram rule.";
}
});
if(!matchingRule){ return YES };
});
if(notAllowedLink) {
suggestions.pushObject("You have one or more links that aren't allowed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment