1 | package com.framsticks.examples; |
---|
2 | |
---|
3 | import com.framsticks.core.Instance; |
---|
4 | import com.framsticks.core.Node; |
---|
5 | import com.framsticks.dumping.PrintWriterSink; |
---|
6 | import com.framsticks.model.*; |
---|
7 | import com.framsticks.model.Package; |
---|
8 | import com.framsticks.params.*; |
---|
9 | import com.framsticks.parsers.F0Parser; |
---|
10 | import com.framsticks.parsers.F0Writer; |
---|
11 | import com.framsticks.parsers.Schema; |
---|
12 | |
---|
13 | import org.apache.commons.configuration.Configuration; |
---|
14 | import org.apache.log4j.Logger; |
---|
15 | |
---|
16 | import java.io.PrintWriter; |
---|
17 | import java.io.StringWriter; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | /** |
---|
21 | * Author: Piotr Śniegowski |
---|
22 | */ |
---|
23 | public class GenotypeBrowser extends Instance { |
---|
24 | |
---|
25 | private static final Logger log = Logger |
---|
26 | .getLogger(Instance.class.getName()); |
---|
27 | protected Schema schema; |
---|
28 | |
---|
29 | public GenotypeBrowser() { |
---|
30 | log.info("model builder created"); |
---|
31 | } |
---|
32 | |
---|
33 | @Override |
---|
34 | public void configure(Configuration config) { |
---|
35 | super.configure(config); |
---|
36 | try { |
---|
37 | schema = new Schema(Schema.getDefaultDefinitionAsStream()); |
---|
38 | } catch (Exception e) { |
---|
39 | log.error("failed to load schema: " + e); |
---|
40 | } |
---|
41 | this.registry = schema.getRegistry(); |
---|
42 | Package.register(this.getRegistry()); |
---|
43 | |
---|
44 | registry.putInfoIntoCache(new FramsClass("ModelBuilderRoot", "ModelBuilderRoot", null) |
---|
45 | .append(Param.build().type("o Model").id("model").name("model")) |
---|
46 | .append(Param.build().type("s 1").id("genotype").name("genotype")) |
---|
47 | ); |
---|
48 | root = new Node((CompositeParam)Param.build().type("o ModelBuilderRoot").id(name).name("Instance").finish(), PropertiesAccess.createPropertiesMap()); |
---|
49 | } |
---|
50 | |
---|
51 | @Override |
---|
52 | protected void run() { |
---|
53 | assert isActive(); |
---|
54 | |
---|
55 | try { |
---|
56 | List<AccessInterface> accesses = new F0Parser(schema, GenotypeBrowser.class.getResourceAsStream("/examples/f0_example.txt")).parse(); |
---|
57 | List<Object> objects = Util.stripAccessInterface(accesses); |
---|
58 | Model model = Model.build(objects); |
---|
59 | |
---|
60 | AccessInterface rootAccess = bindAccess(this.getRootPath()); |
---|
61 | |
---|
62 | rootAccess.set("model", model); |
---|
63 | |
---|
64 | StringWriter w = new StringWriter(); |
---|
65 | new F0Writer(schema, model, new PrintWriterSink(new PrintWriter(w))).write(); |
---|
66 | rootAccess.set("genotype", w.getBuffer().toString()); |
---|
67 | |
---|
68 | } catch (Exception e) { |
---|
69 | log.error("exception caught: " + e); |
---|
70 | } |
---|
71 | //done(); |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|