View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2.env.minimal;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.scxml2.Builtin;
22  import org.apache.commons.scxml2.Context;
23  import org.apache.commons.scxml2.Evaluator;
24  import org.apache.commons.scxml2.EvaluatorProvider;
25  import org.apache.commons.scxml2.SCXMLExpressionException;
26  import org.apache.commons.scxml2.model.SCXML;
27  
28  /**
29   * Minimal Evaluator implementing and providing support for the SCXML Null Data Model.
30   * <p>
31   * The SCXML Null Data Model only supports the SCXML "In(stateId)" builtin function.
32   * </p>
33   */
34  public class MinimalEvaluator implements Evaluator, Serializable {
35  
36      /** Serial version UID. */
37      private static final long serialVersionUID = 1L;
38  
39      public static final String SUPPORTED_DATA_MODEL = Evaluator.NULL_DATA_MODEL;
40  
41      public static class MinimalEvaluatorProvider implements EvaluatorProvider {
42  
43          @Override
44          public String getSupportedDatamodel() {
45              return SUPPORTED_DATA_MODEL;
46          }
47  
48          @Override
49          public Evaluator getEvaluator() {
50              return new MinimalEvaluator();
51          }
52  
53          @Override
54          public Evaluator getEvaluator(final SCXML document) {
55              return new MinimalEvaluator();
56          }
57      }
58  
59      @Override
60      public String getSupportedDatamodel() {
61          return SUPPORTED_DATA_MODEL;
62      }
63  
64      @Override
65      public Object eval(final Context ctx, final String expr) throws SCXMLExpressionException {
66          return expr;
67      }
68  
69      @Override
70      public Boolean evalCond(final Context ctx, final String expr) throws SCXMLExpressionException {
71          // only support the "In(stateId)" predicate
72          String predicate = expr != null ? expr.trim() : "";
73          if (predicate.startsWith("In(") && predicate.endsWith(")")) {
74              String stateId = predicate.substring(3, predicate.length()-1);
75              return Builtin.isMember(ctx, stateId);
76          }
77          return false;
78      }
79  
80      @Override
81      public Object evalLocation(final Context ctx, final String expr) throws SCXMLExpressionException {
82          return expr;
83      }
84  
85      @Override
86      public void evalAssign(final Context ctx, final String location, final Object data, final AssignType type, final String attr) throws SCXMLExpressionException {
87          throw new UnsupportedOperationException("Assign expressions are not supported by the \"null\" datamodel");
88      }
89  
90      @Override
91      public Object evalScript(final Context ctx, final String script) throws SCXMLExpressionException {
92          throw new UnsupportedOperationException("Scripts are not supported by the \"null\" datamodel");
93      }
94  
95      @Override
96      public Context newContext(final Context parent) {
97          return parent instanceof MinimalContext ? parent : new MinimalContext(parent);
98      }
99  }