Permalink
Browse files

Merge branch 'master' of github.com:mwatts15/Crono

  • Loading branch information...
Mark Watts
Mark Watts committed Nov 29, 2012
2 parents b51b0c2 + 6f9071f commit 172a3b28fd4602ff276023aee1b06c1ef7ad4594

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -407,7 +407,8 @@ public CronoType visit(Cons c) {
for(int i = 0; i < types.length; ++i) {
types[i] = argarray[i].typeId();
}
- for(int i = 0; i < fun.args.length; ++i) {
+ int check = Math.min(argarray.length,fun.args.length);
+ for(int i = 0; i < check; ++i) {
if(!(fun.args[i].isType(argarray[i]))) {
String argstr = Arrays.toString(types);
String expected = Arrays.toString(fun.args);
@@ -0,0 +1 @@
+package crono.inf;import java.util.*;import crono.type.*;public class InfClass extends Atom{ public static final TypeId TYPEID = new TypeId("inf:Class",InfClass.class,Atom.TYPEID); protected String name; protected int id; public static int count; public InfClass() { this.id = InfClass.count++; this.name = "i"+this.id; } public InfClass(String name) { this.name = name; this.id = InfClass.count++; } //pseudo-inheritance for user-defined classes achieved by reading through database public String getName(){ return name; } public int getID(){ return id; } //used only by rdf/rdfs/owl public boolean applyFunctions(InfTriple contained, ArrayList<InfTriple> db){ System.out.println("||My name is "+getName()+" and I'm running my function."); return fn(contained,db);//fn(contained,db); } protected boolean fn(InfTriple contained, ArrayList<InfTriple> db){ //System.out.println("Dummy Function running..."); return false; } public TypeId typeId() { return InfClass.TYPEID; } public String toString() { return name; } /*public String repr() { return "\"" + data.toString() + "\""; }*/ //method overrides @Override public boolean equals(Object class2) { if(class2 instanceof InfClass) return this.getID() == ((InfClass)class2).getID(); return false; } @Override public int hashCode() { return getID(); //name is irrelevant }}
@@ -0,0 +1,107 @@
+package crono.inf;
+import java.util.*;
+import crono.type.*;
+import crono.*;
+
+public class InfDatabase{
+
+ private static ArrayList<InfTriple> elements = new ArrayList<InfTriple>();
+
+ public static boolean insert(InfTriple triple){
+ if (elements.contains(triple)){
+ System.out.println("Duplicate of "+triple+" detected!");
+ return false;
+ }
+ return elements.add(triple);
+ }
+
+ public static void insertDefstruct(String name, Map<String, CronoStruct.Field> fields){
+ InfClass nameClass = new InfClass(name);
+ insert(new InfTriple(nameClass,new Rdf.Type(),new InfStruct.structDef()));
+ for(Map.Entry<String,CronoStruct.Field> entry: fields.entrySet()){
+ //CronoStruct.Field field = entry.getValue();
+ String fieldname = entry.getKey();
+ insert(new InfTriple(nameClass,new InfStruct.HasA(),new InfClass(fieldname)));
+ }
+ System.out.println("Inserted defstruct: "+printdb());
+ return;
+ }
+
+ public static void clearEntailments(){
+ //System.out.println("DESTROY");
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ if(t.isEntailed())
+ i.remove();
+ }
+ return;
+ }
+
+ public static ArrayList<InfTriple> getElements(){
+ return elements;
+ }
+
+ public static ArrayList<InfTriple> seek(InfClass iclass,int what){
+ ArrayList<InfTriple> ret = new ArrayList<InfTriple>();
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ switch(what){
+ default: case 0:
+ if(iclass.equals(t.getSubject())) ret.add(t);
+ break;
+ case 1:
+ if(iclass.equals(t.getPredicate())) ret.add(t);
+ break;
+ case 2:
+ if(iclass.equals(t.getPredicate())) ret.add(t);
+ break;}
+ }
+ return ret;
+ }
+
+ /*
+ public static ArrayList<InfTriple> getStructFields(Environment env){
+ ArrayList<InfTriple> ret = new ArrayList<InfTriple>();
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ if(t.getPredicate() instanceof InfStruct.HasA)
+ insert(new InfTriple(t.getSubject(),t.getObject(),,true))
+ }
+ }
+ }*/
+
+
+ public static boolean entail(Environment env){
+ boolean changed;
+ ArrayList<InfTriple> copy;
+ do{
+ //System.out.println("Entailing...");
+ copy = new ArrayList<InfTriple>(elements);
+ changed = false;
+ for(Iterator i = elements.iterator();i.hasNext();){
+ InfTriple t = (InfTriple)i.next();
+ t.getPredicate().fn(t,copy);
+ }
+ boolean exists = false;
+ for(Iterator j = copy.iterator();j.hasNext();){
+ InfTriple t = (InfTriple)j.next();
+ exists = elements.contains(t);
+ if(!exists){
+ changed = true;
+ insert(t);
+ }
+ }
+ copy = null;
+ }while(changed);
+ return false;
+ }
+
+
+ public static String printdb(){
+ String ret = "Database:\n";
+ for(Iterator i = elements.iterator();i.hasNext();){
+ ret+=i.next()+"\n";
+ }
+ return ret;
+ }
+}
@@ -0,0 +1,42 @@
+package crono.inf;
+
+public class InfStruct{
+ public static final IsA isa = new IsA();
+ public static final HasA hasA = new HasA();
+
+ /*
+ public Rdfs(){
+ subClassOf = new SubClassOf();
+ Class = new Class();
+ }*/
+
+ private static enum c{
+ isA(-22,"struct:isA"),
+ hasA(-23,"struct:hasA"),
+ structDef(-24,"struct:structDef"),;
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+ public static class structDef extends InfClass{
+ public structDef(){
+ this.id = c.structDef.id;
+ this.name = c.structDef.name;
+ }
+ }
+
+ public static class IsA extends InfClass{
+ public IsA(){
+ this.id = c.isA.id;
+ this.name = c.isA.name;
+ }
+ }
+
+ public static class HasA extends InfClass{
+ public HasA(){
+ this.id = c.hasA.id;
+ this.name = c.hasA.name;
+ }
+ }
+
+}
@@ -0,0 +1,73 @@
+package crono.inf;
+import java.util.*;
+
+public class InfTriple
+{
+ private InfClass subject;
+ private InfClass predicate;
+ private InfClass object;
+ private boolean entailed;
+
+ //constructors
+ public InfTriple()
+ {
+ this.subject = new InfClass();
+ this.predicate = new InfClass();
+ this.object = new InfClass();
+ this.entailed = entailed;
+ }
+ public InfTriple(InfClass subject,InfClass predicate,InfClass object)
+ {
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.entailed = entailed;
+ }
+ public InfTriple(InfClass subject,InfClass predicate,InfClass object,boolean entailed){
+ this.subject = subject;
+ this.predicate = predicate;
+ this.object = object;
+ this.entailed = entailed;
+ }
+ //accessors/mutators
+ public InfClass getSubject(){
+ return subject;
+ }
+ public InfClass getPredicate(){
+ return predicate;
+ }
+ public InfClass getObject(){
+ return object;
+ }
+ public boolean isEntailed(){
+ return entailed;
+ }
+
+ //overrides
+ @Override public String toString()
+ {
+ return "Triple:("+getSubject().getName()+"("+getSubject().getID()+"),"
+ +getPredicate().getName()+"("+getPredicate().getID()+"),"
+ +getObject().getName()+"("+getObject().getID()+"))"+(isEntailed()?" - Entailed":"");
+ }
+
+ @Override public boolean equals(Object triple2)
+ {
+ if(triple2 instanceof InfTriple)
+ {
+ InfTriple that = (InfTriple)triple2;
+ return this.getSubject().getID() == that.getSubject().getID() &&
+ this.getPredicate().getID() == that.getPredicate().getID() &&
+ this.getObject().getID() == that.getObject().getID();
+ //return this.hashCode() == ((InfClass)triple2).hashCode();
+
+ }
+ return false;
+ }
+
+ @Override public int hashCode()
+ {
+ return (41*(41+getSubject().getID())) + (23*(23+getPredicate().getID())) + getObject().getID();
+ }
+
+}
@@ -0,0 +1,42 @@
+package crono.inf;
+import java.util.*;
+
+public class Rdf{
+ //private Rdfs rdfs = new Rdfs();
+ //public final Property Property = new Property();
+ //public final Type type = new Type();
+
+ /*
+ public Rdf(){
+ Property = new Property();
+ type = new Type();
+ }*/
+ private static enum c{
+ type(-1,"rdf:type"),
+ Property(-2,"rdf:Property");
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+
+ public static class Property extends InfClass{
+ public Property(){
+ this.id = c.Property.id;
+ this.name = c.Property.name;
+ }
+ }
+
+ public static class Type extends InfClass{
+ public Type(){
+ this.id = c.type.id;
+ this.name = c.type.name;
+ }
+
+ protected boolean fn(InfTriple cont, ArrayList<InfTriple> db){
+ InfClass A = (InfClass)cont.getSubject();
+ InfClass B = (InfClass)cont.getObject();
+ return db.add(new InfTriple(B,new Rdf.Type(),new Rdfs.Class(),true));
+ }
+ }
+
+}
@@ -0,0 +1,34 @@
+package crono.inf;
+//import java.util.*;
+
+public class Rdfs{
+ //public final SubClassOf subClassOf = new SubClassOf();
+ //public final Class Class = new Class();
+ /*
+ public Rdfs(){
+ subClassOf = new SubClassOf();
+ Class = new Class();
+ }*/
+ private static enum c{
+ subClassOf(-9,"rdfs:subClassOf"),
+ Class(-2,"rdfs:Class");
+ public final int id;
+ public final String name;
+ private c(int id, String name){this.id=id;this.name=name;}
+ }
+
+ public static class Class extends InfClass{
+ public Class(){
+ this.id = c.Class.id;
+ this.name = c.Class.name;
+ }
+ }
+
+ public static class SubClassOf extends InfClass{
+ public SubClassOf(){
+ this.id = c.subClassOf.id;
+ this.name = c.subClassOf.name;
+ }
+ }
+
+}
@@ -8,6 +8,8 @@
import crono.InterpreterException;
import crono.Visitor;
+import crono.inf.InfDatabase;
+
/**
* A structure, similar to C structures.
* CronoStruct inherits from Function so that they can be accessed as if their
@@ -137,6 +139,9 @@ protected CronoStruct(CronoStruct cs) {
this.parent = cs.parent;
this.fields = new HashMap<String, Field>();
this.fields.putAll(cs.fields);
+ for(Map.Entry<String,Field> a: fields.entrySet()){
+ System.out.printf("field %s = %s\n",a.getKey(),a.getValue().get());
+ }
this.type = cs.type;
}
@@ -159,6 +164,7 @@ public CronoType run(Visitor v, CronoType[] args) {
throw new InterpreterException(_inv_field_name, name,
args[0].toString());
}
+ InfDatabase.clearEntailments();
return field.put(args[1].accept(v));
default:

0 comments on commit 172a3b2

Please sign in to comment.