diff --git a/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java b/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java index e3a966f5df..73fc7af114 100644 --- a/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java +++ b/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java @@ -206,9 +206,8 @@ public void reset() { } public String getDefinedRng() { - if (GamaPreferences.External.CORE_RND_EDITABLE.getValue()) { + if (GamaPreferences.External.CORE_RND_EDITABLE.getValue()) return (String) ((ExperimentPlan) getSpecies()).parameters.get(IKeyword.RNG).value(ownScope); - } return GamaPreferences.External.CORE_RNG.getValue(); } @@ -225,9 +224,7 @@ public Double getDefinedSeed() { public void closeSimulations() { // We unschedule the simulation if any executer.executeDisposeActions(); - if (getSimulationPopulation() != null) { - getSimulationPopulation().dispose(); - } + if (getSimulationPopulation() != null) { getSimulationPopulation().dispose(); } if (!getSpecies().isBatch()) { ownScope.getGui().setSelectedAgent(null); ownScope.getGui().setHighlightedAgent(null); @@ -248,7 +245,7 @@ public void closeSimulations() { @Override public void dispose() { - if (dead) { return; } + if (dead) return; closeSimulations(); GAMA.releaseScope(ownScope); super.dispose(); @@ -261,10 +258,8 @@ public void dispose() { */ @Override public Object _init_(final IScope scope) { - if (scope.interrupted()) { return null; } - if (automaticallyCreateFirstSimulation()) { - createSimulation(getParameterValues(), scheduled); - } + if (scope.interrupted()) return null; + if (automaticallyCreateFirstSimulation()) { createSimulation(getParameterValues(), scheduled); } // We execute any behavior defined in GAML. super._init_(scope); return this; @@ -279,9 +274,7 @@ protected boolean automaticallyCreateFirstSimulation() { public boolean init(final IScope scope) { super.init(scope); final IOutputManager outputs = getOutputManager(); - if (outputs != null) { - outputs.init(scope); - } + if (outputs != null) { outputs.init(scope); } scope.getGui().getStatus(scope).informStatus("Experiment ready"); scope.getGui().updateExperimentState(scope); return true; @@ -294,7 +287,7 @@ public boolean init(final IScope scope) { */ @Override public Object primDie(final IScope scope) throws GamaRuntimeException { - if (dying) { return null; } + if (dying) return null; dying = true; getSpecies().getArchitecture().abort(scope); GAMA.closeExperiment(getSpecies()); @@ -304,7 +297,7 @@ public Object primDie(final IScope scope) throws GamaRuntimeException { public SimulationAgent createSimulation(final ParametersSet parameters, final boolean scheduleIt) { final IPopulation pop = getSimulationPopulation(); - if (pop == null) { return null; } + if (pop == null) return null; final ParametersSet ps = getParameterValues(); ps.putAll(parameters); final IList> list = GamaListFactory.create(Types.MAP); @@ -381,9 +374,8 @@ public void setGeometry(final IShape newGlobalGeometry) {} */ public List getDefaultParameters() { - if (!getSpecies().isHeadless() && !GamaPreferences.External.CORE_RND_EDITABLE.getValue()) { + if (!getSpecies().isHeadless() && !GamaPreferences.External.CORE_RND_EDITABLE.getValue()) return new ArrayList<>(); - } final List params = new ArrayList<>(); final String cat = getExperimentParametersCategory(); ExperimentParameter p = new ExperimentParameter(getScope(), getSpecies().getVar(IKeyword.RNG), @@ -422,9 +414,7 @@ public void setMinimumDuration(final Double d) { // d is in seconds, but the slider expects milleseconds // DEBUG.LOG("Minimum duration set to " + d); setMinimumDurationExternal(d); - if (initialMinimumDuration == null) { - initialMinimumDuration = d; - } + if (initialMinimumDuration == null) { initialMinimumDuration = d; } ownScope.getGui().updateSpeedDisplay(ownScope, currentMinimumDuration * 1000, false); } @@ -446,9 +436,7 @@ public Double getInitialMinimumDuration() { value = ExperimentAgent.MODEL_PATH, initializer = true) public String getWorkingPath() { - if (ownModelPath == null) { - ownModelPath = getModel().getWorkingPath() + "/"; - } + if (ownModelPath == null) { ownModelPath = getModel().getWorkingPath() + "/"; } return ownModelPath; } @@ -554,9 +542,7 @@ public Integer getUsage() { @setter (SimulationAgent.USAGE) public void setUsage(final Integer s) { Integer usage = s; - if (s == null) { - usage = 0; - } + if (s == null) { usage = 0; } getRandomGenerator().setUsage(usage); } @@ -592,7 +578,7 @@ public void setSimulations(final IList simulations) { @Override @getter (IKeyword.SIMULATION) public SimulationAgent getSimulation() { - if (getSimulationPopulation() != null) { return getSimulationPopulation().lastSimulationCreated(); } + if (getSimulationPopulation() != null) return getSimulationPopulation().lastSimulationCreated(); return null; } @@ -611,9 +597,9 @@ public void setOnUserHold(final boolean state) { @Override public IPopulation getPopulationFor(final ISpecies species) { - if (species == getModel()) { return getSimulationPopulation(); } + if (species == getModel()) return getSimulationPopulation(); final SimulationAgent sim = getSimulation(); - if (sim == null) { return IPopulation.createEmpty(species); } + if (sim == null) return IPopulation.createEmpty(species); return sim.getPopulationFor(species.getName()); } @@ -631,9 +617,7 @@ protected void postStep(final IScope scope) { executer.executeEndActions(); executer.executeOneShotActions(); final IOutputManager outputs = getOutputManager(); - if (outputs != null) { - outputs.step(scope); - } + if (outputs != null) { outputs.step(scope); } ownClock.step(this.ownScope); informStatus(); } @@ -714,37 +698,33 @@ public IExperimentAgent getExperiment() { public Object getGlobalVarValue(final String varName) { // First case: we have the variable inside the experiment. - if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) { + if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) return super.getGlobalVarValue(varName); - } // Second case: the simulation is not null, so it should handle it final SimulationAgent sim = getSimulation(); - if (sim != null && !sim.dead()) { return sim.getScope().getGlobalVarValue(varName); } + if (sim != null && !sim.dead()) return sim.getScope().getGlobalVarValue(varName); // Third case, the simulation is null but the model defines this variable (see #2044). We then grab its // initial value if possible // However, if the experiment is defined with keep_simulations: false, we should not give access to the // value, as no simulations can be made available (see #2727) if (this.getModel().getSpecies().hasVar(varName)) { final IVariable var = getModel().getSpecies().getVar(varName); - if (!var.isNotModifiable() && !getExperiment().getSpecies().keepsSimulations()) { + if (!var.isNotModifiable() && !getExperiment().getSpecies().keepsSimulations()) throw GamaRuntimeException.error("This experiment does not keep its simulations. " + varName + " cannot be retrieved in this context", this); - } return var.getInitialValue(this); } // Fourth case: this is a parameter, so we get it from the species - if (getSpecies().hasParameter(varName)) { - return getSpecies().getExperimentScope().getGlobalVarValue(varName); - } + if (getSpecies().hasParameter(varName)) return getSpecies().getExperimentScope().getGlobalVarValue(varName); // Fifth case: it is an extra parameter return extraParametersMap.get(varName); } @Override public boolean hasAccessToGlobalVar(final String varName) { - if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) { return true; } - if (this.getModel().getSpecies().hasVar(varName)) { return true; } - if (getSpecies().hasParameter(varName)) { return true; } + if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) return true; + if (this.getModel().getSpecies().hasVar(varName)) return true; + if (getSpecies().hasParameter(varName)) return true; return extraParametersMap.containsKey(varName); } @@ -773,13 +753,13 @@ public void setAgentVarValue(final IAgent a, final String name, final Object val @Override public Object getAgentVarValue(final IAgent a, final String varName) { - if (a == ExperimentAgent.this) { return getGlobalVarValue(varName); } + if (a == ExperimentAgent.this) return getGlobalVarValue(varName); return super.getAgentVarValue(a, varName); } @Override public IGui getGui() { - if (getSpecies().isHeadless()) { return GAMA.getHeadlessGui(); } + if (getSpecies().isHeadless()) return GAMA.getHeadlessGui(); return GAMA.getRegularGui(); } @@ -790,10 +770,9 @@ public IGui getGui() { */ public Iterable getAllSimulationOutputs() { final SimulationPopulation pop = getSimulationPopulation(); - if (pop != null) { + if (pop != null) return Iterables.filter(Iterables.concat(Iterables.transform(pop, each -> each.getOutputManager()), Collections.singletonList(getOutputManager())), each -> each != null); - } return Collections.EMPTY_LIST; } diff --git a/msi.gama.core/src/msi/gama/runtime/ExecutionScope.java b/msi.gama.core/src/msi/gama/runtime/ExecutionScope.java index 57a82aa866..9b6a91fd80 100644 --- a/msi.gama.core/src/msi/gama/runtime/ExecutionScope.java +++ b/msi.gama.core/src/msi/gama/runtime/ExecutionScope.java @@ -87,7 +87,7 @@ void clear() { } public void copyFrom(final SpecialContext specialContext) { - if (specialContext == null) { return; } + if (specialContext == null) return; each = specialContext.each; graphics = specialContext.graphics; topology = specialContext.topology; @@ -115,9 +115,7 @@ public ExecutionScope(final ITopLevelAgent root, final String otherName, final I final AgentExecutionContext agentContext, final SpecialContext specialContext) { String name = "Scope #" + ++SCOPE_NUMBER; setRoot(root); - if (root != null) { - name += " of " + root.stringValue(root.getScope()); - } + if (root != null) { name += " of " + root.stringValue(root.getScope()); } name += otherName == null || otherName.isEmpty() ? "" : " (" + otherName + ")"; this.scopeName = name; this.executionContext = context == null ? ExecutionContext.create(this) : context.createCopy(); @@ -127,7 +125,7 @@ public ExecutionScope(final ITopLevelAgent root, final String otherName, final I public AgentExecutionContext createChildContext(final IAgent agent) { return AgentExecutionContext.create(agent, agentContext); - }; + } /** * Method clear() @@ -136,13 +134,9 @@ public AgentExecutionContext createChildContext(final IAgent agent) { */ @Override public void clear() { - if (executionContext != null) { - executionContext.dispose(); - } + if (executionContext != null) { executionContext.dispose(); } executionContext = null; - if (agentContext != null) { - agentContext.dispose(); - } + if (agentContext != null) { agentContext.dispose(); } agentContext = null; additionalContext.clear(); currentSymbol = null; @@ -213,14 +207,14 @@ public boolean _root_interrupted() { @Override public boolean isOnUserHold() { final ITopLevelAgent root = getRoot(); - if (root == null) { return false; } + if (root == null) return false; return root.isOnUserHold(); } @Override public void setOnUserHold(final boolean state) { final ITopLevelAgent root = getRoot(); - if (root == null) { return; } + if (root == null) return; root.setOnUserHold(state); } @@ -255,7 +249,7 @@ public synchronized boolean push(final IAgent agent) { } // get rid of the previous context **important** agentContext = null; - } else if (a == agent) { return false; } + } else if (a == agent) return false; agentContext = createChildContext(agent); return true; } @@ -272,7 +266,7 @@ protected void setRoot(final ITopLevelAgent agent) { // @Override @Override public void pop(final IAgent agent) { - if (agentContext == null) { throw GamaRuntimeException.warning("Agents stack is empty", this); } + if (agentContext == null) throw GamaRuntimeException.warning("Agents stack is empty", this); final AgentExecutionContext previous = agentContext; agentContext = agentContext.getOuterContext(); previous.dispose(); @@ -297,9 +291,7 @@ public void push(final ISymbol statement) { @Override public void setCurrentSymbol(final ISymbol statement) { currentSymbol = statement; - if (_trace) { - writeTrace(); - } + if (_trace) { writeTrace(); } } /** @@ -351,7 +343,7 @@ public ISymbol getCurrentSymbol() { */ @Override public ExecutionResult execute(final IExecutable statement, final IAgent agent, final Arguments args) { - if (statement == null || agent == null || interrupted() || agent.dead()) { return FAILED; } + if (statement == null || agent == null || interrupted() || agent.dead()) return FAILED; // We keep the current pushed agent (context of this execution) final IAgent caller = this.getAgent(); // We then try to push the agent on the stack @@ -359,9 +351,7 @@ public ExecutionResult execute(final IExecutable statement, final IAgent agent, try (StopWatch w = GAMA.benchmark(this, statement)) { // Otherwise we compute the result of the statement, pushing the // arguments if the statement expects them - if (args != null) { - args.setCaller(caller); - } + if (args != null) { args.setCaller(caller); } // See issue #2815: we also push args even if they are null statement.setRuntimeArgs(this, args); // We push the caller to the remote sequence (will be cleaned when the remote sequence leaves its scope) @@ -372,45 +362,35 @@ public ExecutionResult execute(final IExecutable statement, final IAgent agent, return ExecutionResult.FAILED; } finally { // We clean the caller that may have been set previously so as to keep the arguments clean - if (args != null) { - args.setCaller(null); - } + if (args != null) { args.setCaller(null); } // Whatever the outcome, we pop the agent from the stack if it has // been previously pushed - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @Override public void stackArguments(final Arguments actualArgs) { - if (actualArgs == null) { return; } + if (actualArgs == null) return; boolean callerPushed = false; final IAgent caller = actualArgs.getCaller(); - if (caller != null) { - callerPushed = push(caller); - } + if (caller != null) { callerPushed = push(caller); } try { actualArgs.forEachFacet((a, b) -> { final IExpression e = b.getExpression(); - if (e != null) { - addVarWithValue(a, e.value(ExecutionScope.this)); - } + if (e != null) { addVarWithValue(a, e.value(ExecutionScope.this)); } return true; }); } finally { - if (callerPushed) { - pop(caller); - } + if (callerPushed) { pop(caller); } } } @Override public ExecutionResult step(final IStepable agent) { - if (agent == null || interrupted()) { return FAILED; } + if (agent == null || interrupted()) return FAILED; try (StopWatch w = GAMA.benchmark(this, agent)) { return withValue(agent.step(this)); } catch (final Throwable ex) { @@ -427,7 +407,7 @@ public ExecutionResult step(final IStepable agent) { @Override public ExecutionResult init(final IStepable agent) { - if (agent == null || interrupted()) { return FAILED; } + if (agent == null || interrupted()) return FAILED; try (StopWatch w = GAMA.benchmark(this, agent)) { return withValue(agent.init(this)); } catch (final Throwable ex) { @@ -444,7 +424,7 @@ public ExecutionResult init(final IStepable agent) { @Override public ExecutionResult step(final IAgent agent) { - if (agent == null || agent.dead() || interrupted()) { return FAILED; } + if (agent == null || agent.dead() || interrupted()) return FAILED; final boolean pushed = push(agent); try { try (StopWatch w = GAMA.benchmark(this, agent)) { @@ -460,15 +440,13 @@ public ExecutionResult step(final IAgent agent) { } } } finally { - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @Override public ExecutionResult init(final IAgent agent) { - if (agent == null || agent.dead() || interrupted()) { return FAILED; } + if (agent == null || agent.dead() || interrupted()) return FAILED; final boolean pushed = push(agent); try { try (StopWatch w = GAMA.benchmark(this, agent)) { @@ -484,15 +462,13 @@ public ExecutionResult init(final IAgent agent) { } } } finally { - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @Override public ExecutionResult evaluate(final IExpression expr, final IAgent agent) throws GamaRuntimeException { - if (agent == null || agent.dead() || interrupted()) { return FAILED; } + if (agent == null || agent.dead() || interrupted()) return FAILED; final boolean pushed = push(agent); try { try (StopWatch w = GAMA.benchmark(this, agent)) { @@ -508,9 +484,7 @@ public ExecutionResult evaluate(final IExpression expr, final IAgent agent) thro } } } finally { - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @@ -521,7 +495,7 @@ public ExecutionResult evaluate(final IExpression expr, final IAgent agent) thro */ @Override public Object getVarValue(final String varName) { - if (executionContext != null) { return executionContext.getTempVar(varName); } + if (executionContext != null) return executionContext.getTempVar(varName); return null; } @@ -532,9 +506,7 @@ public Object getVarValue(final String varName) { */ @Override public void setVarValue(final String varName, final Object val) { - if (executionContext != null) { - executionContext.setTempVar(varName, val); - } + if (executionContext != null) { executionContext.setTempVar(varName, val); } } /** @@ -544,9 +516,7 @@ public void setVarValue(final String varName, final Object val) { */ @Override public void saveAllVarValuesIn(final Map varsToSave) { - if (executionContext != null) { - varsToSave.putAll(executionContext.getLocalVars()); - } + if (executionContext != null) { varsToSave.putAll(executionContext.getLocalVars()); } } /** @@ -556,9 +526,7 @@ public void saveAllVarValuesIn(final Map varsToSave) { */ @Override public void removeAllVars() { - if (executionContext != null) { - executionContext.clearLocalVars(); - } + if (executionContext != null) { executionContext.clearLocalVars(); } } /** @@ -568,9 +536,7 @@ public void removeAllVars() { */ @Override public void addVarWithValue(final String varName, final Object val) { - if (executionContext != null) { - executionContext.putLocalVar(varName, val); - } + if (executionContext != null) { executionContext.putLocalVar(varName, val); } } /** @@ -601,9 +567,8 @@ public Object getEach() { */ @Override public Object getArg(final String string, final int type) throws GamaRuntimeException { - if (executionContext != null) { + if (executionContext != null) return Types.get(type).cast(this, executionContext.getLocalVar(string), null, false); - } return null; } @@ -639,7 +604,7 @@ public final String getStringArg(final String name) throws GamaRuntimeException */ @Override public boolean hasArg(final String name) { - if (executionContext != null) { return executionContext.hasLocalVar(name); } + if (executionContext != null) return executionContext.hasLocalVar(name); return false; } @@ -650,14 +615,12 @@ public boolean hasArg(final String name) { */ @Override public Object getAgentVarValue(final IAgent agent, final String name) throws GamaRuntimeException { - if (agent == null || agent.dead() || interrupted()) { return null; } + if (agent == null || agent.dead() || interrupted()) return null; final boolean pushed = push(agent); try { return agent.getDirectVarValue(ExecutionScope.this, name); } finally { - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @@ -669,20 +632,18 @@ public Object getAgentVarValue(final IAgent agent, final String name) throws Gam */ @Override public void setAgentVarValue(final IAgent agent, final String name, final Object v) { - if (agent == null || agent.dead() || interrupted()) { return; } + if (agent == null || agent.dead() || interrupted()) return; final boolean pushed = push(agent); try { agent.setDirectVarValue(ExecutionScope.this, name, v); } finally { - if (pushed) { - pop(agent); - } + if (pushed) { pop(agent); } } } @Override public ExecutionResult update(final IAgent a) { - if (a == null || a.dead() || interrupted()) { return FAILED; } + if (a == null || a.dead() || interrupted()) return FAILED; final boolean pushed = push(a); try { a.getPopulation().updateVariables(this, a); @@ -691,9 +652,7 @@ public ExecutionResult update(final IAgent a) { GAMA.reportAndThrowIfNeeded(this, g, true); return FAILED; } finally { - if (pushed) { - pop(a); - } + if (pushed) { pop(a); } } } @@ -704,16 +663,15 @@ public ExecutionResult update(final IAgent a) { */ @Override public Object getGlobalVarValue(final String name) throws GamaRuntimeException { - final ITopLevelAgent root = getRoot(); - if (root == null) { return null; } + if (root == null) return null; return root.getDirectVarValue(this, name); } @Override public boolean hasAccessToGlobalVar(final String name) { final ITopLevelAgent root = getRoot(); - if (root == null) { return false; } + if (root == null) return false; return root.hasAttribute(name); } @@ -725,7 +683,7 @@ public boolean hasAccessToGlobalVar(final String name) { @Override public void setGlobalVarValue(final String name, final Object v) throws GamaRuntimeException { final ITopLevelAgent root = getRoot(); - if (root == null) { return; } + if (root == null) return; root.setDirectVarValue(this, name, v); } @@ -753,7 +711,7 @@ public String toString() { @Override public ITopology getTopology() { final ITopology topology = additionalContext.topology; - if (topology != null) { return topology; } + if (topology != null) return topology; final IAgent a = getAgent(); return a == null ? null : a.getTopology(); } @@ -797,7 +755,7 @@ public IGraphics getGraphics() { */ @Override public IAgent getAgent() { - if (agentContext == null) { return null; } + if (agentContext == null) return null; return agentContext.getAgent(); } @@ -809,14 +767,14 @@ public IAgent getAgent() { @Override public SimulationAgent getSimulation() { final ITopLevelAgent root = getRoot(); - if (root == null) { return null; } + if (root == null) return null; return root.getSimulation(); } @Override public IExperimentAgent getExperiment() { final ITopLevelAgent root = getRoot(); - if (root == null) { return null; } + if (root == null) return null; return root.getExperiment(); } @@ -828,7 +786,7 @@ public IExperimentAgent getExperiment() { @Override public IModel getModel() { final ITopLevelAgent root = getRoot(); - if (root == null) { return null; } + if (root == null) return null; return getRoot().getModel(); } @@ -849,7 +807,7 @@ public IType getType(final String name) { @Override public SimulationClock getClock() { final ITopLevelAgent root = getRoot(); - if (root == null) { return null; } + if (root == null) return null; return root.getClock(); } @@ -857,7 +815,7 @@ public SimulationClock getClock() { public IAgent[] getAgentsStack() { try (final Collector.AsOrderedSet agents = Collector.getOrderedSet()) { AgentExecutionContext current = agentContext; - if (current == null) { return new IAgent[0]; } + if (current == null) return new IAgent[0]; while (current != null) { agents.add(current.getAgent()); current = current.getOuterContext(); @@ -899,7 +857,7 @@ public Map peekReadAttributes() { @Override public IGui getGui() { - if (additionalContext.gui != null) { return additionalContext.gui; } + if (additionalContext.gui != null) return additionalContext.gui; final IExperimentAgent experiment = getExperiment(); if (experiment == null) { additionalContext.gui = GAMA.getGui(); @@ -923,7 +881,7 @@ public boolean isPaused() { final IExperimentPlan plan = exp.getSpecies(); if (plan != null) { final IExperimentController controller = plan.getController(); - if (controller != null) { return controller.getScheduler().paused || isOnUserHold(); } + if (controller != null) return controller.getScheduler().paused || isOnUserHold(); } } return isOnUserHold(); @@ -937,7 +895,7 @@ public boolean isPaused() { @Override public RandomUtils getRandom() { final ITopLevelAgent root = getRoot(); - if (root == null) { return new RandomUtils(); } + if (root == null) return new RandomUtils(); return root.getRandomGenerator(); } diff --git a/msi.gama.core/src/msi/gaml/expressions/GamlExpressionFactory.java b/msi.gama.core/src/msi/gaml/expressions/GamlExpressionFactory.java index 4dd1849f51..c11f847557 100644 --- a/msi.gama.core/src/msi/gaml/expressions/GamlExpressionFactory.java +++ b/msi.gama.core/src/msi/gaml/expressions/GamlExpressionFactory.java @@ -87,9 +87,7 @@ public void resetParser() { public UnitConstantExpression createUnit(final Object value, final IType t, final String name, final String doc, final String deprecated, final boolean isTime, final String[] names) { final UnitConstantExpression exp = UnitConstantExpression.create(value, t, name, doc, isTime, names); - if (deprecated != null && !deprecated.isEmpty()) { - exp.setDeprecated(deprecated); - } + if (deprecated != null && !deprecated.isEmpty()) { exp.setDeprecated(deprecated); } return exp; } @@ -101,18 +99,18 @@ public ConstantExpression createConst(final Object val, final IType type) { @Override public SpeciesConstantExpression createSpeciesConstant(final IType type) { - if (type.getGamlType() != Types.SPECIES) { return null; } + if (type.getGamlType() != Types.SPECIES) return null; final SpeciesDescription sd = type.getContentType().getSpecies(); - if (sd == null) { return null; } + if (sd == null) return null; return new SpeciesConstantExpression(sd.getName(), type); } @Override public ConstantExpression createConst(final Object val, final IType type, final String name) { - if (type.getGamlType() == Types.SPECIES) { return createSpeciesConstant(type); } - if (type == Types.SKILL) { return new SkillConstantExpression((String) val, type); } - if (val == null) { return NIL_EXPR; } - if (val instanceof Boolean) { return (Boolean) val ? TRUE_EXPR : FALSE_EXPR; } + if (type.getGamlType() == Types.SPECIES) return createSpeciesConstant(type); + if (type == Types.SKILL) return new SkillConstantExpression((String) val, type); + if (val == null) return NIL_EXPR; + if (val instanceof Boolean) return (Boolean) val ? TRUE_EXPR : FALSE_EXPR; return new ConstantExpression(val, type, name); } @@ -123,28 +121,28 @@ public UnitConstantExpression getUnitExpr(final String unit) { @Override public IExpression createExpr(final IExpressionDescription ied, final IDescription context) { - if (ied == null) { return null; } + if (ied == null) return null; final IExpression p = ied.getExpression(); return p == null ? getParser().compile(ied, context) : p; } @Override public IExpression createExpr(final String s, final IDescription context) { - if (s == null || s.isEmpty()) { return null; } + if (s == null || s.isEmpty()) return null; return getParser().compile(StringBasedExpressionDescription.create(s), context); } @Override public IExpression createExpr(final String s, final IDescription context, final IExecutionContext additionalContext) { - if (s == null || s.isEmpty()) { return null; } + if (s == null || s.isEmpty()) return null; return getParser().compile(s, context, additionalContext); } @Override public Arguments createArgumentMap(final ActionDescription action, final IExpressionDescription args, final IDescription context) { - if (args == null) { return null; } + if (args == null) return null; return getParser().parseArguments(action, args.getTarget(), context, false); } @@ -190,12 +188,12 @@ public IExpression createMap(final Iterable elements) { public boolean hasOperator(final String op, final IDescription context, final EObject object, final IExpression... args) { // If arguments are invalid, we have no match - if (args == null || args.length == 0) { return false; } + if (args == null || args.length == 0) return false; for (final IExpression exp : args) { - if (exp == null) { return false; } + if (exp == null) return false; } // If the operator is not known, we have no match - if (!OPERATORS.containsKey(op)) { return false; } + if (!OPERATORS.containsKey(op)) return false; final IMap ops = OPERATORS.get(op); final Signature sig = new Signature(args).simplified(); // Does any known operator signature match with the signatue of the expressions ? @@ -211,16 +209,12 @@ public boolean hasOperator(final String op, final IDescription context, final EO public IExpression createOperator(final String op, final IDescription context, final EObject eObject, final IExpression... args) { final boolean isReverse = op.equals("reverse"); - if (isReverse) { - DEBUG.OUT(""); - } + if (isReverse) { DEBUG.OUT(""); } if (!hasOperator(op, context, eObject, args)) { final IMap ops = OPERATORS.get(op); final Signature userSignature = new Signature(args).simplified(); String msg = "No operator found for applying '" + op + "' to " + userSignature; - if (ops != null) { - msg += " (operators available for " + Arrays.toString(ops.keySet().toArray()) + ")"; - } + if (ops != null) { msg += " (operators available for " + Arrays.toString(ops.keySet().toArray()) + ")"; } context.error(msg, IGamlIssue.UNMATCHED_OPERANDS, eObject); return null; } @@ -235,10 +229,10 @@ public IExpression createOperator(final String op, final IDescription context, f final Signature[] matching = Iterables.toArray( filter(ops.keySet(), s -> originalUserSignature.matchesDesiredSignature(s)), Signature.class); final int size = matching.length; - if (size == 0) { + if (size == 0) // It is a varArg, we call recursively the method return createOperator(op, context, eObject, createList(args)); - } else if (size == 1) { + else if (size == 1) { // Only one choice userSignature = matching[0]; } else { @@ -285,9 +279,6 @@ public IExpression createAs(final IDescription context, final IExpression toCast private IExpression createDirectly(final IDescription context, final EObject eObject, final OperatorProto proto, final IExpression... args) { // We finally make an instance of the operator and init it with the arguments - if (proto.getName().equals("reverse")) { - DEBUG.OUT(""); - } final IExpression copy = proto.create(context, eObject, args); if (copy != null) { // We verify that it is not deprecated @@ -302,9 +293,8 @@ private IExpression createDirectly(final IDescription context, final EObject eOb @Override public IExpression createAction(final String op, final IDescription callerContext, final ActionDescription action, final IExpression call, final Arguments arguments) { - if (action.verifyArgs(callerContext, arguments)) { + if (action.verifyArgs(callerContext, arguments)) return new PrimitiveOperator(callerContext, action, call, arguments, call instanceof SuperExpression); - } return null; } diff --git a/msi.gama.core/src/msi/gaml/statements/LoopStatement.java b/msi.gama.core/src/msi/gaml/statements/LoopStatement.java index 4743a9f38e..48dca3b2e7 100644 --- a/msi.gama.core/src/msi/gaml/statements/LoopStatement.java +++ b/msi.gama.core/src/msi/gaml/statements/LoopStatement.java @@ -221,10 +221,9 @@ public void validate(final IDescription description) { // final IExpressionDescription step = description.getFacet(STEP); final IExpressionDescription cond = description.getFacet(WHILE); IExpressionDescription name = description.getFacet(NAME); - if (name != null && name.isConst() && name.toString().startsWith(INTERNAL)) { - name = null; - } - + if (name != null && name.isConst() && name.toString().startsWith(INTERNAL)) { name = null; } + // See Issue #3085 + if (name != null) { Assert.nameIsValid(description); } if (times != null) { if (over != null) { description.error("'times' and 'over' are not compatible", IGamlIssue.CONFLICTING_FACETS, TIMES, @@ -245,9 +244,7 @@ public void validate(final IDescription description) { description.error("'times' and 'to' are not compatible", IGamlIssue.CONFLICTING_FACETS, TIMES, TO); return; } - if (name != null) { - description.error("No variable should be declared", IGamlIssue.UNUSED, NAME); - } + if (name != null) { description.error("No variable should be declared", IGamlIssue.UNUSED, NAME); } } else if (over != null) { if (cond != null) { description.error("'over' and 'while' are not compatible", IGamlIssue.CONFLICTING_FACETS, OVER, @@ -263,9 +260,7 @@ public void validate(final IDescription description) { description.error("'over' and 'to' are not compatible", IGamlIssue.CONFLICTING_FACETS, OVER, TO); return; } - if (name == null) { - description.error("No variable has been declared", IGamlIssue.MISSING_NAME, OVER); - } + if (name == null) { description.error("No variable has been declared", IGamlIssue.MISSING_NAME, OVER); } } else if (cond != null) { if (from != null) { description.error("'while' and 'from' are not compatible", IGamlIssue.CONFLICTING_FACETS, WHILE, @@ -304,9 +299,7 @@ public static class LoopSerializer extends SymbolSerializer { @Override protected String serializeFacetValue(final SymbolDescription s, final String key, final boolean includingBuiltIn) { - if (key.equals(NAME)) { - if (s.hasFacet(TIMES) || s.hasFacet(WHILE)) { return null; } - } + if (key.equals(NAME)) { if (s.hasFacet(TIMES) || s.hasFacet(WHILE)) return null; } return super.serializeFacetValue(s, key, includingBuiltIn); } @@ -330,9 +323,7 @@ public void enterScope(final IScope scope) { // 25/02/14: Suppressed because already done in loopBody() : // super.enterScope(scope); - if (varName != null) { - scope.addVarWithValue(varName, null); - } + if (varName != null) { scope.addVarWithValue(varName, null); } } @Override @@ -348,9 +339,7 @@ public Object privateExecuteIn(final IScope scope) throws GamaRuntimeException { protected boolean loopBody(final IScope scope, final Object var, final Object[] result) { scope.push(this); - if (varName != null) { - scope.setVarValue(varName, var); - } + if (varName != null) { scope.setVarValue(varName, var); } result[0] = super.privateExecuteIn(scope); scope.pop(this); return !scope.interrupted(); @@ -372,12 +361,8 @@ class Bounded implements LoopExecuter { Bounded() throws GamaRuntimeException { final IScope scope = null; // final IScope scope = GAMA.obtainNewScope(); - if (from.isConst()) { - constantFrom = Cast.asInt(scope, from.value(scope)); - } - if (to.isConst()) { - constantTo = Cast.asInt(scope, to.value(scope)); - } + if (from.isConst()) { constantFrom = Cast.asInt(scope, from.value(scope)); } + if (to.isConst()) { constantTo = Cast.asInt(scope, to.value(scope)); } if (step == null) { stepDefined = false; constantStep = 1; @@ -401,9 +386,8 @@ public Object runIn(final IScope scope) throws GamaRuntimeException { if (s > 0) { if (!stepDefined) { s = -s; - } else { + } else return null; - } } for (int i = f, n = t - 1; i > n && loopBody(scope, i, result); i += s) {} } else { @@ -424,9 +408,7 @@ public Object runIn(final IScope scope) throws GamaRuntimeException { final Iterable list_ = !(obj instanceof IContainer) ? Cast.asList(scope, obj) : ((IContainer) obj).iterable(scope); for (final Object each : list_) { - if (!loopBody(scope, each, result)) { - break; - } + if (!loopBody(scope, each, result)) { break; } } return result[0]; } @@ -438,9 +420,7 @@ class Times implements LoopExecuter { private Integer constantTimes; Times() throws GamaRuntimeException { - if (times.isConst()) { - constantTimes = Types.INT.cast(null, times.getConstValue(), null, false); - } + if (times.isConst()) { constantTimes = Types.INT.cast(null, times.getConstValue(), null, false); } } @Override diff --git a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/expression/GamlExpressionCompiler.java b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/expression/GamlExpressionCompiler.java index edcdd34382..34ee66b522 100644 --- a/msi.gama.lang.gaml/src/msi/gama/lang/gaml/expression/GamlExpressionCompiler.java +++ b/msi.gama.lang.gaml/src/msi/gama/lang/gaml/expression/GamlExpressionCompiler.java @@ -153,7 +153,7 @@ public class GamlExpressionCompiler extends GamlSwitch implements I public IExpression compile(final IExpressionDescription s, final IDescription parsingContext) { // Cf. Issue 782. Returns the expression if an expression needs its // compiled version to be compiled. - if (s.isConst() || s == getCurrentExpressionDescription()) { return s.getExpression(); } + if (s.isConst() || s == getCurrentExpressionDescription()) return s.getExpression(); setCurrentExpressionDescription(s); final EObject o = s.getTarget(); if (o == null && s instanceof StringBasedExpressionDescription) { @@ -179,7 +179,7 @@ public IExpression compile(final String expression, final IDescription parsingCo try { IExpression result = constantSyntheticExpressions.get(expression); - if (result != null) { return result; } + if (result != null) return result; final EObject o = getEObjectOf(expression, tempContext); result = compile(o); if (result != null && result.isContextIndependant()) { @@ -193,16 +193,12 @@ public IExpression compile(final String expression, final IDescription parsingCo } private IExpression compile(final EObject s) { - if (s == null) { - // No error, since the null expressions come from previous (more + if (s == null) // No error, since the null expressions come from previous (more // focused) errors and not from the parser itself. return null; - } final IExpression expr = doSwitch(s); - if (expr != null && getContext() != null) { - getContext().document(s, expr); - } + if (expr != null && getContext() != null) { getContext().document(s, expr); } return expr; } @@ -212,9 +208,9 @@ private IExpression skill(final String name) { // KEEP private IExpression unary(final String op, final Expression e) { - if (op == null) { return null; } + if (op == null) return null; final IExpression expr = compile(e); - if (expr == null) { return null; } + if (expr == null) return null; if (op.equals(MY)) { final IVarDescriptionProvider desc = getContext().getDescriptionDeclaringVar(MYSELF); if (desc instanceof IDescription) { @@ -230,31 +226,27 @@ private IExpression unary(final String op, final Expression e) { } // The unary "unit" operator should let the value of its child pass // through - if (op.equals("°") || op.equals("#")) { return expr; } + if (op.equals("°") || op.equals("#")) return expr; if (op.equals("every") && expr instanceof ConstantExpression && expr.getGamlType() == Types.INT) { getContext().warning( "No unit provided. If this frequency concerns cycles, please use the #cycle unit. Otherwise use one of the temporal unit (#ms, #s, #mn, #h, #day, #week, #month, #year)", IGamlIssue.DEPRECATED, e); } - if (isSpeciesName(op)) { - return getFactory().createAs(getContext(), expr, getSpeciesContext(op).getSpeciesExpr()); - } + if (isSpeciesName(op)) return getFactory().createAs(getContext(), expr, getSpeciesContext(op).getSpeciesExpr()); // if ( isSkillName(op) ) { return factory.createOperator(AS, context, // e, expr, skill(op)); } return getFactory().createOperator(op, getContext(), e, expr); } private IExpression casting(final String type, final IExpression toCast, final Expression typeObject) { - if (toCast == null) { return null; } + if (toCast == null) return null; final IType castingType = currentTypesManager.get(type).typeIfCasting(toCast); final boolean isSuperType = castingType.isAssignableFrom(toCast.getGamlType()); TypeInfo typeInfo = null; if (typeObject instanceof TypeRef) { typeInfo = ((TypeRef) typeObject).getParameter(); - } else if (typeObject instanceof Function) { - typeInfo = ((Function) typeObject).getType(); - } + } else if (typeObject instanceof Function) { typeInfo = ((Function) typeObject).getType(); } if (isSuperType && typeInfo == null) { getContext().info("Unneeded casting: '" + toCast.serialize(true) + "' is already of type " + type, IGamlIssue.UNUSED, typeObject); @@ -270,12 +262,8 @@ private IExpression casting(final String type, final IExpression toCast, final E ct = kt; kt = null; } - if (ct != null && ct != Types.NO_TYPE) { - contentsType = ct; - } - if (kt != null && kt != Types.NO_TYPE) { - keyType = kt; - } + if (ct != null && ct != Types.NO_TYPE) { contentsType = ct; } + if (kt != null && kt != Types.NO_TYPE) { keyType = kt; } } final IType result = GamaType.from(castingType, keyType, contentsType); // If there is no casting to do, just return the expression unchanged. @@ -291,14 +279,12 @@ private IExpression casting(final String type, final IExpression toCast, final E } IType fromTypeRef(final TypeRef object) { - if (object == null) { return null; } + if (object == null) return null; String primary = EGaml.getInstance().getKeyOf(object); if (primary == null) { primary = object.getRef().getName(); - } else if (primary.equals(SyntacticFactory.SPECIES_VAR)) { - primary = SPECIES; - } + } else if (primary.equals(SyntacticFactory.SPECIES_VAR)) { primary = SPECIES; } final IType t = currentTypesManager.get(primary); @@ -310,11 +296,11 @@ IType fromTypeRef(final TypeRef object) { // case of model_alias if (t.isAgentType() && t.getSpecies().isModel()) { final TypeInfo parameter = object.getParameter(); - if (parameter == null) { return t; } + if (parameter == null) return t; final TypeRef first = (TypeRef) parameter.getFirst(); - if (first == null) { + if (first == null) return t; - } else { + else { final ITypesManager savedTypesManager = currentTypesManager; try { currentTypesManager = t.getSpecies().getModelDescription().getTypesManager(); @@ -325,12 +311,12 @@ IType fromTypeRef(final TypeRef object) { } } - if (t.isAgentType()) { return t; } + if (t.isAgentType()) return t; // / final TypeInfo parameter = object.getParameter(); - if (parameter == null) { return t; } + if (parameter == null) return t; final int numberOfParameter = t.getNumberOfParameters(); if (numberOfParameter == 0) { // Emit a warning (see #2875) @@ -341,7 +327,7 @@ IType fromTypeRef(final TypeRef object) { } final TypeRef first = (TypeRef) parameter.getFirst(); - if (first == null) { return t; } + if (first == null) return t; final TypeRef second = (TypeRef) parameter.getSecond(); if (second == null) { if (numberOfParameter == 2) { @@ -362,13 +348,13 @@ IType fromTypeRef(final TypeRef object) { } private IExpression binary(final String op, final IExpression left, final Expression originalExpression) { - if (left == null) { return null; } + if (left == null) return null; Expression rightMember = originalExpression; // if the operator is "as", the right-hand expression should be a // casting type - if (AS.equals(op)) { return binaryAs(left, rightMember); } + if (AS.equals(op)) return binaryAs(left, rightMember); // if the operator is "is", the right-hand expression should be a type - if (IS.equals(op)) { return binaryIs(left, rightMember); } + if (IS.equals(op)) return binaryIs(left, rightMember); // we verify and compile apart the calls to actions as operators @@ -423,46 +409,40 @@ private IExpression binary(final String op, final IExpression left, final Expres final IExpression right = compile(rightMember); // We make sure to remove any mention of the each expression after the // right member has been compiled - if (isIterator) { - iteratorContexts.pop(); - } + if (isIterator) { iteratorContexts.pop(); } // and we return the operator expression return getFactory().createOperator(op, getContext(), originalExpression.eContainer(), left, right); } private String findIteratorArgName(final Expression e2) { - if (!(e2 instanceof ExpressionList)) { return IKeyword.EACH; } + if (!(e2 instanceof ExpressionList)) return IKeyword.EACH; final ExpressionList params = (ExpressionList) e2; final List exprs = EGaml.getInstance().getExprsOf(params); - if (exprs == null || exprs.isEmpty()) { return IKeyword.EACH; } + if (exprs == null || exprs.isEmpty()) return IKeyword.EACH; final Expression arg = exprs.get(0); - if (!(arg instanceof Parameter)) { return IKeyword.EACH; } + if (!(arg instanceof Parameter)) return IKeyword.EACH; final Parameter p = (Parameter) arg; return EGaml.getInstance().getKeyOf(p); } private Expression findIteratorExpr(final Expression e2) { - if (!(e2 instanceof ExpressionList)) { return e2; } + if (!(e2 instanceof ExpressionList)) return e2; final ExpressionList params = (ExpressionList) e2; final List exprs = EGaml.getInstance().getExprsOf(params); - if (exprs == null || exprs.isEmpty()) { return e2; } + if (exprs == null || exprs.isEmpty()) return e2; final Expression arg = exprs.get(0); - if (!(arg instanceof Parameter)) { return arg; } + if (!(arg instanceof Parameter)) return arg; final Parameter p = (Parameter) arg; return p.getRight(); } private IExpression binaryIs(final IExpression left, final Expression e2) { final String type = EGaml.getInstance().getKeyOf(e2); - if (isTypeName(type)) { - return getFactory().createOperator(IS, getContext(), e2.eContainer(), left, - getFactory().createConst(type, Types.STRING)); - } - if (isSkillName(type)) { - return getFactory().createOperator(IS_SKILL, getContext(), e2.eContainer(), left, - getFactory().createConst(type, Types.SKILL)); - } + if (isTypeName(type)) return getFactory().createOperator(IS, getContext(), e2.eContainer(), left, + getFactory().createConst(type, Types.STRING)); + if (isSkillName(type)) return getFactory().createOperator(IS_SKILL, getContext(), e2.eContainer(), left, + getFactory().createConst(type, Types.SKILL)); getContext().error("'is' must be followed by a type, species or skill name. " + type + " is neither of these.", IGamlIssue.NOT_A_TYPE, e2, type); return null; @@ -475,7 +455,7 @@ private IExpression binaryAs(final IExpression left, final Expression e2) { // if ( isSkillName(type) ) { return // factory.createOperator(AS_SKILL, context, e2, left, skill(type)); // } - if (isTypeName(type)) { return casting(type, left, e2); } + if (isTypeName(type)) return casting(type, left, e2); getContext().error("'as' must be followed by a type, species or skill name. " + type + " is neither of these.", IGamlIssue.NOT_A_TYPE, e2, type); // if (isTypeName(type)) { return casting(type, left, e2); } @@ -492,7 +472,7 @@ private IExpression action(final String name, final IExpression callee, final EO private IExpression binary(final String op, final Expression e1, final Expression right) { // if the expression is " var of agents ", we must compile it apart - if (OF.equals(op)) { return compileFieldExpr(right, e1); } + if (OF.equals(op)) return compileFieldExpr(right, e1); // we can now safely compile the left-hand expression final IExpression left = compile(e1); return binary(op, left, right); @@ -504,10 +484,8 @@ private SpeciesDescription getSpeciesContext(final String e) { private boolean isSpeciesName(final String s) { final ModelDescription m = getContext().getModelDescription(); - if (m == null) { - // can occur when building the kernel + if (m == null) // can occur when building the kernel return false; - } final SpeciesDescription sd = m.getSpeciesDescription(s); return sd != null && !(sd instanceof ExperimentDescription); } @@ -518,24 +496,23 @@ private boolean isSkillName(final String s) { private boolean isTypeName(final String s) { - if (!currentTypesManager.containsType(s)) { return false; } + if (!currentTypesManager.containsType(s)) return false; final IType t = currentTypesManager.get(s); final SpeciesDescription sd = t.getSpecies(); - if (sd != null && sd.isExperiment()) { return false; } + if (sd != null && sd.isExperiment()) return false; return true; } private IExpression compileNamedExperimentFieldExpr(final Expression leftExpr, final String name) { final IExpression owner = compile(leftExpr); - if (owner == null) { return null; } + if (owner == null) return null; final IType type = owner.getGamlType(); if (type.isParametricFormOf(Types.SPECIES)) { final SpeciesDescription sd = type.getContentType().getSpecies(); if (sd instanceof ModelDescription) { final ModelDescription md = (ModelDescription) sd; - if (md.hasExperiment(name)) { + if (md.hasExperiment(name)) return getFactory().createConst(name, GamaType.from(md.getExperiment(name))); - } } } getContext().error("Only experiments can be accessed using their plain name", IGamlIssue.UNKNOWN_FIELD); @@ -544,7 +521,7 @@ private IExpression compileNamedExperimentFieldExpr(final Expression leftExpr, f private IExpression compileFieldExpr(final Expression leftExpr, final Expression fieldExpr) { final IExpression owner = compile(leftExpr); - if (owner == null) { return null; } + if (owner == null) return null; final IType type = owner.getGamlType(); final TypeDescription species = type.getSpecies(); // hqnghi 28-05-14 search input variable from model, not experiment @@ -552,9 +529,7 @@ private IExpression compileFieldExpr(final Expression leftExpr, final Expression if (type.getContentType().getSpecies() instanceof ModelDescription) { final ModelDescription sd = (ModelDescription) type.getContentType().getSpecies(); final String var = EGaml.getInstance().getKeyOf(fieldExpr); - if (sd.hasExperiment(var)) { - return getFactory().createConst(var, GamaType.from(sd.getExperiment(var))); - } + if (sd.hasExperiment(var)) return getFactory().createConst(var, GamaType.from(sd.getExperiment(var))); } } // end-hqnghi @@ -566,7 +541,7 @@ private IExpression compileFieldExpr(final Expression leftExpr, final Expression final OperatorProto proto = type.getGetter(var); // Special case for matrices - if (type.id() == IType.MATRIX && proto == null) { return binary(".", owner, fieldExpr); } + if (type.id() == IType.MATRIX && proto == null) return binary(".", owner, fieldExpr); if (proto == null) { getContext().error("Unknown field '" + var + "' for type " + type, IGamlIssue.UNKNOWN_FIELD, leftExpr, @@ -574,9 +549,7 @@ private IExpression compileFieldExpr(final Expression leftExpr, final Expression return null; } final TypeFieldExpression expr = (TypeFieldExpression) proto.create(getContext(), fieldExpr, owner); - if (getContext() != null) { - getContext().document(fieldExpr, expr); - } + if (getContext() != null) { getContext().document(fieldExpr, expr); } return expr; } // We are now dealing with an agent. In that case, it can be either an @@ -589,10 +562,10 @@ private IExpression compileFieldExpr(final Expression leftExpr, final Expression if (species instanceof ModelDescription && ((ModelDescription) species).hasExperiment(var)) { final IType t = Types.get(IKeyword.SPECIES); expr = getFactory().createTypeExpression(GamaType.from(t, Types.INT, species.getTypeNamed(var))); - } else if (species.getName().equals(IKeyword.PLATFORM) && GAMA.isInHeadLessMode()) { + } else if (species.getName().equals(IKeyword.PLATFORM) && GAMA.isInHeadLessMode()) // Special case (see #2259 for headless validation of GUI preferences) return ((PlatformSpeciesDescription) species).getFakePrefExpression(var); - } else { + else { getContext().error("Unknown variable '" + var + "' for species " + species.getName(), IGamlIssue.UNKNOWN_VAR, fieldExpr.eContainer(), var, species.getName()); return null; @@ -634,9 +607,7 @@ private IDescription setContext(final IDescription context) { final ModelDescription md = currentContext.getModelDescription(); if (md != null) { final ITypesManager tm = md.getTypesManager(); - if (tm != null) { - currentTypesManager = tm; - } + if (tm != null) { currentTypesManager = tm; } } } return previous; @@ -647,7 +618,7 @@ private IDescription getContext() { } private ValidationContext getValidationContext() { - if (currentContext == null) { return null; } + if (currentContext == null) return null; return currentContext.getValidationContext(); } @@ -658,7 +629,7 @@ private ValidationContext getValidationContext() { @Override public Arguments parseArguments(final ActionDescription action, final EObject o, final IDescription command, final boolean actionParameters) { - if (o == null) { return null; } + if (o == null) return null; boolean completeArgs = false; List parameters = null; if (o instanceof Array) { @@ -702,9 +673,7 @@ public Arguments parseArguments(final ActionDescription action, final EObject o, ed = builder.create(exp/* , errors */); } - if (ed != null && actionParameters) { - ed.compile(command); - } + if (ed != null && actionParameters) { ed.compile(command); } // if (!errors.isEmpty()) { // for (final Diagnostic d : errors) { // getContext().warning(d.getMessage(), "", exp); @@ -764,15 +733,15 @@ public IExpression caseExpression(final Expression object) { @Override public IExpression caseVariableRef(final VariableRef object) { final String s = EGaml.getInstance().getKeyOf(object); - if (s == null) { return caseVarDefinition(object.getRef()); } + if (s == null) return caseVarDefinition(object.getRef()); return caseVar(s, object); } @Override public IExpression caseTypeRef(final TypeRef object) { final IType t = fromTypeRef(object); - if (t == null) { return null; } - if (t.isAgentType()) { return t.getSpecies().getSpeciesExpr(); } + if (t == null) return null; + if (t.isAgentType()) return t.getSpecies().getSpeciesExpr(); return getFactory().createTypeExpression(t); } @@ -838,9 +807,8 @@ public IExpression caseUnit(final Unit object) { // Case of dates: #month and #year final String name = EGaml.getInstance().toString(object.getRight()); - if (TimeUnitConstantExpression.UNCOMPUTABLE_DURATIONS.contains(name)) { + if (TimeUnitConstantExpression.UNCOMPUTABLE_DURATIONS.contains(name)) return binary(Dates.APPROXIMATE_TEMPORAL_QUERY, object.getLeft(), object.getRight()); - } // AD: Hack to address Issue 387. If the unit is a pixel, we add +1 to // the whole expression. // final IExpression right = compile(object.getRight()); @@ -862,18 +830,18 @@ public IExpression caseUnary(final Unary object) { public IExpression caseDot(final Access object) { final Expression right = object.getRight(); - if (right instanceof StringLiteral) { + if (right instanceof StringLiteral) return compileNamedExperimentFieldExpr(object.getLeft(), EGaml.getInstance().getKeyOf(right)); - } else if (right != null) { return compileFieldExpr(object.getLeft(), right); } + else if (right != null) return compileFieldExpr(object.getLeft(), right); return null; } @Override public IExpression caseAccess(final Access object) { - if (object.getOp().equals(".")) { return caseDot(object); } + if (object.getOp().equals(".")) return caseDot(object); final IExpression container = compile(object.getLeft()); // If no container is defined, return a null expression - if (container == null) { return null; } + if (container == null) return null; final IType contType = container.getGamlType(); final boolean isMatrix = Types.MATRIX.isAssignableFrom(contType); final IType keyType = contType.getKeyType(); @@ -915,10 +883,9 @@ public IExpression caseAccess(final Access object) { if (species != null) { final Iterable equations = species.getChildrenWithKeyword(IKeyword.EQUATION); for (final IDescription equation : equations) { - if (equation.manipulatesVar(varDiff.getName())) { + if (equation.manipulatesVar(varDiff.getName())) return getFactory().createOperator("internal_integrated_value", getContext(), object, ((IVarExpression.Agent) container).getOwner(), varDiff); - } } } @@ -941,7 +908,7 @@ public IExpression caseArray(final Array object) { @Override public IExpression casePoint(final Point object) { final Expression z = object.getZ(); - if (z == null) { return binary(POINT, object.getLeft(), object.getRight()); } + if (z == null) return binary(POINT, object.getLeft(), object.getRight()); final IExpression[] exprs = new IExpression[3]; exprs[0] = compile(object.getLeft()); exprs[1] = compile(object.getRight()); @@ -959,7 +926,7 @@ public IExpression casePoint(final Point object) { @Override public IExpression caseExpressionList(final ExpressionList object) { final List list = EGaml.getInstance().getExprsOf(object); - if (list.isEmpty()) { return null; } + if (list.isEmpty()) return null; if (list.size() > 1) { getContext().warning( "A sequence of expressions is not expected here. Only the first expression will be evaluated", @@ -973,9 +940,9 @@ public IExpression caseExpressionList(final ExpressionList object) { public IExpression caseFunction(final Function object) { final String op = EGaml.getInstance().getKeyOf(object); IExpression result = tryCastingFunction(op, object); - if (result != null) { return result; } + if (result != null) return result; result = tryActionCall(op, object); - if (result != null) { return result; } + if (result != null) return result; final List args = EGaml.getInstance().getExprsOf(object.getRight()); switch (args.size()) { case 0: @@ -992,7 +959,7 @@ public IExpression caseFunction(final Function object) { } private IExpression tryCastingFunction(final String op, final Function object) { - if (!isCastingFunction(op, object)) { return null; } + if (!isCastingFunction(op, object)) return null; final List args = EGaml.getInstance().getExprsOf(object.getRight()); final int size = args.size(); IExpression toCast; @@ -1006,11 +973,11 @@ private IExpression tryCastingFunction(final String op, final Function object) { private IExpression tryActionCall(final String op, final Function object) { final SpeciesDescription sd = getContext().getSpeciesContext(); - if (sd == null) { return null; } + if (sd == null) return null; final boolean isSuper = getContext() instanceof StatementDescription && ((StatementDescription) getContext()).isSuperInvocation(); final ActionDescription action = isSuper ? sd.getParent().getAction(op) : sd.getAction(op); - if (action == null) { return null; } + if (action == null) return null; final EObject params = object.getRight(); return action(op, caseVar(isSuper ? SUPER : SELF, object), params, action); } @@ -1030,9 +997,9 @@ private boolean isCastingFunction(final String op, final Function object) { final List args = EGaml.getInstance().getExprsOf(object.getRight()); final int size = args.size(); // If there is none, it can't be a casting - if (size == 0) { return false; } + if (size == 0) return false; // If there is one, we match - if (size == 1) { return true; } + if (size == 1) return true; // If more than one, we need to check if there are operators that match. If yes, we return false return !getFactory().hasOperator(op, getContext(), object, toArray(transform(args, a -> compile(a)), IExpression.class)); @@ -1055,7 +1022,7 @@ public IExpression caseDoubleLiteral(final DoubleLiteral object) { String s = EGaml.getInstance().getKeyOf(object); - if (s == null) { return null; } + if (s == null) return null; try { return getFactory().createConst(Double.parseDouble(s), Types.FLOAT); } catch (final NumberFormatException e) { @@ -1083,7 +1050,7 @@ public IExpression caseStringLiteral(final StringLiteral object) { @Override public IExpression caseBooleanLiteral(final BooleanLiteral object) { final String s = EGaml.getInstance().getKeyOf(object); - if (s == null) { return null; } + if (s == null) return null; return s.equalsIgnoreCase(TRUE) ? TRUE_EXPR : FALSE_EXPR; } @@ -1119,9 +1086,7 @@ private IExpression caseVar(final String varName, final EObject object) { return getFactory().createVar(SELF, tt, true, IVarExpression.SELF, null); case SUPER: SpeciesDescription sd = getContext().getSpeciesContext(); - if (sd != null) { - sd = sd.getParent(); - } + if (sd != null) { sd = sd.getParent(); } if (sd == null) { getContext().error("Unable to determine the species of super", IGamlIssue.GENERAL, object); return null; @@ -1133,13 +1098,9 @@ private IExpression caseVar(final String varName, final EObject object) { // check if the var has been declared in an iterator context for (final IVarExpression it : iteratorContexts) { - if (it.getName().equals(varName)) { return it; } + if (it.getName().equals(varName)) return it; } - if (isSpeciesName(varName)) { - final SpeciesDescription sd = getSpeciesContext(varName); - return sd == null ? null : sd.getSpeciesExpr(); - } final IVarDescriptionProvider temp_sd = getContext() == null ? null : getContext().getDescriptionDeclaringVar(varName); @@ -1157,14 +1118,22 @@ private IExpression caseVar(final String varName, final EObject object) { IGamlIssue.UNKNOWN_VAR, object, varName); } } - } - return temp_sd.getVarExpr(varName, false); + // See Issue #3085. We give priority to the variables sporting species names unless they represent the species withing the agents + if (!isSpeciesName(varName)) return temp_sd.getVarExpr(varName, false); + } else + return temp_sd.getVarExpr(varName, false); + } + + // See Issue #3085 + if (isSpeciesName(varName)) { + final SpeciesDescription sd = getSpeciesContext(varName); + return sd == null ? null : sd.getSpeciesExpr(); } - if (isTypeName(varName)) { return getFactory().createTypeExpression(currentTypesManager.get(varName)); } + if (isTypeName(varName)) return getFactory().createTypeExpression(currentTypesManager.get(varName)); - if (isSkillName(varName)) { return skill(varName); } + if (isSkillName(varName)) return skill(varName); if (getContext() != null) { @@ -1183,9 +1152,9 @@ private IExpression caseVar(final String varName, final EObject object) { // an action (used like a variable, see Issue 853) or also any // behavior or aspect final SpeciesDescription sd = getContext().getSpeciesContext(); - if (sd.hasAction(varName, false)) { return new DenotedActionExpression(sd.getAction(varName)); } - if (sd.hasBehavior(varName)) { return new DenotedActionExpression(sd.getBehavior(varName)); } - if (sd.hasAspect(varName)) { return new DenotedActionExpression(sd.getAspect(varName)); } + if (sd.hasAction(varName, false)) return new DenotedActionExpression(sd.getAction(varName)); + if (sd.hasBehavior(varName)) return new DenotedActionExpression(sd.getBehavior(varName)); + if (sd.hasAspect(varName)) return new DenotedActionExpression(sd.getAspect(varName)); getContext().error( "The variable " + varName @@ -1211,9 +1180,7 @@ private EObject getEObjectOf(final String string, final IExecutionContext tempCo if (!resource.hasErrors()) { final EObject e = resource.getContents().get(0); - if (e instanceof StringEvaluator) { - result = ((StringEvaluator) e).getExpr(); - } + if (e instanceof StringEvaluator) { result = ((StringEvaluator) e).getExpr(); } } else { final Resource.Diagnostic d = resource.getErrors().get(0); throw GamaRuntimeException.error(d.getMessage(), tempContext.getScope());