source: java/main/src/main/java/com/framsticks/model/Model.java @ 86

Last change on this file since 86 was 86, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • use java annotations to mark classes and fields to be used when:
    • using java classes with ReflectionAccess? to represent remote objects with FramsClass? description found by "info ..." requests
    • to build up FramsClass? representation of objects not present at remote server
  • allow using primitive types (instead of wraping counterparts) in reflected classes
  • rework FramsClass? creation process (add FramsClassBuilder?)
  • add more tests

CHANGELOG:
Prepare model.World class.

Minor change.

Use primitive types for Genotype and Creature classes.

Use primitive types in model.Neuro* classes.

Use primitive types in model.Joint* classes.

Use primitive types in model.Part* classes.

Fix primitive values.

Extract FramsClassBuilder?.

Add tests of Model classes.

More fixes.

Refactorize out ParamCandidate?.

Several fixes.

Fix all regressions after introducing annotations.

Use annotations throughout the project.

Add exception classes.

Improve creation of FramsClass?.

More changes.

Many changes regarding annotations.

Annotate classes in com.framsticks.model package.

Remove manual FramsClass? constructor.

Construct FramsClass? for Creature. Add test.

Add default values to the ParamAnnotation?.

Add ParamBuilderTest? and ParamAnnotation?.

Add FramsClassAnnotation?.

File size: 3.2 KB
Line 
1package com.framsticks.model;
2
3import com.framsticks.params.annotations.FramsClassAnnotation;
4import com.framsticks.params.annotations.ParamAnnotation;
5import com.framsticks.util.lang.Casting;
6import com.framsticks.util.lang.Containers;
7import com.framsticks.util.lang.IterableIterator;
8import com.framsticks.util.math.Orientation;
9import org.apache.log4j.Logger;
10
11import java.util.ArrayList;
12import java.util.Iterator;
13import java.util.List;
14
15/**
16 * Author: Piotr Śniegowski
17 */
18@FramsClassAnnotation(id = "m")
19public class Model {
20
21        private final static Logger log = Logger.getLogger(Model.class);
22
23        @ParamAnnotation(id = "se")
24        public double startingEnergy;
25
26        @ParamAnnotation
27        public double getEnerg0() { return startingEnergy; }
28        @ParamAnnotation
29        public void setEnerg0(double energ0) { startingEnergy = energ0; }
30
31
32        @ParamAnnotation(id = "Vstyle")
33        public String visualizationStyle;
34
35        @ParamAnnotation
36        public final List<Part> parts = new ArrayList<Part>();
37
38        @ParamAnnotation
39        public final List<Joint> joints = new ArrayList<Joint>();
40
41        @ParamAnnotation
42        public final List<NeuroDef> neurodefs = new ArrayList<NeuroDef>();
43
44        //TODO: why those methods returns and accepts doubles?
45        @ParamAnnotation
46        public double getNumparts() { return (double)parts.size(); }
47        @ParamAnnotation
48        public double getNumjoints() { return (double)joints.size(); }
49        @ParamAnnotation
50        public double getNumneurons() { return (double)neurodefs.size(); }
51
52        //this is impossible to use, because numparts field is marked as readonly
53        @ParamAnnotation
54        public void setNumparts(double numparts) { Containers.resizeList(parts, (int) (double) numparts); }
55        @ParamAnnotation
56        public void setNumjoints(double numjoints) { Containers.resizeList(joints, (int)(double)numjoints); }
57        @ParamAnnotation
58        public void setNumneurons(double numneurons) { Containers.resizeList(neurodefs, (int)(double)numneurons); }
59
60        public List<Part> getParts() { return parts; }
61        public List<Joint> getJoints() { return joints; }
62        public List<NeuroDef> getNeuroDefs() { return neurodefs; }
63
64        public static Model build(List<Object> objects) {
65                Iterator<Object> i = objects.iterator();
66                if (!i.hasNext()) {
67                        return null;
68                }
69                Model f0Genotype = Casting.tryCast(Model.class, i.next());
70                if (f0Genotype == null) {
71                        log.fatal("first object is not a Model");
72                        return null;
73                }
74                for (Object object : new IterableIterator<Object>(i)) {
75                        if (object instanceof Joint) {
76                                f0Genotype.joints.add((Joint)object);
77                                continue;
78                        }
79                        if (object instanceof Part) {
80                                f0Genotype.parts.add((Part)object);
81                                continue;
82                        }
83                        if (object instanceof NeuroDef) {
84                                f0Genotype.neurodefs.add((NeuroDef) object);
85                                continue;
86                        }
87                        log.error("invalid class: " + object.getClass().getCanonicalName());
88                }
89
90                for (Part p : f0Genotype.getParts()) {
91                        p.setOrientation(new Orientation().rotate(p.getRotation()));
92                }
93                for (Joint j : f0Genotype.getJoints()) {
94                        /** based on c++ Joint::attachToParts*/
95                        Part p1 = f0Genotype.parts.get(j.part1);
96                        Part p2 = f0Genotype.parts.get(j.part2);
97                        assert p1 != null && p2 != null;
98                        Orientation o = new Orientation().rotate(j.getRotation());
99                        p2.setOrientation(p1.getOrientation().transform(o));
100                        p2.setPosition(p2.getOrientation().transform(j.getDelta()).add(p1.getPosition()));
101                }
102                return f0Genotype;
103        }
104}
Note: See TracBrowser for help on using the repository browser.