Search in sources :

Example 1 with Function

use of org.btrplace.safeplace.spec.term.func.Function in project scheduler by btrplace.

the class MyCstrSpecVisitor method resolveFunction.

private Function resolveFunction(Token t, List<Term> args) {
    Function f = symbols.getFunction(t.getText());
    if (f == null) {
        throw SpecException.unknownSymbol(filename, t);
    }
    Type[] expected = f.signature();
    if (expected.length != args.size()) {
        throw SpecException.badFunctionCall(filename, t, f, args);
    }
    for (int i = 0; i < expected.length; i++) {
        if (!expected[i].equals(args.get(i).type())) {
            throw SpecException.badFunctionCall(filename, t, f, args);
        }
    }
    return f;
}
Also used : Function(org.btrplace.safeplace.spec.term.func.Function)

Example 2 with Function

use of org.btrplace.safeplace.spec.term.func.Function in project scheduler by btrplace.

the class MyCstrSpecVisitor method visitCall.

@Override
public FunctionCall visitCall(@NotNull CstrSpecParser.CallContext ctx) {
    List<Term> ps = ctx.term().stream().map(t -> (Term<?>) visit(t)).collect(Collectors.toList());
    Function f = resolveFunction(ctx.ID().getSymbol(), ps);
    FunctionCall.Moment m = FunctionCall.Moment.ANY;
    if (ctx.BEGIN() != null) {
        m = FunctionCall.Moment.BEGIN;
    }
    return new FunctionCall(f, ps, m);
}
Also used : FunctionCall(org.btrplace.safeplace.spec.term.func.FunctionCall) org.btrplace.safeplace.spec.term(org.btrplace.safeplace.spec.term) CstrSpecParser(org.btrplace.safeplace.spec.antlr.CstrSpecParser) Function(org.btrplace.safeplace.spec.term.func.Function) Token(org.antlr.v4.runtime.Token) NotNull(org.antlr.v4.runtime.misc.NotNull) org.btrplace.safeplace.spec.prop(org.btrplace.safeplace.spec.prop) Collectors(java.util.stream.Collectors) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ArrayList(java.util.ArrayList) List(java.util.List) org.btrplace.safeplace.spec.type(org.btrplace.safeplace.spec.type) ValueAt(org.btrplace.safeplace.spec.term.func.ValueAt) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CstrSpecBaseVisitor(org.btrplace.safeplace.spec.antlr.CstrSpecBaseVisitor) Function(org.btrplace.safeplace.spec.term.func.Function) FunctionCall(org.btrplace.safeplace.spec.term.func.FunctionCall)

Example 3 with Function

use of org.btrplace.safeplace.spec.term.func.Function in project scheduler by btrplace.

the class SpecScanner method scan.

/**
 * @throws SpecException
 */
public List<org.btrplace.safeplace.spec.Constraint> scan() throws IllegalAccessException, InstantiationException, IOException {
    List<CoreConstraint> coreAnnots = Collections.synchronizedList(new ArrayList<>());
    List<Class<? extends Function>> funcs = Collections.synchronizedList(new ArrayList<>());
    scanner.matchClassesImplementing(Function.class, funcs::add);
    scanner.matchClassesWithAnnotation(CoreConstraint.class, c -> coreAnnots.add(c.getAnnotation(CoreConstraint.class)));
    scanner.matchClassesWithAnnotation(CoreConstraints.class, c -> {
        CoreConstraint[] x = c.getAnnotationsByType(CoreConstraint.class);
        coreAnnots.addAll(Arrays.asList(x));
    });
    scanner.matchClassesWithAnnotation(SideConstraint.class, c -> sides.add(new Side(c.getAnnotation(SideConstraint.class), (Class<? extends SatConstraint>) c)));
    scanner.scan(Runtime.getRuntime().availableProcessors() - 1);
    for (Class<? extends Function> f : funcs) {
        if (!f.equals(Constraint.class)) {
            functions.add(f.newInstance());
        }
    }
    scanner.matchClassesImplementing(Function.class, c -> {
        try {
            functions.add(c.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
    });
    List<org.btrplace.safeplace.spec.Constraint> cstrs = new ArrayList<>();
    for (CoreConstraint c : coreAnnots) {
        cstrs.add(parseCore2(c));
    }
    List<org.btrplace.safeplace.spec.Constraint> l = new ArrayList<>();
    for (Side s : resolveDependencies(sides)) {
        org.btrplace.safeplace.spec.Constraint c = parseSide(s, l);
        l.add(c);
    }
    cstrs.addAll(l);
    return cstrs;
}
Also used : CoreConstraint(org.btrplace.scheduler.CoreConstraint) SideConstraint(org.btrplace.model.constraint.SideConstraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) SideConstraint(org.btrplace.model.constraint.SideConstraint) Function(org.btrplace.safeplace.spec.term.func.Function) CoreConstraint(org.btrplace.scheduler.CoreConstraint)

Example 4 with Function

use of org.btrplace.safeplace.spec.term.func.Function in project scheduler by btrplace.

the class MyCstrSpecVisitor method visitCstrCall.

@Override
public ConstraintCall visitCstrCall(@NotNull CstrSpecParser.CstrCallContext ctx) {
    List<Term> ps = ctx.call().term().stream().map(t -> (Term<?>) visit(t)).collect(Collectors.toList());
    Function f = resolveFunction(ctx.call().ID().getSymbol(), ps);
    return new ConstraintCall(f, ps);
}
Also used : FunctionCall(org.btrplace.safeplace.spec.term.func.FunctionCall) org.btrplace.safeplace.spec.term(org.btrplace.safeplace.spec.term) CstrSpecParser(org.btrplace.safeplace.spec.antlr.CstrSpecParser) Function(org.btrplace.safeplace.spec.term.func.Function) Token(org.antlr.v4.runtime.Token) NotNull(org.antlr.v4.runtime.misc.NotNull) org.btrplace.safeplace.spec.prop(org.btrplace.safeplace.spec.prop) Collectors(java.util.stream.Collectors) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ArrayList(java.util.ArrayList) List(java.util.List) org.btrplace.safeplace.spec.type(org.btrplace.safeplace.spec.type) ValueAt(org.btrplace.safeplace.spec.term.func.ValueAt) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CstrSpecBaseVisitor(org.btrplace.safeplace.spec.antlr.CstrSpecBaseVisitor) Function(org.btrplace.safeplace.spec.term.func.Function)

Aggregations

Function (org.btrplace.safeplace.spec.term.func.Function)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Token (org.antlr.v4.runtime.Token)2 NotNull (org.antlr.v4.runtime.misc.NotNull)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 CstrSpecBaseVisitor (org.btrplace.safeplace.spec.antlr.CstrSpecBaseVisitor)2 CstrSpecParser (org.btrplace.safeplace.spec.antlr.CstrSpecParser)2 org.btrplace.safeplace.spec.prop (org.btrplace.safeplace.spec.prop)2 org.btrplace.safeplace.spec.term (org.btrplace.safeplace.spec.term)2 FunctionCall (org.btrplace.safeplace.spec.term.func.FunctionCall)2 ValueAt (org.btrplace.safeplace.spec.term.func.ValueAt)2 org.btrplace.safeplace.spec.type (org.btrplace.safeplace.spec.type)2 SatConstraint (org.btrplace.model.constraint.SatConstraint)1 SideConstraint (org.btrplace.model.constraint.SideConstraint)1 CoreConstraint (org.btrplace.scheduler.CoreConstraint)1