001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env.minimal;
018
019import java.io.Serializable;
020
021import org.apache.commons.scxml2.Builtin;
022import org.apache.commons.scxml2.Context;
023import org.apache.commons.scxml2.Evaluator;
024import org.apache.commons.scxml2.EvaluatorProvider;
025import org.apache.commons.scxml2.SCXMLExpressionException;
026import org.apache.commons.scxml2.model.SCXML;
027
028/**
029 * Minimal Evaluator implementing and providing support for the SCXML Null Data Model.
030 * <p>
031 * The SCXML Null Data Model only supports the SCXML "In(stateId)" builtin function.
032 * </p>
033 */
034public class MinimalEvaluator implements Evaluator, Serializable {
035
036    /** Serial version UID. */
037    private static final long serialVersionUID = 1L;
038
039    public static final String SUPPORTED_DATA_MODEL = Evaluator.NULL_DATA_MODEL;
040
041    public static class MinimalEvaluatorProvider implements EvaluatorProvider {
042
043        @Override
044        public String getSupportedDatamodel() {
045            return SUPPORTED_DATA_MODEL;
046        }
047
048        @Override
049        public Evaluator getEvaluator() {
050            return new MinimalEvaluator();
051        }
052
053        @Override
054        public Evaluator getEvaluator(final SCXML document) {
055            return new MinimalEvaluator();
056        }
057    }
058
059    @Override
060    public String getSupportedDatamodel() {
061        return SUPPORTED_DATA_MODEL;
062    }
063
064    @Override
065    public Object eval(final Context ctx, final String expr) throws SCXMLExpressionException {
066        return expr;
067    }
068
069    @Override
070    public Boolean evalCond(final Context ctx, final String expr) throws SCXMLExpressionException {
071        // only support the "In(stateId)" predicate
072        String predicate = expr != null ? expr.trim() : "";
073        if (predicate.startsWith("In(") && predicate.endsWith(")")) {
074            String stateId = predicate.substring(3, predicate.length()-1);
075            return Builtin.isMember(ctx, stateId);
076        }
077        return false;
078    }
079
080    @Override
081    public Object evalLocation(final Context ctx, final String expr) throws SCXMLExpressionException {
082        return expr;
083    }
084
085    @Override
086    public void evalAssign(final Context ctx, final String location, final Object data, final AssignType type, final String attr) throws SCXMLExpressionException {
087        throw new UnsupportedOperationException("Assign expressions are not supported by the \"null\" datamodel");
088    }
089
090    @Override
091    public Object evalScript(final Context ctx, final String script) throws SCXMLExpressionException {
092        throw new UnsupportedOperationException("Scripts are not supported by the \"null\" datamodel");
093    }
094
095    @Override
096    public Context newContext(final Context parent) {
097        return parent instanceof MinimalContext ? parent : new MinimalContext(parent);
098    }
099}