View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jlog
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 General Public License as
9    * published by the Free Software Foundation, either version 3 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 Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jlog;
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 ubc.cs.JLog.Terms.jCompoundTerm;
30  import ubc.cs.JLog.Terms.jPredicate;
31  import ubc.cs.JLog.Terms.jTerm;
32  
33  /**
34   * 
35   * @author Jose Zalacain
36   * @since 1.0
37   */
38  class JLogStructure extends JLogTerm implements PrologStructure {
39  
40  	protected JLogStructure(int type, PrologProvider provider, jTerm value) {
41  		super(type, provider, value);
42  	}
43  
44  	protected JLogStructure(PrologProvider provider, String functor, jTerm term) {
45  		super(STRUCTURE_TYPE, provider);
46  		jCompoundTerm compound = new jCompoundTerm(1);
47  		compound.addTerm(term);
48  		value = new jPredicate(functor, compound);
49  	}
50  
51  	protected JLogStructure(PrologProvider provider, String functor, PrologTerm... arguments) {
52  		super(STRUCTURE_TYPE, provider);
53  		value = new jPredicate(functor, adaptCompound(arguments));
54  	}
55  
56  	protected JLogStructure(PrologProvider provider, String functor, jCompoundTerm arguments) {
57  		super(STRUCTURE_TYPE, provider);
58  		value = new jPredicate(functor, arguments);
59  	}
60  
61  	protected JLogStructure(PrologProvider provider, PrologTerm left, String operator, PrologTerm right) {
62  		super(STRUCTURE_TYPE, provider);
63  		PrologTerm[] operands = { left, right };
64  		value = new jPredicate(operator, adaptCompound(operands));
65  	}
66  
67  	protected JLogStructure(PrologProvider provider, jTerm left, String functor, jTerm right) {
68  		super(STRUCTURE_TYPE, provider);
69  		jCompoundTerm compound = new jCompoundTerm(2);
70  		compound.addTerm(left);
71  		compound.addTerm(right);
72  		value = new jPredicate(functor, compound);
73  	}
74  
75  	public PrologTerm getArgument(int index) {
76  		checkIndex(index, getArity());
77  		jPredicate structure = (jPredicate) value;
78  		jCompoundTerm compound = structure.getArguments();
79  		return toTerm(compound.elementAt(index), PrologTerm.class);
80  	}
81  
82  	public PrologTerm[] getArguments() {
83  		jPredicate structure = (jPredicate) value;
84  		int arity = structure.getArity();
85  		PrologTerm[] arguments = new PrologTerm[arity];
86  		jCompoundTerm compound = structure.getArguments();
87  		for (int i = 0; i < arity; i++) {
88  			arguments[i] = toTerm(compound.elementAt(i), PrologTerm.class);
89  		}
90  		return arguments;
91  	}
92  
93  	public int getArity() {
94  		jPredicate structure = (jPredicate) value;
95  		return structure.getArity();
96  	}
97  
98  	public String getFunctor() {
99  		jPredicate structure = (jPredicate) value;
100 		return structure.getName();
101 	}
102 
103 	public final PrologTerm getRight() {
104 		return getArgument(1);
105 	}
106 
107 	public final PrologTerm getLeft() {
108 		return getArgument(0);
109 	}
110 
111 	public final String getOperator() {
112 		return getFunctor();
113 	}
114 
115 }