예제 #1
0
  /**
   *
   *
   * <pre>
   * abstract LogicalBus
   *     realizedBy -> PhysicalBus 1..*
   *         [parent in this.realizes.ref]
   *
   * // Physical Connection Mediums
   * abstract PhysicalBus
   *     realizes -> LogicalBus 1..*
   *         [parent in this.realizedBy.ref]
   *
   * logBusA : LogicalBus
   * logBusB : LogicalBus
   *
   * physBusA : PhysicalBus ?
   * physBusB : PhysicalBus ?
   * physBusC : PhysicalBus ?
   *
   * [logBusA => physBusA]
   * [logBusA => physBusB]
   * [logBusA.realizedBy = (physBusA, physBusB)]
   *
   * [logBusB => physBusC]
   * [logBusB.realizedBy = (physBusC)]
   * </pre>
   */
  @Test(timeout = 60000)
  public void testInverse() {
    AstModel m = newModel();

    AstAbstractClafer logicalBus = m.addAbstract("LogicalBus");
    AstAbstractClafer physicalBus = m.addAbstract("PhysicalBus");

    AstConcreteClafer realizedBy =
        logicalBus.addChild("realizedBy").refToUnique(physicalBus).withCard(Many);
    AstConcreteClafer realizes =
        physicalBus.addChild("realizes").refToUnique(logicalBus).withCard(Many);

    realizedBy.addConstraint(in(joinParent($this()), joinRef(join(joinRef($this()), realizes))));
    realizes.addConstraint(in(joinParent($this()), joinRef(join(joinRef($this()), realizedBy))));

    AstConcreteClafer logBusA = m.addChild("logBusA").extending(logicalBus).withCard(Mandatory);
    AstConcreteClafer logBusB = m.addChild("logBusB").extending(logicalBus).withCard(Mandatory);

    AstConcreteClafer physBusA = m.addChild("physBusA").extending(physicalBus).withCard(Mandatory);
    AstConcreteClafer physBusB = m.addChild("physBusB").extending(physicalBus).withCard(Mandatory);
    AstConcreteClafer physBusC = m.addChild("physBusC").extending(physicalBus).withCard(Mandatory);

    m.addConstraint(implies(some(logBusA), some(physBusA)));
    m.addConstraint(implies(some(logBusA), some(physBusB)));
    m.addConstraint(
        equal(
            joinRef(join(global(logBusA), realizedBy)), union(global(physBusA), global(physBusB))));

    m.addConstraint(implies(some(logBusB), some(physBusC)));
    m.addConstraint(equal(joinRef(join(global(logBusB), realizedBy)), global(physBusC)));

    ClaferSolver solver = ClaferCompiler.compile(m, Scope.defaultScope(10));
    assertEquals(1, solver.allInstances().length);
  }
예제 #2
0
  /**
   *
   *
   * <pre>
   * abstract Eater
   *     Food 1..2
   *     Drink 1..2
   * Patron : Eater 2
   * </pre>
   */
  @Test(timeout = 60000)
  public void breakOnlyInheritedChildrenSwap() {
    AstModel model = newModel();

    AstAbstractClafer eater = model.addAbstract("Eater");
    AstConcreteClafer food = eater.addChild("Food").withCard(1, 2);
    AstConcreteClafer drink = eater.addChild("Drink").withCard(1, 2);
    AstConcreteClafer patron = model.addChild("Patron").extending(eater).withCard(2, 2);

    ClaferSolver solver = ClaferCompiler.compile(model, Scope.defaultScope(3));
    assertEquals(5, solver.allInstances().length);
  }
