JplStructure.java
/*
* #%L
* prolobjectlink-jpi-jpl
* %%
* Copyright (C) 2019 Prolobjectlink Project
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package io.github.prolobjectlink.prolog.jpl;
import static io.github.prolobjectlink.prolog.PrologTermType.STRUCTURE_TYPE;
import io.github.prolobjectlink.prolog.PrologProvider;
import io.github.prolobjectlink.prolog.PrologStructure;
import io.github.prolobjectlink.prolog.PrologTerm;
import jpl.Compound;
import jpl.Term;
/**
*
* @author Jose Zalacain
* @since 1.0
*/
class JplStructure extends JplTerm implements PrologStructure {
JplStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
super(STRUCTURE_TYPE, provider);
Term[] terms = new Term[arguments.length];
for (int i = 0; i < arguments.length; i++) {
terms[i] = ((JplTerm) arguments[i]).value;
}
value = new Compound(removeQuoted(functor), terms);
}
JplStructure(PrologProvider provider, String functor, Term... arguments) {
super(STRUCTURE_TYPE, provider);
value = new Compound(removeQuoted(functor), arguments);
}
JplStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
super(STRUCTURE_TYPE, provider);
Term leftOperand = ((JplTerm) left).value;
Term rightOperand = ((JplTerm) right).value;
value = new Compound(operator, new Term[] { leftOperand, rightOperand });
}
JplStructure(PrologProvider provider, Term left, String functor, Term right) {
super(STRUCTURE_TYPE, provider, new Compound(functor, new Term[] { left, right }));
}
JplStructure(int objectType, PrologProvider provider, Term newJRef) {
super(objectType, provider, newJRef);
}
public PrologTerm getArgument(int index) {
checkIndex(index, getArity());
return getArguments()[index];
}
public PrologTerm[] getArguments() {
Compound structure = (Compound) value;
int arity = structure.arity();
PrologTerm[] arguments = new PrologTerm[arity];
for (int i = 0; i < arity; i++) {
arguments[i] = toTerm(structure.arg(i + 1), PrologTerm.class);
}
return arguments;
}
public int getArity() {
Compound structure = (Compound) value;
return structure.arity();
}
public String getFunctor() {
Compound structure = (Compound) value;
return structure.name();
}
public final PrologTerm getRight() {
return getArgument(1);
}
public final PrologTerm getLeft() {
return getArgument(0);
}
public final String getOperator() {
return getFunctor();
}
}