01: /*
02: * $Id: StructureWithTermsAssembler.java,v 1.1 2003/08/19 01:12:57 jonesde Exp $
03: *
04: * Copyright (c) 1999 Steven J. Metsker.
05: * Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
06: *
07: * Permission is hereby granted, free of charge, to any person obtaining a
08: * copy of this software and associated documentation files (the "Software"),
09: * to deal in the Software without restriction, including without limitation
10: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11: * and/or sell copies of the Software, and to permit persons to whom the
12: * Software is furnished to do so, subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included
15: * in all copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
22: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
23: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: package org.ofbiz.rules.logikus;
27:
28: import java.util.*;
29: import org.ofbiz.rules.engine.*;
30: import org.ofbiz.rules.parse.*;
31: import org.ofbiz.rules.parse.tokens.*;
32:
33: /**
34: * Pops the terms and functor of a structure from an assembly's stack, builds a structure, and pushes it.
35: *
36: * @author Steven J. Metsker
37: * @version 1.0
38: */
39: public class StructureWithTermsAssembler extends Assembler {
40:
41: /**
42: * Reverse a vector into an array of terms.
43: *
44: * @param v the vector to reverse
45: * @return Term[] the vector, reversed
46: */
47: public static Term[] vectorReversedIntoTerms(List v) {
48: int size = v.size();
49: Term[] terms = new Term[size];
50:
51: for (int i = 0; i < size; i++) {
52: terms[size - 1 - i] = (Term) v.get(i);
53: }
54: return terms;
55: }
56:
57: /**
58: * Pops the terms and functor of a structure from an assembly's
59: * stack, builds a structure, and pushes it.
60: * <p>
61: * This method expects a series of terms to lie on top of a
62: * stack, with an open paren token lying underneath. If there
63: * is no '(' marker, this class will throw an <code>
64: * EmptyStackException</code>.
65: * <p>
66: * Beneath the terms of the structure, this method expects to
67: * find a token whose value is the functor of the structure.
68: *
69: * @param Assembly the assembly to work on
70: */
71: public void workOn(Assembly a) {
72: List termVector = elementsAbove(a, new Token('('));
73: Term[] termArray = vectorReversedIntoTerms(termVector);
74: Token t = (Token) a.pop();
75:
76: a.push(new Structure(t.value(), termArray));
77: }
78: }
|