X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fevaluator%2FIf.java;fp=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Freport%2Fevaluator%2FIf.java;h=2ac3620f83dbc9c811f7e35327b204a648ffc9fd;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/evaluator/If.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/evaluator/If.java new file mode 100644 index 000000000..2ac3620f8 --- /dev/null +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/evaluator/If.java @@ -0,0 +1,89 @@ +package org.simantics.document.linking.report.evaluator; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.document.linking.Activator; +import org.simantics.document.linking.ontology.DocumentLink; +import org.simantics.document.linking.report.DocumentLine; +import org.simantics.objmap.graph.annotations.OrderedSetType; + +/** + * Checks that the first size does not return null or "false". + * Depending on results evaluates the second child or the third (if it exists). + * + * @author Marko Luukkainen + * + */ +@OrderedSetType(DocumentLink.URIs.EvaluatorTree_If) +public class If extends EvaluatorNode{ + + @Override + public String getValue(ReadGraph graph, Variable variable, Map context) throws DatabaseException { + if (children.size() > 3) + throw new DatabaseException("If node has more than 3 children."); + + String ifVal = children.get(0).getValue(graph, variable, context); + if (ifVal != null && ifVal.length() > 0 && !Boolean.FALSE.toString().equals(ifVal)) { + return children.get(1).getValue(graph, variable, context); + } else if (children.size() == 3) { + return children.get(2).getValue(graph, variable, context); + } + return null; + } + + @Override + public List getLines(ReadGraph graph, Variable variable, Map context) throws DatabaseException { + if (children.size() > 3) + throw new DatabaseException("If node has more than 3 children."); + String ifVal = children.get(0).getValue(graph, variable, context); + if (ifVal != null && ifVal.length() > 0 && !Boolean.FALSE.toString().equals(ifVal)) { + return children.get(1).getLines(graph, variable, context); + } else if (children.size() == 3) { + return children.get(2).getLines(graph, variable, context); + } + return Collections.emptyList(); + } + + + @Override + public String toString() { + return "if"; + } + + @Override + public List> getPossibleChildren(boolean add) { + if (add && children.size() == 3) + return Collections.emptyList(); + return super.getPossibleChildren(add); + } + + @Override + public EvaluatorItem copy() { + If node = new If(); + copyChildren(node); + return node; + + } + + @Override + public ImageDescriptor getImage() { + return Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/help.png"); + } + + @Override + public boolean acceptChild(EvaluatorItem item) { + return children.size() < 3; + } + + @Override + public boolean acceptChild(int index, EvaluatorItem item) { + return children.size() < 3; + } + +}