@Override public void initStrategy(ThreadContext context) { this.context = context; this.solutionStore = context.getGlobalContext().getSolutionStore(); GlobalContext globalContext = context.getGlobalContext(); if (globalContext.getSharedObject() == null) { isFirstThread = true; shared = new BfsSharedObject(globalContext.getThreadPool().getMaximumPoolSize()); globalContext.setSharedObject(shared); logger.info("Breadth-first exploration strategy is inited."); } else { shared = (BfsSharedObject) globalContext.getSharedObject(); } }
@Override public Set<BatchTransformationRule<?, ?>> getRules() { return globalContext.getTransformations(); }
/** * Stops the exploration asynchronously. It has no effect if the exploration is already terminated or not even * started. */ public void stopExplorationAsync() { if (globalContext.isDone()) { logger.info("Cannot stop exploration - design space exploration has already finished."); } else if (globalContext.isNotStarted()) { logger.info("Cannot stop exploration - design space exploration has not been started."); } else { globalContext.stopAllThreads(); } }
public synchronized void startAllThreads(ThreadContext originalThreadContext, IStrategyFactory strategyFactory) { while (canStartNewThread()) { tryStartNewThread(originalThreadContext, strategyFactory.createStrategy()); } }
AtomicBoolean isFirst = globalContext.getFirstThreadContextIniting(); AtomicBoolean isFirstReady = globalContext.getFirstThreadContextInited(); if (!isFirstReady.get()) { if (!isFirst.compareAndSet(false, true)) { stateCoder = getGlobalContext().getStateCoderFactory().createStateCoder(); stateCoder.init(model); stateCoder.createStateCode(); ConflictResolver activationOrderingCconflictResolver = globalContext.getConflictResolver(); dseConflictResolver = new DseConflictResolver(activationOrderingCconflictResolver, stateCoder); for (BatchTransformationRule<?, ?> tr : globalContext.getTransformations()) { ruleEngine.addRule(tr.getRuleSpecification(), (EventFilter<IPatternMatch>) tr.getFilter()); objectives = globalContext.getObjectives(); leveledObjectives = globalContext.getLeveledObjectives(); globalConstraints = globalContext.getGlobalConstraints(); IObjective[][] leveledObjectivesToCopy = globalContext.getLeveledObjectives(); leveledObjectives = new IObjective[leveledObjectivesToCopy.length][]; for (int i = 0; i < leveledObjectivesToCopy.length; i++) { for (IGlobalConstraint globalConstraint : globalContext.getGlobalConstraints()) { globalConstraints.add(globalConstraint.createNew()); globalContext.initVisualizersForThread(this);
private void initExploration(IStrategy strategy) { Preconditions.checkArgument(model != null, MODEL_NOT_YET_GIVEN); Preconditions.checkArgument(strategy != null, "A strategy must be given. Use the Strategies helper class."); Preconditions.checkState(!globalContext.getTransformations().isEmpty(), "At least one transformation rule must be added to start the exploration."); if (globalContext.getStateCoderFactory() == null) { if (getMetaModelPackages() == null || getMetaModelPackages().isEmpty()) { throw new DSEException("Cannot initialize state coder." + " Please specifiy the EPackages your model uses with addMetaModelPackage(EPackage)"); } globalContext.setStateCoderFactory(new SimpleStateCoderFactory(getMetaModelPackages())); } logger.info("DesignSpaceExplorer started exploration."); if (deepCopyModel) { globalContext.startFirstThread(strategy, model); } else { globalContext.startFirstThreadWithoutModelClone(strategy, model); } }
int threads = context.getGlobalContext().getThreadPool().getMaximumPoolSize(); so.barrierBeforeChildGeneration = new CyclicBarrier(threads); so.barrierAfterChildGeneration = new CyclicBarrier(threads); context.getGlobalContext().stopAllThreads(); so.barrierBeforeChildGeneration.await(); return;
public synchronized void newSolution(ThreadContext context) { solutionSaver.setSolutionsCollection(solutions); Fitness fitness = context.getLastFitness(); DesignSpaceManager dsm = context.getDesignSpaceManager(); Object id = dsm.getCurrentState(); IStateCoderFactory stateCoderFactory = context.getGlobalContext().getStateCoderFactory(); SolutionTrajectory solutionTrajectory = dsm.getTrajectoryInfo().createSolutionTrajectory(stateCoderFactory); solutionTrajectory.setFitness(fitness); if (acceptOnlyGoalSolutions && !fitness.isSatisifiesHardObjectives()) { unsavedSolutionCallbacks(context, solutionTrajectory); return; } boolean solutionSaved = solutionSaver.saveSolution(context, id, solutionTrajectory); if (solutionSaved) { enoughSolutions.solutionFound(context, solutionTrajectory); savedSolutionCallbacks(context, solutionTrajectory); if (enoughSolutions.enoughSolutions()) { context.getGlobalContext().stopAllThreads(); } } else { unsavedSolutionCallbacks(context, solutionTrajectory); } }
@Override public void run() { logger.info("Timeout, stopping threads..."); globalContext.stopAllThreads(); wasTimeout.set(true); } };
/** * Defines the maximum processing threads that the design space exploration can use. Note, that this is only * limiting the threads doing the actual calculation. By default this value will be set to the number of logical * processors (including HyperThreading) in the computer, reported by {@link Runtime#availableProcessors()}. * * @param maxNumberOfThreads * The number of maximum processing threads available to the design space exploration process. */ public void setMaxNumberOfThreads(int maxNumberOfThreads) { globalContext.getThreadPool().setMaximumPoolSize(maxNumberOfThreads); }
@Override public IDesignSpace getDesignSpace() { return globalContext.getDesignSpace(); }
/** * Adds a global constraint to the exploration process. Please see the {@link IGlobalConstraint} interface and its * implementations for details. * * @param constraint * The global constraint. * @see IGlobalConstraint */ public void addGlobalConstraint(IGlobalConstraint constraint) { globalContext.getGlobalConstraints().add(constraint); }
/** * Returns the number of distinct transitions the exploration process has discovered (but not necessarily traversed) * so far. * * @return the number of distinct transitions. */ public long getNumberOfTransitions() { return globalContext.getDesignSpace().getNumberOfTransitions(); }
public boolean checkGlobalConstraints() { for (IGlobalConstraint globalConstraint : globalContext.getGlobalConstraints()) { if (!globalConstraint.checkGlobalConstraint(this)) { return false; } } return true; }
/** * Stops the exploration and waits for termination. It has no effect if the exploration is already terminated or not * even started. */ public void stopExploration() { if (globalContext.isDone()) { logger.info("Cannot stop exploration - design space exploration has already finished."); } else if (globalContext.isNotStarted()) { logger.info("Cannot stop exploration - design space exploration has not been started."); } else { globalContext.stopAllThreads(); waitForTerminaition(); } }
@Override public void initStrategy(ThreadContext context) { this.context = context; dsm = context.getDesignSpaceManager(); initialPopulation = new HashSet<TrajectoryFitness>(populationSize); Objects.requireNonNull(objective, "Hard objective is missing for FixedPriorityInitialSelector."); Preconditions.checkState(objective.isHardObjective(), "Given objective is not hard objective for FixedPriorityInitialSelector."); for (BatchTransformationRule<?, ?> batchTransformationRule : context.getGlobalContext().getTransformations()) { if (!priorities.containsKey(batchTransformationRule)) { throw new IllegalStateException("Missing rule priority for FixedPriorityInitialSelector."); } } FixedPriorityConflictResolver fixedPriorityResolver = ConflictResolvers.createFixedPriorityResolver(); for (Entry<BatchTransformationRule<?, ?>, Integer> entry : priorities.entrySet()) { fixedPriorityResolver.setPriority(entry.getKey().getRuleSpecification(), entry.getValue()); } context.changeActivationOrdering(fixedPriorityResolver.createConflictSet()); objective.init(context); logger.info("FixedPriorityInitialSelector inited."); }
/** * Returns the number of distinct states the exploration process has visited so far. * * @return the number of distinct states. */ public long getNumberOfStates() { return globalContext.getDesignSpace().getNumberOfStates(); }
/** * Adds a {@link BatchTransformationRule}. * * @param rule * The transformationRule. */ public void addTransformationRule(BatchTransformationRule<?, ?> rule) { Preconditions.checkArgument(rule != null); for (BatchTransformationRule<?, ?> rule2 : globalContext.getTransformations()) { if (rule.getPrecondition().equals(rule2.getPrecondition())) { throw new DSEException( "Two transformation rule (" + rule.getName() + "; " + rule2.getName() + ") uses the same LHS VIATRA Query pattern (" + rule.getPrecondition().getFullyQualifiedName() + "), which may lead to hash collision." + " Please wrap the pattern with an other pattern with the 'find' keyword (or duplicate the code), and use that for one of the rules LHS."); } } globalContext.getTransformations().add(rule); }
public DesignSpaceManager(ThreadContext context) { this.context = context; model = context.getModel(); designSpace = context.getGlobalContext().getDesignSpace(); domain = context.getEditingDomain(); conflictSet = context.getConflictResolver().getLastCreatedConflictSet(); stateCoder = context.getStateCoder(); Object initialStateId = stateCoder.createStateCode(); designSpace.addState(null, null, initialStateId); activationCodes = context.getActivationCodesConflictSet(); this.trajectory = new TrajectoryInfo(initialStateId); logger.debug("DesignSpaceManager initialized with initial model: " + initialStateId); }