Exemplo n.º 1
0
 function addRequirement(link, idea) {
   if(!_.isArray(idea)) {
     // if there is only one value (the raw value)
     // then we can loop through the subgraphs and modify them directly
     sgs.forEach(function(sg) {
       sg.addEdge(t, link, sg.addVertex(subgraph.matcher.id, idea));
     });
   } else {
     // if there is a list of values, then we'll OR them together
     // for each sg, create a list of sg (one for each search value)
     // then reduce the ~matrix into one array
     sgs = sgs.map(function(sg) {
       return idea.map(function(id) {
         var copy = sg.copy();
         copy.addEdge(t, link, copy.addVertex(subgraph.matcher.id, id));
         return copy;
       });
     }).reduce(function(ret, list) { Array.prototype.push.apply(ret, list); return ret; }, []);
   }
 }
Exemplo n.º 2
0
  rooms.forEach(function(room) {
    var sg = new subgraph.Subgraph();

    // the values we want to update
    var roomHasGold = sg.addVertex(subgraph.matcher.similar, {unit: discrete.definitions.list.boolean});

    // configure the rest of the subgraph
    var currentRoom = sg.addVertex(subgraph.matcher.discrete,
      discrete.cast({value: room.id, unit: exports.idea('roomDefinition').id}));
    sg.addEdge(currentRoom, links.list.type_of, sg.addVertex(subgraph.matcher.id, exports.idea('room').id));
    sg.addEdge(currentRoom, links.list['wumpus_sense_hasGold'], roomHasGold);
    // find
    subgraph.search(sg);
    if(!sg.concrete) throw new Error('Cannot find room');

    // update the values
    // TODO log when the sensed value differs from the internal value
    sg.getIdea(roomHasGold).update(discrete.cast({value: room.hasGold, unit: discrete.definitions.list.boolean}));
  });
Exemplo n.º 3
0
  router.get('/tasks', function(req, res) {
    // build the query set

    var sgs = new subgraph.Subgraph();
    // the task
    var t = sgs.addVertex(subgraph.matcher.filler);
    // must by of type task
    sgs.addEdge(t, links.list.type_of, sgs.addVertex(subgraph.matcher.id, lwt_task));

    // convert the base into an array
    // all other params must add to the list
    sgs = [sgs];

    function addRequirement(link, idea) {
      if(!_.isArray(idea)) {
        // if there is only one value (the raw value)
        // then we can loop through the subgraphs and modify them directly
        sgs.forEach(function(sg) {
          sg.addEdge(t, link, sg.addVertex(subgraph.matcher.id, idea));
        });
      } else {
        // if there is a list of values, then we'll OR them together
        // for each sg, create a list of sg (one for each search value)
        // then reduce the ~matrix into one array
        sgs = sgs.map(function(sg) {
          return idea.map(function(id) {
            var copy = sg.copy();
            copy.addEdge(t, link, copy.addVertex(subgraph.matcher.id, id));
            return copy;
          });
        }).reduce(function(ret, list) { Array.prototype.push.apply(ret, list); return ret; }, []);
      }
    }

    // restrict to the children of a particular task
    if(req.query.hasOwnProperty('children')) {
      if(!_.isArray(req.query.children)) {
        // if no child is provided (empty/null/undefined query param),
        // then return root thoughts (these are children of the task idea)
        var parent = ideas.proxy(req.query.children || lwt_task);
        sgs.forEach(function (sg) {
          sg.addEdge(sg.addVertex(subgraph.matcher.id, parent), links.list.lm_wumpus_todo__child, t);
        });
      }
    }

    // restrict to children of a particular task, but do a recursive search
    if(req.query.hasOwnProperty('parent') && req.query.parent) {
      if(!_.isArray(req.query.parent)) {
        var some_parent = ideas.proxy(req.query.parent);
        sgs.forEach(function (sg) {
          sg.addEdge(sg.addVertex(subgraph.matcher.id, some_parent), links.list.lm_wumpus_todo__child, t, { transitive: true });
        });
      }
    }

    // restrict to tasks with a certain status
    if(req.query.hasOwnProperty('status')) {
      addRequirement(links.list.lm_wumpus_todo__status, req.query.status);
    }

    // restrict to tasks with a certain type
    if(req.query.hasOwnProperty('type')) {
      addRequirement(links.list.lm_wumpus_todo__type, req.query.type);
    }

    // restrict to tasks with a certain priority
    if(req.query.hasOwnProperty('priority')) {
      addRequirement(links.list.lm_wumpus_todo__priority, req.query.priority);
    }

    // restrict to tasks with a certain set of tags
    if(req.query.hasOwnProperty('tags') && req.query.tags) {
      var tagList = req.query.tags;
      // a single tag will come across as a string
      if(_.isString(tagList))
        tagList = [tagList];
      var tagIdeas = tags.getAsIdeas(tagList, false);
      // if there are less tags then we queried, then there are no matches
      if(tagList.length !== tagIdeas.length) {
        res.json({ list: [] });
        return;
      }
      tagIdeas.forEach(function(id) {
        addRequirement(links.list.lm_wumpus_todo__tag.opposite, id);
      });
    }

    // run the searches, reduce the results to a set, strip out the values
    // TODO lodash chain start/end
    var list = _.values(sgs
      .map(function(sg) { return subgraph.search(sg); })
      .reduce(function(set, next) { next.forEach(function(g) { set[g.getIdea(t).id] = g.getIdea(t); }); return set; }, {}));

    list = list.map(getTaskData);

    if(req.query.text) {
      var text = req.query.text.toLowerCase();
      list = list.filter(function(t) {
        return (t.name && t.name.toLowerCase().indexOf(text) !== -1) ||
          (t.description && t.description.toLowerCase().indexOf(text) !== -1) ||
          (t.resolution && t.resolution.toLowerCase().indexOf(text) !== -1);
      });
    }

    // return the list of task data
    res.json({ list: list });
  });