View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jpl
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jpl;
23  
24  import static io.github.prolobjectlink.prolog.PrologTermType.STRUCTURE_TYPE;
25  
26  import io.github.prolobjectlink.prolog.PrologProvider;
27  import io.github.prolobjectlink.prolog.PrologStructure;
28  import io.github.prolobjectlink.prolog.PrologTerm;
29  import jpl.Compound;
30  import jpl.Term;
31  
32  /**
33   * 
34   * @author Jose Zalacain
35   * @since 1.0
36   */
37  class JplStructure extends JplTerm implements PrologStructure {
38  
39  	JplStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
40  		super(STRUCTURE_TYPE, provider);
41  		Term[] terms = new Term[arguments.length];
42  		for (int i = 0; i < arguments.length; i++) {
43  			terms[i] = ((JplTerm) arguments[i]).value;
44  		}
45  		value = new Compound(removeQuoted(functor), terms);
46  	}
47  
48  	JplStructure(PrologProvider provider, String functor, Term... arguments) {
49  		super(STRUCTURE_TYPE, provider);
50  		value = new Compound(removeQuoted(functor), arguments);
51  	}
52  
53  	JplStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
54  		super(STRUCTURE_TYPE, provider);
55  		Term leftOperand = ((JplTerm) left).value;
56  		Term rightOperand = ((JplTerm) right).value;
57  		value = new Compound(operator, new Term[] { leftOperand, rightOperand });
58  	}
59  
60  	JplStructure(PrologProvider provider, Term left, String functor, Term right) {
61  		super(STRUCTURE_TYPE, provider, new Compound(functor, new Term[] { left, right }));
62  	}
63  
64  	JplStructure(int objectType, PrologProvider provider, Term newJRef) {
65  		super(objectType, provider, newJRef);
66  	}
67  
68  	public PrologTerm getArgument(int index) {
69  		checkIndex(index, getArity());
70  		return getArguments()[index];
71  	}
72  
73  	public PrologTerm[] getArguments() {
74  		Compound structure = (Compound) value;
75  		int arity = structure.arity();
76  		PrologTerm[] arguments = new PrologTerm[arity];
77  		for (int i = 0; i < arity; i++) {
78  			arguments[i] = toTerm(structure.arg(i + 1), PrologTerm.class);
79  		}
80  		return arguments;
81  	}
82  
83  	public int getArity() {
84  		Compound structure = (Compound) value;
85  		return structure.arity();
86  	}
87  
88  	public String getFunctor() {
89  		Compound structure = (Compound) value;
90  		return structure.name();
91  	}
92  
93  	public final PrologTerm getRight() {
94  		return getArgument(1);
95  	}
96  
97  	public final PrologTerm getLeft() {
98  		return getArgument(0);
99  	}
100 
101 	public final String getOperator() {
102 		return getFunctor();
103 	}
104 
105 }