浏览代码

новые типы потоков, улучшен рендер

kpmy 8 年之前
父节点
当前提交
be71fef266
共有 6 个文件被更改,包括 142 次插入43 次删除
  1. 2 1
      bower.json
  2. 63 10
      components/agents.js
  3. 2 2
      components/nav.html
  4. 4 2
      components/root.html
  5. 70 28
      components/root.js
  6. 1 0
      index.html

+ 2 - 1
bower.json

@@ -34,7 +34,8 @@
     "vis": "^4.19.1",
     "angular-marked": "^1.2.2",
     "stochasm": "^0.5.0",
-    "simple-uuid": "^1.0.1"
+    "simple-uuid": "^1.0.1",
+    "should.js": "visionmedia/should.js#^3.3.1"
   },
   "resolutions": {
     "angular": "1.6.3",

+ 63 - 10
components/agents.js

@@ -1,7 +1,7 @@
 /**
  * Created by petry_000 on 07.05.2017.
  */
-angular.module('Qfe7').service("AgentBuilder", function () {
+angular.module('Qfe7').service("Agent", function () {
 
     function AgentBuilder0() {
         const a = this;
@@ -17,7 +17,8 @@ angular.module('Qfe7').service("AgentBuilder", function () {
         const defaultAgent = {
             type: 'male',
             age: 'mature',
-            skin: colors[Math.round(4 * Math.random())]
+            skin: colors[Math.round(4 * Math.random())],
+            supplies: []
         };
 
         a.agent = {
@@ -26,6 +27,7 @@ angular.module('Qfe7').service("AgentBuilder", function () {
             type: undefined,
             age: undefined,
             skin: undefined,
+            supplies: [],
             links: []
         };
 
@@ -49,40 +51,91 @@ angular.module('Qfe7').service("AgentBuilder", function () {
         setter('age', 'mature');
         setter('age', 'monster');
 
+        a.has = function (supply) {
+            a.agent.supplies.push(supply);
+            return a;
+        };
+
         a.build = function () {
+            if (!_.isEqual(a.agent.type, 'organization')) {
+                a.has('inner-energy');
+                a.has('food');
+            }
             return a.agent;
         }
     }
 
-    this.get = function () {
+    this.is = function () {
         return new AgentBuilder0();
     }
-}).service("LinkBuilder", function () {
+}).service("Link", function () {
 
     function LinkBuilder0(from) {
         const l = this;
 
+        var to = from;
+
         l.link = {
             _id: UUID.generate().toUpperCase(),
-            from: from._id,
+            from: {
+                id: from._id,
+                supply: undefined
+            },
             measure: undefined,
             value: undefined,
-            to: undefined,
-            supply: undefined
+            to: {
+                id: to._id,
+                supply: undefined
+            },
+        };
+
+        l.to = function (t) {
+            to = t;
+            l.link.to.id = t._id;
+            return l;
+        };
+
+        l.eat = function () {
+            from._id.should.eql(to._id);
+            _.extend(l.link, {
+                type: 'eat',
+                measure: 'kg/day',
+                value: 1,
+                to: {id: to._id, supply: 'inner-energy'},
+                from: {id: from._id, supply: 'food'}
+            });
+            return l;
         };
 
         l.life = function () {
+            from._id.should.eql(to._id);
             _.extend(l.link, {
                 type: 'life',
-                measure: 'kkal',
+                measure: 'kkal/day',
                 value: 2500,
-                to: l.link.from,
-                supply: 'inner-energy'
+                to: {id: to._id, supply: 'power'},
+                from: {id: from._id, supply: 'inner-energy'}
+            });
+            return l;
+        };
+
+        l.food = function (v) {
+            _.extend(l.link, {
+                type: 'food',
+                measure: 'kg/day',
+                value: v,
+                to: {id: to._id, supply: 'food'},
+                from: {id: from._id, supply: 'food'}
             });
             return l;
         };
 
         l.build = function () {
+            if (_.findIndex(from.supplies, s => l.link.from.supply) < 0)
+                throw new Error("no supply from");
+            if (_.findIndex(to.supplies, s => l.link.to.supply) < 0)
+                throw new Error("no supply to");
+
             from.links.push(l.link);
             return l.link;
         }

+ 2 - 2
components/nav.html

@@ -5,11 +5,11 @@
 </style>
 <div class="container">
     <div class="navbar-header">
-        <a class="navbar-brand">ГРАФ</a>
+        <a class="navbar-brand">GRAPH</a>
     </div>
     <div id="navbar" class="collapse navbar-collapse">
         <ul class="nav navbar-nav">
-            <li class="active"><a ui-sref="root">главная</a></li>
+            <li class="active"><a ui-sref="root">root</a></li>
         </ul>
     </div><!--/.nav-collapse -->
 </div>

+ 4 - 2
components/root.html

@@ -18,9 +18,11 @@
             <vis-network class="cat-graph" data="data" options="options" events="events"></vis-network>
         </div>
         <div class="col-md-3">
-            <button type="button" class="btn" ng-click="initAgents()">СОЗДАТЬ</button>
-            <button type="button" class="btn btn-warning" ng-click="resetAgents()">СБРОС</button>
+            <button type="button" class="btn" ng-click="initAgents()">NEW</button>
+            <button type="button" class="btn btn-primary" ng-click="doNodes()">UPDATE</button>
+            <button type="button" class="btn btn-warning" ng-click="resetAgents()">RESET</button>
             <p marked="data.description" ng-if="!_.isNull(data.description)"></p>
+            <label for="show-self">show self <input type="checkbox" id="show-self" ng-model="settings.self"/></label>
         </div>
     </div>
 </div>

+ 70 - 28
components/root.js

@@ -33,11 +33,16 @@ angular.module('Qfe7')
             return db.destroy();
         }
     })
-    .controller("RootController", function ($scope, $state, VisDataSet, AgentService, AgentBuilder, LinkBuilder) {
+    .controller("RootController", function ($scope, $state, VisDataSet, AgentService, Agent, Link) {
         $scope.err = undefined;
         function errorFn(err) {
             $scope.err = err;
         }
+
+        $scope.settings = {
+            self: true,
+        };
+
         $scope.options = {
             interaction: {
                 hover: true
@@ -49,19 +54,17 @@ angular.module('Qfe7')
             },
             "physics": {
                 "barnesHut": {
+                    "gravitationalConstant": -10000,
                     "centralGravity": 0.1,
-                    "springLength": 100,
-                    "springConstant": 0.03,
-                    "damping": 0.05,
-                    "avoidOverlap": 0.1
+                    "springLength": 400,
+                    "springConstant": 0.05,
+                    "damping": 0.4,
+                    "avoidOverlap": 0
                 },
-                "maxVelocity": 10,
-                "minVelocity": 0.5,
-                "timestep": 0.25
+                "minVelocity": 0.75
             }
         };
 
-
         $scope.data = {
             nodes: new VisDataSet(),
             edges: new VisDataSet(),
@@ -77,7 +80,6 @@ angular.module('Qfe7')
         }
 
         $scope.events = {
-
             selectNode: eventHandler(function (x) {
                 let node = $scope.data.nodes.get(x.nodes[0]);
                 $scope.data.description = JSON.stringify(node.agent);
@@ -90,24 +92,46 @@ angular.module('Qfe7')
         const shapes = {
             'male': 'triangleDown',
             'female': 'triangle',
-            'organization': 'database'
+            'organization': 'dot'
         };
 
         const sizes = {
             'child': {size: 15},
             'mature': {size: 30},
-            'monster': {font: {size: 60}}
+            'monster': {size: 100}
         };
 
         const links = {
             'life': {
                 color: 'red',
                 arrows: 'from'
+            },
+            'food': {
+                color: 'green',
+                arrows: 'to'
+            },
+            'eat': {
+                color: 'green',
+                arrows: 'to'
             }
         };
 
-        function doNodes() {
+        function getValueOf(link) {
+            switch (link.type) {
+                case 'life':
+                    return (link.value / 2500);
+                case 'food':
+                    return link.value;
+                case 'eat':
+                    return link.value;
+                default:
+                    return 1;
+            }
+        }
+
+        let doNodes = $scope.doNodes = function (x) {
             $scope.data.nodes.clear();
+            $scope.data.edges.clear();
             AgentService.load().then(al => {
                 Array.from(al).forEach(a => {
                     let node = {
@@ -123,41 +147,59 @@ angular.module('Qfe7')
                     $scope.data.nodes.add(node);
                 });
                 Array.from(al).forEach(a => {
-                    Array.from(a.links).forEach(l => {
+                    Array.from(a.links)
+                        .filter(l => {
+                            return $scope.settings.self || !(l.type == 'life' || l.type == 'eat');
+                        }).forEach(l => {
                         let link = {
                             id: l._id,
-                            from: l.from,
-                            to: l.to,
+                            from: l.from.id,
+                            to: l.to.id,
+                            value: getValueOf(l)
                         };
                         _.extend(link, links[l.type]);
                         $scope.data.edges.add(link)
                     })
-                })
+                });
             }, errorFn)
-        }
+        };
 
         doNodes();
 
         $scope.initAgents = function () {
-            let m = AgentBuilder.get().male().build();
-            LinkBuilder.for(m).life().build();
+            let m = Agent.is().male().build();
+            Link.for(m).life().build();
+            Link.for(m).eat().build();
+
+            let f = Agent.is().female().build();
+            Link.for(f).life().build();
+            Link.for(f).eat().build();
 
-            let f = AgentBuilder.get().female().build();
-            LinkBuilder.for(f).life().build();
+            let cm = Agent.is().male().child().build();
+            Link.for(cm).life().build();
+            Link.for(cm).eat().build();
 
-            let cm = AgentBuilder.get().male().child().build();
-            LinkBuilder.for(cm).life().build();
+            let cf = Agent.is().female().child().build();
+            Link.for(cf).life().build();
+            Link.for(cf).eat().build();
 
-            let cf = AgentBuilder.get().female().child().build();
-            LinkBuilder.for(cf).life().build();
+            Link.for(m).food(0.5).to(cm).build();
+            Link.for(m).food(0.5).to(cf).build();
+            Link.for(f).food(0.5).to(cm).build();
+            Link.for(f).food(0.5).to(cf).build();
 
-            let o = AgentBuilder.get().organization().build();
+            let o = Agent.is().organization().build();
 
-            AgentService.create([m, f, cm, cf, o]).then(doNodes, errorFn);
+            let l = Agent.is().organization().has('food').build();
+            Link.for(l).food(2).to(m).build();
+            Link.for(l).food(2).to(f).build();
+
+            AgentService.create([m, f, cm, cf, o, l]).then(doNodes, errorFn);
         };
 
         $scope.resetAgents = function () {
             AgentService.reset();
             window.location.reload();
         };
+
     });

+ 1 - 0
index.html

@@ -23,6 +23,7 @@
 <script src="bower_components/moment/moment.js"></script>
 <script src="bower_components/stochasm/lib/stochasm.js"></script>
 <script src="bower_components/simple-uuid/uuid.js"></script>
+<script src="bower_components/should.js/should.min.js"></script>
 
 <script src="bower_components/angular/angular.js"></script>
 <script src="bower_components/angular-aria/angular-aria.js"></script>