1 package org.simantics.document.linking.report.evaluator;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
10 import org.simantics.Simantics;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.NamedResource;
15 import org.simantics.db.common.request.ObjectsWithType;
16 import org.simantics.db.common.request.WriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.request.Read;
19 import org.simantics.document.linking.ontology.DocumentLink;
20 import org.simantics.document.linking.report.templates.CustomizableContentProvider;
21 import org.simantics.document.linking.report.templates.custom.CustomizableContent;
22 import org.simantics.document.linking.report.templates.custom.EvaluatorCustomizableContent;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.graph.IMapping;
25 import org.simantics.objmap.graph.Mappings;
26 import org.simantics.objmap.graph.schema.IMappingSchema;
27 import org.simantics.objmap.graph.schema.MappingSchemas;
28 import org.simantics.objmap.graph.schema.SimpleSchema;
33 * Loads single evaluator tree from graph.
37 * @throws DatabaseException
39 public static EvaluatorItem load(ReadGraph graph, Resource res) throws DatabaseException{
40 IMappingSchema<Resource, Object> schema = getSchema(graph);
41 IMapping<Resource,Object> mapping = Mappings.createWithoutListening(schema);
42 EvaluatorItem item = (EvaluatorItem)mapping.map(graph, res);
47 * Saves a single evaluator tree to graph.
51 * @throws DatabaseException
53 public static Resource save(WriteGraph graph, EvaluatorItem item) throws DatabaseException{
54 IMappingSchema<Resource, Object> schema = getSchema(graph);
55 IMapping<Resource,Object> mapping = Mappings.createWithoutListening(schema);
56 Resource resource = mapping.inverseMap(graph, item);
61 * Creates mapping schema for Evaluator tree.
64 * @throws DatabaseException
66 public static IMappingSchema<Resource,Object> getSchema(ReadGraph g) throws DatabaseException{
68 SimpleSchema schema = new SimpleSchema();
69 schema.addLinkType(MappingSchemas.fromAnnotations(g, AlignmentHint.class));
70 schema.addLinkType(MappingSchemas.fromAnnotations(g, And.class));
71 schema.addLinkType(MappingSchemas.fromAnnotations(g, Constant.class));
72 schema.addLinkType(MappingSchemas.fromAnnotations(g, Date.class));
73 schema.addLinkType(MappingSchemas.fromAnnotations(g, EvaluatorRoot.class));
74 schema.addLinkType(MappingSchemas.fromAnnotations(g, If.class));
75 schema.addLinkType(MappingSchemas.fromAnnotations(g, Lines.class));
76 schema.addLinkType(MappingSchemas.fromAnnotations(g, Or.class));
77 schema.addLinkType(MappingSchemas.fromAnnotations(g, TextSizeHint.class));
78 schema.addLinkType(MappingSchemas.fromAnnotations(g, Variable.class));
79 schema.addLinkType(MappingSchemas.fromAnnotations(g, Path.class));
82 } catch (IllegalAccessException e) {
83 throw new DatabaseException(e);
84 } catch (InstantiationException e) {
85 throw new DatabaseException(e);
90 * Saves evaluator tree configurations of content provider.
94 * @throws DatabaseException
96 public static void save(WriteGraph graph, CustomizableContentProvider provider, Resource template) throws DatabaseException{
97 Layer0 l0 = Layer0.getInstance(graph);
98 DocumentLink sl = DocumentLink.getInstance(graph);
100 Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();
101 for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {
102 if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {
103 String id = graph.getRelatedValue(r, l0.HasName);
104 existingEvaluatorTemplates.put(id, r);
108 for (String id : provider.getContentIds()) {
109 CustomizableContent content = provider.getContent(id);
110 if (content instanceof EvaluatorCustomizableContent) {
111 EvaluatorCustomizableContent ecc = (EvaluatorCustomizableContent)content;
112 Resource item = save(graph, ecc.getItem());
114 Resource treeTemplate = existingEvaluatorTemplates.get(id);
115 if (treeTemplate == null) {
116 treeTemplate = graph.newResource();
117 graph.claim(treeTemplate, l0.InstanceOf, sl.EvaluatorTree_Template);
118 graph.claimLiteral(treeTemplate, l0.HasName, id);
119 graph.claim(template, l0.ConsistsOf, treeTemplate);
121 graph.deny(treeTemplate,l0.ConsistsOf);
123 graph.claim(treeTemplate, l0.ConsistsOf, item);
129 * Loads evaluator tree configurations from a graph and applies them to content provider.
133 * @throws DatabaseException
135 public static void load(ReadGraph graph, Resource template, CustomizableContentProvider provider) throws DatabaseException {
136 Layer0 l0 = Layer0.getInstance(graph);
137 DocumentLink sl = DocumentLink.getInstance(graph);
139 Map<String, Resource> existingEvaluatorTemplates = new HashMap<String, Resource>();
140 for (Resource r : graph.getObjects(template, l0.ConsistsOf)) {
141 if (graph.isInstanceOf(r, sl.EvaluatorTree_Template)) {
142 String id = graph.getRelatedValue(r, l0.HasName);
143 existingEvaluatorTemplates.put(id, r);
147 for (String id : provider.getContentIds()) {
148 Resource treeTemplate = existingEvaluatorTemplates.get(id);
149 if (treeTemplate == null)
152 Resource itemRes = graph.getSingleObject(treeTemplate, l0.ConsistsOf);
153 EvaluatorItem item = load(graph, itemRes);
154 EvaluatorCustomizableContent ecc = new EvaluatorCustomizableContent(provider.getContent(id).getCustomizationDescription());
156 provider.setContent(id, ecc);
162 public static List<NamedResource> getTemplates(final Resource library) throws DatabaseException{
164 throw new IllegalArgumentException("Library cannot be null");
165 List<NamedResource> templates = Simantics.getSession().syncRequest(new Read<List<NamedResource>>() {
167 public List<NamedResource> perform(ReadGraph graph) throws DatabaseException{
168 return getTemplates(graph, library);
174 public static List<NamedResource> getTemplates(ReadGraph graph, final Resource library) throws DatabaseException{
175 Layer0 l0 = Layer0.getInstance(graph);
176 DocumentLink sl = DocumentLink.getInstance(graph);
177 Collection<Resource> templates = graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sl.ReportTemplate));
178 List<NamedResource> result = new ArrayList<NamedResource>(templates.size());
179 for (Resource template : templates) {
180 String name = graph.getRelatedValue(template, l0.HasName);
181 result.add(new NamedResource(name, template));
184 Collections.sort(result);
188 public static void save(final Resource library, final String name,final CustomizableContentProvider provider) throws DatabaseException{
189 Simantics.getSession().syncRequest(new WriteRequest() {
192 public void perform(WriteGraph graph) throws DatabaseException {
193 final List<NamedResource> templates = DBUtil.getTemplates(graph,library);
194 Resource template = null;
195 for (NamedResource nr : templates) {
196 if (name.equals(nr.getName())) {
197 template = nr.getResource();
201 if (template == null) {
202 Layer0 l0 = Layer0.getInstance(graph);
203 DocumentLink sl = DocumentLink.getInstance(graph);
204 template = graph.newResource();
205 graph.claim(template, l0.InstanceOf, sl.ReportTemplate);
206 graph.claim(library, l0.ConsistsOf, template);
207 graph.claimLiteral(template, l0.HasName, name);
209 DBUtil.save(graph, provider, template);