示例#1
0
  @Test
  public void contextTest() {
    final CloudModel model =
        new CloudModel(DataManagerBuilder.create().withScheduler(new DirectScheduler()).build());
    model.connect(
        new KCallback<Throwable>() {
          @Override
          public void on(Throwable throwable) {
            if (throwable != null) {
              throwable.printStackTrace();
            } else {
              CloudUniverse universe = model.newUniverse();
              CloudView time0 = universe.time(0l);
              Node root = time0.createNode();

              // time0.setRoot(root, null);
              root.setName("root");
              Assert.assertEquals("root", root.getName());
              Node n1 = time0.createNode();
              n1.setName("n1");
              Node n2 = time0.createNode();
              n2.setName("n2");
              root.addChildren(n1);
              root.addChildren(n2);

              final int[] counter = {0};
              KModelContext context = model.createModelContext();
              context.listen(
                  new KCallback<long[]>() {
                    @Override
                    public void on(long[] o) {
                      counter[0]++;
                    }
                  });
              context.set(0, 0, 0, 0);
              Assert.assertEquals(counter[0], 1);
            }
          }
        });
  }
示例#2
0
  @Test
  public void badLookupTest() {
    final CloudModel model =
        new CloudModel(DataManagerBuilder.create().withScheduler(new DirectScheduler()).build());
    model.connect(
        new KCallback<Throwable>() {
          @Override
          public void on(Throwable throwable) {
            if (throwable != null) {
              throwable.printStackTrace();
            } else {
              CloudUniverse universe = model.newUniverse();
              CloudView time0 = universe.time(0l);
              Node root = time0.createNode();

              //   time0.setRoot(root, null);
              root.setName("root");
              Assert.assertEquals("root", root.getName());
              Node n1 = time0.createNode();
              n1.setName("n1");
              Node n2 = time0.createNode();
              n2.setName("n2");
              root.addChildren(n1);
              root.addChildren(n2);

              time0.lookup(
                  42,
                  new KCallback<KObject>() {
                    @Override
                    public void on(KObject kObject) {
                      Assert.assertNull(kObject);
                    }
                  });
            }
          }
        });
  }
示例#3
0
  public static void main(String[] args) {

    KMetaModel metaModel = new MetaModel("SmartCityMetaModel");
    KMetaClass metaClassCity = metaModel.addMetaClass("City");
    KMetaClass metaClassDistrict = metaModel.addMetaClass("District");

    metaClassCity.addAttribute("name", KPrimitiveTypes.STRING);

    metaClassDistrict.addAttribute("name", KPrimitiveTypes.STRING);
    metaClassDistrict.addAttribute("nbcitizen", KPrimitiveTypes.LONG);

    // create the reference districts from City to district with multiplicity 0..*
    metaClassCity.addRelation("districts", metaClassDistrict, null);

    KModel model =
        metaModel.createModel(
            DataManagerBuilder.create().withScheduler(new DirectScheduler()).build());
    model.connect(
        o -> {

          // Create reflexively a model object using the metaClass name
          KObject city = model.createByName("City", BASE_UNIVERSE, BASE_TIME);
          city.setByName("name", "MySmartCity");

          // Create reflexively a model object using the metaClass
          KObject district_1 = model.create(metaClassDistrict, BASE_UNIVERSE, BASE_TIME);
          district_1.setByName("name", "District_1");
          district_1.setByName("nbcitizen", 10000);

          // Create reflexively a model object using the metaClass
          KObject district_2 = model.createByName("District", BASE_UNIVERSE, BASE_TIME);
          district_2.setByName("name", "District_2");
          district_2.setByName("nbcitizen", 50000);

          // Add the two district to the City
          city.addByName("districts", district_1);
          city.addByName("districts", district_2);

          // Save the full model as JSON in the console
          model.universe(BASE_UNIVERSE).time(BASE_TIME).json().save(city, System.out::println);

          // Visiting all reachable objects from the city
          city.visit(
              elem -> {
                System.out.println("Visiting..." + elem.toJSON());
                return KVisitResult.CONTINUE;
              },
              o1 -> System.out.println("End of the visit"));

          // Visiting all attributes of an object
          city.visitAttributes(
              (metaAttribute, value) -> {
                System.out.println(
                    "City attribute "
                        + metaAttribute.metaName()
                        + ", type="
                        + metaAttribute.attributeTypeId()
                        + "="
                        + value);
              });

          // Finally any object have a UUID and can be retrieve from it
          long cityUUID = city.uuid();
          System.out.println("City uuid=" + cityUUID);
          model.lookup(
              BASE_UNIVERSE,
              BASE_TIME,
              cityUUID,
              new KCallback<KObject>() {
                @Override
                public void on(KObject resolvedObject) {
                  System.out.println("Resolved=" + resolvedObject.toJSON());
                }
              });
        });
  }
