Skip to content

Commit 9b7efb1

Browse files
committed
Add computer/2, fix error propagation so it can be analyzed.
1 parent 68bf6aa commit 9b7efb1

File tree

7 files changed

+85
-6
lines changed

7 files changed

+85
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
1111
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
1212

1313

14-
version = "0.3.6"
14+
version = "0.3.7"
1515
group = "com.syntheticentropy.ocpro" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
1616
archivesBaseName = "ocpro"
1717

src/main/java/com/syntheticentropy/ocpro/OpenProlog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class OpenProlog
2020
@SuppressWarnings("WeakerAccess")
2121
public static final String NAME = "OpenProlog";
2222
@SuppressWarnings("WeakerAccess")
23-
public static final String VERSION = "0.3.6";
23+
public static final String VERSION = "0.3.7";
2424

2525
// resource path
2626
static final String RESOURCE_PATH = "/com/syntheticentropy/ocpro/";

src/main/java/com/syntheticentropy/ocpro/PrologArchitecture.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ public long waitingCall(int ticks) {
207207

208208
public void crash(String e) {
209209
forceExecutionResult.add(new ExecutionResult.Error(e));
210-
211-
// machine.crash(e);
210+
if (machine.isRunning()) {
211+
machine.crash(e);
212+
close();
213+
}
212214
}
213215

214216
@Override

src/main/java/com/syntheticentropy/ocpro/PrologVM.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public void run() {
2929
} else {
3030
// run main query
3131
}
32+
} catch (JIPRuntimeException e) {
33+
state = State.Terminated;
34+
owner.crash(e.getMessage());
3235
} catch (Exception e) {
3336
state = State.Terminated;
3437
owner.crash(e.getMessage());

src/main/java/com/syntheticentropy/ocpro/builtin/ComponentMethod2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public boolean unify(Hashtable<Variable, Variable> varsTbl) {
5454

5555
if (attempt) {
5656
// One result from this function = one call
57-
getJIPEngine().getOwner().synchronizedCall(() -> null);
57+
// getJIPEngine().getOwner().synchronizedCall(() -> null);
5858

5959
return (addressAtom == null || addressAtom.unify(addressParam, varsTbl)) &&
6060
(methodAtom == null || methodAtom.unify(methodParam, varsTbl));
6161
}
6262
}
6363

6464
// One result from this function = one call
65-
getJIPEngine().getOwner().synchronizedCall(() -> null);
65+
// getJIPEngine().getOwner().synchronizedCall(() -> null);
6666
return false;
6767
}
6868

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.syntheticentropy.ocpro.builtin;
2+
3+
import com.syntheticentropy.ocpro.PrologArchitecture;
4+
import com.ugos.jiprolog.engine.Atom;
5+
import com.ugos.jiprolog.engine.JIPTypeException;
6+
import com.ugos.jiprolog.engine.PrologObject;
7+
import com.ugos.jiprolog.engine.Variable;
8+
import li.cil.oc.api.network.Connector;
9+
10+
import java.util.HashMap;
11+
import java.util.Hashtable;
12+
import java.util.Iterator;
13+
import java.util.Map;
14+
import java.util.function.Function;
15+
16+
public class Computer2 extends OcproBuiltIn {
17+
private Map<String, Function<PrologArchitecture, Object>> exposedValues = createMap();
18+
19+
private static Map<String, Function<PrologArchitecture, Object>> createMap() {
20+
Map<String, Function<PrologArchitecture, Object>> m = new HashMap<>();
21+
m.put("realTime", o -> System.currentTimeMillis() / 1000.0);
22+
m.put("uptime", o -> o.machine.upTime());
23+
m.put("address", o -> o.machine.node().address());
24+
// m.put("freeMemory", o -> o.)
25+
// m.put("totalMemory", o -> o.)
26+
m.put("energy", o -> ((Connector) o.machine.node()).globalBuffer());
27+
m.put("maxEnergy", o -> li.cil.oc.Settings.get().ignorePower() ?
28+
Double.POSITIVE_INFINITY :
29+
((Connector) o.machine.node()).globalBufferSize());
30+
31+
return m;
32+
}
33+
34+
private Iterator<Map.Entry<String, Function<PrologArchitecture, Object>>> resultIterator = null;
35+
36+
@Override
37+
public boolean unify(Hashtable<Variable, Variable> varsTbl) {
38+
final PrologObject keyParam = getParam(1);
39+
final PrologObject valueParam = getParam(2);
40+
41+
if (resultIterator == null) {
42+
final PrologObject keyTerm = getRealTerm(keyParam);
43+
44+
// we know that a provided key must be an atom
45+
if (!(keyTerm == null || keyTerm instanceof Atom))
46+
throw new JIPTypeException(JIPTypeException.ATOM, keyTerm);
47+
48+
resultIterator = exposedValues.entrySet().iterator();
49+
}
50+
51+
PrologArchitecture owner = m_jipEngine.getOwner();
52+
53+
while (resultIterator.hasNext()) {
54+
Map.Entry<String, Function<PrologArchitecture, Object>> next = resultIterator.next();
55+
PrologObject keyAtom = Atom.createAtom(next.getKey());
56+
if (!(keyAtom == null || keyAtom.unifiable(keyParam))) continue;
57+
58+
// don't evaluate the value until after we've matched a key
59+
PrologObject value = rawToPrologObject(next.getValue().apply(owner));
60+
if (!(value == null || value.unifiable(valueParam))) continue;
61+
62+
return (keyAtom == null || keyAtom.unify(keyParam, varsTbl)) &&
63+
(value == null || value.unify(valueParam, varsTbl));
64+
}
65+
66+
return false;
67+
}
68+
69+
@Override
70+
public boolean hasMoreChoicePoints() {
71+
return resultIterator == null || resultIterator.hasNext();
72+
}
73+
}

src/main/java/com/ugos/jiprolog/engine/BuiltInFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ final class BuiltInFactory extends Object {
156156
m_builtInTable.put("consult/2", new Consult2());
157157
m_builtInTable.put("pop_signal/1", new PopSignal1());
158158
m_builtInTable.put("term_string/2", new TermString2());
159+
m_builtInTable.put("computer/2", new Computer2());
159160

160161
// catch((X = 1, throw(gulp)), E, (write(ok-X), nl)).
161162
//m_BuiltInTable.put("wait/1", new Wait1());

0 commit comments

Comments
 (0)