]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagClassFactory.java
Fixed the order of parameters in the transformation
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / FlagClassFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
14 import java.awt.Font;
15 import java.awt.Shape;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.regex.Pattern;
24 import java.util.regex.PatternSyntaxException;
25
26 import org.simantics.databoard.Bindings;
27 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
28 import org.simantics.databoard.util.Bean;
29 import org.simantics.datatypes.literal.RGB;
30 import org.simantics.db.AsyncReadGraph;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.Session;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
36 import org.simantics.db.common.request.ResourceRead;
37 import org.simantics.db.common.request.TernaryRead;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.variable.Variable;
41 import org.simantics.db.layer0.variable.Variables;
42 import org.simantics.db.procedure.AsyncProcedure;
43 import org.simantics.diagram.content.ResourceTerminal;
44 import org.simantics.diagram.flag.AbstractFlagType;
45 import org.simantics.diagram.flag.BasicFlagType;
46 import org.simantics.diagram.flag.FlagSceneGraph;
47 import org.simantics.diagram.flag.FlagUtil;
48 import org.simantics.diagram.flag.IFlagType;
49 import org.simantics.diagram.flag.IFlagType.FlagInfo;
50 import org.simantics.diagram.flag.IFlagTypeReader;
51 import org.simantics.diagram.function.All;
52 import org.simantics.diagram.function.PredefinedVariables;
53 import org.simantics.diagram.query.DiagramRequests;
54 import org.simantics.diagram.query.FlagTables;
55 import org.simantics.diagram.query.FlagTypeFilter;
56 import org.simantics.diagram.query.FlagTypeFilters;
57 import org.simantics.diagram.query.FlagTypeVisual;
58 import org.simantics.diagram.query.FlagTypeVisuals;
59 import org.simantics.diagram.stubs.DiagramResource;
60 import org.simantics.diagram.synchronization.CompositeHintSynchronizer;
61 import org.simantics.diagram.synchronization.IHintSynchronizer;
62 import org.simantics.diagram.synchronization.SynchronizationHints;
63 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
64 import org.simantics.diagram.synchronization.graph.FlagSynchronizer;
65 import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
66 import org.simantics.diagram.ui.DiagramModelHints;
67 import org.simantics.g2d.canvas.ICanvasContext;
68 import org.simantics.g2d.diagram.IDiagram;
69 import org.simantics.g2d.diagram.handler.Topology.Terminal;
70 import org.simantics.g2d.element.ElementClass;
71 import org.simantics.g2d.element.ElementUtils;
72 import org.simantics.g2d.element.IElement;
73 import org.simantics.g2d.element.handler.TextEditor;
74 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
75 import org.simantics.g2d.elementclass.FlagClass;
76 import org.simantics.g2d.elementclass.FlagClass.Mode;
77 import org.simantics.g2d.elementclass.FlagHandler;
78 import org.simantics.g2d.utils.Alignment;
79 import org.simantics.g2d.utils.geom.DirectionSet;
80 import org.simantics.layer0.Layer0;
81 import org.simantics.modeling.ModelingResources;
82 import org.simantics.modeling.template2d.ontology.Template2dResource;
83 import org.simantics.structural2.modelingRules.IModelingRules;
84 import org.simantics.utils.ui.ErrorLogger;
85
86 /**
87  * @author Tuukka Lehtonen
88  */
89 public class FlagClassFactory extends SyncElementFactory {
90
91     private static final Bean[] NO_BEANS = {};
92
93     private static final IHintSynchronizer HINT_SYNCHRONIZER = new CompositeHintSynchronizer(FlagSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE);
94
95     public static ElementClass createFlagClass(Resource elementClass, Resource terminalResource) {
96         Terminal terminal = new ResourceTerminal(terminalResource, new AffineTransform(), DirectionSet.ANY);
97         return FlagClass.create(terminal, FlagSceneGraph.INSTANCE).newClassWith(new StaticObjectAdapter(elementClass));
98     }
99
100     @Override
101     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
102         DiagramResource DIA = graph.getService(DiagramResource.class);
103         procedure.execute(graph, createFlagClass(DIA.Flag, DIA.Flag_Terminal));
104     }
105
106     @Override
107     public void load(ReadGraph g, ICanvasContext canvas, IDiagram diagram, Resource flag, IElement e) throws DatabaseException {
108         Layer0 l0 = g.getService(Layer0.class);
109         DiagramResource dr = g.getService(DiagramResource.class);
110
111         ElementClass ec = e.getElementClass();
112         final FlagHandler fh = ec.getSingleItem(FlagHandler.class);
113
114         fh.connectData(e, flag, FlagUtil.getPossibleCounterpart(g, flag));
115         fh.setExternal(e, FlagUtil.isExternal(g, flag));
116
117         TextEditor ed = ec.getAtMostOneItemOfClass(TextEditor.class);
118         if (ed != null) {
119             final Session session = g.getSession();
120             ed.setModifier(e, new TextEditor.Modifier() {
121                 @Override
122                 public String getValue(IElement element) {
123                     String s = ElementUtils.getText(element);
124                     return s != null ? s : "";
125                 }
126
127                 @Override
128                 public String isValid(IElement element, String text) {
129                     return null;
130                 }
131
132                 @Override
133                 public void modify(final IElement element, final String text) {
134                     final Resource flag = (Resource) ElementUtils.getObject(element);
135                     try {
136                         session.syncRequest(new WriteRequest() {
137                             @Override
138                             public void perform(WriteGraph graph) throws DatabaseException {
139                                 Layer0 l0 = Layer0.getInstance(graph);
140                                 DiagramGraphUtil.setRelatedValue(graph, flag, l0.HasLabel, l0.String, text, Bindings.STRING);
141                                 Resource otherFlag = FlagUtil.getPossibleCounterpart(graph, flag);
142                                 if (otherFlag != null)
143                                     DiagramGraphUtil.setRelatedValue(graph, otherFlag, l0.HasLabel, l0.String, text, Bindings.STRING);
144                             }
145                         });
146                     } catch (DatabaseException e) {
147                         ErrorLogger.defaultLogError("Flag label editing failed, see exception for details.", e);
148                     }
149                 }
150             });
151         }
152
153         Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
154         AffineTransform at = DiagramGraphUtil.getDynamicAffineTransform(g, diagramRuntime, flag);
155         ElementUtils.setTransform(e, at);
156
157         String label = g.getPossibleRelatedValue(flag, l0.HasLabel);
158         if (label == null)
159             label = "";
160         ElementUtils.setText(e, label);
161
162         boolean shapeIsSet = false;
163         boolean flagTextIsSet = false;
164         boolean flagTypeIsSet = false;
165         boolean textAreaIsSet = false;
166
167         // Defaults
168         e.setHint(FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, Alignment.LEADING);
169         e.setHint(FlagClass.KEY_TEXT_VERTICAL_ALIGN, Alignment.CENTER);
170
171         IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
172         if (modelingRules != null) {
173             Resource connectionType = DiagramGraphUtil.getConnectionTypeForFlag(g, flag);
174             IFlagTypeReader ftr = null;
175             if (connectionType != null) {
176                 //System.out.println("FLAG " + NameUtils.getSafeName(g, flag) + ", CONNECTION TYPE " + NameUtils.getSafeName(g, connectionType));
177                 ftr = g.getPossibleAdapter(connectionType, IFlagTypeReader.class);
178             }
179             if (ftr == null) {
180                 //System.out.println("FLAG " + NameUtils.getSafeName(g, flag) + ", NO CONNECTION TYPE");
181                 ftr = g.getPossibleAdapter(flag, IFlagTypeReader.class);
182             }
183
184             if (ftr != null) {
185                 IFlagType ft = ftr.read(g, flag, modelingRules);
186
187                 FlagInfo info = ft.getInfo(g, canvas);
188
189                 Shape shape = info.getShape();
190                 if (shape != null) {
191                     e.setHint(FlagClass.KEY_SHAPE, shape);
192                     shapeIsSet = true;
193                 }
194
195                 FlagClass.Type type = info.getType();
196                 if (type != null) {
197                     e.setHint(FlagClass.KEY_FLAG_TYPE, type);
198                     flagTypeIsSet = true;
199                 }
200
201                 String[] flagText = info.getText();
202                 if (flagText != null) {
203                     e.setHint(FlagClass.KEY_FLAG_TEXT, flagText);
204                     flagTextIsSet = true;
205                 }
206                 
207                 Font flagFont = info.getFont();
208                 if(flagFont != null) {
209                     e.setHint(FlagClass.KEY_FLAG_FONT, flagFont);
210                 }
211
212                 if (info.getTextArea() != null) {
213                     e.setHint(FlagClass.KEY_FLAG_TEXT_AREA, info.getTextArea());
214                     textAreaIsSet = true;
215                 }
216
217                 if (info.getHorizontalAlignment() != null)
218                     e.setHint(FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, info.getHorizontalAlignment());
219                 if (info.getVerticalAlignment() != null)
220                     e.setHint(FlagClass.KEY_TEXT_VERTICAL_ALIGN, info.getVerticalAlignment());
221             }
222         }
223
224         if (!flagTypeIsSet)
225             // Fall back to reading flag type from a property.
226             e.setHint(FlagClass.KEY_FLAG_TYPE, DiagramGraphUtil.toFlagType(dr, g.getPossibleObject(flag, dr.HasFlagType), FlagClass.Type.In));
227         if (!flagTextIsSet)
228 //            e.setHint(FlagClass.KEY_FLAG_TEXT, new String[] { label });
229             e.setHint(FlagClass.KEY_FLAG_TEXT, g.syncRequest(DiagramRequests.getFlagText(flag)));
230         if (!textAreaIsSet) {
231             FlagClass.Type type = e.getHint(FlagClass.KEY_FLAG_TYPE);
232             Mode mode = AbstractFlagType.getMode(g, flag);
233             e.setHint(FlagClass.KEY_FLAG_TEXT_AREA, BasicFlagType.getArea(type, mode));
234         }
235         if (!shapeIsSet) {
236             FlagClass.Type type = e.getHint(FlagClass.KEY_FLAG_TYPE);
237             Mode mode = AbstractFlagType.getMode(g, flag);
238             e.setHint(FlagClass.KEY_SHAPE, BasicFlagType.getShape(type, mode));
239         }
240
241         e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, HINT_SYNCHRONIZER);
242         e.setHint(FlagSceneGraph.KEY_FLAG_VISUALS, NO_BEANS);
243
244         DiagramResource DIA = DiagramResource.getInstance(g);
245         Layer0 L0 = Layer0.getInstance(g);
246
247         Resource template = diagramRuntime != null ? All.getTemplate(g, diagramRuntime) : null;
248         Resource flagTable = getFlagTable(g, template, flag);
249
250         if (template != null && flagTable != null) {
251
252             Resource flagTypeVisual = getVisualOfFlag(g, template, flagTable, flag);
253             if (flagTypeVisual != null) {
254                 Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
255                 float tableWidth = g.getRelatedValue(flagTable, TEMPLATE2D.FlagTable_HasWidth, Bindings.FLOAT);
256                 Resource align = g.getPossibleObject(flagTable, TEMPLATE2D.FlagTable_HasAlignment);
257                 float height = g.getRelatedValue(flagTable, TEMPLATE2D.FlagTable_HasRowHeigth, Bindings.FLOAT);
258                 float halfHeight = height / 2;
259                 float horizontalOffset = tableWidth;
260
261                 if (align != null) {
262                     if (align.equals(TEMPLATE2D.FlagTable_Alignment_Left)) {
263                         horizontalOffset = 0.0f;
264                     } else if (align.equals(TEMPLATE2D.FlagTable_Alignment_Right)) {
265                         tableWidth = -tableWidth;
266                         horizontalOffset = -horizontalOffset;
267                     }
268                 }
269
270                 Collection<Resource> monitorsAndTexts = g.getObjects(flagTypeVisual, L0.ConsistsOf);
271                 List<Bean> flagVisuals = Collections.emptyList(); 
272                 if (!monitorsAndTexts.isEmpty()) {
273                     flagVisuals = new ArrayList<Bean>(monitorsAndTexts.size());
274
275                     ModelingResources MOD = ModelingResources.getInstance(g);
276                     Resource diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
277                     Resource compositeResource = g.getSingleObject(diagramResource, MOD.DiagramToComposite);
278                     Variable compositeVariable = Variables.getVariable(g, compositeResource);
279
280                     for (Resource visual : monitorsAndTexts) {
281                         if (!acceptVisual(g, flag, visual))
282                             continue;
283
284                         if (g.isInstanceOf(visual, DIA.Scenegraph_Monitor)) {
285                             FlagTextInfo i = g.syncRequest(new ReadFlagTextInfo(visual), TransientCacheListener.<FlagTextInfo>instance());
286                             FlagTextInfo monitorInfo = (FlagTextInfo) i.clone();
287                             if (monitorInfo.transform != null)
288                                 monitorInfo.transform[4] += horizontalOffset;
289
290                             String path = g.getRelatedValue(visual, DIA.Scenegraph_Monitor_reference, Bindings.STRING);
291                             String value = "";
292                             if (path != null && path.length() > 0) {
293                                 value = evaluatePath(g,flag,path);
294                             }
295                             monitorInfo.text = value;
296                             monitorInfo.id = Long.toString(visual.getResourceId());
297
298                             flagVisuals.add(monitorInfo);
299                         } else if (g.isInstanceOf(visual, DIA.Scenegraph_Text)) {
300                             FlagTextInfo i = g.syncRequest(new ReadFlagTextInfo(visual), TransientCacheListener.<FlagTextInfo>instance());
301                             FlagTextInfo info = (FlagTextInfo) i.clone();
302                             if (info.transform != null)
303                                 info.transform[4] += horizontalOffset;
304
305                             String path = g.getRelatedValue(visual, DIA.Scenegraph_Text_text, Bindings.STRING);
306                             if (path != null && path.length() > 0) {
307                                 info.text = path;
308                             }
309                             info.id = Long.toString(visual.getResourceId());
310
311                             flagVisuals.add(info);
312                         } else if (g.isInstanceOf(visual, DIA.Scenegraph_SVGImage)) {
313                             SVGImageInfo info = g.syncRequest(new ReadSVGImageInfo(visual, compositeResource, compositeVariable), TransientCacheListener.<SVGImageInfo>instance());
314                             flagVisuals.add(info);
315                         }
316                     }
317                 }
318
319                 if (flagVisuals.size() > 0) {
320                     // Make sure that the flag shape is set to something that
321                     // should contain the flag visual to make selection borders
322                     // work properly.
323                     Rectangle2D newShape = new Rectangle2D.Double();
324                     newShape.setFrameFromDiagonal(0, -halfHeight, tableWidth, halfHeight);
325                     e.setHint(FlagClass.KEY_SHAPE, newShape);
326                     e.setHint(FlagSceneGraph.KEY_FLAG_VISUALS, flagVisuals.toArray(NO_BEANS));
327                 }
328             }
329         }
330     }
331
332     private static String evaluatePath(ReadGraph graph, Resource resource, String path) throws DatabaseException{
333         Variable resourceVariable = Variables.getPossibleVariable(graph, resource);
334         if (resourceVariable == null)
335             return "";
336         return evaluatePath(graph, resource, resourceVariable, path);
337     }
338
339     private static String evaluatePath(ReadGraph graph, Resource resource, Variable resourceVariable, String path) throws DatabaseException{
340         PredefinedVariables vars = PredefinedVariables.getInstance();
341         Variable property = vars.getVariable(graph, path, resource, resourceVariable);
342         if (property == null)
343             return "";
344         Object value = property.getPossibleValue(graph);
345         if (value == null || !(value instanceof String))
346             return "";
347         return value.toString();
348     }
349
350     static class ReadSVGImageInfo extends TernaryRead<Resource, Resource, Variable, SVGImageInfo> {
351
352         public ReadSVGImageInfo(Resource svgImageNode, Resource composite, Variable compositeVariable) {
353             super(svgImageNode, composite, compositeVariable);
354         }
355
356         @Override
357         public SVGImageInfo perform(ReadGraph graph) throws DatabaseException {
358             SVGImageInfo info = new SVGImageInfo();
359             DiagramResource DIA = DiagramResource.getInstance(graph);
360             info.id = Long.toString(parameter.getResourceId());
361             Resource document = graph.getPossibleObject(parameter, DIA.Scenegraph_SVGImage_document);
362             if (document != null) {
363                 Template2dResource TMPL = Template2dResource.getInstance(graph);
364                 String path = graph.getPossibleRelatedValue(document, TMPL.Profiles_VariableReference_path, Bindings.STRING);
365                 if (path != null) {
366                     String svg = evaluatePath(graph, parameter2, parameter3, path);
367                     if (svg != null && !svg.isEmpty())
368                         info.svgDocument = svg;
369                     //System.out.println("svgDocument: " + info.svgDocument);
370                 }
371             }
372             info.transform = graph.getPossibleRelatedValue(parameter, DIA.Scenegraph_SVGImage_transform, Bindings.DOUBLE_ARRAY);
373             return info;
374         }
375
376     }
377
378     static class ReadFlagTextInfo extends ResourceRead<FlagTextInfo> {
379
380         public ReadFlagTextInfo(Resource textNode) {
381             super(textNode);
382         }
383
384         @Override
385         public FlagTextInfo perform(ReadGraph graph) throws DatabaseException {
386             return createTextInfo(graph, resource);
387         }
388
389     }
390
391     /**
392      * @param g
393      * @param visual
394      * @return
395      * @throws DatabaseException
396      * @throws RuntimeBindingConstructionException
397      */
398     private static FlagTextInfo createTextInfo(ReadGraph g, Resource visual) throws DatabaseException {
399         DiagramResource DIA = DiagramResource.getInstance(g);
400         //Template2d TEMPLATE2D = Template2d.getInstance(g);
401         //Layer0 L0 = Layer0.getInstance(g);
402
403         FlagTextInfo info = new FlagTextInfo();
404         info.id = Long.toString(visual.getResourceId());
405
406         info.font = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_font, Bindings.getBindingUnchecked(Font.class));
407         info.color = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_color, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
408         info.borderColor = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_borderColor, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
409         info.backgroundColor = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_backgroundColor, RGB.Integer.BINDING/*Bindings.getBindingUnchecked(RGB.Integer.class)*/);
410         Float width = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_width, Bindings.getBindingUnchecked(Float.class));
411         if (width != null)
412             info.width = width;
413
414         Float borderWidth = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_borderWidth, Bindings.getBindingUnchecked(Float.class));
415         if (borderWidth != null)
416             info.borderWidth = borderWidth;
417
418         Boolean wrapText = g.getRelatedValue2(visual, DIA.Scenegraph_AbstractText_wrapText, Bindings.BOOLEAN);
419         if (wrapText != null)
420             info.wrapText = wrapText; 
421
422         info.hAlignment = Alignment.LEADING;
423         Byte hAlignment = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_horizontalAlignment, Bindings.BYTE);
424         if (hAlignment != null) {
425             if (hAlignment == 1)
426                 info.hAlignment = Alignment.TRAILING;
427             else if (hAlignment == 2)
428                 info.hAlignment = Alignment.CENTER;
429         }
430
431         info.vAlignment = Alignment.LEADING;
432         Byte vAlignment = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_verticalAlignment, Bindings.BYTE);
433         if (vAlignment != null) {
434             if (vAlignment == 1)
435                 info.vAlignment = Alignment.TRAILING;
436             else if (vAlignment == 2)
437                 info.vAlignment = Alignment.CENTER;
438             else if (vAlignment == 3)
439                 info.vAlignment = Alignment.BASELINE;
440         }
441
442         info.transform = g.getPossibleRelatedValue2(visual, DIA.Scenegraph_AbstractText_transform, Bindings.getBindingUnchecked(double[].class));
443         return info;
444     }
445
446     private static Resource getFlagTable(ReadGraph g, Resource template, Resource flag) throws DatabaseException {
447         if (template == null || flag == null)
448             return null;
449
450         DiagramResource DIA = DiagramResource.getInstance(g);
451         String tableName = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
452         if (tableName == null)
453             return null;
454
455         Map<String, Resource> flagTables = g.syncRequest(new FlagTables(template), TransientCacheListener.<Map<String, Resource>>instance());
456         return flagTables.get(tableName);
457     }
458
459     private Resource getVisualOfFlag(ReadGraph g, Resource template, Resource flagTable, Resource flag) throws DatabaseException {
460         if (template == null || flagTable == null || flag == null)
461             return null;
462
463         // First make sure that there are possible visuals.
464         // There's no point in proceeding if no visuals exist.
465         List<FlagTypeVisual> flagTypeVisuals = g.syncRequest( new FlagTypeVisuals(flagTable),
466                 TransientCacheListener.<List<FlagTypeVisual>>instance());
467         if (flagTypeVisuals == null || flagTypeVisuals.isEmpty())
468             return null;
469
470         Variable flagVariable = Variables.getPossibleVariable(g, flag);
471         if (flagVariable == null)
472             return null;
473
474         for (FlagTypeVisual visual : flagTypeVisuals) {
475             Resource visualComposite = visual.getVisualComposite();
476
477             String filterReference = visual.getFilteredPropertyReference();
478             if (filterReference == null || filterReference.isEmpty())
479                 return visualComposite;
480
481             String filterPattern = visual.getFilterPattern();
482             if (filterPattern == null || filterPattern.isEmpty())
483                 return visualComposite;
484
485             String value = evaluatePath(g, flag, flagVariable, filterReference);
486             if (value == null)
487                 return visualComposite;
488
489             try {
490                 if (!Pattern.matches(filterPattern, value)) {
491                     // filter is defined but property don't match
492                     continue;
493                 }
494             } catch (PatternSyntaxException ex) {
495                 ErrorLogger.defaultLogError(ex);
496                 continue;
497             }
498
499             return visualComposite;
500         }
501
502         return null;
503     }
504
505     /**
506      * @param graph
507      * @param flag the flag to which to apply the filter reference paths
508      * @param visual the visual to look for filters from
509      * @return <code>true</code> if filter passes, <code>false</code> if not
510      * @throws DatabaseException
511      */
512     private boolean acceptVisual(ReadGraph graph, Resource flag, Resource visual) throws DatabaseException {
513         List<FlagTypeFilter> filters = graph.syncRequest(new FlagTypeFilters(visual),
514                 TransientCacheListener.<List<FlagTypeFilter>>instance());
515         if (filters.isEmpty())
516             return true;
517
518         Variable flagVariable = Variables.getPossibleVariable(graph, flag);
519         if (flagVariable == null)
520             return false;
521
522         for (FlagTypeFilter filter : filters) {
523             String reference = filter.getReference();
524             if (reference == null || reference.isEmpty())
525                 continue;
526
527             String pattern = filter.getPattern();
528             if (pattern == null || pattern.isEmpty())
529                 continue;
530
531             String value = evaluatePath(graph, flag, flagVariable, reference);
532             if (value == null)
533                 continue;
534
535             try {
536                 if (Pattern.matches(pattern, value) == filter.isMatchRequired()) {
537                     return true;
538                 }
539             } catch (PatternSyntaxException ex) {
540                 ErrorLogger.defaultLogError(ex);
541                 continue;
542             }
543         }
544
545         return false;
546     }
547
548 }