示例#4
0
  @Test
  public void helloTest() {
    CloudModel universe =
        new CloudModel(DataManagerBuilder.create().withScheduler(new DirectScheduler()).build());
    universe.connect(
        new KCallback() {
          @Override
          public void on(Object o) {

            CloudUniverse dimension0 = universe.newUniverse();

            Assert.assertNotNull(dimension0);
            CloudView t0 = dimension0.time(0l);

            Assert.assertNotNull(t0);
            Assert.assertEquals(t0.now(), 0l);
            Node nodeT0 = t0.createNode();
            Assert.assertNotNull(nodeT0);
            Assert.assertNotNull(nodeT0.uuid());
            // assertNotNull(nodeT0.path());
            Assert.assertNull(nodeT0.getName());
            // Assert.assertEquals("name=", nodeT0.domainKey());
            nodeT0.setName("node0");
            Assert.assertEquals("node0", nodeT0.getName());
            // Assert.assertEquals("name=node0", nodeT0.domainKey());
            Assert.assertEquals(0l, nodeT0.now());
            //        assertNull(nodeT0.parentPath());
            Element child0 = t0.createElement();
            // TODO reInsert following test
            // Assert.assertNotNull(child0.timeTree());
            // Assert.assertTrue(child0.timeTree().last().equals(0l));
            // Assert.assertTrue(child0.timeTree().first().equals(0l));

            Node nodeT1 = t0.createNode();
            nodeT1.setName("n1");

            nodeT0.addChildren(nodeT1);

            //        assertTrue(nodeT1.path().endsWith("/children[name=n1]"));

            final int[] i = {0};
            final int[] j = {0};
            nodeT0.getChildren(
                new KCallback<Node[]>() {
                  @Override
                  public void on(Node[] n) {
                    for (int k = 0; k < n.length; k++) {
                      i[0]++;
                    }
                  }
                });

            //        Assert.assertEquals(1, i[0]);
            Node nodeT3 = t0.createNode();
            nodeT3.setName("n3");
            nodeT1.addChildren(nodeT3);

            i[0] = 0;
            j[0] = 0;
            nodeT0.visit(
                new KModelVisitor() {
                  @Override
                  public KVisitResult visit(KObject elem) {
                    i[0]++;
                    return KVisitResult.CONTINUE;
                  }
                },
                new KCallback<Throwable>() {
                  @Override
                  public void on(Throwable t) {
                    j[0]++;
                  }
                });
            //      Assert.assertEquals(2, i[0]);
            //      Assert.assertEquals(1, j[0]);

            Assert.assertEquals(
                "{\"universe\":1,\"time\":0,\"uuid\":1,\"data\":{\"name\":\"node0\",\"children\":[4]}}",
                nodeT0.toJSON());
            Assert.assertEquals(
                "{\"universe\":1,\"time\":0,\"uuid\":1,\"data\":{\"name\":\"node0\",\"children\":[4]}}",
                nodeT0.toString());
          }
        });
  }