View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
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.jtrolog;
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 jTrolog.parser.Parser;
30  import jTrolog.terms.Struct;
31  import jTrolog.terms.Term;
32  
33  /**
34   * 
35   * @author Jose Zalacain
36   * @since 1.0
37   */
38  class JTrologStructure extends JTrologTerm implements PrologStructure {
39  
40  	JTrologStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
41  		super(STRUCTURE_TYPE, provider);
42  		Term[] terms = new Term[arguments.length];
43  		for (int i = 0; i < arguments.length; i++) {
44  			terms[i] = ((JTrologTerm) arguments[i]).value;
45  		}
46  		value = new Struct(functor, terms);
47  	}
48  
49  	JTrologStructure(PrologProvider provider, String functor, Term... arguments) {
50  		super(STRUCTURE_TYPE, provider, new Struct(functor, arguments));
51  	}
52  
53  	JTrologStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
54  		super(STRUCTURE_TYPE, provider);
55  		Term leftOperand = ((JTrologTerm) left).value;
56  		Term rightOperand = ((JTrologTerm) right).value;
57  		value = new Struct(operator, new Term[] { leftOperand, rightOperand });
58  	}
59  
60  	JTrologStructure(PrologProvider provider, Term left, String functor, Term right) {
61  		super(STRUCTURE_TYPE, provider, new Struct(functor, new Term[] { left, right }));
62  	}
63  
64  	public PrologTerm getArgument(int index) {
65  		checkIndex(index, getArity());
66  		return getArguments()[index];
67  	}
68  
69  	public PrologTerm[] getArguments() {
70  		Struct structure = (Struct) value;
71  		int arity = structure.arity;
72  		PrologTerm[] arguments = new PrologTerm[arity];
73  		for (int i = 0; i < arity; i++) {
74  			arguments[i] = provider.toTerm(structure.getArg(i), PrologTerm.class);
75  		}
76  		return arguments;
77  	}
78  
79  	public int getArity() {
80  		Struct structure = (Struct) value;
81  		return structure.arity;
82  	}
83  
84  	public String getFunctor() {
85  		Struct structure = (Struct) value;
86  		if (ops.currentOp(structure.name)) {
87  			return structure.name;
88  		}
89  		return Parser.wrapAtom(structure.name);
90  	}
91  
92  	public final PrologTerm getRight() {
93  		return getArgument(1);
94  	}
95  
96  	public final PrologTerm getLeft() {
97  		return getArgument(0);
98  	}
99  
100 	public final String getOperator() {
101 		return getFunctor();
102 	}
103 
104 }