/
SpeciesDescription.java
854 lines (759 loc) · 28.7 KB
/
SpeciesDescription.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*******************************************************************************************************
*
* msi.gaml.descriptions.SpeciesDescription.java, in plugin msi.gama.core, is part of the source code of the GAMA
* modeling and simulation platform (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package msi.gaml.descriptions;
import static com.google.common.collect.Iterables.transform;
import static msi.gaml.compilation.AbstractGamlAdditions.getAllChildrenOf;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.emf.ecore.EObject;
import com.google.common.collect.Iterables;
import msi.gama.common.interfaces.IGamlIssue;
import msi.gama.common.interfaces.ISkill;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.metamodel.agent.GamlAgent;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.agent.IMacroAgent;
import msi.gama.metamodel.agent.MinimalAgent;
import msi.gama.metamodel.topology.grid.GamaSpatialMatrix.GridPopulation.GamlGridAgent;
import msi.gama.metamodel.topology.grid.GamaSpatialMatrix.GridPopulation.MinimalGridAgent;
import msi.gama.precompiler.ITypeProvider;
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IMap;
import msi.gaml.architecture.reflex.AbstractArchitecture;
import msi.gaml.compilation.GAML;
import msi.gaml.compilation.IAgentConstructor;
import msi.gaml.compilation.IGamaHelper;
import msi.gaml.compilation.kernel.GamaSkillRegistry;
import msi.gaml.expressions.IExpression;
import msi.gaml.expressions.data.ListExpression;
import msi.gaml.expressions.types.DenotedActionExpression;
import msi.gaml.expressions.types.SkillConstantExpression;
import msi.gaml.expressions.types.SpeciesConstantExpression;
import msi.gaml.factories.DescriptionFactory;
import msi.gaml.statements.Facets;
import msi.gaml.types.GamaType;
import msi.gaml.types.IType;
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class SpeciesDescription extends TypeDescription {
// AD 08/16: Behaviors are now inherited dynamically
private IMap<String, StatementDescription> behaviors;
// AD 08/16: Aspects are now inherited dynamically
private IMap<String, StatementDescription> aspects;
private IMap<String, SpeciesDescription> microSpecies;
protected LinkedHashSet<SkillDescription> skills;
protected SkillDescription control;
private IAgentConstructor agentConstructor;
private SpeciesConstantExpression speciesExpr;
protected Class javaBase;
protected boolean canUseMinimalAgents = true;
protected boolean controlFinalized;
public SpeciesDescription(final String keyword, final Class clazz, final SpeciesDescription macroDesc,
final SpeciesDescription parent, final Iterable<? extends IDescription> cp, final EObject source,
final Facets facets) {
this(keyword, clazz, macroDesc, parent, cp, source, facets, Collections.EMPTY_SET);
}
public SpeciesDescription(final String keyword, final Class clazz, final SpeciesDescription macroDesc,
final SpeciesDescription parent, final Iterable<? extends IDescription> cp, final EObject source,
final Facets facets, final Set<String> skills) {
super(keyword, clazz, macroDesc, parent, cp, source, facets, null);
setJavaBase(clazz);
setSkills(getFacet(SKILLS), skills);
}
/**
* This constructor is only called to build built-in species. The parent is passed directly as there is no
* ModelFactory to provide it
*/
public SpeciesDescription(final String name, final Class clazz, final SpeciesDescription superDesc,
final SpeciesDescription parent, final IAgentConstructor helper, final Set<String> skills2, final Facets ff,
final String plugin) {
super(SPECIES, clazz, superDesc, null, null, null, new Facets(NAME, name), plugin);
setJavaBase(clazz);
setParent(parent);
setSkills(ff == null ? null : ff.get(SKILLS), skills2);
setAgentConstructor(helper);
}
protected void addSkill(final SkillDescription sk) {
if (sk == null) return;
if (skills == null) { skills = new LinkedHashSet(); }
skills.add(sk);
}
@Override
public SymbolSerializer createSerializer() {
return SPECIES_SERIALIZER;
}
@Override
public void dispose() {
if (isBuiltIn()) return;
super.dispose();
behaviors = null;
aspects = null;
skills = null;
if (control != null) {
control.dispose();
control = null;
}
microSpecies = null;
}
protected void setSkills(final IExpressionDescription userDefinedSkills, final Set<String> builtInSkills) {
/* We try to add the control architecture if any is defined */
final String controlName = getLitteral(CONTROL);
if (controlName != null) {
final SkillDescription sd = GamaSkillRegistry.INSTANCE.get(controlName);
if (sd == null || !sd.isControl()) {
warning("This control does not belong to the list of known agent controls ("
+ GamaSkillRegistry.INSTANCE.getArchitectureNames() + ")", IGamlIssue.WRONG_CONTEXT, CONTROL);
} else {
control = sd;
// We add it explicitly so as to add the variables and actions
// defined in the control. No need to add it in the other cases
// addSkill(control);
}
}
/* We add the keyword as a possible skill (used for 'grid' species) */
final SkillDescription skill = GamaSkillRegistry.INSTANCE.get(getKeyword());
addSkill(skill);
/*
* We add the user defined skills (i.e. as in 'species a skills: [s1, s2...]')
*/
if (userDefinedSkills != null) {
final IExpression expr = userDefinedSkills.compile(this);
if (expr instanceof ListExpression) {
final ListExpression list = (ListExpression) expr;
for (final IExpression exp : list.getElements()) {
if (exp instanceof SkillConstantExpression) {
final SkillDescription sk = ((ISkill) exp.getConstValue()).getDescription();
final String dep = sk.getDeprecated();
if (dep != null) {
warning("Skill " + sk.getName() + " is deprecated: " + dep, IGamlIssue.DEPRECATED, SKILLS);
}
addSkill(sk);
}
}
}
}
/*
* We add the skills that are defined in Java, either using @species(value='a', skills= {s1,s2}),
* or @skill(value="s1", attach_to="a")
*/
for (final String s : builtInSkills) {
addSkill(GamaSkillRegistry.INSTANCE.get(s));
}
}
@Override
public boolean redefinesAttribute(final String theName) {
if (super.redefinesAttribute(theName)) return true;
if (skills != null) {
for (final SkillDescription skill : skills) {
if (skill.hasAttribute(theName)) return true;
}
}
return false;
}
@Override
public boolean redefinesAction(final String theName) {
if (super.redefinesAction(theName)) return true;
if (skills != null) {
for (final SkillDescription skill : skills) {
if (skill.hasAction(theName, false)) return true;
}
}
return false;
}
public String getControlName() {
String controlName = getLitteral(CONTROL);
// if the "control" is not explicitly declared then inherit it from the
// parent species. Takes care of invalid species (see Issue 711)
if (controlName == null) {
if (parent != null && parent != this) {
controlName = getParent().getControlName();
} else {
controlName = REFLEX;
}
}
return controlName;
}
public String getParentName() {
return getLitteral(PARENT);
}
@Override
public IExpression getVarExpr(final String n, final boolean asField) {
IExpression result = super.getVarExpr(n, asField);
if (result == null) {
StatementDescription desc = getBehavior(n);
if (desc != null) { result = new DenotedActionExpression(desc); }
desc = getAspect(n);
if (desc != null) { result = new DenotedActionExpression(desc); }
}
return result;
}
public void copyJavaAdditions() {
final Class clazz = getJavaBase();
if (clazz == null) {
error("This species cannot be compiled as its Java base is unknown. ", IGamlIssue.UNKNOWN_SPECIES);
return;
}
Class<? extends IAgent> javaBase = getJavaBase();
Iterable<Class<? extends ISkill>> skillClasses = transform(getSkills(), TO_CLASS);
Iterable<IDescription> javaChildren = getAllChildrenOf(javaBase, skillClasses);
for (final IDescription v : javaChildren) {
if (isBuiltIn()) { v.setOriginName("built-in species " + getName()); }
if (v instanceof VariableDescription) {
boolean toAdd = false;
if (this.isBuiltIn() && !hasAttribute(v.getName()) || ((VariableDescription) v).isContextualType()) {
toAdd = true;
} else if (parent != null && parent != this) {
final VariableDescription existing = parent.getAttribute(v.getName());
if (existing == null || !existing.getOriginName().equals(v.getOriginName())) { toAdd = true; }
} else {
toAdd = true;
}
if (toAdd) {
final VariableDescription var = (VariableDescription) v.copy(this);
addOwnAttribute(var);
}
} else {
boolean toAdd = false;
if (parent == null) {
toAdd = true;
} else if (parent != this) {
final StatementDescription existing = parent.getAction(v.getName());
if (existing == null || !existing.getOriginName().equals(v.getOriginName())) { toAdd = true; }
}
if (toAdd) {
v.setEnclosingDescription(this);
addAction((ActionDescription) v);
}
}
}
}
@Override
public IDescription addChild(final IDescription child) {
final IDescription desc = super.addChild(child);
if (desc == null) return null;
if (desc instanceof StatementDescription) {
final StatementDescription statement = (StatementDescription) desc;
final String kw = desc.getKeyword();
if (PRIMITIVE.equals(kw) || ACTION.equals(kw)) {
addAction((ActionDescription) statement);
} else if (ASPECT.equals(kw)) {
addAspect(statement);
} else {
addBehavior(statement);
}
} else if (desc instanceof VariableDescription) {
addOwnAttribute((VariableDescription) desc);
} else if (desc instanceof SpeciesDescription) { addMicroSpecies((SpeciesDescription) desc); }
return desc;
}
protected void addMicroSpecies(final SpeciesDescription sd) {
if (!isModel() && sd.isGrid()) {
sd.error("For the moment, grids cannot be defined as micro-species anywhere else than in the model");
}
getMicroSpecies().put(sd.getName(), sd);
invalidateMinimalAgents();
}
protected void invalidateMinimalAgents() {
canUseMinimalAgents = false;
if (parent != null && parent != this && !parent.isBuiltIn()) { getParent().invalidateMinimalAgents(); }
}
protected boolean useMinimalAgents() {
if (!canUseMinimalAgents || parent != null && parent != this && !getParent().useMinimalAgents()) return false;
if (!hasFacet("use_regular_agents")) return GamaPreferences.External.AGENT_OPTIMIZATION.getValue();
return FALSE.equals(getLitteral("use_regular_agents"));
}
protected void addBehavior(final StatementDescription r) {
final String behaviorName = r.getName();
if (behaviors == null) { behaviors = GamaMapFactory.create(); }
final StatementDescription existing = getBehavior(behaviorName);
if (existing != null && existing.getKeyword().equals(r.getKeyword())) { duplicateInfo(r, existing); }
behaviors.put(behaviorName, r);
}
public boolean hasBehavior(final String a) {
return behaviors != null && behaviors.containsKey(a)
|| parent != null && parent != this && getParent().hasBehavior(a);
}
public StatementDescription getBehavior(final String aName) {
StatementDescription ownBehavior = behaviors == null ? null : behaviors.get(aName);
if (ownBehavior == null && parent != null && parent != this) { ownBehavior = getParent().getBehavior(aName); }
return ownBehavior;
}
private void addAspect(final StatementDescription ce) {
String aspectName = ce.getName();
if (aspectName == null) {
aspectName = DEFAULT;
ce.setName(aspectName);
}
if (!DEFAULT.equals(aspectName) && hasAspect(aspectName)) { duplicateInfo(ce, getAspect(aspectName)); }
if (aspects == null) { aspects = GamaMapFactory.createUnordered(); }
aspects.put(aspectName, ce);
}
public StatementDescription getAspect(final String aName) {
StatementDescription ownAspect = aspects == null ? null : aspects.get(aName);
if (ownAspect == null && parent != null && parent != this) { ownAspect = getParent().getAspect(aName); }
return ownAspect;
}
public Collection<String> getBehaviorNames() {
final Collection<String> ownNames =
behaviors == null ? new LinkedHashSet() : new LinkedHashSet(behaviors.keySet());
if (parent != null && parent != this) { ownNames.addAll(getParent().getBehaviorNames()); }
return ownNames;
}
public Collection<String> getAspectNames() {
final Collection<String> ownNames = aspects == null ? new LinkedHashSet() : new LinkedHashSet(aspects.keySet());
if (parent != null && parent != this) { ownNames.addAll(getParent().getAspectNames()); }
return ownNames;
}
public Iterable<StatementDescription> getAspects() {
return Iterables.transform(getAspectNames(), this::getAspect);
}
public SkillDescription getControl() {
return control;
}
public boolean hasAspect(final String a) {
return aspects != null && aspects.containsKey(a)
|| parent != null && parent != this && getParent().hasAspect(a);
}
@Override
public SpeciesDescription getSpeciesContext() {
return this;
}
public SpeciesDescription getMicroSpecies(final String name) {
if (hasMicroSpecies()) {
final SpeciesDescription retVal = microSpecies.get(name);
if (retVal != null) return retVal;
}
// Takes care of invalid species (see Issue 711)
if (parent != null && parent != this) return getParent().getMicroSpecies(name);
return null;
}
@Override
public String toString() {
return "Description of " + getName();
}
public IAgentConstructor getAgentConstructor() {
if (agentConstructor == null && parent != null && parent != this) {
if (getParent().getJavaBase() == getJavaBase()) {
agentConstructor = getParent().getAgentConstructor();
} else {
agentConstructor = IAgentConstructor.CONSTRUCTORS.get(getJavaBase());
}
}
return agentConstructor;
}
protected void setAgentConstructor(final IAgentConstructor agentConstructor) {
this.agentConstructor = agentConstructor;
}
public SpeciesDescription getMacroSpecies() {
final IDescription d = getEnclosingDescription();
if (d instanceof SpeciesDescription) return (SpeciesDescription) d;
return null;
}
@Override
public SpeciesDescription getParent() {
return (SpeciesDescription) super.getParent();
}
@Override
public void inheritFromParent() {
final SpeciesDescription parent = getParent();
// Takes care of invalid species (see Issue 711)
// built-in parents are not considered as their actions/variables are
// normally already copied as java additions
if (parent != null && parent.getJavaBase() == null) {
error("Species " + parent.getName() + " Java base class can not be found. No validation is possible.",
IGamlIssue.GENERAL);
return;
}
if (getJavaBase() == null) {
error("Species " + getName() + " Java base class can not be found. No validation is possible.",
IGamlIssue.GENERAL);
return;
}
if (parent != null && parent != this && !parent.isBuiltIn() && parent.getJavaBase() != null) {
if (!parent.getJavaBase().isAssignableFrom(getJavaBase())) {
error("Species " + getName() + " Java base class (" + getJavaBase().getSimpleName()
+ ") is not a subclass of its parent species " + parent.getName() + " base class ("
+ parent.getJavaBase().getSimpleName() + ")", IGamlIssue.GENERAL);
}
if (!parent.isBuiltIn()) { inheritMicroSpecies(parent); }
}
super.inheritFromParent();
}
// FIXME HACK !
private void inheritMicroSpecies(final SpeciesDescription parent) {
// Takes care of invalid species (see Issue 711)
if (parent == null || parent == this) return;
if (parent.hasMicroSpecies()) {
for (final Map.Entry<String, SpeciesDescription> entry : parent.getMicroSpecies().entrySet()) {
if (!getMicroSpecies().containsKey(entry.getKey())) {
getMicroSpecies().put(entry.getKey(), entry.getValue());
}
}
}
}
public boolean isGrid() {
return GRID.equals(getKeyword());
}
@Override
public String getTitle() {
return getKeyword() + " " + getName();
}
@Override
public String getDocumentation() {
final StringBuilder sb = new StringBuilder(200);
sb.append(getDocumentationWithoutMeta());
sb.append(getMeta().getDocumentation());
return sb.toString();
}
public String getDocumentationWithoutMeta() {
final StringBuilder sb = new StringBuilder(200);
final String parentName = getParent() == null ? "nil" : getParent().getName();
final String hostName = getMacroSpecies() == null ? null : getMacroSpecies().getName();
sb.append("<b>Subspecies of:</b> ").append(parentName).append("<br>");
if (hostName != null) { sb.append("<b>Microspecies of:</b> ").append(hostName).append("<br>"); }
final Iterable<String> skills = getSkillsNames();
if (!Iterables.isEmpty(skills)) { sb.append("<b>Skills:</b> ").append(skills).append("<br>"); }
sb.append(getAttributeDocumentation());
sb.append("<br/>");
sb.append(getActionDocumentation());
sb.append("<br/>");
return sb.toString();
}
public Iterable<String> getSkillsNames() {
return Iterables.concat(Iterables.transform(skills == null ? Collections.EMPTY_LIST : skills, TO_NAME),
parent != null && parent != this ? getParent().getSkillsNames() : Collections.EMPTY_LIST);
}
/**
* Returns the constant expression representing this species
*/
public SpeciesConstantExpression getSpeciesExpr() {
if (speciesExpr == null) {
final IType type = GamaType.from(SpeciesDescription.this);
speciesExpr = GAML.getExpressionFactory().createSpeciesConstant(type);
}
return speciesExpr;
}
public boolean visitMicroSpecies(final DescriptionVisitor<SpeciesDescription> visitor) {
if (!hasMicroSpecies()) return true;
return getMicroSpecies().forEachValue(visitor);
}
// public boolean visitSortedMicroSpecies(final DescriptionVisitor<SpeciesDescription> visitor) {
// if (!hasMicroSpecies()) { return true; }
// final Iterable<SpeciesDescription> all = getSortedMicroSpecies();
// for (final SpeciesDescription sd : all) {
// if (!visitor.process(sd)) { return false; }
// }
// return true;
// }
@Override
public void setParent(final TypeDescription parent) {
super.setParent(parent);
if (!isBuiltIn() && !verifyParent()) {
super.setParent(null);
return;
}
if (parent instanceof SpeciesDescription && parent != this && !canUseMinimalAgents && !parent.isBuiltIn()) {
((SpeciesDescription) parent).invalidateMinimalAgents();
}
}
/**
* Verifies if the specified species can be a parent of this species.
*
* A species can be parent of other if the following conditions are hold 1. A parent species is visible to the
* sub-species. 2. A species can' be a sub-species of itself. 3. 2 species can't be parent of each other. 5. A
* species can't be a sub-species of its direct/in-direct micro-species. 6. A species and its direct/indirect
* micro/macro-species can't share one/some direct/indirect parent-species having micro-species. 7. The inheritance
* between species from different branches doesn't form a "circular" inheritance.
*
* @param parentName
* the name of the potential parent
* @throws GamlException
* if the species with the specified name can not be a parent of this species.
*/
protected boolean verifyParent() {
if (parent == null) return true;
if (this == parent) {
error(getName() + " species can't be a sub-species of itself", IGamlIssue.GENERAL);
return false;
}
if (parentIsAmongTheMicroSpecies()) {
error(getName() + " species can't be a sub-species of one of its micro-species", IGamlIssue.GENERAL);
return false;
}
if (!parentIsVisible()) {
error(parent.getName() + " can't be a parent species of " + this.getName() + " species.",
IGamlIssue.WRONG_PARENT, PARENT);
return false;
}
if (hierarchyContainsSelf()) {
error(this.getName() + " species and " + parent.getName() + " species can't be sub-species of each other.");
return false;
}
return true;
}
private boolean parentIsAmongTheMicroSpecies() {
final boolean[] result = new boolean[1];
visitMicroSpecies(new DescriptionVisitor<SpeciesDescription>() {
@Override
public boolean process(final SpeciesDescription desc) {
if (desc == parent) {
result[0] = true;
return false;
}
desc.visitMicroSpecies(this);
return true;
}
});
return result[0];
}
private boolean hierarchyContainsSelf() {
SpeciesDescription currentSpeciesDesc = this;
while (currentSpeciesDesc != null) {
final SpeciesDescription p = currentSpeciesDesc.getParent();
// Takes care of invalid species (see Issue 711)
if (p == currentSpeciesDesc || p == this) return true;
currentSpeciesDesc = p;
}
return false;
}
protected boolean parentIsVisible() {
if (getParent().isExperiment()) return false;
SpeciesDescription host = getMacroSpecies();
while (host != null) {
if (host == parent || host.getMicroSpecies(parent.getName()) != null) return true;
host = host.getMacroSpecies();
}
return false;
}
/**
* Finalizes the species description + Copy the behaviors, attributes from parent; + Creates the control if
* necessary. Add a variable representing the population of each micro-species
*
* @throws GamlException
*/
public boolean finalizeDescription() {
if (isMirror()) {
addChild(DescriptionFactory.create(AGENT, this, NAME, TARGET, TYPE,
String.valueOf(ITypeProvider.MIRROR_TYPE)));
}
// Add the control if it is not already added
finalizeControl();
final boolean isBuiltIn = this.isBuiltIn();
final DescriptionVisitor<SpeciesDescription> visitor = microSpec -> {
if (!microSpec.finalizeDescription()) return false;
if (!microSpec.isExperiment() && !isBuiltIn) {
final String n = microSpec.getName();
if (hasAttribute(n) && !getAttribute(n).isSyntheticSpeciesContainer()) {
microSpec.error(
microSpec.getName() + " is the name of an existing attribute in " + SpeciesDescription.this,
IGamlIssue.DUPLICATE_NAME, NAME);
return false;
}
final VariableDescription var =
(VariableDescription) DescriptionFactory.create(LIST, SpeciesDescription.this, NAME, n);
var.setSyntheticSpeciesContainer();
var.setFacet(OF, GAML.getExpressionFactory()
.createTypeExpression(getModelDescription().getTypeNamed(microSpec.getName())));
final IGamaHelper get = (scope1, agent1, skill1, values1) -> ((IMacroAgent) agent1)
.getMicroPopulation(microSpec.getName());
final IGamaHelper set = (scope2, agent2, skill2, values2) -> null;
final IGamaHelper init = (scope3, agent3, skill3, values3) -> {
((IMacroAgent) agent3).initializeMicroPopulation(scope3, microSpec.getName());
return ((IMacroAgent) agent3).getMicroPopulation(microSpec.getName());
};
var.addHelpers(get, init, set);
addChild(var);
}
return true;
};
// recursively finalize the sorted micro-species
if (!visitMicroSpecies(visitor)) return false;
// Calling sortAttributes later (in compilation)
// add the listeners to the variables (if any)
// addListenersToVariables();
return true;
}
/**
*
*/
private void finalizeControl() {
if (controlFinalized) return;
controlFinalized = true;
if (control == null && parent != this && parent instanceof SpeciesDescription) {
((SpeciesDescription) parent).finalizeControl();
control = ((SpeciesDescription) parent).getControl();
}
if (control == null) {
control = GamaSkillRegistry.INSTANCE.get(REFLEX);
return;
}
Class<? extends ISkill> clazz = control.getJavaBase().getSuperclass();
while (clazz != AbstractArchitecture.class) {
final SkillDescription sk = GamaSkillRegistry.INSTANCE.get(clazz);
if (sk != null) { addSkill(sk); }
clazz = (Class<? extends ISkill>) clazz.getSuperclass();
}
}
@Override
protected boolean validateChildren() {
// We try to issue information about the state of the species: at first,
// abstract.
for (final ActionDescription a : getActions()) {
if (a.isAbstract()) {
this.info("Action '" + a.getName() + "' is defined or inherited as virtual. In consequence, "
+ getName() + " will be considered as abstract.", IGamlIssue.MISSING_ACTION);
}
}
return super.validateChildren();
}
public boolean isExperiment() {
return false;
}
public boolean isModel() {
return false;
}
public boolean hasMicroSpecies() {
return microSpecies != null;
}
public IMap<String, SpeciesDescription> getMicroSpecies() {
if (microSpecies == null) { microSpecies = GamaMapFactory.create(); }
return microSpecies;
}
public boolean isMirror() {
return hasFacet(MIRRORS);
}
/**
* Returns whether or not a species implements (directly or indirectly through its parents) a skill named after the
* parameter.
*
* @param skill
* the name of the skill
* @return true if this species implements a skill or if its parent does. WARNING: no possibility, right now, to
* know if a skill extends another skill, so this possibility is not considered in this method.
*/
public Boolean implementsSkill(final String skill) {
if (skills != null) {
for (final SkillDescription sk : skills) {
if (sk.getName().equals(skill)) return true;
}
}
if (parent != null && parent != this) return getParent().implementsSkill(skill);
return false;
}
@Override
public Class<? extends IAgent> getJavaBase() {
if (javaBase == null) {
if (parent != null && parent != this && !AGENT.equals(getParent().getName())) {
javaBase = getParent().getJavaBase();
} else if (useMinimalAgents()) {
javaBase = isGrid() ? MinimalGridAgent.class : MinimalAgent.class;
} else {
javaBase = isGrid() ? GamlGridAgent.class : GamlAgent.class;
}
}
return javaBase;
}
protected void setJavaBase(final Class javaBase) {
this.javaBase = javaBase;
}
/**
* @param found_sd
* @return
*/
public boolean hasMacroSpecies(final SpeciesDescription found_sd) {
final SpeciesDescription sd = getMacroSpecies();
if (sd == null) return false;
if (sd.equals(found_sd)) return true;
return sd.hasMacroSpecies(found_sd);
}
/**
* @param macro
* @return
*/
public boolean hasParent(final SpeciesDescription p) {
final SpeciesDescription sd = getParent();
// Takes care of invalid species (see Issue 711)
if (sd == null || sd == this) return false;
if (sd.equals(p)) return true;
return sd.hasParent(p);
}
@Override
public boolean visitOwnChildren(final DescriptionVisitor<IDescription> visitor) {
if (!super.visitOwnChildren(visitor) || microSpecies != null && !microSpecies.forEachValue(visitor))
return false;
if (behaviors != null && !behaviors.forEachValue(visitor) || aspects != null && !aspects.forEachValue(visitor))
return false;
return true;
}
@Override
public boolean visitOwnChildrenRecursively(final DescriptionVisitor<IDescription> visitor) {
final DescriptionVisitor<IDescription> recursiveVisitor = each -> {
if (!visitor.process(each)) return false;
return each.visitOwnChildrenRecursively(visitor);
};
if (!super.visitOwnChildrenRecursively(visitor)
|| microSpecies != null && !microSpecies.forEachValue(recursiveVisitor))
return false;
if (behaviors != null && !behaviors.forEachValue(recursiveVisitor)
|| aspects != null && !aspects.forEachValue(recursiveVisitor))
return false;
return true;
}
@Override
public Iterable<IDescription> getOwnChildren() {
return Iterables.concat(super.getOwnChildren(),
microSpecies == null ? Collections.EMPTY_LIST : microSpecies.values(),
behaviors == null ? Collections.EMPTY_LIST : behaviors.values(),
aspects == null ? Collections.EMPTY_LIST : aspects.values());
}
@Override
public boolean visitChildren(final DescriptionVisitor<IDescription> visitor) {
boolean result = super.visitChildren(visitor);
if (!result) return false;
if (hasMicroSpecies()) { result &= microSpecies.forEachValue(visitor); }
if (!result) return false;
for (final IDescription d : getBehaviors()) {
result &= visitor.process(d);
if (!result) return false;
}
for (final IDescription d : getAspects()) {
result &= visitor.process(d);
if (!result) return false;
}
return result;
}
/**
* @return
*/
public Iterable<StatementDescription> getBehaviors() {
return Iterables.transform(getBehaviorNames(), this::getBehavior);
}
//
// @Override
// public void collectMetaInformation(final GamlProperties meta) {
// super.collectMetaInformation(meta);
// if (isBuiltIn()) {
// meta.put(GamlProperties.SPECIES, getName());
// }
// }
public boolean belongsToAMicroModel() {
return getModelDescription().isMicroModel();
}
public Iterable<SkillDescription> getSkills() {
final List<SkillDescription> base =
control == null ? Collections.EMPTY_LIST : Collections.singletonList(control);
if (skills == null) return base;
return Iterables.concat(skills, base);
}
}