--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.layer0.utils.binaryPredicates;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class Relation extends BinaryPredicate {\r
+\r
+ Resource relation;\r
+ \r
+ public Relation(Resource relation) {\r
+ this.relation = relation;\r
+ }\r
+\r
+ @Override\r
+ public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {\r
+ return g.getObjects(subject, relation);\r
+ }\r
+\r
+ @Override\r
+ public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {\r
+ return g.getObjects(object, g.getInverse(relation));\r
+ }\r
+\r
+ @Override\r
+ public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {\r
+ return g.hasStatement(subject, relation, object);\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsGetObjects() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsGetStatements() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsGetSubjects() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {\r
+ g.claim(subject, relation, object);\r
+ }\r
+\r
+ @Override\r
+ public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException {\r
+ g.denyStatement(subject, relation, object);\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsAdditions() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsRemovals() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result\r
+ + ((relation == null) ? 0 : relation.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ Relation other = (Relation) obj;\r
+ if (relation == null) {\r
+ if (other.relation != null)\r
+ return false;\r
+ } else if (!relation.equals(other.relation))\r
+ return false;\r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public IBinaryPredicate inverse(ReadGraph g) throws DatabaseException {\r
+ return new Relation(g.getInverse(relation));\r
+ }\r
+ \r
+ @Override\r
+ public String toString(ReadGraph g) throws DatabaseException {\r
+ return NameUtils.getSafeName(g, relation);\r
+ }\r
+ \r
+}\r