예제 #3
0
  /**
   *
   *
   * <pre>
   * abstract Service
   *     cpu: integer
   *     machine -> Machine // the -> means every service must be allocated to some machine
   *     [ this in machine.services ]
   *
   * MailService : Service
   *     [cpu = 8]
   *
   * SearchService : Service
   *     [cpu = 7]
   *
   * CalendarService : Service
   *     [cpu = 5]
   *
   * DriveService : Service
   *     [cpu = 4]
   *
   * GroupsService : Service
   *     [cpu = 5]
   *
   * abstract Machine
   *     services -> Service*
   *     [this.machine = parent]
   *     total_cpu : integer = sum services.cpu
   *     [total_cpu &lt;= this.cpuLimit]
   *     cpuLimit : integer
   *     isFree : integer = (if (#this.services = 0) then 1 else 0)
   *
   * GoogleCA : Machine
   *     [cpuLimit = 20]
   *
   * GoogleNY : Machine
   *     [cpuLimit = 10]
   *
   * GoogleTX : Machine
   *     [cpuLimit = 11]
   *
   * abstract Task
   *     total_free : integer = sum Machine.isFree
   *
   * MyTask: Task
   * &lt;&lt;max MyTask.total_free&gt;&gt;
   * </pre>
   */
  @Test(timeout = 60000)
  public void breakServices() {
    AstModel model = newModel();

    AstAbstractClafer c0_Service = model.addAbstract("Service");
    AstAbstractClafer c0_Machine = model.addAbstract("c0_Machine");
    AstAbstractClafer c0_Task = model.addAbstract("c0_Task");
    AstConcreteClafer c0_cpu = c0_Service.addChild("c0_cpu").withCard(1, 1);
    AstConcreteClafer c0_machine = c0_Service.addChild("c0_machine").withCard(1, 1);
    AstConcreteClafer c0_MailService =
        model.addChild("c0_MailService").withCard(1, 1).extending(c0_Service);
    AstConcreteClafer c0_SearchService =
        model.addChild("c0_SearchService").withCard(1, 1).extending(c0_Service);
    AstConcreteClafer c0_CalendarService =
        model.addChild("c0_CalendarService").withCard(1, 1).extending(c0_Service);
    AstConcreteClafer c0_DriveService =
        model.addChild("c0_DriveService").withCard(1, 1).extending(c0_Service);
    AstConcreteClafer c0_GroupsService =
        model.addChild("c0_GroupsService").withCard(1, 1).extending(c0_Service);
    AstConcreteClafer c0_services = c0_Machine.addChild("c0_services");
    AstConcreteClafer c0_total_cpu = c0_Machine.addChild("c0_total_cpu").withCard(1, 1);
    AstConcreteClafer c0_cpuLimit = c0_Machine.addChild("c0_cpuLimit").withCard(1, 1);
    AstConcreteClafer c0_isFree = c0_Machine.addChild("c0_isFree").withCard(1, 1);
    AstConcreteClafer c0_GoogleCA =
        model.addChild("c0_GoogleCA").withCard(1, 1).extending(c0_Machine);
    AstConcreteClafer c0_GoogleNY =
        model.addChild("c0_GoogleNY").withCard(1, 1).extending(c0_Machine);
    AstConcreteClafer c0_GoogleTX =
        model.addChild("c0_GoogleTX").withCard(1, 1).extending(c0_Machine);
    AstConcreteClafer c0_total_free = c0_Task.addChild("c0_total_free").withCard(1, 1);
    AstConcreteClafer c0_MyTask = model.addChild("c0_MyTask").withCard(1, 1).extending(c0_Task);
    c0_cpu.refTo(IntType);
    c0_machine.refToUnique(c0_Machine);
    c0_services.refToUnique(c0_Service);
    c0_total_cpu.refTo(IntType);
    c0_cpuLimit.refTo(IntType);
    c0_isFree.refTo(IntType);
    c0_total_free.refTo(IntType);
    c0_Service.addConstraint(
        in($this(), joinRef(join(joinRef(join($this(), c0_machine)), c0_services))));
    c0_MailService.addConstraint(equal(joinRef(join($this(), c0_cpu)), constant(8)));
    c0_SearchService.addConstraint(equal(joinRef(join($this(), c0_cpu)), constant(7)));
    c0_CalendarService.addConstraint(equal(joinRef(join($this(), c0_cpu)), constant(5)));
    c0_DriveService.addConstraint(equal(joinRef(join($this(), c0_cpu)), constant(4)));
    c0_GroupsService.addConstraint(equal(joinRef(join($this(), c0_cpu)), constant(5)));
    c0_Machine.addConstraint(
        equal(
            joinRef(join($this(), c0_total_cpu)),
            sum(join(joinRef(join($this(), c0_services)), c0_cpu))));
    c0_Machine.addConstraint(
        lessThanEqual(joinRef(join($this(), c0_total_cpu)), joinRef(join($this(), c0_cpuLimit))));
    c0_Machine.addConstraint(
        equal(
            joinRef(join($this(), c0_isFree)),
            ifThenElse(
                equal(card(join($this(), c0_services)), constant(0)), constant(1), constant(0))));
    c0_services.addConstraint(
        equal(joinRef(join(joinRef($this()), c0_machine)), joinParent($this())));
    c0_GoogleCA.addConstraint(equal(joinRef(join($this(), c0_cpuLimit)), constant(20)));
    c0_GoogleNY.addConstraint(equal(joinRef(join($this(), c0_cpuLimit)), constant(10)));
    c0_GoogleTX.addConstraint(equal(joinRef(join($this(), c0_cpuLimit)), constant(11)));
    c0_Task.addConstraint(
        equal(joinRef(join($this(), c0_total_free)), sum(join(global(c0_Machine), c0_isFree))));

    ClaferOptimizer solver =
        ClaferCompiler.compile(
            model,
            Scope.setScope(c0_services, 5)
                .setScope(c0_Machine, 4)
                .setScope(c0_Service, 5)
                .setScope(c0_Task, 1)
                .setScope(c0_cpu, 5)
                .setScope(c0_cpuLimit, 4)
                .setScope(c0_isFree, 4)
                .setScope(c0_machine, 5)
                .setScope(c0_total_cpu, 4)
                .setScope(c0_total_free, 1)
                .intLow(-128)
                .intHigh(128),
            Objective.maximize(joinRef(join(global(c0_MyTask), c0_total_free))));
    int count = 0;
    while (solver.find()) {
      assertArrayEquals(new int[] {1}, solver.optimalValues());
      count++;
    }
    assertEquals(7, count);
  }